ANTM
AgentsExplainer

AI Agent Memory: Notes, Compaction and Context Editing Compared

An agent that runs for hours outgrows its context window. Three mechanisms keep it going, and each one forgets differently.

Editorial desk

Published 6 min read

Three wireframe cubes in a row on a dark grid floor with a glowing teal horizon line passing through them; the first two are empty outlines and the third holds a lattice of coral points
Illustration generated with AI (FLUX.1 [schnell] (Black Forest Labs) via Cloudflare Workers AI, Apache 2.0). Prompt and direction by ANTM.

An agent that works for hours will eventually have more history than fits in its context window, and what it does at that point decides whether the job finishes. AI agent memory is the set of mechanisms that answer one question: when the window is full, what does the agent still know? There are three common answers. The agent can write notes to storage and read them back, the platform can summarize old turns, or the application can clear old tool results by rule. This guide compares them using the current Claude API documentation, because that documentation describes all three precisely. For the basics of the loop that fills the window, see how AI agents work.

Why a bigger window is not the whole answer

A context window is working memory. Anthropic's engineering team treats it as a finite resource and reports that a model's ability to accurately recall information from the context decreases as the context grows, which it calls context rot.[2] Their stated goal is the smallest set of high-signal tokens that makes the desired outcome likely.[2] That framing matches what we cover in long context windows: capacity versus reliability: fitting text in the window is a different property from using it reliably.

There is also a plain cost reason. Everything in the window is sent and billed on every turn of the loop; see tokens, context windows and what an AI request costs.

Mechanism 1: notes in external storage

The idea is old. The 2023 MemGPT paper proposed "virtual context management", borrowing from operating systems that move data between fast and slow memory so a system appears to have more memory than it does, and evaluated it on document analysis and multi-session chat.[5] Anthropic's engineering post describes the same pattern as structured note-taking: the agent regularly writes notes to memory outside the context window and reads them back later. Their example is Claude playing Pokémon, keeping tallies, maps and strategy notes across thousands of game steps.[2]

Anthropic ships this as the memory tool. The documented facts:

  • Claude stores and retrieves information in a directory of memory files under /memories, and the files persist between sessions.[1]
  • It is client-side. Claude requests file operations and your application executes them against storage you control, such as a per-user directory or keys in a database.[1]
  • The tool entry is {"type": "memory_20250818", "name": "memory"}, and the documentation lists six commands: view, create, str_replace, insert, delete and rename.[1]
  • When the tool is present, the API adds a system instruction to view the memory directory first and to assume the context may be reset at any moment.[1]
memory_agent.py
import anthropic
from anthropic.tools import BetaLocalFilesystemMemoryTool

client = anthropic.Anthropic()
memory = BetaLocalFilesystemMemoryTool(base_path="./memory")

runner = client.beta.messages.tool_runner(
    model="claude-opus-5-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Remember that Acme Corp prefers email follow-ups."}],
    tools=[memory],
)
print(runner.until_done().content)

This is the documentation's own Python example, shortened. Because you own the handler, you own the risks. The documentation warns that a path such as /memories/../../secrets.env can escape the directory unless every path in every command is validated, and recommends capping file sizes, expiring stale files and stripping sensitive data before writing.[1] Our reading: treat the memory directory like any untrusted-writer input, because an agent that read a poisoned web page may write that content into its own memory. The security checklist in how AI agents work, and how to secure them applies directly.

The documentation also describes a pattern for work that spans many sessions: a first session sets up a progress log and a feature checklist, each later session starts by reading them, and each session ends by updating the log. It adds that a feature should be marked complete only after end-to-end verification.[1]

Mechanism 2: compaction

Compaction replaces the older turns of a conversation with a summary that Claude writes on the server, so you need no summarization code of your own.[3] The documentation describes two beta forms. With compaction on demand, you decide when by sending a request, and the returned block goes first in messages in place of the messages it summarizes; its beta header is compact-2026-09-04. With compaction at a token threshold, the API compacts when input tokens reach a trigger you set.[3] A third option is your own summarizer on the client.[3]

The trade-off is built into the word summary. Anthropic's engineering post says that when it compacts, Claude Code keeps architectural decisions and unresolved bugs while dropping redundant tool output, and advises first maximizing recall, then improving precision.[2] The documentation notes you can write your own summarization prompt when the default summary drops something a later turn needs, and that recent turns can be kept word for word.[3] Both are admissions that a summary can lose a detail the next step depends on.

Mechanism 3: context editing

