Context engineering.
Every piece of advice you have read about context engineering optimises one budget and quietly wrecks the other. Trimming a stale tool result out of the middle of the history is the textbook move, and on a cached loop it costs a full re-write of every token behind the cut — around twelve times the price of simply leaving it there. Context engineering is the discipline of spending two budgets at once: the attention the model can actually bring to bear, and the cache that decides what you pay to re-read the window on every turn. They pull in opposite directions, and the thing that reconciles them is not how much you put in the window but the order you put it in.
Step 1Prompt engineering words one message; context engineering curates the whole window.
Prompt engineering is the craft of phrasing a single instruction well — clear task, good examples, the right output shape. It still matters. But an agent does not run one prompt; it runs a loop, and on every turn the model is handed a whole assembled payload: the system prompt, the tool definitions, retrieved documents, memory pulled from an external store, the results of previous tool calls, the conversation history, and the current task. Context engineering is the discipline of deciding what goes into that payload, in what form, at which step.
The context window is a finite, shared budget, and it is re-spent on every single turn. That repetition is the part most explanations skip, and it is what makes context engineering an engineering problem rather than an editorial one. A twenty-step task does not read your 18,000-token preamble once. It reads it twenty times.
A useful image: the window is a desk, and the model clears it and rebuilds it from scratch before every action. Prompt engineering writes a clear note to put on the desk. Context engineering decides which reference books and tool printouts are on the desk at all — and, because the desk is rebuilt each time, how expensive each rebuild is.
Step 2Budget one: attention. More context is not better context.
The naive instinct is to stuff everything possibly relevant into the window "just in case." This backfires. Long contexts degrade in a way often called context rot or lost in the middle: as the window fills, the model attends less reliably to any given token, and material buried in the middle of a long payload gets effectively ignored. A stated 1M-token window is a statement about what the API will accept, not about what the model can use — the gap between the two is the subject of the effective-versus-advertised context deep-dive, and it is wide enough to design around.
- Distraction. Ten loosely-related documents make the one relevant sentence harder to use than a single well-chosen snippet.
- Staleness. A tool result from twelve steps ago may now be wrong. Leaving it in the window invites the model to trust it, and it will.
- Contradiction. Two versions of the same fact in one window is worse than either alone, because nothing in the payload says which is current.
So the goal is not the most context but the right context: the smallest set of high-signal tokens that lets the model take the next correct action. That conclusion is correct, and acting on it naively is what runs you into the second budget.
Step 3Budget two: the cache. The window is a prefix, and prefixes hash cumulatively.
Providers do not re-process an identical prefix on every turn; they cache it. That is the only reason long-prefix agent loops are affordable at all, and the rules it imposes are specific. Taking Anthropic's as the documented example, the cached prefix covers tools, then system, then messages, in that order, up to the point you mark. The hash is cumulative, so — in the documentation's own words — "changes at each level invalidate that level and all subsequent levels," and a hit requires the segments to be 100% identical up to the marked block.
Read that hierarchy as a mutation-rate ordering, because that is what it is. Anything you change invalidates everything behind it:
# the prefix, front to back. a change at any row # re-prices every row below it. tools tool definitions changes here invalidate everything system instructions, policy invalidates system + messages messages history, tool results invalidates from the edit point on # the classic own-goal: a timestamp or a "session started at" # line near the top of the system prompt. it is 12 tokens and # it means you never get a cache hit, on any turn, ever.
Now the prices, which are what turn a structural rule into an engineering constraint. As of November 2026 Anthropic charges 1.25× base input to write a five-minute cache entry (2× for the one-hour TTL) and 0.1× to read one — dropping to 0.05× on Claude Opus 5.5 and Sonnet 5.5. The multipliers are vendor-specific and they move; the structure does not. What matters is the ratio: a cached read is roughly twenty-five times cheaper than re-writing the same tokens.
Two documented edges that bite in practice. Prompts below the model's minimum — between 512 and 4,096 tokens depending on the model — are simply processed uncached, with no error, so a small prompt that you believe is cached is a silent cost. And a cache entry becomes available only once the first response has started, so a fan-out that fires ten identical requests in parallel writes the prefix ten times. Warm it with one call, then fan out.
Step 4Where the two budgets collide: the surgical trim.
Here is the arithmetic that should change how you build the loop. A thirty-turn agent on Opus 5.5, prefix cached at one breakpoint, holding 24,000 tokens by turn 30. Step 12 fetched a 3,000-token tool result that is now stale, and the attention budget says to take it out.
# 24,000 cached tokens at turn 30. # cache read 0.05x, cache write (5m) 1.25x base input. append-only re-read all 24,000 24,000 x 0.05 = 1,200 token-equivalents trim turn 12's 3,000-token result 10,000 before the cut, still cached 10,000 x 0.05 = 500 11,000 after the cut must be WRITTEN again 11,000 x 1.25 = 13,750 --------- 14,250 token-equivalents # you removed 3,000 tokens and paid ~12x for the turn that did it.
The penalty is one-time: the next turn reads the new prefix cheaply again. That is exactly what makes the rule counter-intuitive and worth stating plainly — trim rarely and deeply, never often and shallowly. One compaction that sheds 40% of the window and then runs for twenty more turns pays for itself many times over. A tidy-up every turn that shaves a few hundred tokens is a pure loss, and it is the behaviour that "keep the window minimal" advice produces if you follow it literally.
The design that falls out of this is append-only history with periodic, wholesale compaction at a cache breakpoint. Do not edit the middle. Replace a whole suffix, or leave it alone. The production detail lives in context compaction and KV-cache budgeting for agent loops; the cost model behind it is in context-caching economics.
Step 5The levers, reordered by what they cost.
The familiar techniques are all still right. Sorting them by where they sit in the prefix tells you which are cheap and which are expensive, which the usual flat list does not:
- Trim tool output at the source — free. A raw API response can be thousands of tokens of which three matter. Post-process it to those three fields before it is appended. Nothing is invalidated, because nothing that was already cached changed. This is the highest-leverage move in context engineering and it costs nothing.
- Retrieve, don't preload — cheap. Fetch only what this step needs with retrieval and append the result. The window holds the query result, not the corpus.
- Isolate sub-tasks — cheap, and it buys a second clean window. Hand a self-contained sub-problem to a sub-agent and return only the answer. The noisy sub-task never touches the parent's prefix. Note that the sub-agent pays its own prefix write, which is the real cost of fan-out.
- Offload to memory — cheap on write, watch the read. Push durable state into an external memory store. The trap is reading it back near the front of the window: memory that changes between turns belongs late in the prefix, not early.
- Compact the history — expensive, so batch it. See STEP 4. Summarise the old turns and drop the raw transcript, at a breakpoint, in one move, rarely.
- Swap a tool definition — the most expensive edit available. Tools sit at the very front, so adding one mid-task invalidates the entire prefix. Dynamic tool catalogues are a real technique and they are not free; if the catalogue is large, see MCP context overload.
Finally, the measurement. Both budgets are observable and almost nobody logs them: per-turn cache-hit token counts alongside uncached input tokens tells you whether your prefix is actually stable, and a near-zero hit rate on a long-running agent is nearly always one mutating line near the front. Attention has to be measured the way everything in this section is measured — with an evaluation set on tasks that look like yours, comparing outcomes as you change what goes in. The uncomfortable part is that the model cannot tell you what it is missing or what is drowning it. It will confidently act on whatever window you hand it, at whatever price your ordering implies.
Do this: order the window by mutation rate — tools, then stable policy, then stable retrieved documents, then volatile memory, then history — and put your cache breakpoint at the last block that is identical across turns. Never interpolate a timestamp, a token count or a "step 7 of 20" line above that breakpoint. Make history append-only and compact it in one deep pass rather than tidying it every turn. Then log cache-hit tokens per turn: it is one number, and it is the fastest way to find out that your careful context engineering has been paying full price all along.