Find what fills the context, then remove it
Read a long-running agent's token profile and symptoms, name what fills its context, and pick the remedy (tool result clearing, compaction, subagents or fewer tool definitions) that removes it without losing what the task still needs.
Scenario
Your invoice-reconciliation agent works through a supplier's backlog. At request 48 it matches an invoice it already reconciled, and it quotes the amount from a corrected PDF it read an hour ago.
Nobody changed the prompt. The agent didn't get worse at reconciling: its context filled up with things it no longer needed.
Explanation
Everything in a request counts toward the context window: the system prompt, every message (tool results included), the tool definitions, and the output. More context isn't automatically better. As the token count grows, recall and accuracy degrade (context rot), so the goal is the smallest set of high-signal tokens the next step needs.
Diagnose before you fix. Break the input down by component (the usage field and token counting give you the numbers), then ask two questions: what is the biggest part? and does the task still need it?
| What fills the context | Typical symptom | Remedy |
|---|---|---|
| Old tool results the agent has already used (file reads, query rows, search hits) | Quotes stale data; input grows with every call | Tool result clearing (context editing): past a token trigger, the API clears the oldest results and keeps the most recent few |
| A long back-and-forth: decisions, corrections, discussion | Nears the window; early agreements fade | Compaction: older turns are replaced by a summary Claude writes; recent turns can stay word for word |
| Bulky exploration where only the conclusion matters | One context mixes up many sub-searches | Subagents: each explores in its own clean context and returns a condensed result |
| Many tool definitions | Large input before any work; wrong tool picked | Tool search or a smaller tool set: load only the definitions a request needs |
Keep what the task still needs. Each remedy throws something away, so protect the rest:
- Tool result clearing can exclude tools whose results must stay, and combined with the memory tool Claude is warned before clearing and can save key findings to files first.
- Compaction suits tasks with steady progress. It is a poor fit when later steps need exact early details or must track exact state across many values: write those to a file instead, or give compaction your own summary prompt that names what to keep.
- A subagent's summary is all the lead agent sees, so say what the summary must contain.
A bigger window or a higher max_tokens treats none of these: the noise is still there.
Worked example
The reconciliation agent's request 48 (input: 142,000 tokens):
| Component | Tokens |
|---|---|
| System prompt | 3,000 |
| Tool definitions (4 tools) | 2,500 |
| Conversation text | 9,500 |
fetch_invoice results (41 of them) |
127,000 |
Diagnosis: 89% of the input is invoice PDFs the agent has already matched. They are the cause of the stale amount, and the task doesn't need them again. The rules (system prompt) and the list of what is done do matter.
Fix: clear old fetch_invoice results, keep the last three, and have the agent record each match before the raw result goes. Context editing is in beta, so the request also sends the beta header the context editing page names; the strategy id below is the one the docs use at the time of writing:
{
"context_management": {
"edits": [{
"type": "clear_tool_uses_20250919",
"trigger": {"type": "input_tokens", "value": 60000},
"keep": {"type": "tool_uses", "value": 3},
"exclude_tools": ["get_matching_rules"]
}]
}
}
Plus one line in the system prompt: "After matching an invoice, append its number and status to reconciled.md; check that file before fetching an invoice." Input now stays under about 60,000 tokens, and the done-list survives the clearing.