Give Claude 3.5 Sonnet a short passage and a question whose answer shares no keywords with it, and it finds the answer 87.5% of the time. Pad the passage out to 32K tokens, a sixth of the 200K the model advertises, and the score falls to 29.8%.1 The task did not change. Only the amount of text around it did. The gap between what a window can hold and what a model can actually use is the subject of this chapter, and managing that gap is most of what a harness does.
The job is context, not prompts
In mid-2025 several groups settled on the same name for this work within weeks of each other. Karpathy called it the art and science of filling the window with just the right information for the next step, and warned that too much, or the wrong kind, raises cost and lowers quality.2,3 Cognition put context engineering at the core of agent reliability.4 Manus published the lessons of building its agent around it.5 Anthropic defined it as curating and maintaining the best set of tokens during inference, with prompt writing as one subset of the job.6
The rename matters because it moves the unit of work. A prompt is written once. Context is assembled on every turn of the loop from Chapter 1: instructions, tool definitions, files, earlier tool results, the model's own previous output. Anthropic's documentation describes the harness itself as the layer that supplies tools and manages what the model sees.7 A prompt is a sentence. Context engineering is a policy that runs fifty times per task.
The advertised window is not the usable window
Every serious long-context benchmark finds the same thing: accuracy falls well before the limit on the spec sheet, and often by 8K to 32K tokens.
RULER tested 17 models on 13 tasks. All claimed at least 32K of context; only about half held up at 32K.8 NoLiMa removed literal word overlap between question and answer, so the model had to make a one-step association instead of string matching. At 32K, 11 of the 13 models it tested scored below half their short-context baseline.1 A Databricks study ran 20 models from 2K to 128K in a retrieval setting and found that only a handful of the newest stayed consistent past 64K; the rest failed in their own ways, from refusals to dropped instructions.9 Chroma's study of 18 models, GPT-4.1, Claude 4 and Gemini 2.5 among them, saw decline with length even on copying a run of repeated words. On LongMemEval, a focused prompt of about 300 tokens beat the full one of roughly 113K.10
Position matters as much as length. Lost in the Middle gave the curve its shape: with 20 documents, GPT-3.5-Turbo answered correctly 75.8% of the time when the relevant document came first, 53.8% when it sat tenth, and 63.2% when it came last. With no documents at all it scored 56.1%.11 Burying the answer in the middle made the model slightly worse than giving it nothing.
Anthropic frames the cause as an attention budget. Every token attends to every other, so relationships grow with the square of length, and training data skews toward short sequences.6 Chroma named the symptom: context rot.10 The gauge below plots NoLiMa's numbers. Drag the scanline and watch where each model leaves its own 85% line.
drag the chart to 8K. Claude 3.5 Sonnet already keeps only 71% of its score.
Models (toggle lines)
Accuracy at this length (select one to focus)
| Model | Base | 1K | 4K | 8K | 16K | 32K | Effective |
|---|---|---|---|---|---|---|---|
| GPT-4o | 99.3 | 98.1 | 95.7 | 89.2 | 81.6 | 69.7 | 8K |
| Llama 3.3 70B | 97.3 | 94.2 | 81.5 | 72.1 | 59.5 | 42.7 | 2K |
| Gemini 1.5 Pro | 92.6 | 86.4 | 75.4 | 63.9 | 55.5 | 48.2 | 2K |
| Claude 3.5 Sonnet | 87.5 | 85.4 | 77.6 | 61.7 | 45.7 | 29.8 | 4K |
These models are a generation or more old; NoLiMa is the cleanest controlled measurement, not the newest. Chroma's 2025 run on newer models, Claude 4 and Gemini 2.5 among them, found the same decline with length.10 Windows have grown since: as of October 2026, Claude Code's current models offer 1M tokens.12 A larger window raises the point where the API refuses input. None of these studies shows it moving the point where accuracy starts to fall, which is why you measure that yourself.
/autocompact 200k (any value from 100K to 1M).13Four ways context fails
Drew Breunig sorted long-context failures into four kinds.14 The taxonomy is useful because each one has a different fix, and applying the wrong one wastes a week.
| Mode | What happens | Evidence | Fix |
|---|---|---|---|
| Poisoning | A hallucination or bad result enters context and is re-read as fact on every later turn | Breunig's definition; no single benchmark | Verify tool results before writing them to notes or memory; on a detected bad fact, restart from the last checkpoint file, not the transcript |
| Distraction | History grows until the model copies past actions instead of planning | Gemini 2.5 agent past ~100K tokens | Clear stale tool results, then compact, before your measured effective length; on long tasks, restart from a handoff file |
| Confusion | Irrelevant material, often tools, pulls the model off task | Llama 3.1 8B: fails with 46 tools, passes with 19 | Expose only the tools this step needs; defer the rest behind a tool search, or route each task type to its own toolset |
| Clash | Parts of the context disagree with each other | Sharded prompts: average −39%; o3 98.1 → 64.1 | Run exploration in a subagent and return only its conclusion; keep one thread that makes decisions |
Clash deserves attention because it is the one harnesses cause. In the sharded-prompt study Breunig cites, splitting the same task's information across several turns cost 39% on average.14 An agent that writes exploratory drafts into its main thread is manufacturing clash.
Budget it like RAM
Karpathy's framing holds up: the model is the CPU and the context window is RAM.3 MemGPT made the analogy literal, with a main context the model reads directly and an external store it pages data in from.15 The practical consequence is to split cost into two lines: fixed overhead and per-step spend.
Fixed overhead is everything loaded before the user types. In Claude Code that means the system prompt, CLAUDE.md, the first 200 lines or 25KB of auto memory, skill descriptions and MCP tool names.12,7 MCP stays that small because tool search is on by default: only tool names and server instructions load at startup, and a full definition is fetched when a task needs that tool. On models older than the Claude 4 family, and on deployments that reject tool search, every definition loads upfront.16 Chapter 3 shows what eager loading costs. In your own loop the same pattern is a catalog of names plus a search tool that returns full schemas on demand. Each of those tokens is paid on every turn of every session. Per-step spend is file reads, tool output and the model's own reasoning.
Progressive disclosure keeps overhead small. Agent Skills load in three levels: name and description up front, the body only when relevant, bundled files only when read.17 The same discipline applies to instruction files. Anthropic's test for each line of CLAUDE.md is whether deleting it would cause mistakes; bloated files make the model ignore the instructions that matter.18
When spend does pile up, cut in order of cost. Claude Code clears older tool outputs first and summarizes only if that is not enough.7 At the API level, tool-result clearing triggers at 100,000 input tokens by default and keeps the last three tool uses.19 Anthropic measured context editing alone at +29% on an agentic search eval, +39% with a memory tool added, and 84% fewer tokens over a 100-turn web search run.20 As of October 2026, Claude Code compacts by default when the conversation reaches the model's limit, or at about 967K tokens on models with a native 1M window, and /autocompact sets an earlier trigger.13
Compaction is lossy, so decide what survives it. After compacting, Claude Code re-reads up to five recently modified files (larger ones come back as paths), re-injects skill bodies capped at 5,000 tokens each and 25,000 in total, and reloads CLAUDE.md and memory from disk. Nested instruction files and path-scoped rules are summarized away until Claude next reads a file that triggers them, and SessionStart hooks matching the compact source run again and add their output.12 Anthropic's advice for writing a summarizer is to tune for recall first, then precision, keeping decisions, open bugs and implementation details.6
Copy this · a minimal compaction contract: a handoff file the agent rewrites at each milestone, and a Claude Code hook that re-injects it after every compaction.21
.agent/handoff.md · the agent rewrites it at each milestone; keep it under ~40 lines
## Goal One sentence on what "done" means and the check that proves it (e.g. `npm test` green and the repro in issue #123 passes). ## Decisions (do not relitigate) - Chose X over Y because Z. ## Open bugs and unknowns - [ ] auth/session.test.ts fails about 1 run in 5; cause unknown. ## Files touched - src/auth/session.ts: refresh logic rewritten ## Next step Exactly one action the next context takes first.
.claude/settings.json · re-injects the contract after compaction; the absolute path survives the agent changing directory
{
"hooks": {
"SessionStart": [
{ "matcher": "compact",
"hooks": [{ "type": "command", "command": "cat \"${CLAUDE_PROJECT_DIR}/.agent/handoff.md\"" }] }
]
}
}
In another harness or your own loop, prepend the file to the first message after any summarization step. The contract only works if the loop, not the model, decides that it is re-read.
Move it out, pull it in
The filesystem is the cheapest memory tier because it is restorable. The lightest version is a notes file the agent writes and rereads.6 Manus has its agent rewrite a todo list every step, which pushes the goals to the recent end of context across tasks that average about 50 tool calls. It treats files as unlimited, persistent and directly operable, and compresses only in ways it can undo: keep the URL, drop the page.5
Heavier systems formalize the tiers. Letta splits memory into core blocks the agent edits in context, searchable recall history, and an archival store, and on overflow evicts about 70% of messages behind a recursive summary.22 Mem0 extracts and consolidates facts; on the LOCOMO benchmark it reported a 26% judge-score gain over OpenAI's memory, 91% lower p95 latency and over 90% fewer tokens than replaying full history.23
Every memory tier above shares a weakness: whatever gets written is believed. A wrong fact stored once is retrieved forever, which is context poisoning made permanent. Mycelium, one of my own projects, explores the opposite design: memory that has to earn its way in.
- Perceive in layers. A cheap structural pass reads the shape of each source before a deeper pass extracts entities, relationships and claims, so expensive reasoning is spent only on what structure cannot answer.
- Challenge before storing. An adversarial pass disputes every extracted claim, and a consistency check tests it against what is already known. Only reconciled facts enter, each carrying a confidence score and its provenance.
- Forget on purpose. Confidence decays unless new evidence reinforces it, and duplicates merge, so the store tends toward what is still true rather than everything ever said.
- Let structure create specialists. Dense clusters in the knowledge graph each become a specialist agent with a deep, narrow context, so no single window has to hold everything.
- Route, don't stuff. A supervising tier decides which specialists a question needs and pulls only their answers into the final context. This is the chapter's pull-not-push principle applied to memory itself.
The design bet is that memory quality is a verification problem, not a storage problem. That is the same lesson Chapter 5 draws for evals.
Retrieval has the same push-or-pull choice.
“Claude Code doesn't use RAG currently. In our testing we found that agentic search out-performed RAG for the kinds of things people use Code for.”— Boris Cherny, Hacker News (2025)24
Anthropic calls the pattern just-in-time context: hold lightweight identifiers such as paths, URLs and queries, and load the content only when a step needs it, with upfront retrieval kept for cases where speed matters.6
Subagents push the same idea further: they spend tokens in a separate window and return only a short summary, and read-only investigators reporting to one decision-making thread are the safe form. Chapter 1 covers what fan-out costs and where it breaks.
Pay attention to the cache
The cheapest token is one already sitting in the KV cache.
“the single most important metric for a production-stage AI agent”— Yichao “Peak” Ji, Manus, on KV-cache hit rate (2025)
Agent loops read far more than they write. Manus reports input-to-output ratios around 100:1, so the input price dominates the bill.5 At list prices as of October 2026, Sonnet 4.5 input costs $3 per million tokens, a cache read $0.30, a five-minute cache write $3.75, a one-hour write $6, and output $15.25 The instrument below uses Sonnet 4.5 because its multipliers, 1.25× base for a write and 0.1× for a read, are the ones most models carry. Newer models discount reads further: Sonnet 5.5 lists input at $2 and bills reads at 5% of it.25 The cache matches on prefix in the order tools, then system, then messages, and any change invalidates everything after it. Sonnet 4.5, now deprecated, needs at least 1,024 tokens to cache at all.25
That makes cache discipline an architecture rule, not a style preference. Keep timestamps out of the system prompt, append instead of editing history, serialize deterministically, and mask tools rather than removing their definitions mid-session.5 It also explains a tension from Section 4: clearing old tool results breaks the cached prefix, which is why the API lets you require a minimum amount cleared per edit so the rewrite pays for itself.19
Turn stream · prefix per turn (cyan = cache read, amber = cache write, red = prompt edit) over what that turn bills for input
drag the idle gap past 5 minutes, then drag hit rate below 22%. Either way every turn rewrites the whole prefix at 1.25× input and the cache loses money.
Run the numbers and the shape is clear. A cache miss is billed as a write at 1.25× the base input price,25 so if every miss rewrote the whole prefix, hit rates below about 22% would lose money. Past that point the 10× read discount dominates, and with output a hundredth of input, the session cost follows the hit rate almost exactly. Treat the 22% as a worst-case bound. In a healthy loop each turn reads the cached prefix and writes only the new suffix, so the hit rate is high by construction.
Two things break that in practice. The first is expiry. The default cache lives five minutes, measured from the start of the request, and each hit refreshes it for free. A test suite, a build or a human approval that runs past the window means the next turn rewrites the whole prefix at 1.25×. A one-hour cache costs 2× base to write and pays off after two reads; Anthropic suggests it for side agents that run longer than five minutes.25 The second is invalidation. Changing a tool definition invalidates the whole cache; changing the system prompt, or switches such as web search, invalidates the system and message layers.25 A mid-session edit to either is a full rewrite. On the newest models you can append a system-role message instead of editing the top-level system field, and the cached prefix stays intact.25 In your own loop, log cache-read and cache-write tokens per turn: a turn whose write is close to the whole prefix is the alarm.
Part 06 covers cost per task and model routing across a whole program.
What fills the window next
The pattern across this chapter is that a long context is a liability you manage, not a resource you fill. Keep fixed overhead small, cut stale results before summarizing, put durable state in files, pull content in on demand, and protect the cached prefix.
Most of the per-step spend, though, is tool output: file reads, search hits, command logs. How much of it lands in the window depends on what tools the agent has, how they are described and what they return. That is Chapter 3.
References
- Modarressi et al. (Adobe Research, LMU Munich), “NoLiMa: Long-Context Evaluation Beyond Literal Matching,” ICML 2025 / arXiv, February 2025. arxiv.org/abs/2502.05167
- Pure AI, “Karpathy Puts Context at the Core of AI Coding (secondary report of Karpathy’s June 2025 post),” Pure AI, September 2025. pureai.com/articles/2025/09/23/karpathy-puts-c…
- Lance Martin, “Context Engineering for Agents,” rlancemartin.github.io, June 2025. rlancemartin.github.io/2025/06/23/context_engi…
- Walden Yan, “Don’t Build Multi-Agents,” Cognition, June 2025. cognition.com/blog/dont-build-multi-agents
- Yichao “Peak” Ji, “Context Engineering for AI Agents: Lessons from Building Manus,” Manus, July 2025. manus.im/blog/Context-Engineering-for-AI-Agent…
- Rajasekaran, Dixon, Ryan, Hadfield (Anthropic), “Effective context engineering for AI agents,” Anthropic Engineering, September 2025. anthropic.com/engineering/effective-context-en…
- Anthropic, “How Claude Code works,” Claude Code docs, accessed October 2026. code.claude.com/docs/en/how-claude-code-works
- Hsieh et al. (NVIDIA), “RULER: What’s the Real Context Size of Your Long-Context Language Models?,” arXiv, April 2024. arxiv.org/abs/2404.06654
- Leng, Portes, Havens, Zaharia, Carbin (Databricks), “Long Context RAG Performance of Large Language Models,” Databricks / arXiv, November 2024. arxiv.org/abs/2411.03538
- Hong, Troynikov, Huber (Chroma), “Context Rot: How Increasing Input Tokens Impacts LLM Performance,” Chroma, July 2025. trychroma.com/research/context-rot
- Liu, Lin, Hewitt, Paranjape, Bevilacqua, Petroni, Liang, “Lost in the Middle: How Language Models Use Long Contexts,” TACL / arXiv, July 2023. arxiv.org/abs/2307.03172
- Anthropic, “Explore the context window,” Claude Code docs, accessed October 2026. code.claude.com/docs/en/context-window
- Anthropic, “Model configuration” (Default auto-compact thresholds), Claude Code docs, accessed October 2026. code.claude.com/docs/en/model-config
- Drew Breunig, “How Long Contexts Fail,” dbreunig.com, June 2025. dbreunig.com/2025/06/22/how-contexts-fail-and-…
- Packer et al. (UC Berkeley), “MemGPT: Towards LLMs as Operating Systems,” arXiv, October 2023. arxiv.org/abs/2310.08560
- Anthropic, “Connect Claude Code to tools via MCP” (Scale with MCP tool search), Claude Code docs, accessed October 2026. code.claude.com/docs/en/mcp
- Anthropic, “Equipping agents for the real world with Agent Skills,” Anthropic Engineering, October 2025. anthropic.com/engineering/equipping-agents-for…
- Anthropic, “Claude Code best practices,” Claude Code docs, accessed October 2026. code.claude.com/docs/en/best-practices
- Anthropic, “Context editing,” Claude Developer Platform docs, accessed October 2026. platform.claude.com/docs/en/build-with-claude/…
- Anthropic, “Managing context on the Claude Developer Platform,” Claude blog, September 2025. claude.com/blog/context-management
- Anthropic, “Get started with Claude Code hooks” (Re-inject context after compaction), Claude Code docs, accessed October 2026. code.claude.com/docs/en/hooks-guide
- Letta, “Agent Memory: How to Build Agents that Learn and Remember,” Letta blog, July 2025. letta.com/blog/agent-memory
- Chhikara et al., “Mem0: Building Production-Ready AI Agents with Scalable Long-Term Memory,” arXiv, April 2025. arxiv.org/abs/2504.19413
- Boris Cherny, comment on Hacker News, February 2025. news.ycombinator.com/item?id=43164253
- Anthropic, “Prompt caching,” Claude Developer Platform docs, accessed October 2026. platform.claude.com/docs/en/build-with-claude/…