Sub topic 8
What Caching Changes in Cost and Latency
This chapter assumes you have read the earlier chapters on prefill, exact prefix matching, and cache lifetime. You should be comfortable with token-based pricing and short Python snippets.
What gets cheaper and what does not
Caching only affects the part of the input that matched a stored prefix. Everything else is billed as usual.
| Part of a request | Effect of caching |
|---|---|
| Cached prefix tokens (read) | Billed at a discounted rate, and prefill for them is skipped |
| Prefix tokens written to the cache | Some providers bill a premium over the normal input rate. Others charge nothing extra |
| Input tokens after the prefix | Billed at the normal input rate |
| Output tokens | Unchanged |
The exact multipliers differ by provider, by model, and by cache lifetime option. Read the current price page and take the numbers from there. The examples below use illustrative values: a write costs 1.25 times the normal input price, and a read costs 0.10 times.
Latency
A cache hit lets the provider skip prefill for the cached part of the prompt. That mainly reduces time to first token. The saving grows with prefix length, because prefill work grows with the number of input tokens.
Caching does not speed up generation. The model still produces output tokens one at a time, so a request with a short prompt and a long answer gains little. Providers describe latency gains as typical or up to a figure, not as guarantees. Measure your own time to first token with and without hits.
Break-even arithmetic
Measure cost in units of one uncached prefix. Without caching, n requests cost n units. With caching, the first request pays the write price and the other n−1 pay the read price.
def relative_cost(n_requests, write_mult, read_mult):
"""Prefix cost over n requests, in units of one uncached prefix."""
cached = write_mult + (n_requests - 1) * read_mult
uncached = n_requests
return cached, uncached
for n in (1, 2, 5, 20):
cached, uncached = relative_cost(n, 1.25, 0.10)
print(n, round(cached, 2), uncached)This prints:
1 1.25 1
2 1.35 2
5 1.65 5
20 3.15 20With these multipliers, a single request loses 25%, and the second request already puts you ahead. The break-even count is (write − read) / (1 − read), about 1.3 requests. Caching therefore pays if the prefix is read at least once before it expires.
The failure case: expiry between requests
The calculation assumes every request after the first is a hit. If your traffic is sparse and the entry expires between requests, every request pays the write price. Then n requests cost 1.25n units instead of n, which is worse than not caching.
print(relative_cost(20, 1.25, 1.25)[0]) # 25.0 units, versus 20 uncachedUse the hit ratio from the measuring chapter to check which case you are in. A low ratio on a provider that charges a write premium means you are paying for writes that nobody reads.
Estimating for your traffic
- Count the stable prefix tokens, and confirm they meet the minimum cacheable length.
- Estimate how many requests reuse the prefix within one cache lifetime. Reads refresh the lifetime on most providers, so steady traffic keeps an entry alive.
- Compute the cached and uncached cost with the published multipliers, as in
relative_cost. - Add the uncached suffix and output tokens to both sides. They are the same either way, so they shrink the relative saving without changing the decision.
Exercise
A provider charges 1.25 times the normal input price to write a prefix and 0.10 times to read it. Your prefix is reused once per hour, and the cache lifetime is 5 minutes with no refresh between requests. Does caching save money?
Show answer
No. Each request arrives after the entry has expired, so every request is a write at 1.25 times the normal price. You pay 25% more on the prefix with no reads to offset it. Either send traffic often enough to refresh the entry, use a longer lifetime option if the provider offers one and its price makes sense, or skip caching for this prefix.