Overview
Curated: · Written: · Reviewed:
Agent Memory & Context Management
In one line
Agent memory is an external, governed retrieval corpus that the agent writes into and reads from under a fixed token budget. Reach for it when state must survive a context window or a session, and the application, not the model, decides what gets stored.
Key definitions
- Working memory: what sits in the current window (objective, plan, recent turns, validated results). It disappears when the window or workflow ends unless it was persisted.
- Episodic memory: dated past events with provenance, such as "on 2025-01-12 user cancelled plan X".
- Semantic memory: extracted facts and preferences stored as structured records, with an embedding index built over them.
- Procedural memory: approved, reusable tool sequences.
- Supersession: a new fact marks the old row with
superseded_byandvalid_from. The old row is kept, not overwritten.
Formulas and complexity
Quote headroom, not W, as the space that tool results and retrieved memory compete for. Retrieved items must fit inside it, and the lowest-priority items are evicted first.
How it works
- Read path: derive scope from the authenticated identity, filter (tenant, subject, ACL, purpose, type, time, deletion), rank, then cap top-k and tokens.
- Write path: candidates pass ordered gates (sensitivity, provenance, purpose, stability across sessions) before storage. First sightings go to a durable pending store, not a container-local file.
- Under pressure, eviction follows a written priority: current requirements and safety constraints outrank old conversation.
When to use and when not to
| Situation | Use memory? | Better alternative |
|---|---|---|
| Per-user preference that changes | Yes, supersede rather than append | None: fine-tuning cannot update or delete one user's data |
| Shared, current documents | No | Retrieval over the documents |
| Single-session task | No | Working memory only |
| "What happened last time?" | Yes, episodic records with session IDs and outcomes | A semantic summary loses which session it came from |
| Sensitive domain where persistence is a liability | Only with strict governance and confirmation | Authoritative system of record lookup |
Traps interviewers probe
- Keying memory to a per-visit session ID. Fix: key memory to the stable subject or tenant identity. A session ID scopes one conversation, and a TTL on it deletes the memory with it.
- Letting similarity grant rights. Fix: similarity only ranks eligible records. The server checks each action against authoritative state using the authenticated scope.
- Filtering tenants after top-k. Fix: apply the tenant filter inside the search, before ranking. Otherwise another tenant's record displaces a permitted one.
- Appending every fact. Fix: use entity-level supersession with
valid_fromandsuperseded_by. Retrieval excludes superseded rows, and an as-of query walks the validity intervals. - Treating decay as deletion. Fix: decay only lowers retrieval eligibility. Explicit requests, legal hold and export ignore it. Deletion tombstones the record and removes it from indexes, caches and summaries.
Interview tips
Open with: "Memory is a governed retrieval corpus. I would design the write path first, then scope-first retrieval under the token budget, then deletion and an evaluation against a no-memory baseline."
Expect these follow-ups:
- How do you decide what to write? Name the gates and their order.
- What happens when a fact changes? Supersede it and exclude stale rows.
- How does deletion reach embeddings and summaries? Tombstone first, then rebuild indexes, invalidate caches and regenerate derived artifacts from source turns.
- Why not fine-tune? Per-user mutability and deletion rule it out.
- How do you know memory helps? Compare against a no-memory arm, stratify by subgroup, and check the result against the noise band.
30-second outline:
- Memory is external state, and the model only sees what gets retrieved.
- Write gates: sensitivity, provenance, purpose, and stability across sessions.
- Read path: scope from auth, filter inside search, rank, cap tokens.
- Changes supersede old rows with
valid_fromandsuperseded_by. - Deletion tombstones serving state, then rebuilds indexes, caches and summaries.
- Evaluate against a no-memory baseline before claiming it works.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.