Skip to content

Prompt caching and context management

After lessons 4, 5, and 6 your tool inventory has three layers (custom client, Anthropic-provided, MCP). After a few turns of a real conversation your context has a system prompt, tool definitions across those three layers, prior user messages, prior assistant turns, prior tool_use and tool_result blocks, and possibly server_tool_use and mcp_tool_use round-trips that produced result blocks inline. All of it counts as input tokens on every subsequent turn. Two problems compound from there.

The first is cost. Say your system prompt and tools come to fifty thousand tokens. Sent on turn one, they cost the standard input-token price for that turn. Sent again on turn two (unchanged), they cost the same again, and so on for every turn. A long session pays for the stable prefix once per turn rather than once. Prompt caching is the Anthropic-provided mechanism that turns repeated reads of a stable prefix into a sharp discount: a cached read costs a small fraction of the normal input price.

The second is staleness. The context window is finite, and the Anthropic docs are direct about what happens before you hit the ceiling: 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. Compaction and context editing are the two Anthropic-provided mechanisms for curating, server-side, what reaches the model.

This lesson covers all four pieces (prompt caching, the context-window picture, compaction, context editing) under one frame: cache the stable parts, compact or clear the long parts, and pick the lever that matches your session's shape. The capability the lesson builds maps directly onto that: cache long system prompts and stable context blocks; reason about what compaction and context editing do to a long-running session; trade cost against staleness deliberately rather than discovering both bills at month-end.

The Anthropic docs frame caching this way (verbatim): Prompt caching optimizes your API usage by letting the API resume from specific prefixes in your prompts. This significantly reduces processing time and costs for repetitive tasks or prompts with consistent elements.

The mechanism is a single field, cache_control, used in one of two ways. Put it once at the top level, and the API places the breakpoint for you and moves it forward as the conversation grows; the docs call this automatic caching and suggest starting there. Or attach it to each block you want in the cached prefix, for exact control. The shape:

{ "cache_control": { "type": "ephemeral" } }
// or with the longer TTL:
{ "cache_control": { "type": "ephemeral", "ttl": "1h" } }
# Automatic caching: one top-level field, the API places the breakpoint
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
cache_control={"type": "ephemeral"},
system="<long stable system prompt>",
messages=[{"role": "user", "content": "..."}],
)

With explicit breakpoints, there are three placement layers, and they are always honored in a fixed order, whatever order you write them in:

response = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
system=[
{
"type": "text",
"text": "<long stable system prompt>",
"cache_control": {"type": "ephemeral"},
}
],
tools=[
# custom client tool (L4)
{
"name": "get_inventory",
"description": "...",
"input_schema": {...},
"cache_control": {"type": "ephemeral"},
}
# server tools and Anthropic-schema client tools (L5) and
# mcp_toolset entries (L6) can also carry cache_control here
],
messages=[
{"role": "user", "content": "..."},
],
)

The placement order for cache-prefix construction is: tools, then system, then messages. A change at one level invalidates that level and everything after it. Each request allows only a small, fixed number of explicit breakpoints, and from each breakpoint the system looks back a limited number of blocks for an earlier cache write; the table below gives both numbers. The lookback only finds entries that earlier requests already wrote, so put the breakpoint on the last block that stays identical from one request to the next.

As of November 2026: caching ruleValue
Explicit breakpoints per requestUp to 4. A top-level (automatic) breakpoint uses one of the four slots; if four explicit breakpoints already exist, the API returns a 400 error
Lookback window20 positions per breakpoint, counting the breakpoint itself
Prefix ordertools, then system, then messages
Cache lifetime5 minutes by default, refreshed at no extra cost each time the cached content is used; 1 hour with "ttl": "1h"
Prompt below the minimumProcessed without caching; no error is returned
Cache isolationPer workspace on the Claude API, Claude Platform on AWS, and Microsoft Foundry; per organization on Claude Models on Google Cloud (Google managed) and Amazon Bedrock
SourcesPrompt caching

