2.4 Optimising Context Windows and Token Usage
What You Need to Know
Section titled “What You Need to Know”The context window is everything that Claude sees in one request. The window is measured in tokens. Every token in the window is billed on every request where it appears. The window holds four things:
- the system prompt
- every message in the history, including tool results, images and documents
- the tool definitions
- the output that Claude generates, including thinking tokens
This lesson uses one made-up example throughout: a pharmacy whose counter staff use a Claude assistant. The assistant’s system prompt, stock-lookup tool, conversation and leaflet pages are all in the window. All of them are billed on every request.
Counting the Window
Section titled “Counting the Window”Two documented, free instruments count tokens. The usage field on every response reports what the request consumed. With caching, input tokens are of three kinds: plain, cache-read and cache-write. All three kinds count towards the window. The token counting API estimates a request before you send it. The API’s count is inexact, and the API has its own rate limits. Estimate requests before you send them, and make this part of the design. For example, the pharmacy counts its largest leaflet request before launch.
Overflow of the window is a hard failure, and it has two distinct cases. Oversized input is rejected outright. On current models, generation that reaches the limit stops early, with its own documented stop reason. You can detect both cases, so set alerts on them. Plan the token budget so that routine requests never approach either case.
Context Rot: Capacity Is Not a Strategy
Section titled “Context Rot: Capacity Is Not a Strategy”Each model has a window size from a set of tiers, and output has its own limit. A bigger window gives more space, but it does not decide what that space holds. Anthropic’s docs explain why: “As token count grows, accuracy and recall degrade, a phenomenon known as context rot. This makes curating what’s in context just as important as how much space is available.” Anthropic’s engineering guidance calls context an attention budget. The goal of that guidance is the smallest possible set of high-signal tokens (tokens that carry information the task needs) that maximises the likelihood of the desired outcome.
So ask whether every token is worth including, not whether it fits. If the pharmacy loads its whole leaflet library into a million-token window, this fails in three ways:
- context rot degrades answer quality
- the pharmacy pays for the full load on every request
- latency grows with input size
The Replay Bill
Section titled “The Replay Bill”Each turn resends the whole history. Over 20 turns at the pharmacy counter, a 50k-token history is 50k input tokens twenty times. At high volume, careless context costs more than model choice and becomes the largest cost line. This makes context strategy a Domain 2 economics decision. Curation, caching and compaction each reduce a different part of this replay bill.
The Management Toolkit
Section titled “The Management Toolkit”- Curate first. Send the fields, not the file: send the stock fields that a question needs, not the whole stock list. Summarise verbose history and remove formatting noise. Anthropic’s long-context guidance says to put longform documents, such as a leaflet, at the top of the prompt, above the query. The cheapest optimisation is to leave out tokens that the task does not need.
- Retrieve instead of sending everything. When the source corpus is larger than a sensible context, retrieval brings only the relevant passages for each request. An example is the two leaflets that a question needs. Whether to choose retrieval is a Domain 2 cost-and-context judgement. How to design the RAG pipeline (the system that retrieves the passages) is Domain 3.
- Compact long conversations. Server-side compaction (beta, Claude 4.6-generation and later models) summarises earlier turns into a compaction block when input tokens reach a trigger threshold. The conversation can then continue past the window limit. It is Anthropic’s recommended primary strategy for long-running conversations and agentic sessions, such as a full shift at the counter.
- Edit agentic context. Context editing (beta) clears the oldest tool results (and optionally thinking blocks) server-side once a threshold is reached. It replaces them with placeholders. Use it where the full history adds nothing, such as stock lookups from hours earlier.
- Make smaller savings with structure. Tight output contracts make responses shorter. Minimal, deferred tool definitions make the fixed overhead smaller.
Truncation looks like optimisation. Truncation cuts context to a fixed token count, whatever the task needs, and it loses quality silently. Curation selects by relevance. Truncation cuts by position. If you cut a long leaflet to its opening, the later sections disappear unflagged. Optimisation must preserve the information that the task needs.
In the exam
Section titled “In the exam”The exam tests whether you treat the window as a budget that you actively manage or as capacity to fill. When cost and quality problems share one question, distinguish them. Some options offer “Enable caching” as the fix for window overflow or for a decline in quality late in the conversation. Such an option is a deliberate wrong answer. Truncation is also a deliberate wrong answer. A wrong option in the guide’s own sample question cut a policy to its first 1,000 tokens.
Current window sizes, output limits and stop-reason identifiers are in the Domain 2 quick reference.
Exam Traps
Section titled “Exam Traps”Practice Scenario
Section titled “Practice Scenario”A contract-analysis assistant holds multi-hour analyst conversations. By hour two, requests carry 150k tokens of history including dozens of large stale tool results from clause lookups; costs are 6x projection and answer quality on late-conversation questions is degrading. What should the architect do?
- A. Move to the largest-window model and let the conversation keep growing without a hard limit
- B. Enable prompt caching on the conversation history so replayed turns cost a fraction
- C. Enable server-side compaction and context editing to clear stale tool results
- D. Start a fresh conversation every 30 minutes
Answer & explanation
Correct: C
- A — Treats capacity as the problem when curation is. Context rot and per-request cost both worsen as the history grows into the larger window.
- B — Cuts the cost of replayed tokens but leaves every token in the window, so the rot-driven quality decline continues. Caching changes what you pay, not what fits.
- C — Addresses both symptoms: compaction summarises earlier turns into a short, high-signal context, and clearing stale tool results reclaims window space that was adding cost and rot without adding information, while preserving analyst state.
- D — Controls cost and rot by destroying the accumulated working state. Compaction achieves the same context reduction while preserving what the session has learned.
Sources
Section titled “Sources”- Context windows — Anthropic
- Token counting — Anthropic
- Compaction — Anthropic
- Context editing — Anthropic
- Effective context engineering for AI agents — Anthropic
- Claude Certified Architect Professional Exam Guide (v1.0) — Domain 2 — Anthropic