Blog · 2026-06-21· 4 min read

How Do You Manage Context in Long-Running Claude Agents (Memory Tool + Context Editing)?

Claude context management for long-running agents rests on two layers: context editing automatically clears stale tool calls near the token limit, and the memory tool persists what matters to files outside the window. In Anthropic's 100-turn web-search eval, context editing cut token use by 84% and let agents finish work that would otherwise fail from context exhaustion. This is the heart of CCA-F Domain 5 (Context Management and Reliability, 15% of the exam).

D5cca-fcontext-managementmemory-tool
Loop the mascot as a librarian-archivist running a two-layer context system: a compaction desk that trims stale scrolls and a vault of memory files that persists the important ones, illustrating Claude context management for long-running agents.

Quick answer

Claude context management for long-running agents is a two-layer system. Context editing clears stale tool calls inside the window when you near the token limit; the memory tool persists what matters to files outside it. Anthropic reports context editing cut tokens by 84% in a 100-turn eval and rescued runs that would have failed from context exhaustion. Knowing which lever fixes which failure is the core of CCA-F Domain 5.

What breaks when a Claude agent runs too long?

A single Claude call has a fixed context window. A long-running agent does not respect that fixedness: every tool call, every result, every reasoning step accumulates. Two failure modes follow.

First, context exhaustion: the agent simply runs out of room and the run fails mid-task. Second, the lost-in-the-middle effect: even before you hit the hard limit, the model attends less to content buried in the middle of a bloated window, so accuracy quietly degrades. Domain 5 of the CCA-F is built around recognizing these two failures and picking the right fix.

The two layers: trim inside, persist outside

Anthropic ships two distinct capabilities for this, and the exam wants you to know they are not interchangeable.

DimensionContext editingMemory tool
Where it actsInside the context windowOutside the window (a file directory)
What it doesClears stale tool calls/results (and optionally thinking blocks) near the limitClaude creates, reads, updates, deletes memory files that persist across sessions
LifespanWithin the current runAcross sessions and compaction boundaries
Primary winAvoids context exhaustion (84% fewer tokens in a 100-turn eval)Nothing critical is lost when the window is trimmed
Best forTool-heavy agents that accumulate bulky resultsPlans, user preferences, intermediate results to carry forward

Exam-format facts here are reported by Anthropic from its own evaluation (🟡 reported; the 84% figure is workload-dependent, so reproduce before trusting it on your stack). The capability shapes - editing clears in-window, memory persists out-of-window - are first-hand from the platform docs (🟢 first-hand).

How it actually works: just-in-time context

The design pattern Anthropic recommends is just-in-time context retrieval. Instead of loading everything the agent might need into the opening prompt, the agent stores what it learns in memory and pulls it back only when a step requires it. The active window stays focused on what is relevant right now, which lowers cost and sidesteps the lost-in-the-middle effect.

Worked example - a 100-turn research agent

  1. Turns 1-20: the agent runs web searches; each returns a bulky result block. The window fills fast.
  2. Context editing fires: as the limit nears, stale search results from turns 1-10 are cleared. The conversation continues instead of failing.
  3. But the agent still needs the key finding from turn 7. Before that result was cleared, the agent wrote it to a memory file ("finding_7: vendor X has no SOC2").
  4. Turn 60: the agent reads memory, retrieves finding_7, and uses it - even though the original result left the window 40 turns ago.
  5. Result: the run completes. Editing kept the window alive; memory kept the important fact.

A name for it: Trim-and-Persist

Trim-and-Persist: a long-running agent needs two context layers that do opposite jobs. The TRIM layer (context editing plus server-side compaction) keeps the active window small by removing stale content. The PERSIST layer (the memory tool) keeps the important content alive by writing it outside the window. Get the split wrong - trimming without persisting - and the agent forgets the one fact it needed at turn 60.

Naming the split makes the exam easier: when a Domain 5 scenario says "the agent lost track of an earlier decision," the answer is almost always a PERSIST failure (no memory), not a TRIM failure.

Why it matters for the CCA-F exam

