deep · 6 min
Agent memory
What an agent can get back to later, within one run and across sessions, and where that has to live since the model keeps nothing.
updated 29 Sept 2026
Picture a coding agent that spent all of Monday building half a web app. On Tuesday we start a new session and say "keep going". What does it know?
Nothing. Not the half it built, not the bug it was chasing, not which features are left. The model keeps nothing between calls. Everything it seemed to remember on Monday was text we were sending it, and that text is gone.
So when people say an agent has memory, they mean some text was put somewhere on purpose and brought back later. That leaves two questions to answer for any piece of it. Where does it live: inside the context window, where the model sees it on every call, or outside, where something has to fetch it? And how long does it need to last: a few steps, or until next week?
Let's start with the memory every agent already has.
The free kind
Inside one run, the agent loop resends the whole history every turn. Step 30 knows what happened at step 12 only because step 12 is still in the messages, the same way a chat app resends the conversation through the chat API. So at any moment, what an agent "remembers" is exactly what's sitting in its context window.
Two problems. The window fills up, and a full window gets less precise well before it runs out of room. Then the session ends and all of it disappears.
Within a run, you keep the history small with compaction: summarize old turns, or clear tool output the agent has already used. But none of that survives the end of the session.
A small block that's always there
Here's a step up. Keep a short block of notes at the top of the context on every call, and give the model a tool to rewrite it. One block might say who the user is, another what the current goal is. When it learns something new, it edits the block.
MemGPT, a 2023 research system, called this the working context: a fixed-size bit of text the model can change only by calling a function. Letta, a company that builds memory tooling, calls the same thing core memory blocks.
The catch is that these blocks ride along on every call, so they cost tokens every call. They're for a handful of facts that matter all the time. Not a growing log.
Everything else lives outside
The rest goes outside the window, and the agent reads it back with tools. This is the "write it down" move from context engineering.
The simplest version is a file the agent keeps for itself, like a to-do
list or a NOTES.md. Anthropic's agent playing Pokémon kept notes like
"for the last 1,234 steps I've been training my Pokémon in Route 1, Pikachu
has gained 8 levels toward the target of 10". When its context was reset,
it read the notes and carried on.
Claude's API ships this as a memory tool. Turn it on and the model gets a
/memories directory and six commands: view, create, str_replace,
insert, delete, rename. The API also adds a line to the system prompt
telling the model to check that folder before starting, and to assume its
context could be reset at any moment, so anything not written down could be
lost. The tool runs on our side. The model asks for a file operation and
our code carries it out against whatever storage we pick, maybe a folder
per user or rows in a database.
The other shape is a store the agent searches. MemGPT had two. One held the full message history, so the agent could search old conversations after they'd left the window. The other held longer-lived knowledge the agent could write to and search, often with vector search. Either way, the model decides when to look something up, by calling a function like any other tool.
MemGPT also wired the inside and outside together. When the prompt passed a warning level (70% of the window in their example), the system told the model that old messages were about to be pushed out. That gave it a chance to save what mattered first. When the window was full, the oldest messages moved out to the searchable history and got folded into a running summary.
Back to Tuesday
None of that fully solves our Monday-to-Tuesday problem, though. Memory across sessions is its own thing. A good picture is a project staffed by engineers working in shifts, where each new engineer shows up with no memory of the last shift. Whatever the last shift didn't write down is lost.
Anthropic ran into exactly this, trying to get a coding agent to build a production-quality web app across many context windows. Compaction alone didn't work. The agent tried to do too much at once, and later sessions declared the job done too early. What worked was treating memory as a handoff note, on purpose:
- A first session sets things up. It writes a feature list (over 200 features for a clone of claude.ai), a progress file, and makes a first git commit.
- Every later session starts the same way: check the working directory, read the git log and the progress file, pick the top unfinished feature.
- It builds that one feature, tests it end to end, commits with a clear message and updates the progress file before stopping.
Two small choices in there are worth stealing. The feature list was JSON,
because the model was less likely to wrongly rewrite a JSON file than a
Markdown one. And the agent could only flip a passes field in it, with
firm instructions never to remove or edit tests. Git did double duty: the
log says what happened, and a bad change can be rolled back.
Where people get confused
Is RAG memory? Not really. Retrieval is one way to read memory. In RAG the documents were written by people ahead of time and don't change because the agent ran. Memory is written by the agent as it works, and changes what it does next time. They often share the same search machinery, which is why they get mixed up.
Isn't a compaction summary memory? It keeps a long run going, but it doesn't outlive the session, and it can drop the one detail you needed. The usual advice is to use both: compaction keeps the window small, and memory files hold what must survive the summary.
Then there's memory that's wrong. A mistake written to a file comes back in every later session, so a bad fact that would have scrolled away now sticks around. The harness above guards against that by limiting which fields the agent may change and by only marking a feature done after an end-to-end test passes. Deleting files nobody has read in a long time helps too.
And since our code carries out every file write, we own the risks. A path
like /memories/../../secrets.env can reach outside the memory folder
unless we check every path. Models usually refuse to write sensitive data
to memory, but "usually" isn't a promise, so strip it ourselves if it
matters (see PII handling).
You'll also meet memory described with words from cognitive science: working, episodic, semantic, procedural. They come from a 2023 framework paper called CoALA. Working memory is roughly the context window, episodic is past runs and logs, semantic is saved facts, and procedural is the model's weights plus the agent's code. Handy for talking about a design. They don't tell you how to build one.
What nobody has yet is a fair way to compare these designs. MemGPT was built for models with much smaller windows than today's, and most of the rest comes from teams that sell memory tools. So for now, when two products both say "memory", the most useful thing is still to ask our two questions: where does it live, and how long does it last?
Further reading
- Effective context engineering for AI agents, Anthropic, 2025. Note-taking as memory and the Pokémon notes. A good place to start.
- Memory tool, Anthropic docs. A real memory design you can use today, with the security checks spelled out.
- MemGPT: Towards LLMs as Operating Systems, Packer et al., 2023. The window as RAM and outside storage as disk. Read section 2 for the eviction warning.
- Agent Memory: How to Build Agents That Learn and Remember, Letta, 2025. A tidy tour of the kinds of memory, and the case that retrieval isn't memory on its own.
- Effective harnesses for long-running agents, Justin Young (Anthropic), 2025. The shift-workers picture and the JSON feature list. The most practical one here.
- Cognitive Architectures for Language Agents, Sumers et al., 2023. Where the working, episodic, semantic and procedural names come from.