What you will be able to do
- List everything in a Claude API request that counts toward the context window, including cached prefixes and thinking
- Explain why context rot makes curating the window as important as its size
- Configure tool result clearing and thinking block clearing with context editing, and predict how each one affects prompt caching
- Choose among tool search, programmatic tool calling, prompt caching and context editing based on where the context pressure comes from
Key concept
Context rot — As the context window fills, the model gets less accurate and recalls less of what it was given. So the job is to curate what goes into the window, not to fill it just because there is room.
1.What actually fills the context window
The context window is the model's working memory for one request. It holds all the text Claude can refer to while it writes a response, and the response itself takes up room too. In a standard API conversation nothing falls out by itself. Each turn's user message and assistant reply are added and kept in full, so every request carries the whole history plus the new message. The 1M-token models (including Claude Opus 5.5, Claude Sonnet 5.5 and Claude Fable 5.1) offer a lot of room. Other models, such as Claude Sonnet 4.5, have 200k. But the docs are clear that more context isn't automatically better. Accuracy and recall drop as the token count grows. The exam guide calls this "context drift", and the documentation calls it context rot. It is why the rest of this lesson is about removing things.
Before you can prune anything, you need an accurate count of what is in the window. Everything in the request counts: the system prompt, every message (including tool results, images and documents), and your tool definitions. Claude's output for the turn counts too, extended thinking included. Each response's usage field reports what the request used, and the token counting API estimates a request before you send it. Previous thinking blocks are handled differently by model generation. Claude Opus 4.5 and later Opus models, Claude Sonnet 4.6 and later Sonnet models, and the Fable and Mythos models keep them by default, and they count as ordinary input. Earlier Opus and Sonnet models and all Haiku models strip them automatically when you pass them back.
| Item | Counts toward the window? | Note |
|---|---|---|
| System prompt and tool definitions | Yes | Sent on every request |
| Tool results, images, documents in messages | Yes | Pile up turn after turn |
| Output for the current turn, including extended thinking | Yes | Thinking tokens are part of max_tokens |
| Previous thinking blocks | Depends on model | Kept on Opus 4.5+ / Sonnet 4.6+ / Fable / Mythos; stripped on earlier Opus/Sonnet and all Haiku |
| Cached prompt prefix | Yes | cache_read_input_tokens and cache_creation_input_tokens both count |
Some models can see their own budget. Claude Sonnet 5, Sonnet 4.6, Sonnet 4.5 and Haiku 4.5 have context awareness. The API puts a <budget:token_budget> tag in the system prompt and sends a usage system_warning after each tool call, so the model can pace a long task against the space it has left. You never send these tags yourself. Claude Opus 4.7 and later Opus models, Claude Sonnet 5.5, and the Fable and Mythos models don't get them. For those, the beta task budgets feature lets you give an explicit budget.
Checkpoint 1 of 5· Check yourself
A team caches a 40k-token system prompt and its tool definitions. Every later request is a cache hit. How much of that prefix counts against the context window on those requests?
Cached input is reported in cache_read_input_tokens and cache_creation_input_tokens. Both count toward the window, just as input_tokens does. Caching lowers the price, not the footprint.
“Cached prompt prefixes still occupy the context window: prompt caching changes what you pay for those tokens, not whether they count.”Source: platform.claude.com
Sources1
2.Pruning stale tool output with context editing
In agentic workflows, tool results usually grow fastest. A file Claude read twenty turns ago, or a search result it has already used, keeps taking space and attention on every later request. Context editing deals with this by clearing specific content from the history according to rules you set. The docs frame this as curation, not only a way to cut cost: context is finite, it gives diminishing returns, and irrelevant content hurts focus. You turn it on with the context-management-2025-06-27 beta and a context_management object that lists edits.
Checkpoint 2 of 5· Fill the gap
Which strategy type completes this minimal request that turns on tool result clearing with default settings?
response = client.beta.messages.create(
model="claude-opus-5-5",
max_tokens=4096,
messages=[{"role": "user", "content": "Search for recent developments in AI"}],
tools=[{"type": "web_search_20250305", "name": "web_search"}],
betas=["context-management-2025-06-27"],
context_management={"edits": [{"type": " ? "}]},
)clear_tool_uses_20250919 is the tool result clearing strategy. clear_thinking_20251015 clears thinking blocks, and compact-2026-09-04 is the beta header for compaction, not a context editing strategy.
Source: platform.claude.comOnce the trigger is crossed, the API clears the oldest tool results in chronological order. Each one becomes a placeholder. By default only the results are cleared and Claude's tool calls remain. Set clear_tool_inputs to true to clear the call parameters as well. The edit happens on the server before the prompt reaches Claude, so your client keeps the full, unmodified history. You don't have to sync your local state with the edited version.
context_management={
"edits": [
{
"type": "clear_tool_uses_20250919",
# Trigger clearing when threshold is exceeded
"trigger": {"type": "input_tokens", "value": 30000},
# Number of tool uses to keep after clearing
"keep": {"type": "tool_uses", "value": 3},
# Optional: Clear at least this many tokens
"clear_at_least": {"type": "input_tokens", "value": 5000},
# Exclude these tools from being cleared
"exclude_tools": ["web_search"],
}
]
},| Option | Default | What it controls |
|---|---|---|
| trigger | 100,000 input tokens | When clearing starts (input_tokens or tool_uses) |
| keep | 3 tool uses | How many recent tool use/result pairs survive |
| clear_at_least | None | Minimum tokens to clear, or the strategy is skipped |
| exclude_tools | None | Tools whose uses and results are never cleared |
| clear_tool_inputs | false | Also clear the tool call parameters |
clear_at_least exists because of prompt caching. Clearing tool results invalidates the cached prefix at the point where content is removed, so a small clear means paying a cache write for very little space. If the API can't clear at least the amount you set, it skips the edit. The companion strategy, clear_thinking_20251015, uses keep (for example {type: "thinking_turns", value: 2}, or "all") to decide how many recent thinking turns survive. Keeping them preserves the cache. Clearing them frees space but invalidates the cache from that point. You can list both strategies in one edits array.
Checkpoint 3 of 5· Check yourself
An agent's tool result clearing fires often, each time freeing only a few hundred tokens, and its cache hit rate has collapsed. Which change targets this most directly?
Every clear invalidates the cached prefix. clear_at_least makes each clear free a minimum number of tokens, so the cache write pays for itself. Thinking clearing also invalidates the cache when it clears.
“If the API can't clear at least the specified amount, the strategy will not be applied.”Source: platform.claude.com
Sources2
3.Matching the fix to the source of bloat
Context editing removes results after they have served their purpose. That is only one of four approaches, and each one works at a different point in the pipeline. Tool definitions and the tool_result blocks that pile up both use the window, so start by finding where your tokens are actually going. Tool search keeps definitions out of the window until Claude asks for them: one tool_search tool replaces dozens of upfront schemas, at the cost of an extra turn. Programmatic tool calling turns a chain of calls into one script that runs in Anthropic's code execution sandbox, so the intermediate results never enter the history. Prompt caching doesn't reduce the token count. It only cuts what you pay for a stable prefix.
Checkpoint 4 of 5· Match them up
Match each approach to the context pressure it reduces
Tap a term, then the definition that fits it.
Each approach targets a different source of pressure. Only prompt caching leaves the in-context token count unchanged.
“Prompt caching doesn't reduce the number of tokens in context, but it reduces what you pay for them on subsequent requests.”Source: platform.claude.com
You can combine these approaches. The suggested order for a high-volume agent is: cache tool definitions from day one, add tool search once the toolset passes roughly 20 tools, add context editing when conversations run long enough for early results to stop mattering, and consider programmatic tool calling when you see repeated chains of small calls.
Checkpoint 5 of 5· Exam question
Before a long session compacts, a team wants to archive the full, unsummarized transcript for audit purposes. Which mechanism should they use?
Correct answer: A — A PreCompact hook, which runs custom logic before compaction occurs and can write the full transcript somewhere before the summary replaces it
- A. Correct. PreCompact is a hook that runs custom logic before compaction occurs, which is the documented way to archive the full transcript ahead of summarization, and it receives a trigger field (manual or auto).
- B. Incorrect. PostToolUse fires after individual tool calls, not specifically before compaction, so it isn't positioned to archive the pre-compaction transcript as a whole.
- C. Incorrect. The compact_boundary message marks that compaction happened; it is not documented as carrying the full unsummarized transcript as an embedded field.
- D. Incorrect. Compaction happens mid-session while the context window fills, well before the session ends, so a SessionEnd hook would fire too late to intercept the pre-compaction state.
Sources3
Exam traps
Each one states something that sounds right. Open it to see what is actually true.
1.Prompt caching shrinks how much of the context window a large, stable prefix uses.Why is that wrong?
Cached tokens still count toward the window in full. Caching only changes their price, so it does nothing against context rot or the window limit.
Covered in What actually fills the context window
2.After turning on context editing, your client has to delete the cleared tool results from its own message history to stay in sync.Why is that wrong?
Context editing runs on the server before the prompt reaches Claude. The client keeps and resends the full, unmodified history.
3.Tool result clearing is free from a caching point of view, because it only removes tokens.Why is that wrong?
Clearing invalidates the cached prefix where content is removed, so each clear costs a cache write. clear_at_least exists to make sure that cost is worth paying.
Sources
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.
“Everything in the request counts toward the context window: the system prompt, every message in messages (including tool results, images, and documents)”
↩︎ What actually fills the context window“The output Claude generates for the turn, including its extended thinking, counts too.”
↩︎ What actually fills the context window“On earlier Opus and Sonnet models and all Haiku models, the API automatically strips previous thinking blocks from the conversation history”
↩︎ What actually fills the context window“Context awareness is automatic: there is nothing for you to enable, and you never send the tags shown in this section yourself.”
↩︎ What actually fills the context window“As token count grows, accuracy and recall degrade, a phenomenon known as context rot.”
↩︎ Key concept“Cached prompt prefixes still occupy the context window: prompt caching changes what you pay for those tokens, not whether they count.”
↩︎ Exam trap 1“Cached prompt prefixes still occupy the context window: prompt caching changes what you pay for those tokens, not whether they count.”
↩︎ Checkpoint - 2.
“context is a finite resource with diminishing returns, and irrelevant content degrades model focus.”
↩︎ Pruning stale tool output with context editing“Context editing is applied server-side before the prompt reaches Claude.”
↩︎ Pruning stale tool output with context editing“By default, only tool results are cleared.”
↩︎ Pruning stale tool output with context editing“Your client application maintains the full, unmodified conversation history.”
↩︎ Exam trap 2“Invalidates cached prompt prefixes when content is cleared.”
↩︎ Exam trap 3“The API replaces each cleared result with placeholder text indicating to Claude that it was removed.”
↩︎ Prediction“If the API can't clear at least the specified amount, the strategy will not be applied.”
↩︎ Checkpoint - 3.
“Tool definitions and accumulated tool_result blocks consume your context window.”
↩︎ Matching the fix to the source of bloat“The intermediate results never enter the conversation history.”
↩︎ Matching the fix to the source of bloat“These approaches compose.”
↩︎ Matching the fix to the source of bloat“Prompt caching doesn't reduce the number of tokens in context, but it reduces what you pay for them on subsequent requests.”
↩︎ Checkpoint