1Overview
Mechanisms that keep a long-running agent inside its context window by summarising, clearing or externalising parts of the conversation as it goes.
Context Engineering is about what you put in. This chapter is about what the system takes out while it is running — automatically, mid-conversation, without ending the session. → Compaction, context editing, and the memory tool: three mechanisms with three different trade-offs, and a clear statement of which are generally available and which are still behind a beta flag. → The measurable outcome is a hundred-turn agent that never overflows and never silently forgets the one thing it needed.
The runtime half of context. Not what you write into the prompt, but what the system removes while the agent is still working — compaction, context editing, and a memory tool the model writes to itself. Three mechanisms, three trade-offs, and a hundred-turn run that survives all of them.
1.2After this chapter you can
→Explain what fills a context window in a long agent run, in order of size
→Use compaction to continue past the window without restarting
→Clear stale tool results and thinking blocks without losing the thread
→Give an agent a memory tool and decide what it is allowed to write
→Measure the token and quality cost of each mechanism
→Tell a generally-available capability from one behind a beta header, and plan for both
1.3When to reach for it
Any agent that runs for dozens of turns or accumulates large tool results — coding agents, research agents, anything with a loop.
1.4Key parts
A compaction trigger and threshold, a policy for which tool results may be cleared, a memory store the model can write to, and a way to see what was dropped.
1.5Free vs paid
These are model-platform features, so the cost is tokens: compaction spends tokens to save tokens and only pays off on long runs.
1.6Watch out
Compaction is lossy by design. What survives is what the summariser thought mattered, and you find out which that was at the worst possible moment.
2Lessons 7
2.1 Identify which parts of an agent run consume the most context
The Info panel displays system prompt and tool schema lines alongside token counts for each block.
Prioritise cuts by ranking context contributors from largest to smallest
- Open the transcript of a long run using File Explorer
- Search for sections that contain tool output with the Search box
- Scroll through the file and highlight blocks labelled as thinking with the Scroll bar
- Locate the system prompt and tool schema lines in the Info panel
- You'll see A token breakdown on a real 60‑turn coding‑agent session where tool results dominate the input tokens
- Takeaway When an agent stops early, the first suspect is the accumulated tool results rather than the conversation text
- Check How can you determine which sections of a long‑run transcript are consuming the most tokens before an agent stops early?
- Cost Free — this is a reading exercise against a transcript you already have.
2.2 Continue past the context window without restarting
The anthropic-beta: compact-2026-01-12 header activates the compaction feature.
Enable compaction so a long run proceeds beyond the token limit instead of erroring or requiring a manual restart
- Add the header anthropic-beta: compact-2026-01-12 to every API request
- Set the
trigger parameter to define when compaction fires, e.g. {"type": "input_tokens", "value": 150000} - Send the request and let the API perform the extra sampling step that creates a compaction block
- Read the
usage.iterations array in the response and note the separate compaction entry - Sum all entries in
usage.iterations to calculate the total tokens billed for the call
- You'll see The response’s
usage.iterations array contains two entries – one compaction entry and one normal message entry – while only a single paragraph of output is shown - Takeaway Compaction trades extra tokens for the ability to keep a run alive beyond its original limit
- Check What header and trigger setting must you add to an API request so that compaction creates a separate usage entry beyond the normal token limit?
- Cost Real money on every trigger: a full extra model call over (most of) your accumulated context, on top of the turn you actually wanted.
2.3 Clear stale tool results while preserving conversation flow
The context-management-2025-06-27 header enables context editing with strategies like clear_tool_uses_20250919 and clear_thinking_20251015.
Drop old tool payloads and thinking blocks without invoking compaction’s summarisation tax
- Enable beta context editing by selecting the
context-management-2025-06-27 header - Configure the tool‑result strategy to clear_tool_uses_20250919 and set its parameters such as
keep, clear_at_least and exclude_tools - Configure the thinking‑block strategy to clear_thinking_20251015 and adjust its
keep setting for the model you are using - Inspect the response’s context_management.applied_edits field to verify how many tool uses and input tokens were cleared
- You'll see
applied_edits reports cleared_tool_uses: 8, cleared_input_tokens: 50000 after a run with many large tool calls - Takeaway Context editing acts as a scalpel that removes only the specified tool results and thinking blocks, leaving everything else untouched
- Check Which beta header and strategy names allow you to clear old tool results and thinking blocks without invoking summarisation during a run?
- Cost No separate sampling charge — clearing is inline, not a summarization pass. The cost is entirely in what you can no longer see once it’s gone.
2.4 Enable an agent to store facts safely
Adding a tool entry {"type": "memory_20250818", "name": "memory"} to the tools array activates the memory tool.
Persist facts across sessions while restricting what the agent can save
- Edit your configuration file and insert
{"type": "memory_20250818", "name": "memory"} into the tools array - Implement a server endpoint that processes requests to
/memories/* for operations view, create, str_replace, insert, delete, rename - Validate each incoming path so it resolves inside
/memories; reject any request that escapes this directory - Add a system‑prompt instruction such as “Only write information relevant to the current project” to define the agent’s writing policy
- Restart the application and start a new session to confirm the agent calls view on
/memories automatically
- You'll see A new session starts with Claude issuing a view request on
/memories before any other action - Takeaway The real value lies in the policy that tells the agent what is worth writing
- Check What configuration change and system‑prompt instruction let an agent safely store facts across sessions while restricting its write scope?
- Cost No per-call surcharge for the tool itself; each
view/create/str_replace is a normal tool-call round trip, so a chatty memory habit adds turns — and therefore tokens — like any other tool would.
2.5 Compare token usage and recall for each mechanism in a long run
Logging the usage.iterations array distinguishes compaction entries from regular message entries.
Instrument a lengthy run to measure token costs and verify the agent’s memory after compaction or context edits
- Select a run that exceeds the compaction (150 K tokens) or context‑editing (100 K tokens) thresholds
- Add logging for
usage.iterations on every API call and separate sums for entries of type compaction and message - Log
context_management.applied_edits and sum cleared_input_tokens across the run to capture saved tokens from context editing - Count memory‑tool calls such as
view, create and str_replace to gauge tool round‑trip cost - Later in the same run, query the agent about a detail that was removed by compaction or cleared by a tool result
- You'll see The agent confidently gives an incorrect answer about information that was cleared three turns earlier
- Takeaway Token accounting shows runtime cost but not accuracy loss, which you discover by deliberately testing recall
- Check How do you instrument a long run to compare token usage from compaction, context editing, and memory‑tool calls and then test recall of cleared information?
- Cost Time, not tokens — this lesson is the one place in the chapter where the expensive resource is your own attention to the transcript.
2.6 Identify stable and beta API mechanisms for release planning
The memory tool (memory_20250818) is listed as generally available in the platform docs.
Create a reference table that shows which Anthropic runtime features are GA and which remain beta
- Open platform.claude.com in a browser
- Locate the documentation for the memory tool (
memory_20250818) and note its status as generally available - Locate the documentation for Compaction (
anthropic-beta: compact-2026‑01‑12) and record that it requires a beta header - Locate the documentation for Context editing (
anthropic-beta: context-management-2025‑06‑27) and record its beta header requirement - Create a table listing each feature, its version identifier, and whether it is GA or beta
- You'll see A side‑by‑side table listing the memory tool as GA and compaction plus context editing as beta, each with its required header string
- Takeaway Distinguish GA from beta because only GA guarantees unchanged behaviour across releases
- Check Which documentation pages must you consult to classify the memory tool, compaction, and context editing as GA or beta features for a release plan?
- Cost Free to reason about; the cost of skipping this lesson shows up as a production incident with a confusing root cause months later.
2.7 Choose the right context‑management mechanism for your agent
The keep window and exclude_tools settings control what survives when using context editing, while compaction summarises everything else.
Decide which token‑level strategy to apply and what information must be stored in memory
- Restate the warning precisely: compaction summarises, context editing deletes, and only what the summariser or the
keep window deems important survives - Select context editing for a tool‑heavy run that is not yet near a hard token ceiling, because it is cheaper and lets you control exemptions via
exclude_tools - Choose compaction when the conversation will keep growing regardless of clearing, as it resets the ceiling at the cost of a sampling tax
- Use the memory tool for any fact that must survive across restarts or compaction events, writing those facts to memory before they are needed
- Combine the approaches: apply compaction to keep active context small and rely on the memory tool for the handful of indispensable facts
- You'll see You can name, for your own agent, which failure mode belongs to each mechanism and what you have allowed to be lost
- Takeaway The real decision is what you refuse to lose and write that to memory; everything else may be lossy
- Check When deciding between compaction, context editing, and the memory tool, what key factor determines which mechanism you should apply to a given agent run?
- Cost Free — but the decision you make here is the one that determines whether the next compaction event costs you a summarized detail or a fact that was already safe in memory.