S2
Context Engineering: Curate, Don't Accumulate
Treat every turn as a fresh context assembly problem: write, select, compress, and isolate.
- 01Apply write / select / compress / isolate under a fixed token budget
- 02Explain why more tokens is not better (lost-in-the-middle, context rot, cost)
- 03Name the four context failure modes and their matching fixes
The four moves of context engineering
topic 2Context is assembled fresh each turn. Four levers:
- Write — persist to a scratchpad / memory so state survives the step.
- Select — retrieve only the relevant docs, tools, and examples.
- Compress — summarize or compact history to stay small.
- Isolate — give side-quests their own windows (sub-agents).
A fixed window bar. Step 1 dumps raw history and documents — signal is buried. Selecting, then compressing, then isolating reclaim space and push the signal-density meter up.
Text description
A fixed window bar. Step 1 dumps raw history and documents — signal is buried. Selecting, then compressing, then isolating reclaim space and push the signal-density meter up.
Why more tokens ≠ better
Standard self-attention compute grows ~quadratically with length; models get lost in the middle of long inputs (Liu et al., 2023); quality rots before the hard limit; and KV/prompt caching is ~an order of magnitude cheaper on cached input only if the prefix is token-stable.
Common MisconceptionBigger window = just dump everything in.
CorrectionYou still pay quadratic cost and suffer lost-in-the-middle, rot, and distraction.
Common MisconceptionPrompt caching always saves money.
CorrectionOnly token-stable prefixes hit the cache. Volatile content (timestamps, shuffled tools) early in the prompt busts it.
The four context failure modes (Breunig)
Name the mode → know the fix
- Poisoning — a wrong fact enters context and gets repeated. Fix: prune it.
- Distraction — history grows until it drowns the instructions. Fix: compact.
- Confusion — too many tools/options muddy tool choice. Fix: scope the tools.
- Clash — contradictory retrieved chunks collide. Fix: rerank / dedupe.
Production failure modes
- KV/prompt-cache invalidation from a volatile prefix (timestamps, reshuffled tools).
- Context poisoning: a hallucinated fact recirculates and compounds.
- Tool confusion from too many registered tools.
- Stale or contradictory RAG chunks producing a clash.
References & deeper reading (2)
- Context WindowsAnthropic
- Conversation StateOpenAI Platform
Retrieval Practice
Check one idea at a time
You move from an 8K to a 200K window and stuff in every document. Mid-context accuracy drops. Best fix?