Skip to content

RAG and Memory

Yigtwxx edited this page Jul 12, 2026 · 1 revision

RAG & Memory

Agents remember. Each task pulls the relevant per-user context — past conversation memory plus chunks of uploaded documents — from Qdrant and injects it into the agent prompts. Everything is strictly user-scoped. The logic lives in services/memory_service.py and services/document_service.py.

Vector store

Two Qdrant collections (see Database-Schema):

Collection Holds
conversation_memories Embeddings of past conversation turns
document_chunks Embeddings of uploaded document chunks

EMBEDDING_DIM = 768, produced by nomic-embed-text via Ollama. Every query is filtered by user_id, so one user's memory can never surface for another.

Embeddings

embed_texts (in llm_service.py) calls the embedding endpoint. It resolves to EMBEDDING_ENDPOINT when set, otherwise falls back to FREE_MODEL_ENDPOINT (the dev default). In production this points at a small dedicated ollama service that serves embeddings only — see Deployment and Configuration. If the embedding endpoint is unreachable, document upload returns 500 and RAG silently degrades, which is why the dedicated prod service exists.

Conversation memory

memory_service:

  • add_memory — after a task finalizes, its conversation is embedded and stored (called from the FINALIZE step, see Agent-Orchestration).
  • retrieve_memories — at task start, semantically relevant memories are fetched (user-filtered) and formatted into the prompt via format_memory_block.
  • ensure_collection — lazily creates the collection with the right vector size.

Document ingestion (RAG knowledge base)

document_service + the /documents endpoints (see API-Reference):

  • ingest — accepts .txt / .md up to DOCUMENT_MAX_BYTES = 2 MB. Splits with chunk_text (DOCUMENT_CHUNK_SIZE = 1000, overlap 150), embeds each chunk, and writes them to document_chunks. Document metadata goes to MongoDB documents.
  • list_documents — lists the user's uploaded documents.
  • delete_document — removes the metadata and the associated vector chunks (delete_document_chunks).

Context injection at task start

task_service._gather_context assembles the RAG context (relevant memories + document chunks) that the orchestrator/main-agent/subagents receive through AgentContext.memory_context. This is how a subagent "knows" what the user discussed before or uploaded.

Deletion

On account purge, memory_service.purge_user_vectors removes both conversation_memories and document_chunks for the user. This runs before the PostgreSQL row is deleted (purge order Mongo → Qdrant → PG), so vectors are never orphaned. See Security.

Clone this wiki locally