What is context caching? Repeated prompt prefixes can cost far less
Context caching bills a prompt prefix you send again and again at a lower cache-read price. How OpenAI, Claude, Gemini and DeepSeek handle caching, how to write prompts that hit the cache, and how much it saves at live prices.
If every request starts with the same long block — a system prompt, a product manual, tool definitions — models that support context caching store that prefix. When the same prefix comes back, the matched part is billed at the cache-read price. Take gpt-5.4-mini: normal input is ¥1.5 per 1M tokens, cache reads only ¥0.15. The longer the fixed prefix and the more often you call, the more you save.
How caching works
To read a prompt, a model runs a computation over every token. If two requests begin with the same long block, the second can reuse the first one's results, and the saved compute shows up in the price.
That is why caching matches on the prefix: only text that is identical from the very first token counts as a hit. Change one character in the middle and nothing after it can be matched.
How each vendor caches
| Models | How it is enabled | Billing |
|---|---|---|
| OpenAI GPT | Automatic once the prompt reaches a minimum length (currently 1,024 tokens per OpenAI, subject to change) and the prefix repeats | Hits billed at the cache-read price |
| Claude | Mark where to cache with cache_control in the request | Cache writes billed at the cache-write price, above normal input; hits billed at the much lower cache-read price |
| Gemini | Newer models cache implicitly; you can also create an explicit cache and reference it | Hits billed at the cache-read price |
| DeepSeek and other Chinese models | Most cache repeated prefixes automatically | Hits billed at the cache-read price |
These are the vendors' published general rules. Minimum lengths, lifetimes and other details change, so check each model's official documentation. Whether a model has a cache price shows in its cache-read column in Models and pricing.
Reading cache prices
Here is Claude Sonnet 5.5 in every group:
| Group | Input ¥/1M | Output ¥/1M | Cache read ¥/1M |
|---|---|---|---|
| Standard | ¥4 | ¥20 | ¥0.4 |
| Official · Enterprise | ¥21.9 | ¥109.5 | ¥2.19 |
| Claude Direct | ¥15 | ¥75 | ¥1.5 |
| Claude List Price | ¥24 | ¥120 | ¥2.4 |
| Claude Plus | ¥8 | ¥40 | ¥0.8 |
| Claude LiteLowest | ¥3.2 | ¥16 | ¥0.32 |
Claude Sonnet 5.5 · Prices in CNY, cheapest group for each model; updated Nov 12, 2026, 8:17 PM.
In its cheapest group, a cache write costs ¥4 per 1M tokens, more than normal input, but every later hit is billed at the cache-read price. The more a prefix is reused, the better the deal.
Input and cache-read prices for a few other common models (per 1M tokens, cheapest group):
- deepseek-v4.1-flash: input ¥2.8, cache read ¥0.056
- gemini-3.8-flash: input ¥1.5, cache read ¥0.15
- gpt-5.5: input ¥10, cache read ¥1
How much it saves
A support bot sends a fixed 6,000-token system prompt plus product material with every request, about 1,000 tokens of question and recent turns, and gets a 400-token answer, 100,000 times a month.
Without cache hits:
Cost estimate · gpt-5.4-mini (Standard)
- Unit price
- input ¥1.5 / output ¥9 (per 1M tokens)
- Usage per request
- 7,000 input tokens, 400 output tokens
- Requests
- 100,000
- Cost per request
- ¥0.0141
Total about ¥1,410Prices in CNY, cheapest group for each model; updated Nov 12, 2026, 8:17 PM.
With the fixed 6,000 tokens served from cache:
Cost estimate · gpt-5.4-mini (Standard)
- Unit price
- input ¥1.5 / output ¥9 (per 1M tokens)
- Usage per request
- 7,000 input tokens (6,000 from cache), 400 output tokens
- Requests
- 100,000
- Cost per request
- ¥0.006
Total about ¥600Prices in CNY, cheapest group for each model; updated Nov 12, 2026, 8:17 PM.
Real hit rates stay below 100%: expired entries and edited prefixes send some requests back to full price. Once you are live, compute your actual hit rate from the cached tokens in usage and budget with that.
Writing prompts that hit the cache
- Put what never changes first and what changes last. A good order: system prompt, tool definitions, fixed material, chat history, the current question.
- Keep values that change on every request — current time, request IDs, user names — out of the system prompt. If you need them, put them in the last user message.
- Keep tool definitions and examples in a fixed order, and serialize JSON with a stable key order.
- Send similar requests close together. Cache entries expire if they are not hit, and writing them again costs money.
- Claude needs explicit marking: put
cache_controlon the last block of the fixed content.
import os
import anthropic
client = anthropic.Anthropic(base_url="https://open.tutujin.com", api_key=os.environ["TUTUJIN_API_KEY"])
with open("product_manual.md", encoding="utf-8") as f:
manual = f.read() # several thousand tokens of fixed material
resp = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
system=[
{"type": "text", "text": "You are product support. Answer only from the manual below."},
{"type": "text", "text": manual, "cache_control": {"type": "ephemeral"}},
],
messages=[{"role": "user", "content": "How long is the warranty?"}],
)
print(resp.usage.cache_creation_input_tokens, resp.usage.cache_read_input_tokens)The first request writes the cache and reports cache_creation_input_tokens; later requests within the cache lifetime hit it and report cache_read_input_tokens.
Checking for hits
| API | Where cached tokens are reported |
|---|---|
| OpenAI-compatible (Chat Completions) | usage.prompt_tokens_details.cached_tokens |
| Claude native Messages | usage.cache_read_input_tokens; tokens written to the cache are in usage.cache_creation_input_tokens |
| Gemini native generateContent | usageMetadata.cachedContentTokenCount |
When you call other vendors' models through the OpenAI-compatible API, whether the cache field is returned depends on the model. The Usage page in the console is the record of what each request actually cost. For savings beyond caching, see How to control API costs.
FAQ
Do I need to turn caching on?
Not on this site. Whether caching happens depends on the model. OpenAI, Gemini, DeepSeek and others cache long repeated prefixes automatically; Claude needs you to mark the content to cache with cache_control in the request.
Will cached requests return identical answers?
No. The cache stores the model's intermediate computation for the prompt prefix, not the answer. Every answer is still generated fresh.
Why does writing to the cache cost more than normal input?
Some models, such as Claude, charge more than the normal input price to write content into the cache, and much less for every cache hit after that. The more often a prefix is reused, the more you save; content used only once is not worth caching.
How long does the cache last?
It depends on the model, typically somewhere between a few minutes and an hour; entries that are not hit for a while are evicted. Check each model's official documentation for details.
How do I know whether the cache was hit?
Look at the usage field in the response. In the OpenAI format it is prompt_tokens_details.cached_tokens; in Claude's native API it is cache_read_input_tokens. The Usage page in the console shows what each request cost.