Prompt caching
A stable prefix is read from the cache at a tenth of the price; a timestamp or a reordered tool list breaks it.
Check yourself ↓ three questions and what to remember, at the end
Loading the animation…
Concept
Chapter 1 showed that an agent re-sends its whole context on every call. Almost all of it is the same as last time: the system prompt, the tool definitions, and every message so far; only the newest messages are new. A provider that keeps the processed prefix of recent prompts (the model's key-value cache for those tokens) can skip recomputing it and charge less for it. That is prompt caching, and an agent is the best possible customer for it.
Three numbers describe a provider's cache, and the rest of this chapter is written in them. A cache read costs times the input price, the read multiplier. A cache write costs times the input price, the write premium. And a prefix is only cached if it is at least tokens long, the minimum cacheable length. They differ by model and by generation (the table below):
- On the Claude API the 5-minute write costs . Reads cost on most models, but 0.05 on Sonnet 5.5 and Opus 5.5 and 0.025 on Fable 5.1. The minimum is 512 tokens on the 5.x models, against 1,024 for Sonnet 4.6 and 4,096 for Haiku 4.5.
- On OpenAI's API, models before GPT-5.6 charge no write premium (, so none for GPT-5 mini) and count cached tokens in blocks of 128. GPT-5.6 and later charge , read at (0.05 on GPT-6.1 Sol), cache from 1,024 tokens and count them exactly.
(Sources and dates below; the simulator's cache is a simple model of these rules, not either provider's implementation.)
Run the research task from chapter 3, with a window big enough not to compact, under three prompt layouts:
- Stable prefix: the system prompt and the tools never change and messages are only appended. With Sonnet 5.5 prices, 77% of all prompt tokens are read from the cache and the run costs $0.014 instead of $0.0363, a saving of 61%. It is faster too: cached tokens need no prefill, and the run finishes in 9.59 s against 12.1 s without hits.
- Current time in the system prompt: one line that changes every call, a few dozen tokens in. No prompt ever matches the last one past that point, so nothing is read from the cache, and every prompt is still written to it at the premium: the run costs $0.0442, more than with no caching at all ($0.0366).
- Tools reordered every turn: the prefix survives only up to the end of the system text. Hits drop to 44% and the cost rises to $0.0268.
The other current models tell the same story. Haiku 5.5 caches from the same 512 tokens and reads 77% of this task's prompt tokens from the cache. GPT-6.1 Sol charges a write premium too, so its timestamp layout also costs more than no cache ($0.0437 against $0.0366).
Now switch the generation to earlier. On Sonnet 4.6 the stable prefix reads only 71% of the prompt tokens from the cache, because the first, shorter prompts stay under its 1,024-token minimum, and saves 53% rather than 61%: fewer hits, and each read costs twice as much. On Haiku 4.5 nothing is cached at all: none of this task's prompts reaches its 4,096-token minimum. And GPT-5 mini charges no write premium, so there the timestamp layout costs no more than no cache ($0.00487 against $0.00487).
The rule a harness follows is simple to state, and it holds on every row of the table: put what never changes first, in a fixed order, and only ever append.
| Model | Generation | Input | Output | Cache read | ρ (read ÷ input) | w (write ÷ input) | Minimum |
|---|---|---|---|---|---|---|---|
| Claude Opus 5.5 (API list price) | Claude 5.x | $4 | $20 | $0.20 | 0.05 | 1.25 | 512 |
| Claude Sonnet 5.5 (API list price) | Claude 5.x | $2 | $10 | $0.10 | 0.05 | 1.25 | 512 |
| Claude Haiku 5.5 (API list price, prompts up to 100,000 tokens) | Claude 5.x | $0.10 | $0.50 | $0.01 | 0.1 | 1.25 | 512 |
| Claude Fable 5.1 (API list price) | Claude 5.x | $10 | $50 | $0.25 | 0.025 | 1.25 | 512 |
| GPT-6.1 Sol (API list price, standard tier, short context) | GPT-6 | $2 | $10 | $0.10 | 0.05 | 1.25 | 1,024 |
| GPT-6 Luna (API list price, standard tier, short context) | GPT-6 | $0.10 | $0.50 | $0.01 | 0.1 | 1.25 | 1,024 |
| GPT-6 Astra (API list price, standard tier, short context) | GPT-6 | $10 | $50 | $1 | 0.1 | 1.25 | 1,024 |
| gpt-oss-120b, open weights (Together AI serverless list price) | open weights | $0.15 | $0.60 | — | — | — | no cache listed |
| Claude Sonnet 4.6 (API list price) | Claude 4.x | $3 | $15 | $0.30 | 0.1 | 1.25 | 1,024 |
| Claude Haiku 4.5 (API list price) | Claude 4.x | $1 | $5 | $0.10 | 0.1 | 1.25 | 4,096 |
| GPT-5 mini (API list price, standard tier) | GPT-5 | $0.25 | $2 | $0.025 | 0.1 | 1 | 1,024 (×128) |
Claude prices are the 5-minute cache write; Haiku 5.5 is the rate for prompts up to 100,000 tokens; OpenAI prices are the standard tier, short context. Claude's pricing page notes that Claude 4.7 and later models use a tokenizer that gives about 30% more tokens for the same text, and every count on this site is Qwen2.5's, so compare the rates, not the bills. "(×128)": cached tokens are counted in 128-token blocks.
Maths
A call with an -token prompt, of which a fraction is read from the cache, and output tokens costs, at prices per million tokens (read), (write) and (output),
when the prompt is written to the cache, or with the plain input price in place of when it is not. Write the prices as multiples of the input price, and . Against no caching, , a prefix written once and read times pays off when
With the 5-minute write () the bound is about a quarter of a read for any in the table (0.28 at , 0.26 at ), so one re-use repays the write premium on every model listed, as Anthropic's pricing page says. With the 1-hour write () it takes two. Where there is no premium (), caching never costs more. The timestamp layout is the case : every prompt is written and none is read, so it costs times as much as no cache.
Over many re-uses the cost of the cached prefix tends to of its uncached cost: the most a cache can save is , 90% at , 95% at and 97.5% at . That is why the same layout saves more on the current models. One more condition applies before any of this: a prompt shorter than is never written, so a short agent loop sees no cache at all on a model with a large minimum. The other chapters and sites that use the cache (sub-agents, cost and latency, Context's long context or retrieval? and Orchestration's multi-agent patterns) rest on these three numbers, , and .
Code
The cost of one call, cut from the vendored engine (accounting.ts):
export function callCost(price: Obj, input: number, cached: number, stored: boolean, output: number): number {
const fresh = input - cached;
const rate = stored ? price.cache_write : price.input;
return (cached * price.cache_read + fresh * rate + output * price.output) / 1e6;
}
In production. On the Claude API you mark where the cached prefix ends with cache_control. The prefix is cached in the order tools, then system, then messages, so a breakpoint on the last system block caches the tools and the whole system prompt; a second breakpoint on the newest message caches the conversation so far, which is what an agent loop wants (up to four breakpoints per request). With the Python SDK:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
tools=TOOLS, # a fixed list in a fixed order: part of the cached prefix
system=[
{"type": "text", "text": SYSTEM_PROMPT, "cache_control": {"type": "ephemeral"}},
],
messages=history[:-1] + [
{"role": "user", "content": [
{"type": "text", "text": history[-1]["content"], "cache_control": {"type": "ephemeral"}},
]},
],
)
u = response.usage
print(u.cache_creation_input_tokens, u.cache_read_input_tokens, u.input_tokens)
The usage block reports what was written, what was read and what was neither, the three terms of the cost above. (Run here with the anthropic Python SDK 1.15.0 against a local stand-in server, which checks the request the SDK builds but not the API's reply; TOOLS, SYSTEM_PROMPT and history are yours.)
What breaks a cache
The Claude API builds its cache prefix in the order tools, then system, then messages, and a change at one level invalidates that level and everything after it: changing a tool definition invalidates the whole cache (prompt caching). Qwen2.5's template puts the system text first and the tools after it, so in this simulator a rotated tool list keeps the system text cached and loses the rest. Either way, the cures are the same: a fixed tool list in a fixed order, nothing time-varying near the top, history only appended, and compaction (chapter 3) used sparingly, since rewriting the history is itself a cache miss. Claude Code's cost page notes that it uses prompt caching automatically and counts its own compactions as expected rebuilds (Claude Code, manage costs).
An MCP server can help: the 2026-07-28 revision of the protocol asks servers to return tools/list in a deterministic order "to improve LLM prompt cache hit rates" (Protocols, gateways and composition), which is the reordering layout above, prevented at the source.
Sources, accessed 2026-10-16: Claude API pricing (1.25× write, the read multipliers, the 5-minute TTL, the tokenizer note); Claude API prompt caching (minimum lengths, prefix order, four breakpoints); OpenAI API pricing and prompt caching (GPT-5.6 and later: 1.25× write, 0.1× read and 0.05× on GPT-6.1 Sol, a 1,024-token minimum).
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: 1.21 (counted right within 10%)
More than 1: nothing is ever read from the cache, and every prompt is still written to it at the premium, $0.0442 against $0.0366.
Commit to a number, then reveal.
Reveal the answer
Answer: 0.263 (counted right within 10%)
The write costs times the input price and a read = 0.05 times, so the bound is about a quarter: one re-use repays the write.
Commit to a number, then reveal.
Reveal the answer
Answer: 0% (counted right within ± 5%)
None: Haiku 4.5 caches only prompts of at least 4,096 tokens, and no prompt in this task is that long. A good layout cannot help a prompt under the minimum.
Commit to a number, then reveal.