Prompt caching in AI APIs: lower costs for repeated context
If your AI chatbot, assistant or automation sends the same long instructions, tool definitions, terms and conditions or product guide with every request, you are paying to process the same text over and over. Prompt caching lets the AI API provider reuse the already processed, unchanged beginning of a request. The result can be lower input-token costs and a shorter time before the response starts, but only when the prompt is structured correctly and requests arrive often enough.
This article explains how prompt caching works with OpenAI, Anthropic Claude and Google Gemini, how to structure a prompt so the cache is actually used, how to measure the hit rate and when it is not worth it. Prices and limits vary by model, so always check the provider’s current documentation and pricing before you decide.
What prompt caching is and when it helps
When a model processes input, it computes internal intermediate states. Prompt caching stores them for a reusable prefix, the unchanged beginning of the prompt. When a later request starts with exactly the same prefix and finds a valid cache entry, the model does not process those tokens again and continues only with the new content. The OpenAI documentation lists cheaper input tokens and less time spent processing input before the response starts as the benefits.
The key word is prefix. The cache matches from the first token up to the point where requests first differ. Google’s Gemini documentation explicitly warns that the first differing token breaks the cache for everything after it. So if you put the current date and time at the start of the prompt, the rest of the identical instructions will no longer be served from the cache.
Prompt caching makes the most sense in these situations:
- a customer chatbot with long rules, a service price list or a returns policy,
- an AI agent with many tools and detailed argument schemas,
- repeated questions about the same internal handbook, contract or technical documentation,
- long conversations in which the history is sent again at every step.
How OpenAI, Anthropic and Google handle it
The principle is the same, but activation, minimum length, lifetime and price multipliers differ. The table summarises data from the official documentation for current models at the time of writing. Multipliers apply to the standard input-token price of the given model.
| Feature | OpenAI | Anthropic Claude | Google Gemini |
|---|---|---|---|
| Activation | On by default for supported models; newer models also support explicit cache breakpoints | A single cache_control field for automatic caching, or explicit breakpoints (max. 4) |
Implicit caching is on by default for Gemini 2.5 and newer |
| Minimum prefix length | 1,024 tokens for GPT-5.6 and later | Depends on the model, for example 512 or 1,024 tokens | 4,096 tokens for Gemini 4 |
| Lifetime | At least 30 minutes, optionally 12 hours (Responses API) | 5 minutes, refreshed on each use; optionally 1 hour | Not guaranteed, at most about 24 hours in memory |
| Cache write price | 1.25× for 30 minutes, 2.5× for 12 hours | 1.25× for 5 minutes, 2× for 1 hour | No write surcharge and no storage fee |
| Cache read price | 0.1× for most newer models, 0.05× for some | 0.1× by default, lower for some models | 95% lower for Gemini 4, up to 90% lower for Gemini 3 and 2.5 |
| Field to measure | cached_tokens and cache_write_tokens |
cache_read_input_tokens and cache_creation_input_tokens |
total_cached_tokens |
Data sources: OpenAI Prompt caching, Anthropic Prompt caching and Google Gemini Context caching. Anthropic states that cache multipliers stack with other pricing modifiers, such as the Batch API discount.
Practical example: an online store chatbot with long rules
Illustrative example: an online store runs a chatbot for questions about shipping, returns and product selection. Every request contains the chatbot instructions, shipping and complaint rules, a short category guide and tool definitions for checking order status. Together this is roughly 12,000 tokens. The customer’s question itself is about 300 tokens.
Without caching, these 12,000 tokens are billed at full price for every question. At OpenAI’s rates for newer models with a 30-minute lifetime, the first write costs 1.25 times and each further full reuse 0.1 times the standard price. OpenAI’s documentation calculates that one write and nine full reads cost 2.15 times the price of the prefix, while ten requests without caching cost 10 times. In both cases you pay the standard rate for the customer’s question.
This saving, however, assumes that all ten questions arrive while the cache is valid and that the prefix is exactly the same. If the chatbot receives one question per hour, the cache may expire in between, and with models that charge for writes you may pay more than without caching. That is why the decision should be based on real traffic, not on the best-case scenario.
Step-by-step implementation
1. Measure what is actually repeated
Use your logs to find out how many tokens the unchanged part of the prompt has, how many requests arrive per hour and how long the gaps between them are. If the repeated part is shorter than the model’s minimum, no cache is created. Anthropic notes that a shorter prompt is processed without caching and the API returns no error.
2. Order the prompt from stable to variable
Put tool definitions, system instructions, examples and reference documents at the beginning. The user’s question, the current time, data about the specific customer and search results belong at the end. Anthropic builds the prefix in the order tools, system and messages; a change at one level invalidates that level and all following levels.
3. Choose automatic or explicit mode
Automatic mode is enough for simple conversations. Place explicit cache breakpoints after the last block that is identical across all requests. For parts that change at different rates, such as stable tools and context updated daily, use several breakpoints.
4. Set the lifetime to match the request rhythm
A shorter lifetime is cheaper to write but expires with sparse traffic. A longer one costs more to write and only pays off when the same prefix is reused repeatedly within that window. OpenAI notes that the minimum lifetime does not guarantee that every request actually uses the cache.
5. Measure the hit rate in production
Log the cache read and write fields from the API response and track the share of cached tokens among all input tokens. Google recommends monitoring the ratio total_cached_tokens / total_input_tokens to catch prompt edits that lower the hit rate.
Tradeoffs and when caching is not worth it
Prompt caching is not a universal discount. With OpenAI and Anthropic, writing to the cache costs more than standard input, so a prefix that is never read again increases costs. With Gemini, implicit caching has no surcharge, but Google explicitly states that it is opportunistic and guarantees neither a retention window nor savings.
Another tradeoff is flexibility. If you want to tailor instructions to a specific customer in every request, move that part after the cache, otherwise you lose the prefix match. Truncating or summarising the conversation history changes the prefix and, according to OpenAI, can reset cache reuse.
For sensitive data, verify isolation. Both OpenAI and Anthropic state that different organisations do not share caches; on the Claude API, Anthropic also isolates caches between workspaces, and Google isolates implicit caches at project level. For an application serving multiple clients, OpenAI recommends separate prompt_cache_key values per customer, which helps separate cache accounting and prevent probing whether certain content was already cached.
Most common mistakes
- Date, time or request ID at the start of the prompt. The prefix differs on every call and the cache never matches.
- A breakpoint on a block that changes. Anthropic describes a case where you pay for a new write on every request and a read never happens.
- Unstable order of tools or JSON keys. Even changing the order or description of a tool creates a different prefix.
- A prefix below the model’s minimum. The cache is silently not created and your logs show zero reads and zero writes.
- Estimating savings without measuring. The best case from a calculator may not repeat in real traffic with gaps between requests.
Pre-launch checklist
- The repeated part of the prompt exceeds the minimum length of the chosen model.
- Tools, instructions and reference documents come first and in a stable order.
- Variable data comes only after the last cache breakpoint.
- The cache lifetime matches the actual rhythm of requests.
- Logs record tokens read from cache, tokens written to cache and standard input tokens.
- A multi-client application uses separate cache keys or other separation as documented by the provider.
- Before changing the model or prompt, a comparison of cache hit rate and costs is ready.
Frequently asked questions
Does prompt caching change the model’s answers?
That is not its purpose. The cache stores the processed state of an identical prompt beginning, and the model still processes the new content and generates a new response. The input is cached, not a finished answer.
Do I have to turn prompt caching on?
It depends on the provider. OpenAI has it on by default for supported models, Gemini uses implicit caching for 2.5 and newer models, and with Claude you enable it with the cache_control field.
How do I know whether the cache works?
Check the usage data in the API response. If both cache read and cache write values are zero with Claude, the prompt was not cached. With OpenAI, watch cached_tokens, and with Gemini, total_cached_tokens.
Is prompt caching the same as the Batch API?
No. The Batch API handles bulk asynchronous processing, while prompt caching reuses an identical prompt beginning. According to Anthropic, cache price multipliers stack with the Batch API discount, so the two approaches can complement each other.
Need to reduce the cost of your AI automation?
If you already run an AI chatbot, agent or automation and your API costs keep growing, we can help you measure which part of the prompt repeats, restructure it and set up cache monitoring. Explore our custom AI agents, process automation and API integrations and more practical guides on the Webiant blog. You can describe your situation through the contact form.