Context Engineering for AI Systems: Selecting What the Model Should See
The retrieval worked. The document was in the prompt. The model still answered wrong.

Research updated Sep 10, 2026
Key topics
The retrieval worked. The document was in the prompt. The model still answered wrong.
That failure is worth sitting with, because it kills the mental model most teams carry into production: that the context window is a container you fill with everything relevant. It is not a container. It is a budget under competition, and every token you admit is bidding against every other token for influence over the next decision.
If you already understand long-context limits and treat retrieval as evidence selection, the bridge is short: the interesting problem is no longer how do I get more context in. It is what deserves the space, in what form, in what order, and how do I prove the choice was right. That discipline is context engineering — the practice of deciding what a model sees before it reasons.
The Context Budget Is a Competition, Not a Container
Start by separating two things people routinely collapse into one: context length and context quality. Length is capacity. Quality is selection. A larger window raises the ceiling on how much you can admit; it says nothing about which evidence should win. Teams that scale the window without changing selection usually just move the failure from "the model didn't have it" to "the model had it and didn't use it well."
Four claimants compete for the same budget on nearly every call:
- Instructions — system rules, policies, output contracts.
- Retrieved evidence — documents, chunks, records pulled to support the answer.
- Task and conversation state — what has happened so far, what the user wants, what the agent has already tried.
- Tool output — schemas, API responses, execution results.
They are not additive in effect. Attention is finite, and irrelevant context is not neutral filler — it competes. A near-duplicate chunk, a stale tool result, or a chatty history segment can crowd out the passage that would have justified the correct answer. The model does not politely ignore the noise; it weighs it.
That gives the thesis of this article: context engineering is the discipline of allocating a fixed budget to the evidence and state that most changes the next decision. Not the most information. The most decision-relevant information.
This matters most under three conditions. Multi-step tasks, where state accumulates and drifts. Heterogeneous evidence, where sources differ in trust and freshness. And real cost per call, where every token is paid on every request. If your task is single-turn and well-scoped, most of what follows is overkill — and I'll say so again at the end.
What Actually Counts as Context
You cannot audit what you treat as one undifferentiated blob. So split it. Each category below has a different refresh rate, a different trust level, and a different failure mode.
- System instructions — durable, high-trust, slow-changing. Failure mode: stale rules that no longer match the product.
- Retrieved documents — volatile, medium-trust, refreshed per query. Failure mode: irrelevant or near-duplicate evidence flooding the budget.
- Structured state — durable-ish, high-trust, updated as the task progresses. Failure mode: silent corruption that propagates.
- Tool schemas and outputs — volatile, variable trust, refreshed per call. Failure mode: untrusted output treated as instruction.
- Memory — durable, medium-trust, accumulated across sessions. Failure mode: drift and poisoning.
- Conversation history — volatile, low-density, grows monotonically. Failure mode: distraction and repetition.
The distinction that pays off fastest is durable versus volatile. Durable context is stable rules, schemas, and facts. Volatile context is fresh retrieval and tool results. Mixing them in one undifferentiated block is how drift starts: a volatile fact gets rewritten into a durable-looking summary, and now a stale value looks authoritative.
One design-space note worth flagging: agent-based tools allow more structured, task-specific context injection than single-turn chat. You can scope a sub-task to its own narrow context instead of dragging the whole session along. That changes what's possible, not just what's convenient.
Audit prompt: list every source that writes into your prompt. Label each by trust (how much you'd bet on it) and volatility (how often it changes). You will usually find two or three sources nobody consciously admitted.
Selection: Choosing Evidence Before the Model Sees It
The highest-leverage decision happens before the model call, in what gets selected, ranked, and admitted. Frame retrieval as a ranking-and-admission problem, not a search problem. Search asks "what matches?" Admission asks "what earns a seat?"
That reframing exposes the precision/recall tradeoff in context terms. Precision here means the share of admitted passages that were actually relevant; recall means the share of relevant passages you managed to admit. Too much recall floods the budget with plausible-but-irrelevant material. Too little recall starves the answer of the one passage it needed. Neither is a search failure; both are admission failures.
Practical selection strategies that hold up:
- Relevance scoring — rank by match to the actual query, not the topic.
- Recency weighting — fresh evidence beats stale evidence when both are relevant.
- Source trust tiers — official docs outrank scraped commentary; enforce it in code, not in hope.
- Deduplication — collapse near-identical chunks before they compete.
- Hard caps per source type — a ceiling on how many tokens any one category can claim.
The classic failure is "retrieve top-k and hope." Top-k optimizes for a count, not for coverage of the decisive fact. Five near-duplicate chunks can crowd out the single passage that actually justifies the answer, and the model, seeing five consistent-looking sources, confidently produces the wrong one.
Decision rule: select the smallest set of passages that can justify the answer, then verify the answer is actually supported by them. Selection and verification are two steps, not one.
Compression Without Losing the Signal

Compression is a lossy engineering tradeoff with measurable consequences, not a summarization convenience. Treat it that way and you'll make better calls.
The core problem is brevity bias. Ask a model to summarize and it favors fluent generalities over specific facts. The summary reads well and drops the domain detail that made the source useful. You compressed the tokens and lost the signal.
Compression approaches, roughly in order of how much they cost you:
- Extractive trimming — keep the exact sentences, drop the rest. Lossless in content, lossy in coverage.
- Structured state extraction — pull facts into a schema. Lossless if the schema captures them; catastrophic if it doesn't.
- Rolling summaries — compress history as it ages. Convenient, and the main source of detail erosion.
- Hierarchical memory — keep summaries plus pointers to the originals, so detail is recoverable.
The failure mode to name explicitly is context collapse: iterative rewriting erodes detail across turns until the system forgets what it learned. Each summary is slightly lossier than the last, and the loss compounds silently. Research on evolving contexts describes exactly this erosion as a central problem, which is why the proposed fixes use structured, incremental updates rather than wholesale rewriting.
The useful split is lossless structural compression versus lossy semantic compression. Schemas, IDs, and references preserve facts exactly — a record ID costs a few tokens and points at the full record. Prose summaries trade precision for brevity. Use structure where the fact matters; use prose where the gist is enough.
Decision rule: compress volatile history, preserve structured facts verbatim, and never compress the evidence the current task depends on.
Ordering, Placement, and the Attention Economy
Where context sits can change how much it influences the output. This is not a formatting preference; it is a placement policy — and a task-dependent one.
Material buried mid-context competes with material at the edges, and it often loses. Instructions placed far from the decision point can get ignored under load — the model is generating near the end, and the rule it needs is thousands of tokens back. The practical consequence: the same instruction can be followed or dropped depending on where you put it.
Treat that as a hypothesis to test on your own task, not a universal law. Position effects vary by model, prompt structure, and task, so the honest move is to compare placements on your evaluation set rather than assume edge placement always wins.
Isolation strategies that reduce the competition:
- Separate instructions from evidence. Keep the rules in a stable block, the evidence in a clearly delimited one.
- Quarantine untrusted tool output. Treat external text as data, never as instruction.
- Scope sub-tasks to their own context. Give a narrow task a narrow window instead of the whole session.
There is a real tradeoff between one large context and several narrow calls. One call is cheaper in round-trips and simpler to reason about; several narrow calls are more reliable per step and easier to debug, at the cost of latency and orchestration. There is no universal winner — it depends on whether your bottleneck is cost, latency, or reliability.
Placement rule: put the decision-critical instruction and the decisive evidence close to the point of generation, then verify with a comparison rather than trusting the heuristic.
Measuring Whether Context Changes Actually Help
Here is the uncomfortable part: context engineering without a golden dataset is unverifiable. You cannot tell improvement from noise. You will ship changes that feel better and measure nothing.
A golden dataset is a set of real user questions paired with expected outputs. Build it before you build the pipeline. A small representative set is usually more valuable than a large synthetic one, because it captures the phrasing and edge cases your users actually produce. The point is ground truth you can check against.
What to measure:
- Task pass rate — did the output do the job?
- Grounding / faithfulness — is the answer supported by the admitted evidence, or is it plausible text the evidence does not back?
- Retrieval precision — how much of what you admitted was actually used?
- Token cost — what did the context actually cost per call?
- Latency — did the added context slow the system down?
The measurement that most teams skip is context inspection. You do not need to log raw prompts, tool outputs, or memory contents to debug a failure. Log small records: counts, IDs, hashes, and policy labels. The goal is not to keep more context; it is to leave enough evidence that you can tell which strategy ran and whether it changed the next call in the intended way.
That gives you the debugging question worth memorizing: did the agent load too much context, the wrong context, or miss context it needed? Three different failures, three different fixes. Without inspection records, you cannot tell them apart.
A Baseline-Versus-Change Protocol
Metrics alone do not tell you what to do when they disagree. So run context changes like experiments:
- Freeze the task set and the model version. Change one context variable at a time.
- Record a baseline on the golden set: task pass rate, support, cost, latency.
- Apply one change — a new ranking rule, a tighter cap, a compression step.
- Re-run the same set and compare against the baseline.
- Keep the change only if the primary task metric improves without breaking an explicitly chosen safety or cost constraint.
The primary metric depends on the workflow. For a support agent, it might be answer correctness; for a code assistant, whether the patch runs. Pick it before you run the experiment, or you will rationalize whatever moved.
One warning: do not optimize a proxy metric that does not track the real task. Retrieval precision can improve while task pass rate falls, because you optimized for tidy retrieval instead of correct answers. Measure the task.
Evolving Context: Playbooks Instead of Static Prompts
The frontier direction is context that accumulates and refines across runs — moving from a static system prompt to an evolving playbook of strategies and evidence. The mechanism, as described in recent research, is a loop of generation, reflection, and curation: the system proposes context, evaluates what worked, and curates the useful parts, using structured incremental updates to avoid the collapse problem above.
Treat this as a research signal, not a production default. Reported gains on agent and domain benchmarks are promising, and one framework reports adapting effectively without labeled supervision by using natural execution feedback. But benchmark results under research conditions are not proof of mainstream adoption, and the operational cost is real: an evolving context adds infrastructure, versioning, and evaluation burden. You now have to version the context itself, not just the prompt.
Decision rule: adopt evolving context only after you can measure a static context reliably. If you cannot tell whether a fixed context works, you cannot tell whether an evolving one is improving.
Where Context Engineering Breaks
Name the failure modes, and name when more context work is the wrong investment.
- Context distraction. Accumulated history pulls the model away from what it learned, producing repetition and drift. Models can start making mistakes before the window is full.
- Context poisoning. A wrong or stale fact admitted early contaminates every downstream decision. This is why trust tiers and quarantine matter.
- Cost and latency creep. Every added token is paid on every call, and long contexts raise both cost and latency. The bill scales with the pipeline, not the feature.
- The overkill boundary. Simple, single-turn, well-scoped tasks rarely justify a context pipeline. If a single good prompt solves it, build that.
- The wrong-bottleneck trap. Sometimes the failure is the model, the tool, or the task definition — not the context. Adding context to a task-definition problem makes it worse.
That last one deserves emphasis. Context engineering is a powerful lever, which makes it tempting to apply everywhere. Before you rebuild the pipeline, ask whether the bottleneck is actually context. If the task is ambiguous, no amount of evidence fixes it.
What to Build Next
Convert the model into a sequence you can actually run.
- Audit. Enumerate every context source. Label trust and volatility. Set hard token budgets per category.
- Build a small golden dataset before building the context pipeline. Real questions, expected outputs.
- Ship one selection improvement and one compression improvement. Measure both against the same evaluation set.
- Add context inspection logging. Counts, IDs, hashes, policy labels. Make the next failure diagnosable in minutes.
- Then, and only then, consider evolving context.
The next skills to build are evaluation design, retrieval ranking, and structured state management. Those three compound: better evaluation tells you which ranking change helped, and better state management gives you facts you can preserve losslessly instead of summarizing away.
The leverage question to close on: which part of your context assembly is still manual, and what would it take to make it a reusable, measurable system? A context system you cannot measure is a context system you cannot improve. Build the measurement loop first. The pipeline can wait.