Context editing clears content by rule instead of summarizing it. It is applied server-side before the prompt reaches Claude, so your client keeps the full history.[4] Two strategies are documented (beta header context-management-2025-06-27):

  • Tool result clearing (clear_tool_uses_20250919) clears the oldest tool results once a threshold is crossed and replaces each with placeholder text. Defaults: trigger at 100,000 input tokens, keep the 3 most recent tool use and result pairs. Options include clear_at_least, exclude_tools and clear_tool_inputs.[4]
  • Thinking block clearing (clear_thinking_20251015) manages thinking blocks from earlier assistant turns.[4]

Caching matters here. The documentation says tool result clearing invalidates cached prompt prefixes when content is cleared, and recommends clear_at_least so each clear removes enough tokens to justify losing the cache.[4] That is the practical cost of this mechanism: a smaller window, but a cache miss on the next call.

Side by side

PropertyNotes (memory tool)CompactionContext editing
What it doesAgent writes and reads filesOlder turns become a summaryOld tool results or thinking cleared by rule
Survives a new sessionYes, if your storage persists[1]Not by itselfNot by itself
Who runs itYour application executes operations[1]Server-side, beta[3]Server-side, beta[4]
What can go wrongBad paths, stale or poisoned notes[1]Summary omits a needed detail[3]Cleared result was needed later; cache invalidated[4]
Best fitMulti-session projectsOne long conversationTool-heavy loops

The "what can go wrong" row for notes and the last row are our synthesis of the documented behaviour, not benchmark results. We did not run a test comparing them.

How to choose

  1. Does the work span sessions or survive restarts? You need external notes. Neither compaction nor context editing is documented as cross-session storage.
  2. Is one conversation simply getting long? Start with compaction, and write a custom summarization prompt if you can name what must never be dropped.
  3. Are large tool outputs (search results, file reads) the bulk of the window? Context editing targets exactly that. Exclude tools whose output you will need again.
  4. Is it a long-running agent? The memory documentation suggests both: compaction keeps the active context small, and memory preserves the information that must survive summarization.[1]

Whichever you pick, evaluate it. A memory mechanism changes what the model sees, so the only reliable check is a task suite that runs to the length where you expect forgetting; see how to read an AI benchmark claim for why vendor numbers will not answer that for your workload.

What remains uncertain

Compaction and context editing are marked beta in the documentation we read on 2026-10-14, so parameters and defaults may change. We have not measured how much each mechanism degrades task success; the sources describe the designs, not comparative accuracy. If you rely on one, re-read the linked pages before deploying.

Frequently asked questions

What is AI agent memory?
In practice it is whatever an agent can read back when its context window no longer holds the full history: notes it wrote to storage, a summary of older turns, or retrieved documents.[2][1]
Does the Claude memory tool store data on Anthropic's servers?
No. Anthropic documents it as client-side: Claude requests file operations and your application executes them against storage you control.[1]
What is the difference between compaction and context editing?
Compaction replaces older turns with a summary written by Claude on the server. Context editing clears specific content, such as old tool results, by rule and replaces it with placeholder text.[3][4]
Do I need memory if my context window is large?
Not always. A larger window delays the problem but does not remove it, because Anthropic's engineering team reports that recall decreases as context grows.[2]

The ANTM newsletter

The signal, not the noise.

Sourced AI coverage in your inbox. Double opt-in, unsubscribe in one click.

Referenced sources

  1. 1.
    Memory tool (Claude API documentation)(opens in a new tab)

    AnthropicPrimary sourcePublished Oct 14, 2026Accessed Oct 3, 2026

  2. 2.
    Effective context engineering for AI agents(opens in a new tab)

    AnthropicPrimary sourcePublished Sep 29, 2025Accessed Oct 3, 2026

  3. 3.
    Compaction overview (Claude API documentation)(opens in a new tab)

    AnthropicPrimary sourcePublished Oct 14, 2026Accessed Oct 3, 2026

  4. 4.
    Context editing (Claude API documentation)(opens in a new tab)

    AnthropicPrimary sourcePublished Oct 14, 2026Accessed Oct 3, 2026

  5. 5.
    "MemGPT: Towards LLMs as Operating Systems (Packer et al.)"(opens in a new tab)

    arXivResearchPublished Oct 12, 2023Accessed Oct 3, 2026

ANTM Editorial

Editorial desk

The editorial desk at AI's Next Top Model. Every article is sourced to primary documents and approved by an editor before publication. See the editorial policy for how we work.

Concentric teal orbital rings with a dashed coral line running from a small circle to a central ellipse, marked with coral ticks
Agents

How AI Agents Work, and How to Secure Them

An agent is a model that directs its own tool use. That flexibility is useful and risky. Here is how agents differ from workflows, when to use one, and the controls that matter.

Explainer4 min read