AI agent memory management with the Anthropic API: a practical guide

Persist what re-deriving would get wrong, compact on purpose, and keep shared memory in one place.

By · Published

AI agent memory management with the Anthropic API means deciding what your application stores between calls, because the API itself keeps nothing. Each request carries the conversation you send; the context window is working memory with a ceiling. Durable memory is whatever you persist outside it and load back when the next task starts.

This guide covers what to persist, how to manage the window, how to wire memory in Pydantic AI, and how several agents can share it.

What does memory mean for an agent built on Claude?

The Messages API supports stateless multi-turn conversations: you pass earlier turns in the messages parameter. Nothing survives between requests unless you send it again. Four layers keep this manageable:

Layer Holds Lifetime
Context window What the model sees now One request
Session state Messages and tool results of one run One run
Project files Stable rules, such as AGENTS.md or CLAUDE.md As long as true
External store Decisions, dead ends, state, read on demand Project lifetime

Mixing them up means a decision lives in whichever layer heard it first.

What should you store, and what can the model re-derive?

The test: if re-deriving costs tokens but not correctness, leave it out. If it risks a different answer, store it.

Store Let the model re-derive
Decisions and their reasons File structure, function names
Dead ends ("this test setup does not work") Fixes already in the code
Environment facts: versions, deploy target, variable names What the diff or git log shows
Hard constraints ("never touch migrations/") Anything one command reveals

A wrongly re-derived decision is the expensive failure: the agent reopens a closed question or walks into a known dead end.

How do you manage the context window without losing decisions?

Compact before the window fills

Waiting for a failed request loses the agent's working state. Track the input token count on each response and compact at a threshold you choose: summarize the older turns, keep the last few verbatim, and continue from that. The API also offers server-side tools for this, in beta: compaction, which replaces older turns with a summary Claude writes on the server, and context editing, which clears old tool results before the prompt reaches the model.

Make the summary keep what matters

A generic summary drops the parts that count. Ask it to keep:

  • decisions and the reason for each;
  • dead ends, so they are not explored again;
  • the task's state: done, in progress, blocked;
  • the files and branches in use.

Drop raw tool output and stack traces; keep their conclusions. Then write the summary outside the window, to a file or a store, so the next run starts from it.

How do you wire memory in Pydantic AI?

Pydantic AI gives three hooks:

  • Instructions or a dynamic system prompt, registered with a decorator, run before the request. Load the stable rules and the decisions log there, because the agent must always follow them.
  • Tools, declared with @agent.tool, let the model fetch what a task needs: a file, a past summary, a list of dead ends. A prompt prefix costs tokens on every call; a tool costs them only when used.
  • Message history: pass earlier messages with message_history to agent.run, read them back with result.all_messages() or result.new_messages(), and serialize them with ModelMessagesTypeAdapter to store a run and resume it later (message history documentation).

Split rules and lookups: rules in the instructions, everything else behind tools.

How do several agents share memory?

Repository files are the floor. AGENTS.md gives every agent the same briefing, CLAUDE.md does the same for Claude Code, and a decisions log records what was settled. Their limit is that they hold only what someone remembered to write down, and Codex, Cursor and your own Pydantic AI agent each keep their own conversation.

The next step is one store every agent reads and writes, often over MCP, with one path for changes so two agents do not overwrite each other: append-only entries, or proposals reviewed before they count. Fileaify provides that store as a hosted workspace: decisions, state and services in documents with fixed sections, read over REST or its MCP connector, changed through proposals your publication policy reviews, with API keys kept in a separate vault and used without being shown to the agent. The trade-off is depending on a service, where repository files are free and versioned with the code.

What does a minimal production setup look like?

  1. In the repository: AGENTS.md for standing rules, DECISIONS.md for dated choices and their reasons.
  2. In your agent loop: load DECISIONS.md into the instructions, expose the rest as tools, and write a short summary of decisions and state at the end of every run.
  3. Compact on a threshold you set, and keep the summary outside the window.
  4. Move to a shared store when agents run on several machines, when several of them change the same records, or when keys must stop appearing in each tool.

Sources

Try it

The demo runs on a prepared project, with no account. To give your agents one shared project record, create a free workspace and give your agent https://fileaify.com/agent.

Common questions

Does the Anthropic API remember anything between calls?

No. The Messages API is stateless: you send the earlier turns with each request. Anything an agent must remember later has to be stored by your application.

When should an agent compact its conversation?

Before the request fails, with a summary you control. Server-side compaction and context editing exist in beta on the API; in your own loop, watch the input token count and summarize at a threshold you choose.

Can a Pydantic AI agent and Claude Code share memory?

Yes, if the memory lives outside both: repository files for stable rules, and a store both reach for decisions and state, for example over MCP.

Do I need a vector database for agent memory?

Not to start. Decisions, dead ends and environment facts fit in a few dated files. Add semantic search when the store grows past what an agent can read at the start of a task.

Written with AI assistance, reviewed by Stéphane Mermet and checked against the product. Fileaify publishes this guide and sells the service it describes.