Context Engineering
Official scope (Exam Guide v1.0): context window management, drift and bloat prevention (tool output pruning, compaction), and isolation through subagents or multi-step workflows.
What counts
System prompt, every message, tool definitions, tool results, images, documents, and the model's own output including thinking. Cached tokens still occupy the window: caching changes price, not size. Windows are 1M tokens on current models.
More is not better
As tokens grow, accuracy and recall degrade: context rot. Curate; do not fill.
Overflow
- Input alone over the window → 400 ("prompt is too long").
- Input plus
max_tokensover the window (Claude 4.5+) → accepted, may stop withstop_reason: model_context_window_exceeded. - Estimate first with the token counting API.
Drift and bloat (design guidance on symptoms, verified tools)
- Drift: early instructions lose influence over a long task. Keep critical rules in the stable system prompt, restate them, or keep a short persistent facts block (ids, decisions, constraints).
- Bloat: stale tool output, whole files where a section would do, oversized tool results.
| Remedy | What it does |
|---|---|
| Trim tool output | Return only high-signal fields; cap result size |
| Context editing (verified) | Server-side clearing of old tool results or thinking blocks, with trigger, keep, clear_at_least, exclude_tools; clearing invalidates the cached prefix |
| Compaction (verified, beta) | A summary replaces older turns, on demand or at a token threshold; keep decisions, open questions and state, drop dead ends |
| Just-in-time retrieval | Keep identifiers; fetch detail when needed instead of preloading |
| Memory tool (verified) | Client-side files Claude reads and writes; survives resets; you implement and secure it |
Isolation
Run noisy work (search, exploration, large reads) in a subagent or a separate workflow step; only its final summary enters the main context.
Structured handoff (design guidance)
For multi-session work, end a session by writing state: goal, done, next, constraints, key files. A fresh session recovers quickly from it.
Prompt vs context (design guidance)
The prompt says what to do; the context is what the model knows while doing it. Fix the right one.
Exam traps
- "Use the biggest window" instead of curating.
- Assuming cached tokens do not use the window.
- Letting tool results accumulate unbounded.
- Relying on a summary to preserve exact ids, with no facts block.
- Treating the memory tool as Anthropic-hosted storage.
References
- Context windows — checked Oct 16, 2026
- Context editing — checked Oct 16, 2026
- Compaction — checked Oct 16, 2026
- Memory tool — checked Oct 16, 2026
- Subagents in the SDK — checked Oct 16, 2026
- Handling stop reasons — checked Oct 16, 2026
Check your understanding
Late in a long session the agent ignores a constraint from the start. Cause and two remedies?Show answerHide answer
Answer
Drift/context rot; move the rule to the system prompt or a facts block, and compact or clear stale context.Tool results from 30 steps ago still sit in context. Cheapest fix?Show answerHide answer
Answer
Clear old tool results (context editing).500-file codebase. Load everything or retrieve per task?Show answerHide answer
Answer
Retrieve just in time; explore in a subagent.Input fits, but input plus
max_tokensdoes not. What can happen?Show answerHide answerAnswer
Generation may stop withmodel_context_window_exceeded.