How to Calculate the Real Cost of LLM APIs: Cache Hit Rates and Pricing Plans
This guide explains how to compare LLM API costs on a consistent basis. It covers how cached input, uncached input, and output tokens are billed, shows how cache hit rates can change which pricing plan is cheaper, and provides a sensitivity analysis script and methods for measuring your own hit rate.
1. Why List Prices Cannot Be Compared Directly
LLM inference services typically charge by token. The tokens in a single request fall into three parts:
- Cache-hit tokens: the prefix that repeats an earlier request and is served from the server-side cache. These are billed at the cache price.
- New input tokens: input tokens that miss the cache. These are billed at the input price.
- Output tokens: tokens generated by the model. These are billed at the output price.
The cost of a single request is calculated as follows:
Cost per request = cache-hit tokens × cache price+ new input tokens × input price+ output tokens × output price
Cache prices are usually only about one-tenth to a few percent of the input price, so the cache hit rate determines most of the real cost. When comparing costs, the accounting basis must be consistent. This includes whether failed retries are billed, whether the provider rounds usage up to a minimum billing unit, whether prices include tax, and how exchange rates are applied. Without a consistent basis, the bills from two services cannot be compared directly.
2. How Cache Hit Rates Change Cost
2.1 Test Scenario and Parameters
Consider a coding application. System prompts, code repository context, and conversation history are reused repeatedly. The token distribution is therefore 98% cache hits, 1.5% new input, and 0.5% output. This pattern is common in AI-assisted programming, multi-turn conversations, and long-running agent workflows.
The table below lists the unit prices of two hypothetical plans, in CNY per million tokens. These figures are for illustration only. For real calculations, replace them with the published prices of the target service.
| Pricing item | Plan A (standard) | Plan B (cache-discount) |
|---|---|---|
| Cache price | 0.115 | 0.050 |
| Input price | 2.0 | 4.0 |
| Output price | 8.0 | 9.0 |
2.2 Results at Two Hit Rates
Using 100 million (100,000,000) tokens as the basis, a 98% hit rate means 98 million tokens are cache hits, 1.5 million are new input, and 0.5 million are output.
- Plan A: 98M × 0.115 + 1.5M × 2.0 + 0.5M × 8.0 = 11.27 + 3.00 + 4.00 = 18.27 CNY
- Plan B: 98M × 0.050 + 1.5M × 4.0 + 0.5M × 9.0 = 4.90 + 6.00 + 4.50 = 15.40 CNY
At this hit rate, Plan B is 2.87 CNY cheaper.
At a 95% hit rate, new input makes up 3.75% and output makes up 1.25%:
- Plan A: 95M × 0.115 + 3.75M × 2.0 + 1.25M × 8.0 = 10.93 + 7.50 + 10.00 = 28.43 CNY
- Plan B: 95M × 0.050 + 3.75M × 4.0 + 1.25M × 9.0 = 4.75 + 15.00 + 11.25 = 31.00 CNY
At this hit rate, Plan A is 2.57 CNY cheaper. The ranking of the two plans reverses within this range.
The first conclusion is that you must quantify your own cache hit rate before estimating cost. Do not reuse someone else’s cost estimates directly.
2.3 The Crossover Point: Why Costs Reverse
In the model above, input and output tokens are allocated to the uncached portion in fixed proportions. As a result, the cost of each plan is linear in the hit rate:
Unit: CNY; total tokens in millionsCost = total tokens (M) × [hit rate × cache price + (1 − hit rate) × average miss price]Average miss price = 0.75 × input price + 0.25 × output price
Plan A’s average miss price is 0.75 × 2.0 + 0.25 × 8.0 = 3.50 CNY per million tokens. Plan B’s is 0.75 × 4.0 + 0.25 × 9.0 = 5.25 CNY per million tokens.
The hit rate at which the two lines intersect is the crossover point:
Crossover hit rate = (B average miss price − A average miss price) / [(B average miss price − A average miss price) + (A cache price − B cache price)]= 1.75 / (1.75 + 0.065) ≈ 96.4%
Above a 96.4% hit rate, Plan B is cheaper. Below it, Plan A is cheaper. The larger the gap between the two services’ cache prices and the smaller the gap between their miss prices, the closer the crossover point moves to 100%. Conversely, when one plan has only a slightly lower cache price but much lower input and output prices, the crossover falls at a lower hit rate.
2.4 Sensitivity Analysis Code
The code below calculates costs across several hit rates. It checks that the three token shares sum to 1, which prevents incorrect ratio inputs.
def cost(total_tokens, hit_rate, in_ratio, out_ratio,cache_price, in_price, out_price):"""total_tokens: total number of tokens; prices: CNY per million tokens; return value: CNY"""assert abs(hit_rate + in_ratio + out_ratio - 1) < 1e-9, "The three token shares must sum to 1"m = total_tokens / 1_000_000return m * (hit_rate * cache_price + in_ratio * in_price + out_ratio * out_price)for hr in [0.90, 0.93, 0.95, 0.97, 0.98]:miss = 1 - hrin_r, out_r = miss * 0.75, miss * 0.25a = cost(1e8, hr, in_r, out_r, 0.115, 2.0, 8.0)b = cost(1e8, hr, in_r, out_r, 0.050, 4.0, 9.0)print(f"Hit rate {hr:.0%}: Plan A={a:.2f} CNY, Plan B={b:.2f} CNY")
The output, rounded to two decimal places, is shown below:
| Hit rate | Plan A (CNY) | Plan B (CNY) | Cheaper plan |
|---|---|---|---|
| 90% | 45.35 | 57.00 | A |
| 93% | 35.20 | 41.40 | A |
| 95% | 28.43 | 31.00 | A |
| 97% | 21.66 | 20.60 | B |
| 98% | 18.27 | 15.40 | B |
These results match the calculations in Section 2.2. The crossover falls between 95% and 97%, which is consistent with the 96.4% value calculated in Section 2.3.
2.5 How to Measure Your Own Hit Rate
In practice, you can collect the data in three ways:
- Log analysis: Most inference services return the number of cache-hit tokens and the total input tokens in each response. Aggregate these values in your logging system by day and by business line.
- Traffic replay: Run small-scale tests of candidate plans with real business requests, and observe how the hit rate changes across different session lengths.
- Interval sampling: Take several sample points between 85% and 99%, and substitute them into the formula above
