Cached input tokens can cost as little as one tenth of fresh ones, so the fastest way to cut the bill on a chat, agent, or RAG app is often not a smaller model but reusing the prompt prefix you already sent. The move is simple: put your stable content (system instructions, tool schemas, reference documents) at the front, mark it cacheable, and let the provider bill repeat reads at a fraction of the input rate. Anthropic makes you opt in with one field; OpenAI and Gemini 2.5 do it automatically once your prompt clears a size threshold.
Key takeaways
- Short answer: move everything that repeats to the top of the prompt and cache it. On Claude, add
cache_control; on OpenAI and Gemini 2.5 it happens automatically above the token minimum. - Anthropic cache reads bill at 0.1x the input rate, roughly 90% off. OpenAI bills cached tokens at a reduced cached-input rate. Gemini explicit caches discount referenced tokens by 75%-90%.
- Caching only fires on an exact, repeated prefix. Every token up to the first change is reusable; the first difference and everything after it is not.
- Size gates the feature: 1,024 tokens for most Claude and OpenAI models, 2,048 for Gemini 2.5 Flash and Pro. Below that, nothing caches and no error is raised.
- Default cache life is short: 5 minutes on Anthropic, about 5 to 10 idle minutes on OpenAI. Anthropic sells a 1-hour TTL at a higher write price.
- Always confirm the hit with the usage fields (
cache_read_input_tokens,cached_tokens) before you trust the savings.
What Do You Need Before You Start?
Three things, nothing exotic:
- An API key for Anthropic, OpenAI, or Google Gemini, and the matching SDK installed (
pip install anthropic,pip install openai, orpip install google-genai). - A prompt with a large stable portion: a long system prompt, tool or function schemas, a reference document, or a block of few-shot examples that you send on nearly every call.
- A way to read the response usage object, because caching is invisible until you inspect it.
How to Add Prompt Caching to Your API Calls
Follow these six steps in order. The first two apply to every provider; the rest are provider-specific and self-contained.
- Sort the prompt so stable content leads. Put instructions, tool definitions, and long documents first. Keep the user's variable text, timestamps, and anything dynamic last. The cache reuses the longest identical prefix, so a single changing token near the top wastes the whole thing.
- Confirm the prefix clears the minimum. You need at least 1,024 tokens before the cache point on Claude Opus 4.8 and Sonnet 5, at least 1,024 on OpenAI, and at least 2,048 on Gemini 2.5. Under the floor the request runs normally but caches nothing.
- On Anthropic, set the breakpoint with
cache_control. Attach it to the last block you want cached. Everything before that block becomes the cached prefix.import anthropic client = anthropic.Anthropic() resp = client.messages.create( model="claude-opus-4-8", max_tokens=1024, system=[ {"type": "text", "text": LONG_SYSTEM_PROMPT}, {"type": "text", "text": REFERENCE_DOC, "cache_control": {"type": "ephemeral"}}, ], messages=[{"role": "user", "content": "Summarize section 4."}], ) print(resp.usage) - On OpenAI, change nothing, then pin routing if you shard traffic. Caching applies automatically above 1,024 tokens. The optional
prompt_cache_keyroutes similar requests to the same cache so hit rates stay high under load.from openai import OpenAI client = OpenAI() resp = client.chat.completions.create( model="gpt-4o", prompt_cache_key="rag-tenant-42", messages=[ {"role": "system", "content": LONG_SYSTEM_PROMPT}, {"role": "user", "content": "Summarize section 4."}, ], ) print(resp.usage.prompt_tokens_details) - On Gemini, rely on implicit caching or create an explicit cache for a guarantee. Gemini 2.5 caches automatically. When you must guarantee the discount, register the content once and reference the handle.
from google import genai from google.genai import types client = genai.Client() cache = client.caches.create( model="gemini-2.5-flash", config=types.CreateCachedContentConfig( contents=[LONG_REFERENCE_DOC], ttl="3600s", ), ) resp = client.models.generate_content( model="gemini-2.5-flash", contents="Summarize section 4.", config=types.GenerateContentConfig(cached_content=cache.name), ) print(resp.usage_metadata) - Verify the hit before you trust it. Run the same request twice. The first call writes the cache; the second should read it. Read the counts described in the next section. If reads stay at zero, your prefix is not stable.
How the Prefix Rule Actually Works
Caching keys on an exact prefix match, not on meaning. The provider hashes your prompt from the start; the moment a byte differs from a stored request, the match ends and everything after pays full price.
That single rule drives every layout decision. Anthropic reads its prefix in a fixed order, tools then system then messages, and a change at one level invalidates that level and all the levels below it. Swap a tool definition and you lose the system and message caches too; change only the last user turn and the expensive front stays warm.
This is why non-deterministic details near the top quietly kill savings. A timestamp in the system prompt, tool schemas serialized in random key order, or a per-user greeting before the shared instructions all reset the prefix on every call.
Which Provider Caches How?
The three major APIs converge on the same idea and differ on the mechanics that matter for cost and code. This table lines up the specifics you plan around.
| Provider | How to enable | Minimum tokens | Cache read price | Default lifetime |
|---|---|---|---|---|
| Anthropic (Claude) | Opt in with cache_control breakpoints | 1,024 (Opus 4.8, Sonnet 5); 512 on Fable 5 | 0.1x input, about 90% off | 5 minutes; 1-hour TTL optional |
| OpenAI | Automatic, no code change | 1,024 | Reduced cached-input rate | About 5 to 10 idle minutes, up to 1 hour off-peak |
| Google Gemini 2.5 | Implicit by default; explicit via caches.create | 2,048 (2.5 Flash and Pro) | 75%-90% off referenced tokens (explicit) | Default 60 minutes, TTL adjustable |
Read the row that matches your stack, then decide two things: is your prefix big enough to qualify, and do your calls repeat inside the cache lifetime.
When Is the 1-Hour TTL Worth Paying For?
Anthropic's default 5-minute window suits back-to-back agent turns and busy endpoints. The 1-hour TTL costs 2x the base input rate to write instead of 1.25x, so it earns its keep only when a large prefix gets reused across a gap longer than five minutes: a support session with think time between messages, or a document a user returns to over an afternoon.
Do the arithmetic before you switch. If the reused prefix is small or traffic is constant, the standard window already captures the win and the higher write price is dead weight.
What Do the Usage Fields Tell You?
Every provider reports the split so you can audit it. On Anthropic, cache_creation_input_tokens counts what got written and cache_read_input_tokens counts what came back cheap; input_tokens is the uncached remainder. On OpenAI, check usage.prompt_tokens_details.cached_tokens. On Gemini, read cached_content_token_count in usage_metadata.
Log the ratio of cached reads to total input tokens as a first-class metric. A cache hit rate that drifts toward zero is the earliest signal that a recent prompt change broke the prefix.
How Do You Fix a Cache That Never Hits?
Most caching failures trace to a handful of causes. Work through them in this order.
- Reads always show zero, writes keep happening. Your prefix changes every call. Hunt for a timestamp, a UUID, a random-ordered JSON serialization, or user-specific text sitting ahead of the shared content, and move it after the cache point.
- Nothing caches and no error appears. The cacheable region is under the token minimum. Count tokens; if you are below 1,024 (or 2,048 on Gemini 2.5), consolidate more static context ahead of the breakpoint or accept that the prompt is too small to benefit.
- Hits work in tests but vanish in production. Requests arrive too far apart and the cache expires between them. Either raise the TTL where the provider allows it, or keep the prefix warm with steady traffic.
- Anthropic writes far more than you expect. Your
cache_controlblock sits after variable content, so the breakpoint lands too late and little gets cached. Move it to the last stable block. - OpenAI hit rate is erratic under load. Requests scatter across cache nodes. Set a stable
prompt_cache_keyper tenant or session to route them together.
What to Do Next
Wire caching into how you measure and spend, not just how you call the API.
- Read the primary docs for your provider and confirm the current numbers before you commit a budget: Anthropic prompt caching, OpenAI prompt caching, and Gemini context caching.
- Instrument the cache hit rate alongside your token spend. My notes on AI token budgeting and on shrinking the token budget cover the metrics that make this visible.
- Understand why the reuse is cheap at all. The same mechanism that makes cached tokens fast is the subject of why KV cache is the biggest lever in inference cost.
- Pair caching with disciplined prompt structure. If your calls also parse model output, reliable JSON with structured outputs keeps the variable tail clean and predictable.