Context Window Management
Mechanics (window contents, overflow behaviour, compaction and context editing, memory tool) are in context preservation. This note is the architecture decision.
Budget the window (verified facts, design use)
- Everything counts: system prompt, messages including tool results, documents, images, tool definitions and the model's output including thinking.
- Windows are 1M tokens on the current model families; maximum output is 128K tokens. Long-context requests are billed at standard pricing except Haiku 5.5 above 100,000 tokens (verified).
- Context rot: accuracy and recall degrade as tokens grow, so a bigger window is not a licence to include everything.
Where information should live
| Information | Best placement |
|---|---|
| Stable rules, tool definitions, reference | System prompt or cached prefix (D2.2) |
| Large corpus, queried selectively | Retrieval or tools, loaded just in time (D3.6) |
| Facts that must survive resets | Memory tool or state files (client-side) |
| Old tool output no longer needed | Cleared by context editing |
| Very long dialogue | Compaction (summary) |
| Exploration noise | A subagent's own context |
Choosing a long-context strategy
| Situation | Choice |
|---|---|
| Conversation grows past the window | Compaction (on demand or at a token threshold; beta) |
| Agent loop accumulates tool results | Context editing (clear old tool results; configurable trigger and keep) |
| Work spans sessions | Memory tool plus progress files; start fresh sessions by reading state |
| Many tools bloat the prompt | Tool search with deferred loading (D3.5) |
| Heavy exploration | Subagents |
Compaction keeps active context small; memory preserves what must survive summarisation (verified guidance).
Placement inside a prompt
Long documents at the top, query at the end; wrap documents in <document> tags with source metadata.
Cost and latency implications
More tokens cost more and slow responses; cached prefixes reduce price but still occupy the window; clearing content invalidates the cache, so clear enough tokens to justify the rewrite. Use the token counting API to estimate requests.
Exam traps
- "1M tokens, so include everything."
- Relying on a summary to carry exact identifiers with no persistent facts.
- Context editing that thrashes the cache (small, frequent clears).
- Treating the memory tool as hosted storage. It is client-side.
References
- Context windows — checked Oct 16, 2026
- Compaction — checked Oct 16, 2026
- Memory tool — checked Oct 16, 2026
Check your understanding
A 400-turn support session nears the window. Options?Show answerHide answer
Answer
Compaction for the dialogue, memory for durable facts, context editing for old tool results.Why does clearing tool results need
clear_at_least?Show answerHide answerAnswer
Clearing invalidates the cached prefix; clear enough to pay for rewriting it.Where does the memory tool store data?Show answerHide answer
Answer
In your application's storage; Claude only requests operations.