Topic 3 of 5 · 5 min read
Measuring cache hit rate and savings
Goal: Compute the hit rate and savings from API usage logs and tell whether caching is paying off.
Measure caching from the usage fields of real responses: the share of input tokens read from the cache, per route, and the cost and latency before and after. A cache that you have not measured is a guess, and a silently broken one looks exactly like a working one in the code.
Check yourself · 1 of 3
Where do you find out how many input tokens of a request were served from the cache?
Why it matters
Caching bugs are quiet. When a prefix stops matching, the app still answers correctly. The only symptom is a higher bill and slower first tokens. Usage data is the only place you will see it.
Which fields to read
- Claude returns
input_tokens(not cached),cache_creation_input_tokens(written) andcache_read_input_tokens(read). The three add up to total input. - OpenAI reports
cached_tokensinside the prompt token details of usage, next to the total prompt tokens. - Gemini reports a cached content token count in its usage metadata.
Field names change between versions, so confirm them in the current docs for your model.
How to compute it
- Log usage for every call, with the route name, model and timestamp.
- Hit rate = cached read tokens / total input tokens, per route and per day. Do not average across routes, because one busy route hides a broken one.
- Cost = input x base price + writes x write price + reads x read price. Compare it with what the same tokens would cost uncached.
- Latency: compare time to first token for calls with and without a read.
- Alert when a route's hit rate drops sharply after a deploy.
Worked example
One route, one hour of logs on a model with base 3.00, write 3.75 and read 0.30 per million tokens:
| Calls | input_tokens | cache_creation | cache_read |
|---|---|---|---|
| 1 | 1,200 | 18,000 | 0 |
| 49 | 1,200 each | 0 | 18,000 each |
Totals: 58,800 fresh, 18,000 written, 882,000 read. Hit rate = 882,000 / 958,800, about 92 percent. Cost with caching: 58,800 x 3 + 18,000 x 3.75 + 882,000 x 0.30 = 176,400 + 67,500 + 264,600 = 508,500, divided by one million, about 0.51 dollars. Uncached: 958,800 x 3 / 1,000,000 = 2.88 dollars. Savings are about 82 percent.
Before and after
Weak: "We enabled caching, so costs must be down." Better: "Hit rate on the support route fell from 91 to 4 percent on Tuesday after the prompt change. We fixed the ordering and it is back to 90."
What goes wrong
- Averaging hit rate across all routes.
- Counting cache writes as savings.
- Testing with one request, which can only be a write.
Examples
Hit rate from a log line
{"route": "support", "input_tokens": 1200, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 18800}
Total input = 1,200 + 0 + 18,800 = 20,000. Hit rate = 18,800 / 20,000 = 94 percent.
A small hit-rate function
def hit_rate(rows):
read = sum(r["cache_read_input_tokens"] for r in rows)
total = sum(r["input_tokens"] + r["cache_creation_input_tokens"] + r["cache_read_input_tokens"] for r in rows)
return read / total if total else 0.0
Call it per route and per day, and alert when it falls by more than a set margin.
Key points
- Every provider reports cached tokens in response usage data, so log it for each call.
- Hit rate is cached read tokens divided by total input tokens, tracked per route rather than as one global average.
- Savings must include the higher price of cache writes, not only the cheaper reads.
- A sudden drop in hit rate after a deploy is the usual sign of a broken prefix.
Common mistakes
- Using one global hit rate, which hides a broken route behind a busy one.
- Ignoring write cost and reporting only the read discount.
- Judging savings from a single test request.
- Assuming usage field names are the same across providers and versions.
Check yourself · 2 of 3
A call reports input_tokens 500, cache_creation_input_tokens 0, cache_read_input_tokens 9,500. What is the hit rate?
Check yourself · 3 of 3
After a Tuesday deploy the total bill rises, though traffic is flat and the answers look fine. What should you check first?
Practice items, not counted towards your mastery.
Start here
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
Breakpoints, cache lifetimes, what invalidates the cache and the usage fields that report reads and writes.
- Prompt caching (OpenAI API docs) (opens in a new tab)DocsFree
How automatic prefix caching works on OpenAI models, the cache key and the cached-token usage field.
- Context caching (Gemini API docs) (opens in a new tab)DocsFree
Implicit and explicit caching on Gemini, with storage-time and token-count details.
More for this topic
- Claude API pricing (opens in a new tab)DocsFree
The price table with base input, cache write and cache read rates per model, for cost arithmetic.