Sub topic 8
Where memories live: key-value, document, and vector stores
This chapter assumes you know basic Python and the idea of long-term memory as facts kept outside the model. It also assumes you have seen embeddings and cosine similarity. Here you will choose where to keep memories, because the storage type decides how you can read them back.
Three storage shapes
Each shape answers a different kind of question.
| Store | You ask by | Good for | Weak at |
|---|---|---|---|
| Key-value | Exact key | Profile fields, settings, counters | Anything you can't name in advance |
| Document | Fields and filters | Episodes with metadata, such as date, tags, and session | Fuzzy wording matches |
| Vector | Meaning, via embeddings | Free-text facts and past events | Exact lookups, and explaining why something matched |
A key-value store maps a key to a value, like a Python dict. Use it when the agent knows what it wants: user:42:timezone. The lookup is fast and exact, and nothing can be "almost" retrieved.
A document store keeps records with several fields and lets you filter on them. For example, ask for all episodic memories from one user created this week. It gives structure without needing embeddings.
A vector store keeps an embedding next to each text and returns the nearest ones to a query. The MemoryStore class from earlier in this tutorial is a minimal version. Real systems add an index so the search doesn't compare against every item.
Choosing a store
Using them together
Most agents combine shapes. Stable facts go in key-value storage. Records with metadata go in a document store. Free text that the agent can't predict a query for goes in a vector store. This sketch shows the first two, using plain Python structures:
import time
profiles = {} # key-value: exact lookups
episodes = [] # document-style: filter by fields
def set_fact(user_id, key, value):
profiles[(user_id, key)] = value
def get_fact(user_id, key, default=None):
return profiles.get((user_id, key), default)
def add_episode(user_id, text, tags):
episodes.append({
"user_id": user_id,
"text": text,
"tags": set(tags),
"created": time.time(),
})
def find_episodes(user_id, tag, since=0.0):
return [
e for e in episodes
if e["user_id"] == user_id
and tag in e["tags"]
and e["created"] >= since
]
set_fact("u1", "timezone", "Asia/Kolkata")
add_episode("u1", "Debugged a failing deploy script.", ["coding", "deploy"])
print(get_fact("u1", "timezone"))
print(find_episodes("u1", "deploy"))Every record carries user_id, so each lookup is already scoped to one user. Keep that habit with any store you choose.
Common errors
- Using a vector store for everything. Asking for a user's timezone by similarity can return a wrong but similar memory. Use a key for facts you can name.
- Storing only the embedding. Keep the original text and metadata too. You need them to show, update, or delete the memory.
- Skipping metadata. Without
created,kind, anduser_id, you can't filter, expire, or scope memories later. - Changing the embedding model without re-embedding. Vectors from different models are not comparable, so old memories stop matching.
Exercise
An agent must recall a user's preferred language (always asked by name) and also find past conversations "similar to this bug". Which store fits each, and why?
Show answer
Use a key-value store for the preferred language, because the agent knows the exact key and wants an exact answer. Use a vector store for the similar conversations, because the query is described by meaning rather than by a fixed field. Store user_id and timestamps with both so results can be scoped and filtered.