Overview
Context engineering is the per-call decision about what goes into the model’s window, in what order, and within what token budget. The prompt is one part of the context, alongside system instructions, retrieved documents, tool definitions, conversation history, and tool results. For an application, output quality tracks context quality more than wording; for wording see prompt-design.
Treat the window as a budget
Budget the window across system prompt, tool definitions, retrieved evidence, history, and room for the response, and measure what each section earns. A larger window does not make irrelevant tokens free: they raise cost and latency and dilute attention. See context-window.
Retrieve, do not stuff
Pull only the evidence the current step needs, ranked by relevance, instead of pasting everything that might help. Tune chunk size, top-k, and filters so the model sees signal. See rag and rag-retrieval.
Put long documents first and the question last
For inputs over about 20k tokens, place documents above the instructions and query, wrap each in <document> tags with a <source>, and ask for quotes before analysis. Anthropic reports up to 30 percent better quality in tests with the query at the end. Models also tend to use information at the start or end of a long context better than the middle (Liu et al., “Lost in the Middle”, Transactions of the ACL, 2024), so keep the key evidence out of the middle and confirm with an eval on your model.
Compact history without breaking the cache
Summarize or drop old turns before they crowd the window, and keep a running state object instead of the raw transcript. Two constraints on Claude:
- Anthropic offers server-side compaction (beta) and context editing, which clear old tool results or thinking blocks by rule.
- On Fable 5.1, Opus 5.5, and Sonnet 5.5, editing earlier turns in place invalidates later thinking blocks and restarts the prompt cache. Keep history append-only, and send per-turn reminders as mid-conversation
role: "system"messages.
See agent-architecture-patterns and prompt-chaining.
Keep stable content at the front
Place content that does not change between calls (tools, system prompt, few-shot examples) first so it hits the prompt cache. Editing a cached prefix raises cost and latency. See prompt-caching-strategies and prompt-cache.
Log what the model actually saw
Log the assembled context per call, not just the template. Most “the model ignored the instruction” bugs are context bugs: the instruction was truncated, buried mid-window, or crowded out by retrieved text. Inspect the real window before blaming the model.