The pricing works in multipliers off the model's base input-token price, and the principle is simple. Writing a prefix into the cache costs a little more than sending it normally, and the longer cache lifetime costs more to write than the short one. Reading the prefix back from the cache costs much less than sending it normally, and on the newest models the read discount is deeper still. The table has each model's numbers.

As of November 2026 (price per million tokens)Base input5-minute cache write (1.25x)1-hour cache write (2x)Cache hit or refreshMinimum cacheable prompt
Claude Fable 5.2$8$10$16$0.20 (0.025x)512 tokens
Claude Opus 5.5$4$5$8$0.20 (0.05x)512 tokens
Claude Sonnet 5.5$2$2.50$4$0.10 (0.05x)512 tokens
Claude Haiku 5.5, prompts up to 100,000 tokens$0.10$0.125$0.20$0.01 (0.1x)512 tokens
Claude Haiku 5.5, prompts over 100,000 tokens$0.50$0.625$1$0.05 (0.1x)512 tokens
Claude Opus 4.8 (legacy)$5$6.25$10$0.50 (0.1x)1,024 tokens
Claude Opus 4.7 (legacy)$5$6.25$10$0.50 (0.1x)2,048 tokens
Claude Opus 4.6 and 4.5 (legacy)$5$6.25$10$0.50 (0.1x)4,096 tokens
Claude Sonnet 4.6 (legacy)$3$3.75$6$0.30 (0.1x)1,024 tokens
Claude Haiku 4.5 (legacy)$1$1.25$2$0.10 (0.1x)4,096 tokens
Break-even, per the Pricing pageAfter one cache readAfter two cache reads
SourcesPrompt caching: pricing and cache limitations, Pricing. Minimums are for the Claude API, Claude Platform on AWS, Claude Models on Google Cloud (Google managed) and Microsoft Foundry; Bedrock publishes its own.

The shape of the trade-off: you pay a small premium once, on the write turn, and a deep discount on every read after it. The short-lived cache pays back after a single hit within its lifetime; everything after that is savings. The one-hour cache costs more to write, so it needs a couple of hits to pay back, which suits long-running sessions and heavily reused system prompts. Cache refreshes (re-reads within the lifetime) cost the same discounted read price, not a new write.

Two floor rules worth knowing. There is a minimum cacheable size per model, and it differs a lot between models (the table lists it). Shorter prompts marked for caching are processed without caching, and no error is returned. And caching is exact-match: a single character change anywhere in the cached span invalidates the prefix.

Reading whether your prefix is actually hitting

Section titled “Reading whether your prefix is actually hitting”

The usage object on every response tells you what happened:

{
"usage": {
"input_tokens": 50,
"cache_creation_input_tokens": 0,
"cache_read_input_tokens": 100000,
"output_tokens": 503
}
}

The docs publish the identity: total input equals the cache-read tokens, plus the cache-creation tokens, plus the input tokens (literal below). The plain input-tokens field here means only the tokens after the last cache breakpoint (the part of the prompt that was neither read from nor written to the cache). If both cache fields are zero, nothing was cached, most often because the prompt was under the minimum. When you log production traffic, watch the share of cache-read tokens in the total; a production stack with stable prompts can push that share very high, and that share is exactly where the savings come from.

total_input_tokens = cache_read_input_tokens + cache_creation_input_tokens + input_tokens

Cache storage is isolated per workspace on the Claude API and on some cloud platforms, and per organization on others (the caching-rules table lists which). If you split production and dev into separate workspaces, their caches do not see each other.

Two facts about context windows are load-bearing for this lesson. First, the actual size depends on the model. Current models have a very large window, older models a smaller one, and the number of images or document pages one request can carry depends on the window too. The table below has the values.

