Skip to main content

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.

ChangeUseful behaviorCommon failure
A result is verifiedStore the conclusion with scope, date and evidenceSaving every intermediate guess
A decision is replacedLink the replacement and retain the earlier rationaleLeaving both versions apparently current
Sources disagreePreserve the conflict and conditionsLetting the newest summary silently win
A record becomes staleRecheck against current sources before reuseTreating a date or expiry timer as proof of truth
Information must be removedRemove it from the source and refresh derived indexesDeleting only the search result

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?

ContentWhere it goesExample
Rules and commands that apply every timeThe instruction file; the docs advise keeping each CLAUDE.md under 200 lines“Run tests with pnpm test”, “never commit .env”
Preferences you corrected the agent onAuto memory, written by the agent“Use pnpm, not npm”
Decision rationale, research notes, earlier designsA searchable note store; see the next section“Why commission moved to a per-trade fee”
Anything derivable from the code and Git historyNowhereFile paths, a one-off debugging session

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.

QMDzvec-grepBasic Memory
What it indexesMarkdown and other document folders you registerSource code, documents and structured data in a workspace; the index lives in .zvec-grep/ at the project rootMarkdown notes under ~/basic-memory, with their observations and relations
Default local models (first download)embedding embeddinggemma-300M about 300 MB, reranker qwen3-reranker-0.6b about 640 MB, a 1.7B query-expansion model about 1.1 GB; search alone needs no modelpotion-code-16m-v2, about 30 MB; for multilingual documents switch to potion-multilingual-128m, about 0.5 GB; the model guide lists 11 local modelsFastEmbed running bge-small-en-v1.5, about 67 MB, an English model; reranking off by default
Remote optionNo remote embeddings documented; only other local GGUF modelsQwen remote embeddings, which need explicit authorisation with --allow-remote or zg auth grantEmbeddings can use an OpenAI-compatible endpoint; optional hosted cloud at $15/month with a 7-day trial
MCPqmd mcp, with query, get, multi_get and other toolszg server runs a shared MCP server; zg install connects agentsbm_* tools such as bm_write, bm_search and bm_context
Who writesPeople write the notes; QMD only readsPeople write code and docs; zvec-grep only readsPeople and the assistant both write
Installnpm install -g @tobilu/qmd, Node ≥ 22 or Bun ≥ 1.0npm install -g @zvec/zvec-grep, Node ≥ 22; Homebrew also availableuv tool install basic-memory --prerelease=allow
LicenceMITApache-2.0AGPL-3.0; read the terms before embedding it in your own product

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.

Explore connectionsOpen network