7403: Vector Memory and Embedding-Based Storage¶
Difficulty: โญโญโญ Advanced
Estimated Time: 4 hours
Abstract¶
Agent memory becomes scalable when memories are embeddings: a fuzzy, natural-language recall ("what did we decide about auth last week?") turns into nearest-neighbor search. This document covers the memory taxonomy, the write/read paths of an embedding-backed memory store, a Qdrant integration, the memory lifecycle (consolidation, decay, dedup), and a reference long-term memory architecture with token-budgeted recall.
Table of Contents¶
- 1. Overview
- Learning Objectives
- 2. Memory Taxonomy
- 3. Write Path: Creating Memories
- 4. Read Path: Semantic Recall
- 5. Qdrant Integration
- 6. Memory Lifecycle
- 7. Long-Term Memory Architecture
- 8. Performance & Cost
- 9. Troubleshooting
- Summary
- 10. References
1. Overview¶
1.1 Prerequisites¶
- 7401: Long-term Memory - memory fundamentals
- 6102: Semantic Similarity - embeddings
- 6401: Qdrant Setup - deployment
Learning Objectives¶
After completing this document, you will:
- โ Distinguish episodic, semantic, and procedural memories and store each appropriately
- โ Implement the write path: consolidation โ embed โ metadata-indexed store
- โ Implement budgeted recall: embed query โ filtered top-k โ inject
- โ Run consolidation, decay, and dedup so memory stays useful over months
- โ Size embedding latency, storage, and token budget for production
2. Memory Taxonomy¶
| Type | Content | Example | Storage shape |
|---|---|---|---|
| Working | Current conversation context | In-flight task state | Context window |
| Episodic | What happened, when | "User reported 500s on /checkout on 2026-09-20" | Event record + timestamp + entities |
| Semantic | Distilled facts, preferences | "User prefers TypeScript; deploys to GCP" | Atomic fact statements |
| Procedural | How to do things | "Their release flow: tag โ CI โ staged rollout" | Checklist/playbook text |
Two design rules fall out of the taxonomy:
- Episodic โ semantic consolidation: episodes are raw; periodically distill them into semantic facts (Section 6) โ recall of facts beats recall of transcripts
- Metadata is mandatory:
type,timestamp,session_id,entities[]โ vector similarity alone returns stale and off-type noise
3. Write Path: Creating Memories¶
from datetime import datetime, timezone
WRITE_PROMPT = """Extract durable memories from this exchange.
Return one JSON object per memory: {"type": "episodic|semantic|procedural",
"content": "<one atomic statement>", "entities": [str]}
Exchange:
{exchange}"""
def remember(llm, store, embed, exchange: str, session_id: str):
for m in parse_json_objects(llm(WRITE_PROMPT.format(exchange=exchange))):
mem = {
"type": m["type"],
"text": m["content"],
"entities": m["entities"],
"ts": datetime.now(timezone.utc).isoformat(),
"session_id": session_id,
}
store.upsert(
id=uuid4().hex,
vector=embed(mem["text"]),
payload=mem,
)
Where:
- One atomic statement per memory: "prefers TypeScript" and "uses GCP" are two memories โ atomic memories dedup and decay independently
- Write-time LLM extraction costs one small-model call per exchange; gate it on exchange importance (tool errors, explicit user statements, decisions) to control spend
- Never store secrets (keys, tokens) as memories โ filter at write time
4. Read Path: Semantic Recall¶
RECALL_TEMPLATE = """Relevant memories about this user/project:
{memories}
Use them if relevant; ignore if not."""
def recall(embed, query: str, k=5, token_budget=400,
types=None, entities=None):
hits = search( # Qdrant layer defined in section 5
vec=embed(query),
k=k,
types=types, # e.g. ["semantic", "procedural"]
entities=entities,
)
lines, used = [], 0
for h in hits: # budgeted: newest-first, stop at cap
line = f"- [{h.payload['type']}] {h.payload['text']}"
if used + len(line.split()) > token_budget:
break
lines.append(line)
used += len(line.split())
return RECALL_TEMPLATE.format(memories="\n".join(lines)) if lines else ""
Where:
- Filters before similarity: type and recency constraints prune the candidate set; similarity ranks within it
- Token budget cap: recall text competes with retrieved documents and the task itself โ memory gets a fixed slice (~300-500 tokens typical)
- Inject memories as a clearly-marked block, not woven into the task prompt โ the model must be able to tell memory from current instruction
5. Qdrant Integration¶
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PayloadSchemaType, PointStruct, Filter, FieldCondition, MatchAny
client = QdrantClient(url="http://localhost:6333")
# one collection per agent/tenant; named vector = embedding model version.
# Guarded create - recreate_collection would wipe the agent's memory
# every time this snippet re-runs.
if not client.collection_exists("agent_memory_v1"):
client.create_collection(
collection_name="agent_memory_v1",
vectors_config=VectorParams(size=1024, distance=Distance.COSINE),
)
# payload indexes make filtered recall fast
client.create_payload_index("agent_memory_v1", "type", PayloadSchemaType.KEYWORD)
client.create_payload_index("agent_memory_v1", "entities", PayloadSchemaType.KEYWORD)
def upsert(mem_id, vec, payload):
client.upsert("agent_memory_v1", [PointStruct(id=mem_id, vector=vec, payload=payload)])
def search(vec, k=5, types=None, entities=None):
must = []
if types:
must.append(FieldCondition(key="type", match=MatchAny(any=types)))
if entities:
must.append(FieldCondition(key="entities", match=MatchAny(any=entities)))
return client.query_points(
"agent_memory_v1", query=vec, limit=k,
query_filter=Filter(must=must) if must else None,
).points
- Named/versioned collections: swapping embedding models means a new collection plus backfill โ never mix vector spaces in one collection
- Payload indexes on
type/entitiesturn filtered search from full-scan to index-lookup
6. Memory Lifecycle¶
Memory that only grows becomes noise. Three maintenance jobs:
6.1 Consolidation (episodic โ semantic)¶
nightly / per-N-sessions:
1. Pull recent episodes for a session/user
2. LLM distillation: merge into atomic semantic facts
("3 debugging sessions on /checkout" -> "recurring pain: /checkout latency")
3. Store semantic fact; keep source episode ids in payload.provenance
4. Mark episodes as consolidated (kept for audit, excluded from recall)
6.2 Decay & Dedup¶
def decay_score(payload, now, half_life_days=90):
age = (now - parse_ts(payload["ts"])).days
return 0.5 ** (age / half_life_days) # exponential recency
def is_duplicate(new_vec, store, threshold=0.95):
hits = store.search(vector=new_vec, k=1)
return hits and hits[0].score > threshold
- Decay re-ranks recall by
similarity ร decay ร importancerather than deleting outright โ deletion is a separate, auditable job - Dedup at write: near-identical memories (>0.95 cosine) merge via the consolidation pass instead of accumulating
6.3 Deletion Policy¶
Memories are personal data. Support: delete-by-user (GDPR-style), TTL per memory type (episodes 90d, semantic facts until superseded), and provenance so a deletion cascades to facts distilled from a deleted episode.
7. Long-Term Memory Architecture¶
โโโโโโโโโโโโโโโโโโโโโโโโโโโโ Agent Loop (7101) โโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ
โ turn start โโโบ recall() โโโบ inject memory block (โค400 tok) โ
โ โ โ
โ turn end โโโโบ remember() โโโบ extract โโโบ embed โโโบ upsert โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โฒ
write path read path
โผ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Vector store (Qdrant) โ
โ collection: agent_memory_v1 โ
โ payload: type, ts, entities, session โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โฒ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Memory manager (batch) โ
โ consolidate ยท decay ยท โ
โ dedup ยท delete ยท backfill โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
- The agent loop touches memory through exactly two calls (
recall,remember) โ everything else is an offline memory-manager job - MemGPT-style paging (self-managed memory in/out of context) is the dynamic variant: the agent itself decides what to pin and evict; start with the static budget version, add paging only when recall misses are demonstrably context-fit problems
8. Performance & Cost¶
| Component | Typical magnitude | Notes |
|---|---|---|
| Embed query | 10-50 ms | Same model as write path โ mandatory |
| Filtered ANN search | 5-20 ms @ 1M memories | With payload indexes |
| Write extraction (small LLM) | 100-400 ms / exchange | Gate on importance |
| Consolidation batch | Nightly, ~1 LLM call / 10 episodes | Off the hot path |
| Storage | ~1.5 KB / memory incl. payload | 1M memories โ 1.5 GB |
| Context cost | 300-500 tokens / turn | The real currency |
๐ Rule of Thumb: Embedding-model swaps invalidate every stored vector. Version the collection (
_v1), record the model name in collection metadata, and plan the backfill job before the first model upgrade, not after.
9. Troubleshooting¶
| Symptom | Likely Cause | Fix |
|---|---|---|
| Recall returns stale/irrelevant facts | No recency weighting | similarity ร decay ranking; filter by ts |
| Same memory stored dozens of times | No dedup | 0.95-cosine dedup at write; consolidation pass |
| Agent ignores injected memories | Memory block unmarked or buried | Clearly labeled block near the top; instruct usage |
| Recall misses obviously relevant memory | Filter too narrow / threshold too high | Loosen type filter; tune k; check embedding domain fit |
| Context flooded by memory text | No budget | Hard token cap on recall block |
| Search slow at scale | Missing payload indexes | Index type/entities; check HNSW config (6101) |
| Vectors nonsense after model upgrade | Mixed vector spaces | Versioned collections + backfill |
Summary¶
Agent memory becomes scalable when memories are embeddings: a fuzzy natural-language recall - what did we decide about auth last week - turns into a nearest-neighbor search. This lesson covered the memory taxonomy, the write and read paths of an embedding-backed store, and the Qdrant integration that carries them. The rule it leaves: episodic recall is a retrieval problem once you embed it - design the metadata schema before the first memory is written, because you cannot filter on fields you never stored.
10. References¶
Academic Papers / Systems¶
- [1] Packer et al. "MemGPT: Towards LLMs as Operating Systems". 2023.
- [2] Park et al. "Generative Agents: Interactive Simulacra of Human Behavior". UIST, 2023. (memory stream + reflection)
- [3] Zhong et al. "MemoRAG: Boosting Long-Term Memory for LLMs". 2024.
Documentation¶
- Qdrant Filtering - payload filters and indexes
Related Minder Academy Documents¶
- 7401: Long-term Memory - memory fundamentals
- 6101: HNSW Indexing - ANN internals
- 6102: Semantic Similarity - embedding fundamentals
- 6401: Qdrant Setup - deployment
Next Steps¶
- Continue with: 7404: Reflective Memory and Context Paging
- Review: 7401: Long-term Memory
- Retrieval: 6203: Advanced Retrieval Techniques
- Assessment: 7400: Memory Systems - Quiz
Document ID: 7403
Status: Complete
Related Documents: [7401, 6101, 6102, 6401]