Context Engineering
Deciding what goes into the context window, in what order. The discipline that replaced prompt engineering once systems got complicated.
On this page
Prompt engineering asks how to word a request. Context engineering asks a bigger question: given a limited budget of tokens, what belongs in it and in what order?
For a single question, wording dominates. For an agent with tools, retrieved documents, conversation history, and memory, the wording is a small part of what determines output quality. Everything competes for the same budget.
What competes
Every request assembles some subset of:
- System prompt — role, constraints, output conventions
- Tool definitions — one schema per available tool
- Few-shot examples
- Retrieved documents
- Conversation history
- Tool results from this session
- Stored memories
- The actual current request
- Room for the answer
Each is useful. Together they exceed any window. Context engineering is the allocation decision.
Position matters as much as content
The single most actionable fact: attention is not uniform across the context. Material at the beginning and end is used more reliably than material in the middle.
Two independent causes. Some architectures use approximate attention that does not look everywhere. And even with exact attention, the model must have learned to use distant positions — extreme distances were rare in training.
The layout that follows:
[system prompt] ← stable, cached, high attention
[tool definitions] ← stable, cached
[few-shot examples] ← stable, cached
[retrieved context] ← variable, strongest items first and last
[conversation history] ← variable, truncated as needed
[current request] ← last, highest attention
Stable content first serves two purposes at once: it occupies a high-attention position, and it enables prefix caching, which is nearly free cost reduction. Variable content last.
Within retrieved context, order by relevance and put the strongest items at the edges rather than dumping them in ranked order — which buries item three in the low-attention middle.
Less is frequently more
The counterintuitive result worth internalizing: adding relevant-ish material often makes output worse.
Retrieving twenty chunks where five suffice costs four times as much and produces worse answers. The extra fifteen dilute attention, and occasionally one gets used — producing a confidently wrong answer sourced from a passage that merely looked related.
This is why reranking improves quality rather than just efficiency, and why “we have a million-token window so send everything” is a mistake. Precision beats volume.
Tool definitions are context too
Easy to forget: every tool definition is tokens on every request. Twenty verbose definitions is substantial overhead per call, and selection accuracy degrades as the toolset grows.
Route to a relevant subset rather than exposing everything. This helps cost and accuracy simultaneously.
Managing growth
Agent contexts grow every iteration. The techniques, in order of value:
Compact tool results. The highest-return intervention. Return identifiers and summaries; provide a separate tool to fetch full content on demand.
Truncate old history, always preserving the task statement explicitly. Losing the goal to truncation is a common and baffling failure.
Restate the objective each iteration. Cheap, and it counteracts goal drift as the transcript grows.
Externalize state. Write findings to a file the agent can re-read. The only approach that scales to genuinely long tasks.
Isolate contexts. Give subtasks their own short transcripts — the strongest argument for multi-agent systems.
What to remember
- Context engineering allocates a limited token budget among competing content, and matters more than wording once systems get complex.
- Position matters: put stable content first (high attention plus prefix caching) and the current request last.
- Within retrieved context, put the strongest items at the edges, not in ranked order.
- Adding marginal content degrades output — precision beats volume, which is why reranking improves quality.
- Tool definitions consume context and degrade selection; route to a subset.
- For growth: compact tool results, preserve the goal through truncation, restate the objective, externalize state.
Next: Coding Agents