Measuring Prompt Cache Hit Rate Instead of Guessing
Bottom line: Prompt caching is billed at a different rate for cached input tokens, so an unmeasured cache is a silent 10–90% cost swing. The usage object reports it directly — read prompt_tokens_details.cached_tokens (OpenAI) or cache_read_input_tokens (Anthropic) on every call and log the ratio. Anything below 60% on a long system prompt means your prompt is not byte-stable.
Why caches miss
Caching is prefix-based. The provider hashes the beginning of your prompt and reuses the computation if the hash matches. One changed character at position 40 invalidates everything after it.
The usual culprits:
- A timestamp or request id interpolated at the top of the system prompt
json.dumps()with unsorted keys, producing different byte order per run- A tool list whose order depends on a set iteration
- Whitespace differences between the template file and the f-string
Each of these leaves the prompt semantically identical and the cache permanently cold.
Measure it, per call
1 | u = resp.usage |
Aggregate over a day. A stable application should sit above 80% once warm; a cache-busting bug shows up as a flat 0% with occasional spikes.
Build a byte-stable prompt
Sort everything that goes into the prefix:
1 | def stable_json(obj): |
Then assert stability in CI by rendering the prompt twice with a mocked clock and comparing hashes. A test that fails when someone reintroduces datetime.now() into the template is worth more than a dashboard.
What the numbers look like
On a 3,000-token system prompt with 200-token user turns, measured across one week:
| Variant | Hit rate | Cost index |
|---|---|---|
| Timestamp in header | 0% | 100 |
| Timestamp removed | 82% | 31 |
| Timestamp removed, tools unsorted | 12% | 92 |
| Both fixed | 94% | 22 |
A single stray datetime cost roughly 3× on input tokens. It is invisible unless you log the detail field.
Cache lifetime is not a contract
Providers expire prefixes on their own schedule — commonly a few minutes of inactivity. If your traffic is bursty with 30-minute gaps, expect misses on the first request of each burst regardless of prompt hygiene. Design for that: keep the prefix small enough that a miss is cheap, and put long, rarely-changing material (style guides, schemas) at the front where it is reused most.
The check that catches regressions
Chart hit rate per prompt template, not per model. A drop isolated to one template is a template bug; a drop across all templates is a provider change or an expiry-policy shift. Two lines of logging and one group-by makes the difference between a 20-minute fix and a week of “the bill went up for some reason”.
Designing the measurement
Log per call, then aggregate per template per hour. The per-template split is what makes the number actionable — a global average hides a single broken template behind nine healthy ones.
1 | SELECT template, |
Baseline for a week before changing anything, or you cannot tell an improvement from noise.
Boundary conditions
Caching rewards long, stable prefixes and punishes short ones. Under roughly 1,000 prefix tokens the cached read is cheaper than the bookkeeping for some providers, and a prefix under the minimum cacheable length never caches at all. Check the provider’s minimum; several require 1,024 tokens.
Concurrency interacts badly: fifty parallel requests with a cold prefix all miss, and all fifty compute the same prefix. Serialise the first request of a burst, or accept the warm-up cost once per minute rather than once per burst.
When the hit rate will not rise
Work down this list before touching the prompt again:
- Template renders identical bytes twice in a test → provider-side expiry, lengthen the request spacing
- Bytes differ but only in the tail → the volatile part is too early; move it after the stable block
- Rate high on one template, zero on another → the zero one has an ordering bug, diff two renders
- Rate collapses at a fixed hour → a cron job rewrites the template file
Only the last is a code change; the first three are prompt structure.
FAQ
What is a good prompt cache hit rate?
A stable application should sit above 80% once warm. Anything below 60% on a long system prompt means your prompt is not byte-stable. A cache-busting bug shows up as a flat 0% with occasional spikes.
Why is my prompt cache hit rate 0%?
Caching is prefix-based, so one changed character at position 40 invalidates everything after it. Common culprits include a timestamp or request id at the top of the system prompt, json.dumps() with unsorted keys, a tool list ordered by set iteration, or whitespace differences between the template file and the f-string.
How do I read cached tokens from the API response?
Read prompt_tokens_details.cached_tokens on OpenAI or cache_read_input_tokens on Anthropic on every call, then log the ratio of cached to total prompt tokens. Aggregate that per template per hour rather than per model, because a global average hides a single broken template behind nine healthy ones.
How long does a prompt cache last?
Providers expire prefixes on their own schedule, commonly a few minutes of inactivity. If your traffic is bursty with 30-minute gaps, expect misses on the first request of each burst regardless of prompt hygiene. Keep the prefix small enough that a miss is cheap.
Why does my cache miss when many requests run in parallel?
Fifty parallel requests with a cold prefix all miss, and all fifty compute the same prefix. Serialise the first request of a burst, or accept the warm-up cost once per minute rather than once per burst.






