Memory for Agents
What memory is, and how it differs from RAG
Memory - an agent's ability to use facts gathered over earlier turns with a user, and to reason about how they changed.
Example
Project starts in PyTorch (March). Switch to JAX (April). Debugging pain, back to PyTorch (May). Ask the agent "which framework should I use?" The right answer is PyTorch, and JAX already failed. Detecting this -> Memory.
| RAG | Memory (state over time) | |
|---|---|---|
| Storage | Conversations embedded in a vector DB | Facts stored as state with time order |
| Query | Top-k similarity search | Latest state is the present truth |
| On the example | Top hit is "started with PyTorch". Answer ignores JAX. | March PyTorch, April JAX, May PyTorch. Answer is PyTorch. |
| Embeddings | Needed, and must be debugged | Not needed |
Two problems overlap here. What do I know about this project? What do I remember about this user? The boundary between them is fuzzy. That is why memory is hard.
Human memory pipeline
| Type | Holds | Example |
|---|---|---|
| Episodic | Events | What did I eat for lunch? Trip to Goa last summer |
| Semantic | Facts, constants | Value of pi is 3.14 |
| Procedural | Skills | Riding a bicycle, typing fast |
- Consolidation - long-term memories get updated as we learn new things.
- Forgetting - facts of no use are dropped. A feature, not a bug.
- Working memory and short-term memory are the same thing here.
- Do not map these blocks one-to-one onto an LLM context window. It is only an intuition.
File-based memory in Claude Code, Codex and Hermes-style agents
- Anthropic and OpenAI: CLAUDE.md / AGENTS.md, written by the user, loaded at the start of every session.
- Agent-written memories folder: what failed, what the user enforced, what not to use. Created on request or when the agent notices repeated patterns.
- Nous Research's Hermes agent: memory.md (project) and user.md (who the user is, style, preferences), capped at 4KB.
- Claude caps memory files at 200 lines. OpenAI caps at 32KB.
- No embeddings or vector DB. Agents can search by tool calls, which avoids embedding debugging.
Simple, yet it runs at huge production scale.
Failure modes of file memory
Reasons to fail
- Cap problem - at 200 lines a new entry pushes out the oldest. That may be the core fact about the project.
- Evict least-used instead? Still fails. A fact used twice a month (update the dashboard from the DB) can be critical but rarely recalled.
- Semantic miss - memory says "run cpt_train.sh". Grep for "training" finds nothing. Only reasoning about the file name rescues it.
- Portability - files live on one machine. Not on cloud, not shared with team or other devices.
- Temporal - "lives in Delhi" and later "lives in Bangalore" both exist. Which is current?
Letta (MemGPT): the LLM manages its own memory
Idea from operating systems: active data stays in RAM, idle data goes to disk. Map RAM -> context window, disk -> external storage. The LLM gets functions to evict from or load into the context.
- Core - always loaded.
- Recall - searchable, via RAG or another tool.
- Archival - on disk, must be brought into context before use.
Letta is the startup from the MemGPT researchers. It is the most opinionated design. The whole agent runtime is built on Letta. The underlying model must be smart enough to manage memory.
Mem0: a separate memory service
- Add - new fact.
- Update - fact already exists.
- Delete / invalidate - old fact is wrong.
- No-op - nothing worth storing.
"I moved to Bangalore": the service finds the existing Delhi memory and updates it, instead of adding a second one. The cost: the old value is gone. Mem0 is open source.
Graph memory (Supermemory)
The Problem
After Mem0 overwrites Delhi with Bangalore, "where did I live last summer?" is unanswerable.
The Solution
- Store facts as nodes with relationships (used to live in Delhi, now lives in Bangalore).
- Update, extend nodes, and infer: "teaches an AI course" + "prefers minimalism" -> slides should be lean. The user never said this.
- Supermemory has its own custom engine deciding when to update a node, a relationship, or infer.
- Overkill for small single-agent memory. Worth it with many agents, browsers, apps and sources.
Time and forgetting
Example
Joined Anthropic in January, left in October to start own lab. "Where did the user work last spring?" A relationship alone cannot answer.
- Attach event start and event end to each fact. No end time -> still active.
- Anthropic node: ended October. Own lab node: active.
- Memory is closed with a tag, not deleted.
- Metadata can sit beside each memory, or be structural (separate active and closed tables/folders).
Forgetting
"Explain transformer attention" asked before finals is stale six months later. It should not surface as a study habit.
- Option A: delete by threshold (age or use count). Lossy.
- Option B: down-weight. Each memory has a weight that decays by rules. The agent sees the weight and relies less on it. Lossless.
- Memory decay - the weight drops over time. Delete goes to zero at once. Decay slides down slowly.
- Based on MemoryBank (2023), which attaches weights to memories in a memory bank.
Memory benchmarks test retrieval, not memory
Mem0, Zep and Supermemory each report 91-97% on LoCoMo, LongMemEval or ConvoMem. These give a very long conversation (e.g. 1M tokens), then ask questions.
The Problem
- The transcript is still available at query time.
- The agent can search it again and again, e.g. "when did they go to dinner?"
- That is retrieval, not recall of learned memory. A search benchmark, like needle in a haystack.
The Solution
The agent can take time to memorize. When it starts answering, it can only read what it wrote. This tests real memory. No proper memory evaluation exists yet.
Claude Code vs Codex memory architecture
- Claude Code has two memories: CLAUDE.md (project root, user-written, passed as is) and auto-memory files.
- Auto-memory types: user, feedback, project, reference. Capped at 200 lines.
- Retrieval: file names plus a one-line description go to a side Sonnet call. It returns the ~5 most relevant files. These join CLAUDE.md and the system prompt. No embeddings.
- Codex: AGENTS.md always loaded (32KB cap). Memory is extracted asynchronously when a session ends and idle, then merged into memory.md.
- Claude can update memory during a run. Codex mostly after it.
ChatGPT: one memory file about the user, a markdown file per project, and summaries of roughly the last 15 chats, all loaded at the start of every chat.
Memory for voice agents
Chat tolerates a one-second lookup. Voice does not. One network call can eat the budget. A simple vector DB search takes about 80 ms.
- Preload user profile, last call summary and open issues while the websocket connects.
- Write memory asynchronously in a background thread, every 3-5 turns, for the next session.
- Failure modes: anonymous callers have no memory; frequent LLM calls raise cost.
- Voice models also use far more tokens than text. Unsolved, open problem.
No single correct memory solution exists. Pick the framework whose opinion fits: LLM self-managed (Letta), separate service (Mem0), graph across many sources (Supermemory).
Glossary
- Memory
- Tracking facts over time so the agent uses the current state, not just the most similar text.
- Mem0
- Separate background service that adds, updates, deletes or ignores facts after each turn.
- Memory decay
- Lowering a memory's weight over time instead of deleting it.