LLM API Bill Calculation: Cache Hit Rate Is the Key Metric
Comparing per-million-token prices can mislead cost decisions for online LLM workloads. This article builds a total-cost model that accounts for cache hit rate, input/output mix, time-of-day pricing, and subscription plans. It explains how to measure and improve cache hit rate, works through two pricing examples, and provides a practical decision checklist for selecting an inference service.
When teams choose an LLM inference service, the first instinct is often to compare the price per million tokens. For offline batch processing, that comparison can still be useful. For online workloads, it frequently leads to the wrong conclusion.
There are three reasons for this.
First, real traffic does not have a fixed input/output structure. In a chat or code-completion request, input tokens include conversation history, system prompts, and retrieval results, while output tokens are usually fewer. Input and output prices often differ by several times, so the actual ratio between them directly changes total cost.
Second, caching changes how input tokens are billed. Most mainstream inference services provide context caching. When the input prefix of a request largely overlaps with previous requests, the cached portion is charged at a lower cache price rather than the normal input price. In the examples below, Plan A has a cache price that is roughly one-quarter of its normal input price. The impact of a hit-rate change depends on the gap between cache price and normal input price, and on how large input tokens are as a share of total tokens. With the same drop in hit rate from 98% to 95%, the cost of Plan A in the example rises by about 9%, while the cost of Plan B rises by about 76%.
Third, time-of-day pricing and subscription plans add another layer. Some services charge more during peak hours, and subscription plans can have an effective per-token price that is much lower than pay-as-you-go pricing.
Therefore, the right thing to model is total cost, not unit price. What is needed is a cost model driven by traffic structure and cache hit rate.
A general cost model per 100 million tokens
Define the token composition of a typical workload as follows:
H: cached input tokens as a share of total tokensI: uncached normal input tokens as a share of total tokensO: output tokens as a share of total tokensH + I + O = 1
Let:
P_cache: cache price per million tokensP_in: normal input price per million tokensP_out: output price per million tokens
Then the cost per 100 million tokens is:
total cost = 100 × (H × P_cache + I × P_in + O × P_out)
The factor 100 converts per-million-token prices into per-100-million-token cost.
Use two example plans:
- Plan A:
P_cache = 0.115,P_in = 0.5,P_out = 2 - Plan B:
P_cache = 0.05,P_in = 2.5,P_out = 2
Plan B has a much lower cache price, but a much higher normal input price.
Scenario 1: high reuse
H = 98%, I = 1.5%, O = 0.5%
- Plan A:
100 × (0.98 × 0.115 + 0.015 × 0.5 + 0.005 × 2) = 100 × 0.1302 ≈ 13.0CNY per 100 million tokens - Plan B:
100 × (0.98 × 0.05 + 0.015 × 2.5 + 0.005 × 2) = 100 × 0.0965 = 9.65CNY per 100 million tokens
Scenario 2: long conversation with changing context
H = 95%, I = 4.5%, O = 0.5%
- Plan A:
100 × (0.95 × 0.115 + 0.045 × 0.5 + 0.005 × 2) = 100 × 0.14175 ≈ 14.2CNY per 100 million tokens - Plan B:
100 × (0.95 × 0.05 + 0.045 × 2.5 + 0.005 × 2) = 100 × 0.17 = 17.0CNY per 100 million tokens
At a 98% hit rate, Plan B is about 26% cheaper than Plan A. When the hit rate falls to 95%, Plan B becomes about 20% more expensive. The conclusion reverses completely.
The lesson is that the cache price difference dominates only when the hit rate is high. Once the hit rate drops, the normal input price quickly becomes more influential. Before comparing costs, you must measure your own real-world cache hit rate.
How to measure and improve cache hit rate
Cache hits depend on prefix consistency. When measuring, use a consistent definition:
cached_tokens: input tokens served from the cachetotal_input: total input tokens, including cached tokensoutput: output tokens
Two ratios are commonly used:
H = cached_tokens / (total_input + output)input cache hit rate = cached_tokens / total_input
The first ratio is for the cost model. The second ratio is for evaluating cache effectiveness. They are not interchangeable.
To improve cache hit rate:
- Put static content such as system prompts and tool definitions at the very beginning of the input, and keep it byte-identical across requests. Do not mix dynamic values like timestamps or random request IDs into the prefix.
- Keep the order of multi-turn conversation history stable. Inserting or rewriting earlier messages invalidates the cache from the point of change onward.
- In retrieval-augmented generation scenarios, retrieval results often change every time. Place them near the end of the request so their variation does not invalidate the prefix cache. The trade-off is that these tokens will be billed as uncached input.
- Aggregate cached, input, and output token counts daily from server logs or the usage API. Use measured values in the cost model instead of assumptions.
Here is an example Python function for daily cost calculation. p_cache, p_in, and p_out are prices in CNY per million tokens.
def daily_cost(usage_rows, p_cache, p_in, p_out):total_input = sum(r.input for r in usage_rows)output = sum(r.output for r in usage_rows)cached = sum(r.cached for r in usage_rows) # assume cached <= inputtotal = total_input + outputif total == 0:return None # no data, no conclusionh = cached / totalo = output / totali = (total_input - cached) / totalreturn 100 * (h * p_cache + i * p_in + o * p_out) # CNY per 100M tokens
Time-of-day pricing and subscription plans
For time-of-day pricing, peak windows vary by provider. Some cover most daytime hours on weekdays; others cover only a few afternoon hours. Whether off-peak pricing is actually available depends on your traffic distribution. Before deciding, export at least two weeks of hourly usage data, calculate the share of tokens consumed during off-peak windows, and then weight peak and off-peak prices accordingly. Do not estimate using the off-peak price alone.
For subscription plans, convert the monthly fee, quota, and time restrictions into an effective unit price:
effective unit price = monthly fee / (weekly available tokens × 4.33) × 1e6
The unit is CNY per million tokens.
For example, if the monthly fee is 99 CNY and the plan provides 300 million tokens per week, then:
99 / (3 × 10^8 × 4.33) × 10^6 ≈ 0.076 CNY per million tokens
Compare this with Plan A at a 98% hit rate, where pay-as-you-go costs about 13.0 CNY per 100 million tokens, or 0.130 CNY per million tokens. In that case, the subscription plan is about 60% of the pay-as-you-go price.
Two caveats matter.
First, unused quota is still a cost. If you use only 60% of the allowance, the effective unit price becomes:
0.076 / 0.6 ≈ 0.127 CNY per million tokens
That is almost the same as the pay-as-you-go price.
Second, if the subscription plan separates peak and off-peak allowances, calculate coverage separately:
plan coverage = min(actual peak usage, peak allowance)+ min(actual off-peak usage, off-peak allowance)effective unit price = (monthly fee + overage pay-as-you-go cost) / actual total usage
Decision checklist
- Collect a real traffic profile covering input/output ratio, cache hit rate, and time distribution. Data should cover at least two weeks.
- Use the total-cost model with each candidate service’s cache, input, and output prices. Calculate a cost range per 100 million tokens under optimistic and pessimistic hit-rate assumptions.
- Apply time-of-day weights to the pay-as-you-go cost.
- Convert subscription plans into an effective unit price based on expected actual usage, then compare with the pay-as-you-go cost.
- Run sensitivity analysis. Vary hit rate by ±3 percentage points and output share by ±0.2 percentage points, then recalculate. If the conclusion flips, improve measurement precision before making a decision.
LLM inference cost is a structural problem. Unit price is only one variable. Cache hit rate determines how much influence cache pricing has, input/output ratio determines the real weight of each price component, and time-of-day pricing and subscription plans determine the final discount space. When these variables are measured and placed into one unified model, low advertised unit prices cannot mislead the decision, and complex billing rules can be reduced to comparable numbers.
