Database Schema
Magnet AI uses PostgreSQL as its primary database, leveraging the pgvector extension to store both relational data and vector embeddings in a single system.
Core Models
Agents
Stores AI agent configurations.
Fields:
id- Primary key (UUID)name- Agent namedescription- Agent descriptionsystem_prompt- Base system promptmodel_id- Associated LLM modeltemperature- Model temperature settingmax_tokens- Maximum tokenstools- JSON array of available toolstopics- Agent topics configurationcreated_at- Creation timestampupdated_at- Last update timestamp
Prompt Templates
Reusable prompt templates.
Fields:
id- Primary key (UUID)name- Template namedescription- Template descriptiontemplate- Prompt template textvariables- JSON array of variablestype- Template type (system, user, etc.)is_default- Default template flagcreated_at- Creation timestampupdated_at- Last update timestamp
Knowledge Sources
Data source configurations.
Fields:
id- Primary key (UUID)name- Source nametype- Source type (file, api, database, etc.)connection_string- Connection details (Encrypted)configuration- JSON configurationstatus- Connection statuslast_sync- Last synchronization timecreated_at- Creation timestampupdated_at- Last update timestamp
RAG Tools
RAG (Retrieval Augmented Generation) tool configurations.
Fields:
id- Primary key (UUID)name- Tool namedescription- Tool descriptionknowledge_source_id- FK to knowledge sourceembedding_model- Embedding model nameretrieval_params- JSON retrieval parameterschunk_size- Document chunk sizechunk_overlap- Chunk overlaptop_k- Number of results to retrievecreated_at- Creation timestampupdated_at- Last update timestamp
Retrieval Tools
Semantic search and retrieval configurations.
Fields:
id- Primary key (UUID)name- Tool namedescription- Tool descriptionknowledge_source_id- FK to knowledge sourceretrieval_strategy- Retrieval strategyfilters- JSON filterscreated_at- Creation timestampupdated_at- Last update timestamp
Models
LLM model configurations.
Fields:
id- Primary key (UUID)name- Model nameprovider- Provider (OpenAI, Azure, etc.)model_id- Provider's model identifierapi_key- Encrypted API keyendpoint- Custom endpoint (if applicable)pricing- JSON pricing informationcapabilities- JSON capabilitiesis_active- Active statuscreated_at- Creation timestampupdated_at- Last update timestamp
Conversations
Agent conversation history.
Fields:
id- Primary key (UUID)agent_id- FK to agentuser_id- User identifiermessages- JSON array of messagesmetadata- JSON metadatacreated_at- Creation timestampupdated_at- Last update timestamp
Evaluations
Evaluation runs and results.
Fields:
id- Primary key (UUID)name- Evaluation nametype- Evaluation typetarget_id- FK to evaluated entitytest_cases- JSON test casesresults- JSON resultsmetrics- JSON metricsstatus- Evaluation statuscreated_at- Creation timestampcompleted_at- Completion timestamp
Usage Metrics
Track LLM usage and costs.
Fields:
id- Primary key (UUID)entity_type- Entity type (agent, rag_tool, etc.)entity_id- Entity IDmodel_id- FK to modelprompt_tokens- Input tokenscompletion_tokens- Output tokenstotal_tokens- Total tokenscost- Calculated costtimestamp- Usage timestamp
Relationships
Models
└── 1:N → Agents
└── 1:N → Usage Metrics
Knowledge Sources
└── 1:N → RAG Tools
└── 1:N → Retrieval Tools
Agents
└── 1:N → Conversations
└── 1:N → Evaluations
└── 1:N → Usage Metrics
RAG Tools
└── 1:N → Evaluations
└── 1:N → Usage Metrics
Prompt Templates
└── M:N → Agents (used by agents)Vector Database (pgvector)
Magnet AI uses the pgvector extension within PostgreSQL to store and query embeddings. This allows for unified transaction management and simplified infrastructure.
Document Embeddings Table
Fields:
id- Document ID (UUID)source_id- FK to knowledge sourcecontent- Original textembedding-vector(1536)(or other dimension)metadata- JSON metadatacreated_at- Creation timestamp
Vector Similarity Search
Queries use the <=> (cosine distance) or <-> (Euclidean distance) operators for efficient similarity search.
Migration Management
Migrations are managed using Alembic.
Creating Migrations
npm run db:migrate -- -m "Add new field"Applying Migrations
npm run db:upgradeDatabase Configuration
Connection String
We use the asyncpg driver for high-performance async access.
DATABASE_URL = "postgresql+asyncpg://user:password@host:port/database"Fixtures and Seed Data
Located in /api:
manage_fixtures.py- Load/save fixtures- Sample data for development
- Test data for evaluations
Indexes
Key indexes for performance:
agents.name- Agent lookupconversations.agent_id- Conversation queriesusage_metrics.timestamp- Time-based queriesknowledge_sources.type- Source filtering- IVFFlat / HNSW Indexes: On vector columns for fast approximate nearest neighbor search.
Constraints
- Foreign key constraints enabled
- Unique constraints on names (where applicable)
- NOT NULL constraints on required fields
- Check constraints for enums
Best Practices
- Use Migrations: Always create migrations for schema changes
- Index Strategy: Add indexes for frequently queried fields
- JSON Fields: Use JSON for flexible, schema-less data
- Soft Deletes: Consider soft deletes for audit trails
- Timestamps: Always include created_at/updated_at
Next Steps
- Backend Architecture - Backend implementation
- REST API - API endpoints
- Getting Started - Development setup