Domain 5, Context Management and Reliability, is 15% of the CCA-F on its own, but its reach is larger because reliability concepts recur inside the other four domains: a Domain 1 multi-agent question often turns on how sub-agents hand off context, and a Domain 3 Claude Code question can turn on compaction behavior. The distractors in this domain reward precision:

  • Confusing the memory tool with RAG (memory carries the agent's own working state; RAG retrieves external reference knowledge).
  • Reaching for a bigger model or a bigger window when the real fix is clearing stale tool results.
  • Assuming compaction alone is enough, when the critical fact needed memory to survive the summary.

Start at the Claude Certified Architect certification hub for the full Domain 5 blueprint, ground the mechanics on the context window primitive, then go applied with handling long prompts and context update sequencing. For the authoritative API behavior, read Anthropic's own context-management documentation and memory-tool guide before you rely on a specific number.

How to apply it in one sitting

  1. Name the failure first. Context exhaustion (the run dies) or quiet accuracy drop (lost-in-the-middle)? The fix differs.
  2. Turn on the TRIM layer for tool-heavy agents: server-side compaction as the base, context editing to clear stale tool results.
  3. Add the PERSIST layer for anything that must survive a trim: write plans, decisions, and key findings to memory, and read them back just-in-time.
  4. Test on a long run, not a short one. These problems only appear past the window limit - a 5-turn demo will never surface them.

Long-running reliability is the difference between an agent that demos well and one that survives production. Trim what is stale, persist what is critical, and the 100th turn behaves like the first.

01 · Read next in the pillars

Where this lands in the exam-prep map

Each blog post bridges into the evergreen pillars. These are the most relevant follow-ups for this story.

02 · FAQ

6 questions answered

What is the difference between context editing and the memory tool?
They solve different halves of the same problem. Context editing works INSIDE the window: as you approach the token limit, it automatically clears stale tool calls and results (and optionally thinking blocks) so the conversation can continue (🟢 first-hand: Anthropic's context-management docs). The memory tool works OUTSIDE the window: Claude creates, reads, updates, and deletes files in a memory directory that persist across sessions, so important facts survive even after the in-window content is trimmed (🟢 first-hand: the memory-tool API docs). Editing trims; memory persists.
How much does context editing actually save?
In Anthropic's reported 100-turn web-search evaluation, context editing reduced token consumption by 84% and let agents complete workflows that would otherwise fail from context exhaustion (🟡 reported by Anthropic; reproduce on your own workload before quoting a number). The exact saving depends on how tool-call-heavy your agent is - the more bulky tool results you accumulate, the more clearing stale ones helps.
When should I use compaction versus context editing versus memory?
Use all three as layers. Compaction (server-side summarization) is the primary strategy for long conversations: it keeps the active context manageable without client-side bookkeeping. Context editing adds targeted clearing of stale tool results and thinking blocks. Memory persists the critical facts across compaction boundaries so nothing important is lost in the summary. A robust long-running agent typically runs compaction plus memory together, with context editing as the in-window pressure valve.
Does the memory tool replace RAG or a vector database?
No. RAG retrieves from a large external corpus you did not generate in-session; the memory tool persists what the AGENT itself learned or decided during its run (a plan, a user preference, an intermediate result). They are complementary: RAG brings in reference knowledge, memory carries forward working state. On the CCA-F, confusing the two is a classic Domain 5 distractor.
What is just-in-time context retrieval?
Instead of stuffing everything the agent might need into the prompt up front, the agent stores what it learns in memory and pulls it back on demand when a step needs it. This keeps the active window focused on what is currently relevant, which both lowers cost and reduces the lost-in-the-middle failure mode. It is the memory-tool design pattern Anthropic recommends for long-running workflows.
Why does this matter so much for the CCA-F exam?
Domain 5 (Context Management and Reliability) is 15% of the exam and it shows up INSIDE the other domains too - a Domain 1 multi-agent question often hinges on how sub-agents hand off context, and a Domain 3 Claude Code question can hinge on compaction. The exam rewards knowing which lever (compaction, editing, memory) fixes which failure, not just defining each term.
Blog post · D5 · Blog

How Do You Manage Context in Long-Running Claude Agents (Memory Tool + Context Editing)?, complete.

You've covered the full ten-section breakdown for this primitive, definition, mechanics, code, false positives, comparison, decision tree, exam patterns, and FAQ. One technical primitive down on the path to CCA-F.

More platforms →