As of November 2026: context factValue
1M-token context windowClaude Fable 5.2, Opus 5.5, Sonnet 5.5, Haiku 5.5; legacy Opus 4.8, 4.7, 4.6 and Sonnet 4.6 (full list on the Context windows page). Up to 128K output tokens per request
200K-token context windowOther models, including Claude Sonnet 4.5 (deprecated) and Claude Haiku 4.5
Images or PDF pages per requestUp to 600 (100 on models with a 200K window)
Context awareness (injected budget tags)Claude Sonnet 5, Sonnet 4.6, Sonnet 4.5 and Haiku 4.5. Opus 4.7 and later Opus models, Sonnet 5.5, Fable models and Haiku 5.5 do not receive the tags; use task budgets (beta) instead
Input alone exceeds the window400 invalid_request_error ("prompt is too long") on every model
Input plus max_tokens exceeds the windowClaude 4.5 models and newer accept the request and stop with stop_reason: "model_context_window_exceeded"; earlier models return a validation error unless you send the model-context-window-exceeded-2025-08-26 beta header
SourcesContext windows, Models overview

Second, the published guidance is explicit that more context is not automatically better. The docs use the term context rot for the accuracy-and-recall degradation that grows with length; the line worth quoting: 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. Curation is what the next two sections operationalize.

Two helpful properties. Context awareness, on the models that have it, tells the model its total token budget at the start (the API injects a budget tag like the one below) and its remaining tokens after each tool call, so the model can pace itself rather than guess. Some newer models do not get these tags; for them the docs describe task budgets instead, and the table says which is which.

<budget:token_budget>1000000</budget:token_budget>

Overflow handling: if the input alone is too big for the window, the API rejects it on every model. If the input fits but the input plus your maximum output does not, recent models accept the request and stop cleanly with a stop reason that says the context window was exceeded; older models need a beta header for that behavior and otherwise return a validation error upfront. The token-counting API is available for pre-flight estimation.

The docs name compaction's job in two sentences: Compaction replaces the older turns of a conversation with a summary that Claude writes on the server, so you need no summarization code of your own. It keeps a long conversation or agent task inside the context window, and it keeps the active context small, because response quality degrades as a conversation grows.

There are two ways to run it. With compaction on demand, your application decides when: it sends a compaction request, gets back one summary block instead of a reply, and puts that block first in the next request in place of the messages it summarizes. With compaction at a token threshold, the API decides: you set a trigger, and the API summarizes inside the request that reaches it. The docs advise the on-demand kind wherever it is available; the threshold kind is the one-parameter version, and the worked example here. You opt in with a beta header and a context_management entry on the request:

# Compaction at a token threshold
response = client.beta.messages.create(
model="claude-opus-5-5",
max_tokens=4096,
messages=conversation,
context_management={
"edits": [
{
"type": "compact_20260112",
"trigger": {"type": "input_tokens", "value": 150000},
"pause_after_compaction": False,
}
]
},
betas=["compact-2026-01-12"],
)
# Compaction on demand (a separate request; cannot be combined
# with context_management on the same request)
summary = client.beta.messages.create(
model="claude-opus-5-5",
max_tokens=4096,
messages=conversation,
compaction={"type": "summarize"},
betas=["compact-2026-09-04"],
)
As of November 2026: compactionOn demandAt a token threshold
Who decides whenYour application, by sending a compaction requestThe API, when input tokens reach your trigger
Beta headercompact-2026-09-04compact-2026-01-12
Request shapeTop-level "compaction": {"type": "summarize"}A compact_20260112 entry in context_management.edits
TriggerNot applicable150,000 input tokens by default; at least 50,000
Models on the Claude API (beta)Claude Fable 5.2, Opus 5.5, Sonnet 5.5, Haiku 5.5; legacy Opus 4.8, 4.7, 4.6 and Sonnet 4.6; also Fable 5.1, Fable 5, Opus 5, Sonnet 5 and the Mythos modelsThe on-demand list, plus Claude Haiku 4.5
Docs advice (verbatim from the Compaction overview)"Use on-demand compaction wherever it is available."
SourcesCompaction overview, Compaction on demand, Compaction at a token threshold

