Sub topic 14
Deciding when caching is worth it
This chapter assumes you know how exact prefix matching works, how cache lifetime (TTL) behaves, and how to read the usage fields from earlier chapters. Here you will use those pieces to decide whether caching saves money for a given workload.
Caching is not free everywhere
A cache hit is cheaper than normal input processing. A cache write may cost more than normal input, depending on the provider. Some providers charge a premium to write an entry. Others write automatically at the normal input price. Check your provider's current price page and note two numbers, both as multipliers of the normal input price:
write_mult: the cost of writing the prefix to the cache (1.0 means no premium).read_mult: the cost of reading it on a hit.
The values below are examples only. Replace them with the published ones.
Break-even hit rate
Let h be the fraction of cacheable requests that hit. Each request costs read_mult on a hit and write_mult on a miss. Caching pays off when the average is below 1.0 (the uncached cost):
h * read_mult + (1 - h) * write_mult < 1
Solving for h gives the break-even hit rate.
def break_even_hit_rate(write_mult, read_mult):
"""Minimum hit rate at which caching costs less than no caching."""
if write_mult <= 1:
return 0.0 # no write premium: any hit saves money
return (write_mult - 1) / (write_mult - read_mult)
def relative_cost(hit_rate, write_mult, read_mult):
"""Average prefix cost per request, where 1.0 means uncached."""
return hit_rate * read_mult + (1 - hit_rate) * write_mult
# Example multipliers only; use your provider's published prices.
print(round(break_even_hit_rate(1.25, 0.10), 3)) # 0.217
print(round(relative_cost(0.90, 1.25, 0.10), 3)) # 0.215With these example prices, one hit in about five requests is enough to break even. A 90% hit rate cuts the prefix cost to roughly a fifth. This applies only to the cached prefix. The uncached suffix and the output tokens still cost the normal amount.
Traffic patterns decide the hit rate
The hit rate comes from your traffic, not from your prompt alone. Entries expire after the TTL unless a hit refreshes them. So the gap between requests that share a prefix matters most.
| Pattern | Likely result |
|---|---|
| Steady traffic, one long shared prefix | High hit rate. Caching pays off. |
| Bursts with long idle gaps | Entries expire between bursts. Each burst pays a write. |
| Many different long prefixes, each used rarely | Mostly writes. Caching can cost more than it saves. |
| Prefix below the minimum cacheable length | Nothing is cached. No savings. |
| Short prompts dominated by output tokens | Savings are small compared with total cost. |
A decision flow
Measure before and after
Estimates are a starting point. Enable caching, log the three usage counters per request, and compute the hit ratio from the earlier chapter. Then compare real spend with the model. If the measured hit ratio sits below break-even for a week, remove the cache markers or fix the cause of the misses.
Other costs to weigh
- Latency: hits reduce time to first token for long prefixes. If latency matters more than cost, a modest hit rate can still justify caching.
- Complexity: stable-first layout can force you to restructure prompts, and tools and history must stay byte-identical. Count that engineering effort.
- Prompt changes: each edit to the prefix causes one write. Frequent prompt releases lower the hit rate.
Exercise
A provider charges 2.0x the normal input price to write a cache entry and 0.1x to read one. What is the break-even hit rate, and would caching pay off at a 40% hit rate?
Show answer
Break-even is (2.0 - 1) / (2.0 - 0.1) = 1 / 1.9, about 0.526, or 53%. At a 40% hit rate the average cost is 0.4 0.1 + 0.6 2.0 = 1.24 times the uncached cost, so caching would cost more than not caching.