Section650

Writing

TopicAI
Reading8 min

Memory Engineering: Decide What an Agent Should Remember

Memory carries selected information from one agent run into the next. What should persist, how it is retrieved and corrected, when it should expire, and how to keep durable project facts apart from temporary hypotheses, through a coding agent that fixed a sorting bug.

By Ka Lun Chan · Cloud & AI · AI / Architecture

What does memory engineering decide?

Memory engineering decides what an agent carries from one run into the next: what to store, how it is retrieved, how it is corrected when it is wrong, and when it expires. The useful rule is to persist verified, durable facts about the project and keep hypotheses with the run that formed them, unless something confirms them.

Part of a series on what it takes to make an AI system reliable. Memory carries selected information into future runs. What the model sees within a single run is context engineering.

Every run of an agent starts with an empty head unless you give it something. Memory is that something: a file, a database or a store that outlives the run and gets read back in later. Whatever you store eventually becomes context again, so it competes for the same limited attention as the code the next task needs. A wrong memory is also worse than none, because it arrives looking like established knowledge.

What should persist from one bug fix?

The illustrative scenario: a coding agent was asked to fix a ticket saying the Quantity column on the Orders page sorts as 1, 10, 2. Along the way it found that the table sorted formatted display text, tried converting the strings with Number(), failed verification on 1,200 and blanks, and then fixed it properly by sorting raw values. It also read a README note claiming quantities arrive from the API as strings, which turned out to be stale.

The run is over. Here is what it learned and what should happen to each piece.

Fig. 744-1 What one run leaves behind
ItemKindDecisionLifetime
Numeric columns sort on the raw value; formatCell is for display onlyProject factKeep. A regression test proves itUntil the code changes; the test says when
Run table tests with npm test -- OrdersTableProcedureKeepUntil the command stops working
A product owner confirmed blanks sort last in both directionsDecisionKeep, with who decidedUntil someone revisits it
The README says quantities arrive from the API as stringsHypothesis, disprovedDon’t keep as a fact. Record that the README is stale, and fix the READMEEnds with the run
Attempt 1 with Number() failed on 1,200 and blanksEpisodeKeep with the pull request, not in general memoryLives with the task history
The browser check timed out once on TuesdayObservationDon’t keep unless it recursA week, then drop
Two of the six items become general memory. Most of what a run learns belongs to that run, its pull request or the code itself.

LangGraph’s memory documentation gives a useful vocabulary for the kinds: semantic memory for facts, episodic memory for past experiences, procedural memory for how to do things. The table shows why the kind matters. Facts and procedures help every future task in this repository. Episodes are mostly useful to someone investigating that specific change, and they belong in the pull request and the run log.

Put “sort on raw values” near the comparator and protect it with a test. Use project memory for facts that can’t live next to the code, such as environment pitfalls. The worktree example in harness engineering illustrates that distinction.

Durable project facts and temporary hypotheses

A durable fact has evidence behind it and stays true until something changes. A hypothesis helps decide what to investigate next and still needs verification.

“Quantities arrive from the API as strings” is a hypothesis. The agent read it in a README and could have acted on it. If it had been stored as a fact, every future run on this codebase would start with a false belief about where the data comes from, and some future agent would “fix” the API client for a bug that lives in the table. The danger is that a stored hypothesis loses its question mark. A week later it reads exactly like a fact.

So each entry should carry its status and its evidence:

Memory entry (illustrative)
- fact: Numeric table columns sort on the raw value.
    formatCell output is display-only.
  status: verified
  evidence: src/orders/OrdersTable.test.jsx "Quantity sorts numerically"
  scope: this repository
  recorded: from the Quantity sort fix (PR link)
  recheck: when src/table/ changes

- note: README says quantities arrive from the API as strings.
  status: disproved. Values are formatted on the client.
  action: README corrected in the same PR

A hypothesis can be promoted to a fact, but only by a test passing or a person confirming it. The agent’s own confidence isn’t evidence.

Writing memory

LangGraph describes writing memory during a run or afterwards in a background task. Writing during a run captures details while they are fresh. Reviewing the transcript afterwards gives an opportunity to reject unsupported claims.

For a coding agent, the simplest approval mechanism already exists. Keep project memory in a file in the repository and let changes to it go through the same review as code. Claude Code works roughly this way with CLAUDE.md files, which it loads at the start of every session, at user and project scopes, and it also has an auto memory feature that records notes across sessions without you writing them. Its documentation adds a caveat worth repeating: these files are treated as context, not enforced configuration. Memory can advise the agent. It can’t stop it. Rules that must hold belong in the harness.

Be careful about what is allowed to write memory at all. A ticket, a web page or a log can contain text crafted to steer the agent, and OWASP lists this indirect prompt injection as a top risk. An injected instruction that only affects one run is bad. One that gets written into memory affects every run after it. Anything derived from untrusted input should need a review before it persists.

Retrieving it

Memory only helps if the right piece shows up at the right time. Two patterns cover most needs. A small set of always-loaded project facts, kept short enough that every line earns its place, the way a good CLAUDE.md or onboarding doc does. And a larger store searched on demand, organized by topic or scope. LangGraph uses namespaces for this, so a memory about the Orders table doesn’t show up during an unrelated task.

The always-loaded part is where discipline matters most. It is tempting to keep adding lines, and every line is read on every run. When a project memory file reaches the length where a person would skim it, the model is skimming it too. Anthropic’s writing on context engineering treats memory as notes kept outside the context window and pulled back in when needed, and that framing helps: memory is storage, and loading it is a context decision every time.

How is memory corrected, and when should it expire?

Correct memory as soon as something contradicts it: if the code, a test or a person disagrees with a stored fact, the stored fact loses, and it gets updated or deleted. Expire it on the event that makes it stale, such as a code change, a resolved issue or the end of a run, and only fall back to a date when no such event exists.

Before acting on a memory that names a file, function or command, the agent should check that it still exists. A memory saying “sort logic lives in src/table/sort.js” is useful until someone moves it, and then it sends the agent to the wrong place with confidence. Checking is cheap. Acting on a stale pointer is not.

Different kinds of memory go stale in different ways:

  • Hypotheses expire with the run, unless a test or a person promotes them.
  • Project facts expire when the code they describe changes, which is why the entry above names a path to recheck.
  • Decisions last until someone revisits them, and should say who made them so the agent knows whom to ask.
  • Environment observations, like a flaky check, get a short time limit. If the problem recurs, it becomes a fact with evidence. If it doesn’t, it was noise.

Fix a bad memory and add the failure to the agent’s evaluation set. The principle also applies to people: know where to look things up and how to check them.

Sources

Checked in November against the documentation below. LangGraph and Claude Code are concrete references for how memory can work, not the only ways. The memory entries are illustrative.

Short answers

What is the difference between an agent’s context and its memory?

Context is what the model sees during one run. Memory is what survives after the run ends and is brought back into a later run. Memory always ends up as context again, so everything stored competes for the same limited attention as the code and documents the next task needs.

Should an agent remember its hypotheses?

Not as facts. A hypothesis belongs to the run that formed it. If it is worth keeping, store it marked as unverified, with the evidence and an expiry, or promote it to a fact only after a test or a person confirms it.

What is worth keeping

Keep what a test or a person has confirmed, write down the evidence and the event that would make it stale, and let every guess end with the run that made it.

The series overview maps all eight disciplines and helps identify which part of the system to investigate first.

Back to the series overview Discuss what you’re building