In the threshold mode there is a default trigger and a floor below which you cannot set it (the table has both). When the threshold is crossed, the API generates a summary and emits a compaction block in the response that stands in for the older conversation; on subsequent requests, the API drops everything before that compaction block, so you continue from a shorter history. If you turn on the pause-after-compaction option, the response stops with a compaction stop reason after writing the summary, which lets you manually preserve recent turns verbatim. A common pattern is the compaction block followed by the last three messages (literal below):

[compaction_block, last 3 messages]

Then you continue.

Both kinds run on a list of models that changes as new ones ship; the table lists them and their status. Threshold compaction is eligible for Zero Data Retention.

Two threshold-compaction costs that change how you reason about a long session. Compaction itself runs as an extra sampling step, billed separately; the response's usage.iterations surfaces the breakdown (a compaction entry with its input and output tokens, and a message entry for the regular turn). The top-level input_tokens and output_tokens do not include the compaction cost, so production monitoring needs to sum across iterations. And the same model is used to write the summary; there is no option to delegate summarization to a cheaper model.

Compaction interacts cleanly with prompt caching if you put a cache breakpoint at the end of your system prompt. The recommended pattern, per the docs: cache the system prompt separately, so the post-compaction request can read the system from cache and only write the (much shorter) summary fresh. Without that breakpoint, compaction can invalidate a previously-cached system prefix.

One pitfall to plan for: when tools are defined, the model occasionally calls a tool during the summarization step instead of writing the summary, producing a compaction block with content: null. The published workaround is to set custom instructions on the compaction edit explicitly forbidding tool calls inside the summary.

The docs frame context editing as the finer-grained alternative: Context editing allows you to selectively clear specific content from conversation history as it grows. Beyond optimizing costs and staying within limits, this is about actively curating what Claude receives: context is a finite resource with diminishing returns, and irrelevant content degrades model focus. Where compaction summarizes the older part of the conversation wholesale, context editing clears specific block types (server-side, before the prompt reaches the model) and replaces them with placeholder text.

Two server-side strategies today, both gated by one beta header (the table below names it).

Tool result clearing (clear_tool_uses): clears the oldest tool_result blocks in chronological order when the conversation exceeds a configurable threshold. Best for agentic workflows where old tool results (file contents, search dumps, mcp_tool_result blocks) are no longer needed once Claude has acted on them. The optional clear_tool_inputs flag also clears the corresponding tool_use parameters; clear_at_least sets a minimum tokens-cleared-per-edit floor.

Thinking block clearing (clear_thinking): clears thinking blocks from earlier assistant turns. The keep parameter controls how many recent thinking blocks survive. When you leave the strategy out, the default depends on the model: newer models keep all prior thinking, while older ones keep thinking only for the turn in progress. The table lists the split. If your code runs across several models, set keep explicitly rather than rely on the default.

As of November 2026: context editingValue
Beta headercontext-management-2025-06-27
Tool result clearingclear_tool_uses_20250919, with optional clear_tool_inputs and clear_at_least
Thinking block clearingclear_thinking_20251015, with the keep parameter
Default when the thinking strategy is left out: keep all prior thinkingClaude Opus 4.5 and later, Sonnet 4.6 and later, Haiku 5.5 and later, all Fable and Mythos models
Default when the thinking strategy is left out: keep thinking only for the turn in progressClaude Opus 4.1 and earlier, Sonnet 4.5 and earlier, Haiku 4.5 and earlier
Supported modelsAll supported Claude models
SourcesContext editing

Two facts about context editing that matter for production. First, it is server-side; your client keeps the full unmodified history and the API applies the edits before the prompt reaches the model. You do not have to sync client state to the edited version. Second, the interaction with caching is asymmetric: tool result clearing invalidates cached prefixes at the clear point (use clear_at_least to make the invalidation worth the write cost); thinking-block clearing only invalidates the cache where blocks are actually cleared, so leaving thinking blocks in place preserves cache hits.

The docs are explicit that compaction is the default and context editing is the specialist: For most use cases, server-side compaction is the primary strategy for managing context in long-running conversations. The strategies on this page are useful for specific scenarios where you need more fine-grained control over what content is cleared. Reach for editing when you have a specific block class to clear (mostly tool results in agentic workflows) and want to preserve the rest verbatim.

