Prompt caching explained: how it cuts LLM cost and latency
Prompt caching reuses the processed opening part of a prompt, so repeated instructions bill at a fraction of input price and long prompts answer sooner.
Prompt caching lets the model provider keep the processed state of the opening part of your prompt, so the next request that starts with exactly the same text skips that work. Those cached tokens are billed at a fraction of the normal input price (a tenth on many models) and long prompts start answering sooner. You get the saving only if the start of your prompt stays byte-for-byte identical and requests arrive often enough to keep the cache alive.
Turning caching on takes minutes. Getting a high hit rate is a prompt design problem, and it is worth an afternoon because most business prompts are mostly repeated text.
What actually gets cached
Before a model writes the first word of its answer, it computes internal state for every prompt token, usually called the key-value (KV) cache. For a 20,000-token prompt that is real work, and it is why long prompts feel slow to start.
Most of that prompt is the same on every call: tool definitions, system instructions, a policy document. Only the last few hundred tokens (the question, perhaps some retrieved passages) are new.
Prompt caching keeps the computed state for that repeated opening part, called the prefix. OpenAI's documentation is explicit that it stores the key-value tensors, not your text. When the next request begins with the same prefix, the provider loads the stored state and only processes the new tail.
Two properties follow, and they explain most surprises.
First, the match is on the prefix, from the very first token. Anthropic's docs say the prefix must be 100% identical up to the cache point, text and images included. A single changed character on line one means nothing after it can be reused, even if the remaining 19,999 tokens are identical.
Second, the answer does not change. Anthropic states that a response with caching is identical to one without it. No stored answer is replayed, which makes this very different from semantic caching, where an old answer to a similar question comes back. (Semantic and response caching are covered briefly in the guide to LLM cost control.)
The price of a hit, a write and a miss
Caching is not a flat discount. With some providers the request that stores the prefix costs more than an uncached call, and later requests inside the cache lifetime cost much less.
Anthropic publishes the clearest numbers. Writing to the default 5-minute cache costs 1.25 times the base input price, writing to the 1-hour cache costs 2 times, and a cache read costs 0.1 times on most models. Its newest Opus and Sonnet models read at 0.05 times, and a few top models at 0.025 times. Each read refreshes the lifetime at no extra charge. The pricing page states the break-even directly: the 5-minute cache pays for itself after one read, the 1-hour cache after two.
OpenAI caches automatically on supported models and says cached input is discounted by up to 95%. Google's Gemini API turns on implicit caching by default for Gemini 2.5 and newer models and passes the saving on when a request hits, though the caching page itself does not state a rate.
Here is a worked illustration, using Anthropic's standard multipliers. Say a support assistant has a 12,000-token prefix (tool definitions, instructions and a returns policy) and the average question adds 300 tokens.
Without caching, every request pays for 12,300 input tokens. With a cache hit, it pays for 12,000 × 0.1 = 1,200 token-equivalents for the prefix plus 300 for the question, so 1,500 in total: about 88% less input cost on that request. On a miss that writes the cache, it pays 12,000 × 1.25 = 15,000 plus 300, which is 15,300, or about 24% more than not caching at all.
So everything depends on the ratio of hits to writes. In office hours, with a question every 20 seconds, the 5-minute cache never expires and nearly every request hits. At 2am, with one question every 20 minutes, every request is a write and costs more. A daytime dashboard will not show it.
Output tokens are unaffected. If your cost is dominated by long answers rather than long prompts, caching will not move the bill much, and you should look at answer length or model choice first. The small vs large language models article covers that side.
How the three big APIs compare
A prompt tuned for one provider may cache poorly on another. This table reflects each provider's documentation as of November 2026; these pages change often, so check before relying on a number.
| Anthropic (Claude API) | OpenAI | Google (Gemini API) | |
|---|---|---|---|
| How it is turned on | One top-level cache_control field (automatic) or markers on specific blocks, up to 4 |
On by default for supported models | Implicit caching on by default for Gemini 2.5 and newer |
| Minimum prefix | 512 to 4,096 tokens, by model | 1,024 tokens on recent models | 2,048 to 4,096 tokens, by model |
| Lifetime | 5 minutes default, 1 hour option | Retention options from minutes up to 24 hours, by model and setting | Short window; send similar prefixes close together |
| Cached read price | 0.1x on most models (0.05x or 0.025x on some) | Up to 95% off input | Saving passed on; rate not stated on the caching page |
| Where hits show | cache_read_input_tokens and cache_creation_input_tokens |
usage.input_tokens_details.cached_tokens |
usage.total_cached_tokens |
The minimum lengths matter more than they look. Anthropic's docs say a prompt below the minimum is simply processed without caching, and no error is returned. Your code will look correct and your bill will not change.
There is also a rate-limit difference worth knowing. Anthropic's documentation says cache hits are not deducted against your rate limit, which is one of the reasons it gives for using the 1-hour cache. OpenAI's guide says the opposite for its API: cached input tokens still count toward tokens-per-minute limits.
Laying out a prompt so it caches
Everything that is the same on every call goes first, everything that changes goes last. Teams break this rule in small, innocent ways.
The most common one is a timestamp at the top. Someone adds "Today is Monday 23 November 2026, 14:32 IST" to the first line of the system prompt so the model knows the date, and the prefix now changes every minute. Move the date to the end, near the user's message, or round it to the day.
The second is personalisation in the system prompt. "You are helping Priya from the Pune branch" (an invented example) makes every user's prefix unique. Put the user context after the shared instructions.
The third is tool definitions that are rebuilt on every call. If your code assembles tools from a dictionary or a set, their order or the key order inside their JSON schemas can change between requests. Tools come first in the prefix on Anthropic's API, and its docs show that changing them invalidates everything after, including the system prompt and messages. Sort them, serialise them deterministically, and keep the list fixed per agent. OpenAI's guide gives the same advice and suggests restricting which tools are allowed per call through settings such as tool_choice or allowed_tools instead of removing definitions.
Retrieval-augmented generation (RAG) needs one extra decision. Retrieved chunks differ per question, so they belong after the stable instructions, never before them. If you have a reference document that every question needs, such as a rate card, put it in the prefix and let retrieval add only the variable passages after it. The RAG explainer covers the retrieval half.
Hidden settings count too. Anthropic's table of what invalidates the cache includes switching tool_choice, adding or removing images, and, on some models, changing thinking parameters. A/B testing two system prompt variants splits your traffic between two caches, which halves the hit rate on low-volume features.
Many of these mistakes come from services each building prompts their own way, and one gateway gives you one place to fix the template. That is the role of the LLM gateway in CloudOEngine's AI runtime, which sits on the same platform as your data connections and pipelines.
Agents and long conversations
Agents gain the most, because they resend a growing conversation at every step. An eight-step agent sends its full history eight times. Without caching, input cost grows with roughly the square of the number of steps.
With a cache point that moves forward as the conversation grows (Anthropic's automatic caching does this), each step pays full price only for what was added since the last step. Everything before is a read.
There are limits to watch. Anthropic's cache lookup only checks back up to 20 content blocks from the cache point, so in a long conversation with many tool calls the docs suggest adding a second cache point earlier. Google's documentation for Gemini 4 says the full previous request is cached only when the new request adds at least 2,050 tokens; for smaller additions, the last 2,050 tokens of the previous request are processed again at the normal rate. Agents that take many short steps should expect fewer savings there than the headline suggests.
Timing matters in agents too. If a human approval step or a slow tool takes longer than five minutes, the 5-minute cache is gone when the agent resumes. Anthropic recommends the 1-hour cache for exactly this case: prompts used less often than every 5 minutes but more often than every hour.
When an agent's cost suddenly jumps, the cause is usually a single step that changed the prefix. CloudOEngine traces every agent step and tool call, and each one can be opened and inspected, so you can find the step where the prompt changed instead of guessing. The agent observability guide lists what to record in those traces, including cached token counts.
Latency: what to expect
Caching skips the work of reading the prefix, so the first token of the answer arrives sooner. Anthropic says you generally see improved time-to-first-token for long documents, and OpenAI says caching reduces the time spent processing input. Neither page gives a single percentage, and you should not trust anyone who quotes one without your prompt length and model. Measure it yourself: time-to-first-token with the cache cold and warm, on your real prompts.
The gain is largest with a large prefix and a small question, such as a 50,000-token contract and a one-line question about it.
Generation time is unchanged, but people judge chat speed mostly by when text starts appearing.
The first request after a quiet period pays the full wait. Anthropic offers pre-warming: a request with max_tokens set to 0 writes the cache without generating output. You pay the write, and a 5-minute cache needs re-warming every 5 minutes, so it suits features with predictable start times, like a call centre opening at 9am.
Cache isolation and multi-tenant products
A shared cache is a side channel. If two users' requests can hit the same cache, the faster response time tells one user something about what the other sent. This is not theoretical: in a 2025 paper (accepted at ICML 2025), researchers built statistical audits and detected global cache sharing across users at seven API providers, including OpenAI. The paper notes that this risks leaking information about other users' prompts.
Providers have moved since. OpenAI's current guide says caches are not shared across organisations. Anthropic says different organisations never share caches, and that on its own API caches are also isolated per workspace.
That protects you from other companies, not your tenants from each other. If several customers share one API key, a prefix with one customer's data is cached under your organisation. Keep tenant data out of shared prefixes, and if you need per-tenant cache isolation, use a separate workspace or project per tenant. On CloudOEngine, tenant isolation, role-based access and per-tenant encrypted secrets are part of the platform, which makes separate credentials per tenant a configuration choice rather than a project.
If you run open models on your own servers, the same idea applies at the inference engine. vLLM's automatic prefix caching hashes each block of the KV cache together with everything before it, so a block is reused only when the full prefix matches. Its design notes describe a cache_salt that limits reuse to requests sharing the same salt, to reduce timing-based leakage. Set it per tenant.
Measuring whether it works
Do not assume caching works because you turned it on. Every major API reports cached tokens on each response, and the number you want is the cached share of input:
hit rate = cached input tokens ÷ total input tokens
On Anthropic's API, total input is cache_read_input_tokens + cache_creation_input_tokens + input_tokens. If both cache fields read zero, the docs say the prompt was not cached, usually because it was below the minimum length.
Track that ratio per feature and per hour of the day. A sudden drop almost always means a deploy changed the top of a prompt. Track writes too: many writes and few reads means you pay a premium for nothing.
If you are starting this week, I would do it in this order:
- Log the cache fields from every response for one day, per feature.
- For the three features with the most input tokens, read the first 200 tokens of their prompts and remove anything that varies.
- Sort tool definitions and serialise them deterministically.
- Compare hit rate during the day and at night, and pick the cache lifetime that matches your traffic.
- Add a hit-rate alert so the next prompt change that breaks the cache is caught the same day.
How CloudOEngine helps
Prompt caching is easiest to get right when every model call goes through one place that builds prompts consistently and records what happened. CloudOEngine is one platform and console for connecting data (databases, files and camera streams), pipelines, an AI runtime with an LLM gateway, agents with tool access and RAG, plus Vision AI and live video. It all runs under one permission model and one audit trail.
For caching, three parts matter. The LLM gateway gives you one place to own the prompt template, so a timestamp or a reordered tool list is fixed once instead of in every service. Agents run real tools, such as filing a ticket, and every step and tool call is traced and inspectable, so a sudden cost jump can be followed back to the step where the prompt changed. Governance is included, not sold as an add-on: tenant isolation, role-based access, audit logs and per-tenant encrypted secrets, which help keep one customer's data out of another customer's cached context.
For agents that act on their own, Autopilot adds approval rules: a person approves an action in the app or through an email approve link before it runs. 500+ connectors reach the data your prompts depend on, and a 99.9% uptime SLA option is available, for example on the managed platform. CloudOEngine is built in Bengaluru, India.
Book a demo or read about the AI runtime.
Frequently asked questions
What is prompt caching?
Prompt caching lets a model provider keep the processed state of the opening part of a prompt (the prefix) for a few minutes or longer. When the next request starts with exactly the same prefix, the provider skips that work and bills those tokens at a discount.
Does prompt caching change the model's answer?
No. Anthropic's documentation states that a cached response is identical to one produced without caching. The cache stores the model's intermediate state for the prefix, not a previous answer.
How much cheaper are cached tokens?
On Anthropic's API, cache reads cost 0.1 times the base input price on most models and 0.05 times on its newest Opus and Sonnet models, while writing the cache costs 1.25 times (5 minutes) or 2 times (1 hour). OpenAI says cached input is discounted by up to 95%.
Why am I not getting cache hits?
The usual causes are a prefix below the minimum length (512 to 4,096 tokens depending on provider and model), something that changes near the start of the prompt such as a timestamp or user name, tool definitions that change order, or traffic too sparse for the cache to survive between requests.
Is prompt caching safe for multi-tenant applications?
Major providers now state that caches are not shared across organizations. Inside your own account, keep tenant-specific data out of shared prefixes, and if you run your own inference server, scope the cache per tenant, for example with vLLM's cache salt.
Is prompt caching the same as semantic caching?
No. Prompt caching reuses computation for an identical prefix and the model still generates a fresh answer. Semantic caching returns a stored answer for a similar question, which saves more but can return a stale or wrong answer.
Sources
- Prompt caching (Claude API documentation)
- Pricing (Claude API documentation)
- Prompt caching (OpenAI API documentation)
- Context caching (Google AI Studio / Gemini API documentation)
- Automatic prefix caching (vLLM design documentation)
- Auditing Prompt Caching in Language Model APIs (Gu, Li, Kuditipudi, Liang, Hashimoto, ICML 2025)
