Agent Memory: Retention, Revision and Expiry
An AI assistant working on a project needs both the current request and relevant background. Keeping every past message in the prompt is not the only way to supply that background. You can save useful information outside the conversation, retrieve it when needed, and check it against the current task.
Memory is retained information, such as a decision note or research summary. Retrieval selects material from stored files or an index. The context window is the information available to the model for its current response. Saving a note does not mean the model has read it, and retrieving one does not mean the note is correct.
A Memory Has a Lifecycle
A conversation summary is a candidate record, not an established fact. Before retaining it, identify what future question it answers, where the evidence lives, and whether it describes an observation, a decision or a hypothesis. Keep source material separate from conclusions derived from it.
Suppose a note says a parser assumes UTF-8, but the current input contract now permits another encoding. The old note explains a past implementation; it must not override the current contract. Record which decision replaced it and retrieve the current specification during the next task.
Writing a memory is a separate action from retrieving it. Choose what merits retention and who may update it; do not let a model turn every confident sentence into a project rule. Retrieval Pipelines: Chunking, Recall, Reranking, and Citations covers finding records, while Context Engineering for Agents covers selecting evidence for the current request.
Start with the memory you already have
Anyone using a coding agent probably has a first memory already: the instruction file written for the agent in the repository. Claude Code reads the project's CLAUDE.md at the start of every session; when the repository has an AGENTS.md and no CLAUDE.md, it reads AGENTS.md instead. Codex also reads AGENTS.md, from the repository root down to the current directory, up to 32 KiB combined by default.
Claude Code also keeps an auto memory. It writes its own notes from your corrections and preferences and stores them on your machine in ~/.claude/projects/<project>/memory/, shared by every worktree of the repository. At the start of each session it loads the first 200 lines or first 25 KB of the index file MEMORY.md; other notes are read when needed. Both the instruction file and auto memory are context, not enforced configuration: to actually block an action, use a hook.
What goes where follows from two questions: how often is it needed, and can it be derived from the code?
Claude Code's auto memory skips that last category itself: it does not record architecture, file paths or debugging fixes. Keys and tokens also go nowhere.
Match the tool to the question
Add a retrieval tool when the notes outgrow the instruction file and you need to search them by meaning. Each of the three covers one kind of material: QMD for a set of Markdown notes, zvec-grep for code and documents, and Basic Memory for linked notes the assistant writes too. When you already know the file name or symbol, open it or search for it exactly; a small project often needs only a decision file and ordinary text search.
The facts come from each project's own documentation: QMD, zvec-grep, Basic Memory and its configuration reference. The size of zvec-grep's default model is that of the model of the same name on Hugging Face; Basic Memory's default model size is from FastEmbed's model list. For Chinese notes in Basic Memory, switch the embedding model to a multilingual one first.
The three tools share three limits, stated once here:
- A local index does not mean local processing. Retrieved text goes to the connected AI client; zvec-grep's remote embeddings and Basic Memory's cloud service also send content elsewhere. Check both the retrieval setup and the assistant receiving the results before adding sensitive material. Local models also need a download on first use.
- Retrieved text carries no authority. A tool hands text to the assistant over MCP; that does not turn the text into instructions. Basic Memory does not control who may edit which note either; “approve before editing a decision note” has to come from client permissions and working practice.
- Results are leads, not conclusions. What a score or an empty result can show is covered in retrieval pipelines; open the source before acting.
A worked example: fix fee allocation
Suppose the request is to fix how slippage and commission are allocated in a backtest, using test data only. A backtest replays historical market data to estimate how much a trading strategy would have earned: slippage is the gap between the price you wanted and the price you actually got, commission is the fee paid to the broker for each trade, and both must be spread correctly across individual fills for the computed returns to be trustworthy.
- Establish the current scope. Start with the request and applicable project instructions. An earlier decision affects this fix, so retrieve its note and check its date, author, and status. Suppose retrieval returns an older decision note saying “allocate commission by trade value,” but the project has since moved to a per-trade fee and the test data has been updated. That note is the table's “a decision is replaced” case: link it to the new fee decision before changing code. Another note saying “keep this as a command-line script” may explain the design; it does not grant permission to change production accounts.
- Locate the implementation. Search a known function name directly. If the name is unknown, a semantic query such as “slippage allocation fee matching” can suggest entry points. The setup and CLI examples are in zvec-grep: Search Code and Documents by Meaning.
- Read enough to understand the change. Inspect the matching function, its callers, data types, and relevant tests. Broaden the reading if the result omits important context, for example when you see the allocation function but not the fee configuration it reads.
- Change and verify. Apply the bounded fix for the per-trade fee and run the relevant tests, confirming that the commission allocated to each fill in the test data adds up to the total commission.
- Save only useful new knowledge. If the work produces a lasting decision, record its rationale and sources through the project's agreed write process. A routine patch need not produce a new memory note.
The same approach helps with documentation: look for existing coverage before creating a new page, inspect the closest matches, and update or link them when appropriate, which keeps duplicate pages down.
Keep evidence separate from instructions
A retrieved note may contain an accepted decision, a tentative proposal, a quotation, or obsolete guidance. Keep these distinctions visible. Dates, source links, and an explicit indication that a decision was replaced are more useful than treating everything in a “memory” folder as settled truth.
Current permissions and applicable instructions govern actions. Stored text is context to evaluate, not an automatic override. If a remembered decision conflicts with the current request, resolve the conflict before acting. Instructions embedded in retrieved documents or tool output may be untrusted, even when the content was indexed locally.
Search indexes are derived from source material; they miss things and go stale. So before changing anything, reopen the current source, inspect supporting citations, and verify with tests. What a score or an empty result can prove is covered in retrieval pipelines. Keep enough context to understand the evidence; removing relevant information merely to make a prompt smaller deletes the cause of the problem along with it.