If you run agents, RAG pipelines, or long multi-turn conversations in production, you are probably re-sending — and re-paying for — the same system prompt, tool schemas, and retrieved documents on every single call. Prompt caching fixes exactly that: the provider stores the stable prefix of your prompt and reuses it instead of reprocessing it.
In this guide I show you how caching actually works in 2026 on Anthropic, OpenAI, and Gemini — with verified limits, real pricing math, and code you can paste into your project today.
1. The Problem: You Pay for the Same Tokens Twice
Consider a coding agent with a 40k-token system prompt plus tool definitions, doing 10 turns per session. Without caching, every turn reprocesses all 40k tokens at full input price — 400k billed input tokens for one session. With caching, the 40k prefix is written once and read back at a fraction of the price on the following 9 turns, and time-to-first-token drops because the cached prefix skips prompt processing.
The Expensive Default
Most production apps I audit resend identical system prompts and few-shot examples on every request with caching never enabled — on Anthropic that means paying 10x more than necessary on repeat tokens, because Claude’s caching is explicit and does nothing until you mark it.
Caching is not memoization of answers: the model still generates fresh output every time. It only skips reprocessing the input prefix. That makes it safe for anything with a stable head and a changing tail — chat history, agents with fixed tools, RAG with reused context.
2. Minimum Concepts: Three Providers, Three Philosophies
All three labs sell the same idea with different controls. Learn this table once and every code sample below will make sense.
Anthropic (Claude) — Explicit
cache_control breakpoints · up to 4 per request
You mark what gets cached with cache_control: { type: “ephemeral” } on system, messages, or tools blocks. Writes cost 1.25x base input (5-minute TTL) or 2x (1-hour TTL); reads cost 0.1x. Minimum prefix length is model-dependent (1,024–4,096 tokens). Breakpoints themselves are free.
OpenAI (GPT-5.6+) — Implicit or Explicit
prompt_cache_options mode/ttl · min 1,024 tokens
On GPT-5.6 and later, caching is on by default (implicit breakpoint at the latest eligible user/tool message) with a 1,024-token minimum: writes cost 1.25x, reads 0.1x (cached_tokens / cache_write_tokens). Set prompt_cache_options { mode: 'implicit' | 'explicit', ttl: '30m' } (30m is the default and only value), add prompt_cache_breakpoint { mode: 'explicit' } after stable content, and reuse a stable prompt_cache_key. Pre-5.6 models differ: 2,048-token minimum, free writes, 128-token rounding, implicit-only intervals, prompt_cache_retention (in_memory / 24h).
Google (Gemini) — Implicit + Explicit
implicit by default on Gemini 2.5+ · explicit caches.create API
Implicit caching needs no setup on Gemini 2.5 and newer, with minimums of 2,048 tokens (2.5 Flash/Pro) and 4,096 tokens (Gemini 3.x), and passes automatic savings back. Explicit context caching gives guaranteed reuse with a TTL you control, plus hourly storage cost per million cached tokens.
The One Rule Behind Every Hit
Cache hits require an exact prefix match. Order every prompt stable-first: tool schemas, system instructions, long-lived RAG context — then conversation history, then the fresh user message last. One dynamic byte at the top invalidates everything below it.
3. Tutorial: Enable Caching Step by Step
Three setups, in order of control. Pick the provider you bill on and follow its steps — the prompt-ordering discipline is identical everywhere.
Step 1 — Anthropic: mark the stable prefix
Add cache_control to the blocks that rarely change. Keep the fresh user message unmarked, and reuse the exact same prefix on the next call. Watch usage for cache_creation_input_tokens on the first call and cache_read_input_tokens afterwards.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model=“claude-sonnet-4-5-20250929”,
max_tokens=1024,
system=[
{
“type”: “text”,
“text”: SYSTEM_PROMPT, # long, stable instructions
“cache_control”: {“type”: “ephemeral”},
}
],
messages=[{“role”: “user”, “content”: user_question}],
)
print(response.usage.cache_creation_input_tokens)
print(response.usage.cache_read_input_tokens)Step 2 — Anthropic: pick the TTL deliberately
Default TTL is 5 minutes. For agentic loops or long conversations where follow-ups can arrive later, set ttl to “1h” — it doubles the write price but cache reads stay at 0.1x. Mix both TTLs only with 1-hour entries before 5-minute ones.
system=[
{
“type”: “text”,
“text”: SYSTEM_PROMPT,
“cache_control”: {“type”: “ephemeral”, “ttl”: “1h”},
}
]
# Or automatic caching: one top-level marker,
# the breakpoint follows the last cacheable block.
response = client.messages.create(
model=“claude-sonnet-4-5-20250929”,
max_tokens=1024,
cache_control={“type”: “ephemeral”},
system=[{“type”: “text”, “text”: SYSTEM_PROMPT}],
messages=[{“role”: “user”, “content”: user_question}],
)Step 3 — OpenAI: choose a mode, place a breakpoint
On GPT-5.6 and later, keep the default implicit mode for append-only threads, or set prompt_cache_options.mode to “explicit” and mark the end of stable content with prompt_cache_breakpoint so the changing suffix is never written at 1.25x. Reuse a stable prompt_cache_key, keep the prefix ≥ 1,024 tokens with TTL 30m (default, only value), then read cached_tokens and cache_write_tokens. Pre-5.6: automatic only, 2,048-token minimum, free writes, prompt_cache_retention instead of prompt_cache_options.
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model=“gpt-5.6”,
prompt_cache_key=“my-app-v1:session-42”,
prompt_cache_options={“mode”: “explicit”, “ttl”: “30m”},
input=[
{
“role”: “developer”,
“content”: [
{
“type”: “input_text”,
“text”: LONG_SYSTEM_PROMPT, # stable, >= 1,024 tokens
“prompt_cache_breakpoint”: {“mode”: “explicit”},
}
],
},
{“role”: “user”, “content”: user_question}, # dynamic last
],
)
print(response.usage.input_tokens_details.cached_tokens)
print(response.usage.input_tokens_details.cache_write_tokens)
# Pre-5.6: automatic only, 2,048-token min, free writes,
# prompt_cache_retention (in_memory / 24h), 128-token rounding.Step 4 — Gemini: rely on implicit, graduate to explicit
On Gemini 2.5+ do the same stable-first ordering and implicit caching just works — check usageMetadata for cached tokens. When you need guaranteed reuse of a big corpus (docs, repo, video), create an explicit cache with a TTL and reference its name on later calls.
from google import genai
client = genai.Client()
cache = client.caches.create(
model=“gemini-2.5-flash”,
config={
“system_instruction”: LONG_SYSTEM_PROMPT,
“contents”: [LARGE_DOCUMENT_CORPUS],
“ttl”: “3600s”,
},
)
response = client.models.generate_content(
model=“gemini-2.5-flash”,
contents=f“Question: {user_question}”,
config={“cached_content”: cache.name},
)
print(response.usage_metadata)What Good Looks Like
A healthy production hit rate is 60–90% of input tokens served from cache. On Claude that means ~90% off those tokens; on OpenAI and Gemini implicit, the discounted cached-input rate. If your first measurement shows 0 hits, the cause is almost always ordering or a prefix below the minimum — not the provider.
4. Common Mistakes That Zero Your Hit Rate
Caching fails silently: no error, just full price. These are the five failure modes I see most, and each has a one-line fix.
1. Dynamic content before stable content
A timestamp, user ID, or changing RAG snippet at the top of the prompt changes the prefix hash, so every request is a miss. Fix: move anything variable after the cached blocks.
2. Prefix below the minimum, marked anyway
Anthropic and Gemini ignore cache marks on prefixes under the model’s minimum (1,024–4,096 tokens depending on model). Short prompts cache nothing — no error, just no savings. Fix: only mark blocks you know clear the threshold; verify with the token counter.
3. Tool schemas serialized in random order
Dict iteration or regenerated JSON with shuffled keys produces a byte-different prefix each call. Fix: serialize tools deterministically (sorted keys, fixed order) so the prefix matches byte-for-byte.
4. TTL shorter than your conversation gaps
A 5-minute TTL on a support bot where users reply after 20 minutes means every turn is a fresh write. Fix: measure your inter-request gaps and use the 1-hour TTL (Claude) or explicit cache with a longer TTL (Gemini) when gaps exceed minutes.
5. Growing conversations outrun the lookback window
On Claude, a single breakpoint can fall outside the 20-block lookback as history grows, silently ending hits mid-session. Fix: add a second breakpoint closer to the growing tail — one for stable instructions, one after recent history.
Golden rule
Measure hits before celebrating savings: log cache_creation_input_tokens and cache_read_input_tokens (Claude), cached_tokens (OpenAI), or usageMetadata (Gemini) on every call. One cache write pays for itself with a single hit — but an unmeasured cache is just a hope.
Conclusion
Prompt caching is the highest-ROI optimization most LLM apps never apply: same model, same quality, up to ~90% cheaper repeat tokens and visibly faster first tokens. Explicit on Claude, implicit-or-explicit on OpenAI GPT-5.6+ (writes 1.25x, reads 0.1x, TTL 30m), implicit-or-explicit on Gemini — but the discipline is one: stable prefix first, dynamic content last, hits measured.
This week, pick your hottest endpoint, reorder its prompt, enable the cache, and compare one day of usage before and after. If you build agents or RAG, also read my MCP guide and the multi-agent production guide next — caching plus clean tool context is where the real margin lives.
Cheat Sheet
Anthropic
- • cache_control: ephemeral
- • Writes 1.25x / 2x, reads 0.1x
- • Min 1,024–4,096 by model
OpenAI
- • 5.6+: writes 1.25x, reads 0.1x
- • mode implicit/explicit + ttl 30m
- • Pre-5.6: 2,048 min, free writes, 24h retention
Gemini
- • Implicit default on 2.5+
- • Min 2,048 (2.5) / 4,096 (3.x)
- • Explicit cache + TTL + storage
Sources
- 1. Anthropic — Prompt caching docs (breakpoints, TTLs, thresholds)
- 2. OpenAI — Prompt caching guide (GPT-5.6 mode/ttl/breakpoint, pre-5.6 path)
- 3. Google — Gemini context caching docs (implicit minimums)
- 4. Google — Gemini API pricing (cached + storage rates)
- 5. Anthropic — Pricing (cache write/read multipliers)