The three levers, summarized as a single decision per session:

  • Cache the stable parts. Anything whose text does not change for the run of the session (the system prompt, tool definitions across the tool layers of lessons 4, 5, and 6, mcp_toolset entries via their cache_control, any large stable reference passage in the first message) is a caching candidate. The break-even is one or two hits depending on the cache lifetime; a session that turns more than a few times will almost always pay back a well-placed cache.
  • Compact the long parts. If your session is going to run long, opt in to compaction well before the window fills, and put a cache breakpoint at the end of your system prompt. You get a focused active context, a cached system that survives compaction, and a small compaction cost each time it runs.
  • Clear the heavy parts. If your session is agentic with many tool calls (web searches that pull pages, file reads, MCP tool results that get large), tool result clearing surgically pulls the old result content out of the context window without touching anything else. Combine with caching by using clear_at_least so the inevitable cache write pays back.

The trade is not free in either direction. Caching saves money on hits but costs more on the write turn; if a prefix never actually repeats, caching it wastes money. Compaction reduces token-budget pressure but spends a model sampling step on the summary; if a session stays well inside the window, you are buying focus you do not need. Context editing keeps the cache mostly intact (especially with thinking-block clearing where the newer default is "keep") but for tool result clearing you pay for cache invalidation at every clear. The lesson's call to action is to reach for each lever deliberately rather than turn them all on by default.

The cost profile of a tool-heavy Claude application over a long conversation is dominated by repeated reads of a stable prefix and, eventually, by the context window filling up. The four mechanisms in this lesson are the production-grade levers for both. With a cached system prompt and tool stack, most of your input-token bill on repeat turns disappears. With compaction enabled on long sessions, you push the context-rot ceiling back and keep the model focused. With tool result clearing in an agent loop, you stop carrying old retrieval payloads forever.

Phase 2 closes here. Lesson 8 opens Phase 3 by turning the per-step capability set you have now (custom client tools, Anthropic-provided tools, MCP tools, all sitting on a cached and curated prefix) into a multi-turn agent loop. Everything you cached and pruned in this lesson is what keeps that loop affordable and focused over many iterations.

  • Prompt caching is the cost lever. Use cache_control once at the top level (automatic caching, the suggested start) or on the system prompt, tool definitions (custom, server, Anthropic-schema, mcp_toolset), and stable message content. A few explicit breakpoints per request. Placement order tools, then system, then messages.
  • The pricing principle: writes cost a little more than normal input, more again for the longer lifetime; reads cost much less. The short cache pays back after one hit, the one-hour cache after a couple.
  • Minimum cacheable size and exact matching. Every model has a floor, and the floors differ a lot between models; below it, the prompt is simply not cached. Caching is exact-match: one character change invalidates the prefix.
  • Read the cache in usage. Cache-creation tokens (written this turn), cache-read tokens (hit this turn), input tokens (uncached, after the last breakpoint). Total input is the sum of the three. In production, watch the read-to-total share.
  • Context window picture. Window sizes depend on the model. Context rot makes curation matter as much as raw size. On recent models, an output that runs into the window stops cleanly with its own stop reason.
  • Compaction is the default long-session lever. On demand (your application decides when) or at a token threshold (the API decides); either way a summary block replaces older history. In the threshold kind the same model writes the summary, in an extra sampling step billed in the usage iterations. Cache the system prompt separately so it survives.
  • Context editing is the surgical lever. Two strategies: tool result clearing (use clear_at_least to make cache invalidation pay back) and thinking block clearing (the keep parameter; thinking-block clearing preserves more cache than tool-result clearing).
  • The unifying frame: cache the stable parts (cost), compact the long parts (staleness), clear the heavy parts (surgical). Reach for each deliberately; do not turn them all on by default.

A tool-heavy Claude application's costs and its focus both compound over a long conversation. Caching takes the cost down; compaction and context editing take the staleness down. The next lesson turns the result into an agent loop.