Agent Harnesses Explained
← /learn · 04

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 ρ\rho times the input price, the read multiplier. A cache write costs ww times the input price, the write premium. And a prefix is only cached if it is at least Lmin⁡L_{\min} 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 w=1.25w = 1.25. Reads cost ρ=0.1\rho = 0.1 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 (w=1w = 1, so none for GPT-5 mini) and count cached tokens in blocks of 128. GPT-5.6 and later charge w=1.25w = 1.25, read at ρ=0.1\rho = 0.1 (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.

The engine's price table (US dollars per million tokens; list prices accessed 2026-10-16)
ModelGenerationInputOutputCache readρ (read ÷ input)w (write ÷ input)Minimum
Claude Opus 5.5 (API list price)Claude 5.x$4$20$0.200.051.25512
Claude Sonnet 5.5 (API list price)Claude 5.x$2$10$0.100.051.25512
Claude Haiku 5.5 (API list price, prompts up to 100,000 tokens)Claude 5.x$0.10$0.50$0.010.11.25512
Claude Fable 5.1 (API list price)Claude 5.x$10$50$0.250.0251.25512
GPT-6.1 Sol (API list price, standard tier, short context)GPT-6$2$10$0.100.051.251,024
GPT-6 Luna (API list price, standard tier, short context)GPT-6$0.10$0.50$0.010.11.251,024
GPT-6 Astra (API list price, standard tier, short context)GPT-6$10$50$10.11.251,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.300.11.251,024
Claude Haiku 4.5 (API list price)Claude 4.x$1$5$0.100.11.254,096
GPT-5 mini (API list price, standard tier)GPT-5$0.25$2$0.0250.111,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 NN-token prompt, of which a fraction hh is read from the cache, and MM output tokens costs, at prices per million tokens prp_r (read), pwp_w (write) and pop_o (output),

cost=pr hN+pw(1−h)N+poM106,\text{cost} = \frac{p_r\, h N + p_w (1-h) N + p_o M}{10^6},

when the prompt is written to the cache, or with the plain input price pip_i in place of pwp_w when it is not. Write the prices as multiples of the input price, pr=ρ pip_r = \rho\, p_i and pw=w pip_w = w\, p_i. Against no caching, piNp_i N, a prefix written once and read kk times pays off when

w+kρ<1+k⟺k>w−11−ρ.w + k\rho < 1 + k \quad\Longleftrightarrow\quad k > \frac{w - 1}{1 - \rho} .

With the 5-minute write (w=1.25w = 1.25) the bound is about a quarter of a read for any ρ\rho in the table (0.28 at ρ=0.1\rho = 0.1, 0.26 at 0.050.05), so one re-use repays the write premium on every model listed, as Anthropic's pricing page says. With the 1-hour write (w=2w = 2) it takes two. Where there is no premium (w=1w = 1), caching never costs more. The timestamp layout is the case k=0k = 0: every prompt is written and none is read, so it costs ww times as much as no cache.

Over many re-uses the cost of the cached prefix tends to ρ\rho of its uncached cost: the most a cache can save is 1−ρ1 - \rho, 90% at ρ=0.1\rho = 0.1, 95% at 0.050.05 and 97.5% at 0.0250.025. That is why the same layout saves more on the current models. One more condition applies before any of this: a prompt shorter than Lmin⁡L_{\min} 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, ρ\rho, ww and Lmin⁡L_{\min}.

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.

Q1Put the current time in the system prompt. On Sonnet 5.5, how many times the cost of no caching at all does the run cost?
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.

Three prompt layouts →

Commit to a number, then reveal.

Q2With the 5-minute write on Sonnet 5.5, how many re-uses k of a cached prefix repay the write premium? (k > (w − 1)/(1 − ρ))
Reveal the answer

Answer: 0.263 (counted right within 10%)

The write costs w=1.25w = 1.25 times the input price and a read ρ\rho = 0.05 times, so the bound is about a quarter: one re-use repays the write.

When a cache write pays →

Commit to a number, then reveal.

Q3Switch to Haiku 4.5 with the stable prefix. What share of the research task's prompt tokens is read from the cache?
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.

Three prompt layouts →

Commit to a number, then reveal.