Context isn't memory: how agents remember

A model forgets everything between calls. What looks like memory is the harness re-sending, trimming, summarising and looking things up. The four layers that make an agent remember, and what goes wrong in each.

A framed context window holding a few recent blocks, with older blocks shrinking as they leave it and a database below feeding facts back in

People talk about an assistant "remembering" what they said earlier, or "forgetting" halfway through a long task. Both are slightly wrong. The model doesn't remember anything. Every call starts from nothing. It reads the text it's given, writes a reply, and that's the end of it.

Everything that looks like memory is the code around the model, the harness, deciding what text to send next time. Once you see it that way, "the agent forgot" becomes a concrete question: what did we leave out of the request, and why?

Every turn sends the whole conversation

A chat feels like a conversation, but each turn is a fresh request containing everything so far: the system prompt, the tool definitions, every message, every tool call and every tool result.

Turn 12, as the model sees it
text
[system prompt]           2,000 tokens
[tool definitions]        3,000 tokens
[turns 1–11]             41,000 tokens   ← including every file it read
[the new message]           150 tokens

This has two consequences that drive everything else.

Cost grows faster than the conversation. If each turn adds about the same amount of text, turn n sends n turns' worth. Add that up over a session and the total tokens grow with the square of its length. A conversation twice as long costs roughly four times as much. Prompt caching cuts the price of the repeated part a lot, but it doesn't change what the model has to read.

Quality drops before the window is full. Models handle long contexts, but not evenly. Details in the middle of a long context get less attention than those at the start and end, and an instruction from turn 3 has to compete with forty thousand tokens of tool output since. Long before you hit the limit, the agent starts to feel vague.

So the job isn't "fit as much in as possible". It's "send what this step needs, and nothing else".

Four layers of memory

It helps to think of an agent's memory as four layers, from most to least immediate.

LayerWhat it holdsLifetimeWho decides what's in it
Working contextThe current request: recent turns, live tool resultsOne callThe harness, every turn
Compacted historySummaries of older turnsOne sessionThe harness, when space runs low
Durable memoryFacts, preferences, decisionsAcross sessionsThe agent or the user, deliberately
Searchable historyPast sessions, documents, ticketsIndefiniteRetrieval, when asked

Most of the "it forgot" bugs come from treating one layer as if it were another.

Layer 1: working context, kept lean

The cheapest wins are here, before anything gets summarised.

  • Tool results are the biggest offender. An agent that reads ten files has ten files in its context for the rest of the session, long after it used them. Clear old tool results once they've been acted on, leaving a one-line note: "read config.yaml (240 lines), see earlier". The model can always read the file again.
  • Return less from tools in the first place. A tool that returns forty fields when the next decision needs three is paying for thirty-seven fields on every turn after.
  • Keep the stable parts first and unchanged. System prompt and tool definitions at the start, identical every turn. That's what lets prompt caching work.

Layer 2: compaction, done carefully

Eventually a long session needs more than trimming. Compaction replaces the older part of the conversation with a summary and keeps the recent part word for word.

compact.py
python
KEEP_RECENT = 10          # turns kept exactly as they were
BUDGET = 120_000          # tokens; well under the model's limit
 
 
def build_context(system, history, summary, count_tokens, summarise):
    if count_tokens(system, summary, history) < BUDGET:
        return system, summary, history
 
    old, recent = history[:-KEEP_RECENT], history[-KEEP_RECENT:]
    summary = summarise(previous=summary, turns=old)
    return system, summary, recent

The code is easy. The summary prompt is where it goes right or wrong. A summary that says "the user and assistant discussed the deployment" has thrown away everything useful. A good one keeps exactly the things the agent would otherwise have to ask again:

  • the goal, in the user's words
  • decisions made, and what was ruled out and why
  • names, IDs, paths and numbers that came up
  • what's done, what's in progress, what's next
  • anything the user said not to do

Compaction is lossy, always. Two rules limit the damage. Summarise the previous summary together with the new turns, rather than stacking summaries on top of each other. And never compact away the user's constraints: if they said "don't touch production", that line survives every compaction verbatim.

Layer 3: durable memory, written on purpose

Compaction lives and dies with the session. For anything that should carry over, like a user's preferred language, the name of their team's staging environment, or a decision made last week, you need memory that's written down somewhere outside the conversation.

The design questions are all about discipline, not storage:

What gets written. Facts that will matter again and can't be looked up elsewhere. Not the conversation itself, not things already in a document or a database, not passing remarks.

When it gets written. Either the agent has a "remember this" tool and uses it during the conversation, or a separate step reads the finished session and extracts what's worth keeping. The first is more precise. The second catches things the agent didn't think to save.

How it comes back. A small, always-loaded index ("this user prefers UK spelling, works on the billing team") plus a search over everything else. Loading every memory into every request recreates the problem you were trying to solve.

How it goes stale. Memories are claims about the past. People change teams, preferences change, files get renamed. Store when each memory was written, prefer the newer one when two conflict, and treat a memory that names a file or a setting as something to check, not something to trust.

Whose it is. Memory must be scoped to a user, or a team, and never leak across. And users should be able to see what's remembered about them and delete it. That's a privacy requirement in most places, and it's also the fastest way to find out your memory is wrong.

Layer 4: searchable history

The last layer isn't memory in the usual sense. It's retrieval over everything the agent didn't keep: past sessions, tickets, documents, commit history. The agent asks for it when it needs it, with a search tool, instead of carrying it around.

This is the right home for most of what people instinctively want to put in memory. "What did we decide about the retry policy in March?" doesn't need to be a memory if the meeting notes are searchable. Memory is for the small set of facts that should shape every conversation; search is for everything else.

Where it usually goes wrong

  • Treating the context window as storage. Bigger windows make this tempting. They also make each call slower, pricier and vaguer.
  • Summaries that keep the narrative and lose the facts. If an ID or a decision didn't survive compaction, the agent will either ask again or, worse, guess.
  • Memory with no forgetting. An agent confidently using a preference from eight months ago is more annoying than one that asks.
  • No visibility. If nobody can see what was compacted or remembered, nobody can debug why the agent did something odd. Log what went into each request, not just what came out.

This is part of the wider job the harness does around a model, which I've written about in what's in an agent harness.

The short answer

The model has no memory. The harness has a context budget, a summariser, a notebook and a search box. Memory is how well it uses them.

Keep the working context lean, compact with a summary that keeps facts, write durable memory on purpose, and search for the rest. An agent built that way remembers what matters and, just as importantly, forgets what doesn't.