The context window as a budget
System prompt, tools, history and tool results share one window; when it fills, truncation, clipping or summarising decide what is kept and what is lost.
Check yourself ↓ three questions and what to remember, at the end
Loading the animation…
Concept
Everything the model reads shares one window: the system prompt, the tool definitions, the task, the model's own messages and every tool result. The window also has to leave room for the answer. In the animation the harness keeps 512 tokens free for the answer, and compacts when the next prompt would pass 80% of the window.
The task reads four design notes, each a few hundred tokens of mostly background with a few facts the answer needs (★). With a 3,000-token window and no strategy, the run stops at turn 5: the prompt no longer fits. The three strategies keep it going, and lose different things:
- Truncate drops the oldest messages until the prompt is back under 50% of the window. It is cheap and blind: here it threw away mem, bw, params, kv, the two notes read first, every number the answer needs.
- Clip cuts old tool results to their first 6 lines and keeps a marker of what was cut. A fact near the top of a note survives; one further down does not: it lost bw, kv, free, batch.
- Summarise asks the model to write a summary of the older messages and replaces them with it. Here the summariser keeps every fact marked important, and nothing needed was lost. It is not free: each summary is another model call that reads everything it replaces, so the run cost $0.0227 against $0.0181 for truncation.
With a 4,000-token window, compaction fires once, at the end. Summarising then loses only facts nobody needed (the meeting day and an old revision note).
Maths
With window , reserve for the answer and trigger fraction , the harness compacts before turn when . Truncation then removes the oldest messages until
with the target = 0.5 here. Summarising replaces messages by a summary , for the price of one more model call that reads them all:
It pays when enough later calls re-read the saving; and it is the only one of the three that can keep a fact from a dropped message.
Code
The trigger, cut from the vendored engine (harness.ts):
const before = this.promptTokens(system, toolNames, msgs, turn);
if (strategy === "none" || before + reserve <= ctx.trigger * window)
return msgs;
const target = ctx.target * window;
const keep = ctx.keep_last as number;
What the public harnesses do
Claude Code compacts automatically: it "summarizes conversation history when approaching context limits", accepts instructions on what to keep (/compact Focus on code samples and API usage), and counts its own compactions and clearing of old tool results as expected cache rebuilds, because rewriting the history changes the prompt prefix (Claude Code, manage costs, accessed 2026-10-07). The same page notes that /compact "reads the conversation it summarizes, so compacting a large context is itself a large request", the cost in the second equation above. Clipping tool output before it reaches the model is the other lever: the same page shows a hook that filters test output down to the failures.
Keeping things out of the window: what 2025–26 harnesses add
Everything above manages a window that is already full. The features the harnesses and APIs added in 2025–26 stop things getting in at all, and each one answers a cost this site measures. None of them is modelled by the engine; the arithmetic is the same as above.
- Tool search (deferred tool loading). Chapter 2 showed that four tool definitions cost 424 tokens on every call. With tokens of definitions, calls re-send , however few of the tools are used. On the Claude API you mark tools
defer_loading: trueand add a tool search tool: the context then starts with only the search tool and the tools you keep loaded, and a search adds the definitions it finds (up to 5 by default). Anthropic's page puts a typical five-server setup at about 55k tokens of definitions and says tool search usually cuts this by over 85%. Deferred tools are kept out of the cached prefix and found tools are appended inline, so the cache survives (tool search tool). The OpenAI Agents SDK has the same idea asToolSearchToolwithdefer_loading=True(tools). - Skills. Instructions loaded only when needed. A skill is a folder whose
SKILL.mdfront matter (a name and a description, about 100 tokens) sits in the system prompt; the body (under 5k tokens) enters the context only when a request matches the description, and bundled scripts run without their code entering the context at all (Agent Skills). It is the same trade as tool search, applied to instructions: a short, stable index in the prefix, the content on demand. - Code execution over MCP. Calling tools from code the model writes, instead of one tool call per turn, so a large intermediate result goes from one tool to the next inside the sandbox rather than through the context; "the agent only sees what you explicitly log or return". Anthropic's example falls from 150,000 tokens to 2,000 (code execution with MCP, 2025-11-04). In this site's terms it is result clipping done by the agent itself, with the sandbox of chapter 7 as the price.
- Todo and plan tools. A written task list the model keeps up to date, which survives a compaction better than the reasoning that produced it. The Claude Agent SDK still has
TodoWriteand the Task tools, but now provides them by default only on older models (Opus 4.7, Sonnet 4.6, Haiku 4.5 and earlier), because newer models "track multi-step work without a written todo list" (track todos). A good example of a harness feature that a better model made optional.
Sources accessed 2026-10-16. The window-filling side of the same question, measured on a real corpus, is Agent Context Explained.
Check yourself
Commit to an answer before you reveal it: a wrong guess you can explain teaches more than a right one you skimmed. Every number in the answers comes from the same tested model as the chapter’s.
Reveal the answer
Answer: Summarise
Summarise. Truncating dropped the two notes read first, and with them every number the answer needs; clipping kept only each note's first lines. Only a summary can carry a fact out of a message it removes.
Pick one, then reveal.
Reveal the answer
Answer: 1.26 (counted right within 10%)
It cost $0.0227 against $0.0181. Each summary is another model call that reads everything it replaces; it pays only if enough later calls re-read the saving.
Commit to a number, then reveal.
Reveal the answer
Answer: 5 (counted right within 10%)
The prompt no longer fits after a few notes have been read, and the harness has nothing to drop. The reserve of 512 tokens for the answer counts against the window too.
Commit to a number, then reveal.