Prompt caching is a discount, in both money and latency, on the part of your prompt that is identical from one call to the next. The provider keeps the already-processed prefix, and when your next request starts with exactly the same tokens it skips that work and bills the repeated portion at a reduced rate. For an app with a long system prompt, a big tool schema, or a pasted reference document, it is often the largest cost lever that takes less than a day to pull.
The catch is the word exactly. Caches match on prefixes, so a single changed token near the top invalidates everything after it. Most disappointing hit rates come from prompt layout, not from the feature. This guide covers how the main providers differ, how to lay out a prompt so the cache can work, a cost model with real arithmetic, and how to verify you are getting hits.
How the three common implementations differ
| OpenAI API | Anthropic API | Self-hosted vLLM | |
|---|---|---|---|
| How you turn it on | Automatic for eligible prompts | Explicit: add cache_control to blocks (or at the top level) | Prefix caching setting on the server |
| Match rule | Longest shared prefix, prompts of 1,024+ tokens | Prefix up to a marked breakpoint, up to a limited number of breakpoints | Block-level prefix hash on the KV cache |
| Lifetime | Short, in-memory; evicted after inactivity | 5 minutes by default, refreshed on each hit; 1-hour option at higher write price | Until blocks are evicted under memory pressure |
| Billing effect | Cached input tokens billed at a reduced rate | Writes cost more than base input; reads cost a fraction of it | No bill; you save GPU time |
| How to see hits | Cached token count in the usage object | Cache creation and cache read token counts in usage | Server metrics and logs |
Read the primary sources before you budget: OpenAI’s prompt caching guide, Anthropic’s prompt caching documentation, and vLLM’s automatic prefix caching page. Minimum lengths, TTLs, discount rates, and supported models change, and the current numbers belong to those pages, not to a blog post. The mechanism underneath all three, reusing stored attention state, is explained in KV cache explained for builders.
Lay out the prompt so a prefix can match
Order content from most stable to most volatile. Everything above the first changed token can be cached; everything below it cannot.
- System instructions and policies (change weekly at most).
- Tool and function definitions, serialized in a fixed order with fixed key ordering.
- Few-shot examples and reference documents shared across users.
- Conversation history, appended only (never edited or reordered).
- The new user message and per-request variables (timestamp, user name, retrieved snippets).
If you are building retrieval, the retrieved chunks sit near the bottom, which is one more reason to keep them out of the system prompt. Teams that apply the habits from the prompt engineering guide usually already separate static policy from dynamic context, and the cache rewards exactly that discipline.
A worked cost example
Take a support assistant with a 6,000-token static prefix (instructions, tool schema, policy excerpts) and 400 tokens of dynamic input per call, at one million calls a month. Assume a placeholder base input price of $3.00 per million tokens, and use Anthropic’s published structure of a 1.25× write price and a 0.1× read price on the 5-minute cache. Output tokens are the same in every scenario and ignored here.
| Scenario | Input cost per call | Monthly input cost |
|---|---|---|
| No caching | 6,400 × $3 / 1M = $0.0192 | $19,200 |
| Cache hit every time (ideal) | 6,000 × $0.30 / 1M + 400 × $3 / 1M = $0.0030 | $3,000 |
| Cache miss (rewrite the prefix) | 6,000 × $3.75 / 1M + $0.0012 = $0.0237 | n/a per call |
| Realistic 85% hit rate | 0.85 × $0.0030 + 0.15 × $0.0237 = $0.0061 | $6,105 |
| Poor 40% hit rate | 0.40 × $0.0030 + 0.60 × $0.0237 = $0.0154 | $15,420 |
At 85% hits the bill falls by about 68%. At 40% hits you save only 20%, and a 20% saving can vanish once you account for the engineering time spent on it. The moral: the hit rate, not the discount rate, decides the outcome, so define a hit-rate objective (for example ≥ 80% on routes with a static prefix) and monitor it like any other SLO.
What silently destroys hit rates
- A timestamp or request ID in the system prompt (“Today is …”). Move it to the final user turn.
- Tool lists assembled from a dictionary or set, so ordering changes between processes.
- A/B testing system prompts per user, which splits traffic across many prefixes that each fall below the reuse rate.
- Rotating between model snapshots or aliases; caches are model-specific.
- Gaps longer than the cache lifetime. A route that sees one request per ten minutes pays write prices constantly on a 5-minute cache.
- Editing earlier conversation turns (for example, summarizing history in place), which changes the prefix.
- Mixing per-tenant data into the shared prefix. It lowers reuse and widens the blast radius of any leak.
TTL strategy: 5 minutes or 1 hour?
Using Anthropic’s published multipliers, a 5-minute write costs 1.25× and a 1-hour write costs 2×, while reads cost 0.1× either way. Compared with sending the prefix uncached each time, the 5-minute option pays for itself after a single read within the window (1.25 + 0.1 = 1.35× versus 2×). The 1-hour option needs two reads (2 + 0.2 = 2.2× versus 3×). So choose the longer TTL when a prefix is reused often but with gaps longer than five minutes, such as an internal tool used in bursts, and the shorter one for steady traffic. Treat these multipliers as of the documentation at the time of writing and recheck them.
Do not cache what is rarely reused. If a prefix gets one request a day, a cache write is pure extra cost.
Measure it: fields, dashboards, alerts
- OpenAI: read the cached-token count from the usage details on each response and compute
cached / prompt tokens. - Anthropic: log cache-creation and cache-read input tokens alongside uncached input tokens; hit rate is reads ÷ (reads + creations + uncached).
- Dashboards: hit rate by route and prompt version, plus effective input price per call. Add both to the stack in the LLM observability guide.
- Alert: page when hit rate drops 20 points below its 7-day median; that is almost always a deploy that touched the prefix.
- Attribute savings: record cached and uncached tokens per tenant and feature so finance sees real numbers, as described in LLM cost attribution.
Safety and governance notes
Provider caches are documented as isolated to your organization or workspace, but your own design still matters. Keep secrets, per-user personal data, and anything under a retention policy out of the cached prefix, and check each vendor’s data-handling terms if you operate under data-residency constraints. Prefix tokens are also exposed to anyone who can influence the prompt, so treat the cached region as read-only, trusted content only.
Self-hosting changes the economics. With vLLM you do not get a bill discount, you get freed GPU time: the shared prefix skips prefill, so first-token latency drops and capacity rises. See the vLLM serving guide for the flag and LLM batching and latency for what that does to your SLOs.
FAQ
What is prompt caching in LLM APIs?
Prompt caching lets a provider reuse the processed form of a repeated prompt prefix. When a new request begins with the same tokens as a recent one, the shared part is billed at a reduced rate and starts faster, because it is not recomputed.
How do I get cache hits with OpenAI and Anthropic?
Put static content first and dynamic content last, keep the prefix byte-identical, and make sure it meets the provider’s minimum length. OpenAI applies caching automatically; Anthropic requires you to mark cacheable blocks with cache_control.
Does prompt caching change the model’s output?
No. It reuses computation for the prefix; the model still processes the full prompt and generates from it. Output quality should be unchanged, though normal sampling variation still applies.
How long does a prompt cache last?
It is short by design. Anthropic’s default is five minutes, refreshed each time the cache is read, with a one-hour option at a higher write price. OpenAI describes in-memory caches that expire after inactivity. Check current documentation for exact lifetimes.
Is prompt caching worth it for small prompts?
Usually not. Providers set minimum cacheable lengths, such as 1,024 tokens on OpenAI, and savings scale with the size of the repeated prefix. The biggest gains are long system prompts, tool schemas, and shared reference documents.
What is the difference between prompt caching and semantic caching?
Prompt caching reuses internal computation for an identical prefix, and the model still generates a fresh answer. Semantic caching stores final answers and returns them for similar questions, which can save the whole call but risks serving a stale or mismatched response.
AI Generated Apps AI Code Learning Technology
