Context Preservation
How the window works
- Everything counts toward the window: system prompt, every message (including tool results, images, documents), tool definitions, and the model's own output including thinking. With prompt caching, all of
input_tokens,cache_read_input_tokensandcache_creation_input_tokenscount; caching changes price, not whether tokens count. - More context is not automatically better. As tokens grow, accuracy and recall degrade: context rot. Curating what is in context matters as much as capacity.
- Windows: 1M tokens on the current model families listed in the docs; 200k on some older models. A request can generate up to 128k output tokens.
- Overflow: if the input alone exceeds the window, the API returns a 400 ("prompt is too long"). On Claude 4.5 and newer, if input plus
max_tokensexceeds it, the request is accepted and generation may stop withstop_reason: "model_context_window_exceeded". Estimate with the token counting API. - Context awareness (model tracks its remaining budget) exists on some models (for example Sonnet 5, Sonnet 4.6, Haiku 4.5); newer models do not get the injected tags, and task budgets (beta) provide an explicit budget.
Placement inside a prompt
For long inputs (20k+ tokens) put long documents at the top, above the query and instructions; putting the query at the end can improve quality by up to 30% in tests on complex multi-document inputs. Wrap each document in <document> tags with <source> and <document_content> subtags.
Strategies that keep information alive
| Strategy | What it does | Notes (verified unless marked) |
|---|---|---|
| Persistent facts block (design guidance) | A short block of key facts (ids, decisions, constraints) re-stated at a stable position | Protects critical details from being lost in the middle or in summarisation |
| Trim tool output (verified) | Return only high-signal fields from tools | Bloated tool results waste context (2.1) |
| Context editing | Server-side clearing of old tool results (clear_tool_uses_20250919) or thinking blocks (clear_thinking_20251015) | Configurable: trigger (for example input tokens), keep (how many tool uses to keep), clear_at_least, exclude_tools. Clearing invalidates the cached prefix, so clear enough to be worth it |
| Compaction | Server-written summary replaces older turns | Beta. On demand (you decide when; header compact-2026-09-04, top-level compaction parameter) or at a token threshold (API decides). You can write your own summarisation prompt and keep recent turns word for word |
| Memory tool | Client-side /memories files Claude reads and writes | Survives resets; see below |
| Subagents | Isolate exploration in their own context | Only the final message returns (1.3) |
Memory across sessions
The memory tool ({"type": "memory_20250818", "name": "memory"}) is client-side: Claude requests operations (view, create, str_replace, insert, delete, rename) and your application executes them against storage you control. Restrict every path to /memories (path-traversal protection is your job). Use it with compaction: compaction keeps active context small; memory keeps what must survive summarisation. For multi-session work, an initializer session sets up a progress log and checklist; later sessions read them first.
Exam traps
- "Just use the biggest window." Context rot makes curation necessary.
- Putting the question before long documents.
- Relying on a summary to preserve exact ids or numbers with no persistent facts block.
- Letting tool results accumulate unbounded.
- Treating the memory tool as server-side storage. You implement and secure it.
References
- Context windows — checked Oct 16, 2026
- Compaction — checked Oct 16, 2026
- Context editing — checked Oct 16, 2026
- Memory tool — checked Oct 16, 2026
- Prompting best practices — checked Oct 16, 2026
Check your understanding
Where do long documents and the question go in a prompt?Show answerHide answer
Answer
Documents first, query last.Who stores memory-tool files?Show answerHide answer
Answer
Your application; Claude only requests the operations.Why use
clear_at_leastwith context editing?Show answerHide answerAnswer
Clearing invalidates the cached prefix; clear enough tokens to justify the cache rewrite.