AI Orchestration & Memory
Database tables governing LangGraph trace steps, execution states, pgvector RAG embeddings, and long-term user memory profiles.
AI Orchestration & Memory
This module configures agent execution tracing, step latency logging, pgvector RAG indexes, and long-term cognitive semantic memories.
Migrated by: 00006_create_ai_orchestration_and_memory.sql and optimized in 00008_create_audit_logs_and_indexing.sql.
1. Execution Traces
ai_execution_traces
Header level tracking for top level agent orchestration workflows (such as generating daily meal plans).
- Schema:
Column Type Constraints Default Description iduuidPRIMARY KEYgen_random_uuid()Trace identifier. user_iduuidREFERENCES users(id) ON DELETE SET NULL- User context trigger. session_typetextNOT NULL- Target function (e.g. 'chat_response','meal_plan_generation').session_iduuid- - Reference linking back to trigger (e.g. thread_id).statustextNOT NULL'running'Execution state: 'running','completed', or'failed'.started_attimestamptzNOT NULLnow()Execution start timestamp. completed_attimestamptz- - Execution end timestamp. total_latency_msinteger- - Latency in milliseconds. total_tokensinteger- 0Total LLM tokens used. total_costnumeric(10,6)- 0.000000Summed financial cost ($) of LLM queries.
ai_execution_steps
Granular tracker log for nested agent workflow stages.
- Schema:
id(PRIMARY KEY),trace_id(foreign key traces),parent_step_id(nested loop parent),step_name(e.g.,'Safety Agent check'),step_type(e.g.'llm_call','tool_call'),status,input_payload(JSONB),output_payload(JSONB),model_name(e.g.'gpt-4o'),prompt_tokens,completion_tokens,latency_ms,error_message,started_at,completed_at.
2. Long-Term Memory
agent_memories
Stores long term semantically extracted user attributes (preferences, habits, and medical rules).
- Schema:
Column Type Constraints Default Description iduuidPRIMARY KEYgen_random_uuid()Memory record identifier. user_iduuidNOT NULL- Foreign key referencing users.id (ON DELETE CASCADE). memory_typetextNOT NULL- Classification: 'preference','restriction', or'habit'.contenttextNOT NULL- Text content of the extracted preference. confidencenumeric(3,2)NOT NULL1.00System confidence score from 0.00 to 1.00. importanceintegerNOT NULL CHECK (importance >= 1 AND importance <= 10)5Priority mapping. is_activebooleanNOT NULLtrueTrue if the memory is currently active. metadatajsonbNOT NULL'{}'Source conversation contexts metadata. created_attimestamptzNOT NULLnow()Extracted date. updated_attimestamptzNOT NULLnow()Last revision date.
3. Vector Embeddings (RAG)
These tables support Cosine Similarity search over raw catalogs and user memories. Originally declared with 2560 dimensions, they are rebuilt as 1536-dimension vectors to fit within pgvector HNSW limits.
recipe_embeddings
Stores vector coordinates for recipe searches.
- Schema:
recipe_id(foreign key recipes),embedding(vector(1536)),chunk_text,updated_at.
food_item_embeddings
Stores vector coordinates for food item searches.
- Schema:
food_item_id(foreign key food_items),embedding(vector(1536)),chunk_text,updated_at.
agent_memory_embeddings
Stores vector coordinates for semantic user memory searches.
- Schema:
memory_id(foreign key agent_memories),embedding(vector(1536)),chunk_text,updated_at.