Engineering Around a Stochastic Component

Context Budgets, Caching and Why a Big Window Is Not a Plan

A long context window is permission to be sloppy and a bill for being sloppy. Treat the window as a curated budget with a cache strategy.

Large context windows removed a constraint and created two new failure modes: quality that degrades as you fill the window, and cost that grows while nobody is watching. The advanced skill is treating context as a curated budget with an explicit cache strategy, not as a bucket you pour history into.

More context measurably degrades output

Two effects compound. Material in the middle of a long prompt is attended to less reliably than material at the edges, which has replicated across models. And irrelevant retrieved text actively pulls answers off course rather than being harmlessly ignored — adding three marginal chunks to a correct retrieval can turn a right answer into a confidently wrong one. Benchmarks that look for a single exact string planted in bland filler pass easily and tell you almost nothing, because real work means reasoning over several facts scattered among near-duplicates that look just as relevant.

  • Full tool payloads. Return the fields the model needs; keep the raw response server-side behind an identifier it can request if it genuinely needs more.
  • Prior turns that are no longer live. Compress the thread into decisions made and state still open, then drop the transcript.
  • Duplicate retrieved chunks and repeated boilerplate headers, which are pure cost and active distraction.
  • Long policy documents resent on every call. Move stable rules into a cached prefix, or into weights if the volume justifies a fine-tune.

Prefix caching is the lever most teams drop

Vendors cache a prompt prefix, matched from the very first token. Cached input bills at a fraction of fresh input and returns faster. The entire discipline follows: stable content first — system rules, tool schemas, worked examples, the corpus that will not change this turn — and volatile content last, meaning the user's message, the timestamp, the session identifier, anything per-request.

The classic self-inflicted wound: putting the current date, a request identifier or a randomised greeting at the top of the system prompt. One changing token at position five invalidates the entire prefix on every call for every user. Your cache hit rate is zero, your bill is several times what it should be, and nobody finds it because the output looks perfectly fine. Move volatile content to the bottom and verify the hit rate in the response metadata rather than assuming.
  1. Stable prefixrules, schemas, examples — cached, never reordered
  2. Retrieved evidencereranked, deduped, trimmed to what is used
  3. Live turnuser message, timestamp, ids — always last

Prefix order

stable first, volatile last — one changing token near the top zeroes your cache discount silently

Memory is a store, not a longer transcript

Durable memory means extracting what is worth keeping — preferences, decisions, entities, open state — into a real store and retrieving the relevant handful, not appending forever. A growing transcript costs more every turn, degrades with length, and still forgets what matters, because recency and importance are not the same thing. Write memories explicitly with a timestamp and a source, and make them visible, editable and deletable. A user who cannot correct what the system believes about them stops trusting it, and they are usually right to.

Compaction for long-running work

When an agent approaches its window, naive truncation drops the goal and keeps the chatter, which is exactly backwards. Compact deliberately: preserve the original objective close to verbatim, the decisions already made, the current state, and the open question. Discard intermediate tool noise and superseded attempts. Handing a fresh context a tight brief outperforms dragging a bloated one forward, and it is cheaper — the compaction call is one cost, carrying a full history for another fifty turns is fifty.

Key takeaways

1Filling the window costs quality as well as money; irrelevant context actively degrades correct answers
2Order the prompt stable-first and volatile-last, then verify your actual cache hit rate in the response metadata
3Memory belongs in a queryable store with timestamps and a delete path, not in an ever-growing transcript
4Compact long agent runs around objective, decisions, state and open question — discard tool noise

This is one lesson of 541.

The whole Foundation is free — 16 subjects, beginner to advanced. Sign in to track what you have read, take the end-of-module quizzes, and use the terminal.