Cache Hit Rate and Real LLM API Costs Under Pricing Changes
This article explains why list prices alone can mislead when LLM API pricing changes. It breaks down the three billing buckets—cache-miss input, cache-hit input, and output—then shows how cache hit rate, input/output ratio, and output length determine blended unit cost. It also provides a cost-estimation formula, a Python function, an example table, and engineering practices for improving cache hit rate and choosing invocation strategies by workload characteristics.
1. Looking Only at List Price Can Lead to Wrong Conclusions
LLM API billing is usually split into three categories: input that misses the cache, input that hits the cache, and output. The prices of these three categories differ substantially, and their adjustment magnitudes often differ as well. Looking only at the increase in one tier can easily lead to one-sided conclusions such as “prices have risen across the board” or “there has been no increase at all.”
For example, after a pricing adjustment, the output unit price may rise by about four times, the cache-miss input unit price by about three times, and the cache-hit price by a relatively modest amount. These numbers need to be considered together to show what has changed in the cost structure. When assessing the impact of a pricing change, you should first break down the billing tiers and then estimate total cost based on your own invocation characteristics.
2. What the Three Price Tiers Represent
- Cache-miss input: the part of the prompt that the server needs to recompute for each request.
- Cache-hit input: the prefix that the server has already computed and cached. When reused, its unit price is significantly lower.
- Output: content generated token by token by the model. It is usually the most expensive of the three tiers, and the generation process cannot be skipped.
Two ratios determine the actual impact of a pricing change. The first is the ratio of the cache-miss unit price to the cache-hit unit price, which reflects the discount for reuse. The larger this ratio is, the greater the savings from reuse, and the more obvious the benefit for callers with high reuse. The second is the ratio of the output unit price to the cache-hit unit price, which reflects the cost of “generating one more token” relative to “reading one more token.” As this ratio grows, controlling output length becomes more important.
3. Why Cache Hits Reduce Cost
LLM inference is usually divided into two stages: prefill and decode. The prefill stage needs to compute the intermediate states required for attention over the entire input, namely the KV cache. The decode stage generates output tokens one by one. If two requests share exactly the same prefix, the server can reuse the already-computed KV cache and skip repeated computation. This is the engineering meaning of a “cache hit.”
A hit requires the prefix to be identical token by token. If a single character in the prefix changes, the cache after that point becomes invalid. Therefore, the cache hit rate depends largely on how the prompt is organized, not just on business volume.
A common industry approach is disaggregated inference architecture, which assigns prefill and decode to different resource pools and uses a centralized cache layer to increase the probability of prefix reuse. This is a general technical direction; implementation details vary by provider and are not described here.
4. How to Estimate Unit Cost
First define the variables, all in CNY per million tokens:
- M: cache-miss input unit price
- H: cache-hit input unit price
- O: output unit price
- h: cache hit rate
- r: ratio of input tokens to output tokens, i.e., r:1
The effective input unit price is:
E = H × h + M × (1 − h)
The weighted unit cost per million total tokens is:
C = (r × E + O) / (r + 1)
The denominator r + 1 indicates that a group of requests contains r parts of input and 1 part of output. The input portion is billed at the effective input unit price, and the output portion is billed at the output unit price.
The following code implements this estimation and can be used directly to calculate costs under different workloads:
def blended_cost(miss_price: float, hit_price: float, output_price: float,hit_rate: float, io_ratio: float) -> float:"""Calculate the weighted unit cost per million total tokens (CNY per million tokens).miss_price: cache-miss input unit pricehit_price: cache-hit input unit priceoutput_price: output unit pricehit_rate: cache hit rate, from 0 to 1io_ratio: ratio of input tokens to output tokens"""effective_input = hit_price * hit_rate + miss_price * (1 - hit_rate)return (io_ratio * effective_input + output_price) / (io_ratio + 1)
The table below uses assumed unit prices (cache-miss input 20, cache-hit input 2, output 100, all in CNY per million tokens) for example calculations. These values are only used to demonstrate the method and are not actual quotes for any product.
| Scenario | Cache hit rate | Input:Output | Effective input unit price | Blended unit cost |
|---|---|---|---|---|
| Long-context coding assistant with repeated calls to a fixed template | 90% | 20:1 | 3.8 | ≈ 8.4 |
| Individual developer with a different prompt each time | 20% | 5:1 | 16.4 | ≈ 30.3 |
The unit costs of the two scenarios differ by about 3.6 times. The main source of the gap is the cache hit rate, not the unit price itself. Under the same price table, whether prefixes can be reused determines the cost level.
There is also an easily overlooked phenomenon: even at a 90% hit rate, output still accounts for about 57% of total cost in the first scenario. In the second scenario, output also accounts for close to 55%. Therefore, controlling output length often yields more direct benefits than compressing input.
5. Engineering Practices for Improving Cache Hit Rate
- Fix the prefix: Place system instructions, tool definitions, specification documents, and similar content at the very beginning of the request and keep them byte-identical.
- Put variable content later: Place user questions, real-time data, and other dynamic content at the end. Do not embed the current timestamp or random IDs in the system prompt, or the prefix will change every time.
- Reuse conversation history: For multi-turn conversations, reuse the same historical prefix and avoid rebuilding the entire context every turn.
- Batch similar tasks together: Send batch tasks based on the same template consecutively within a short period to increase the probability that the cache will be hit again.
- Constrain output: Use clear format requirements, length limits, and structured output to reduce unnecessary generation.
The following example shows how to organize the message structure so that fixed parts are in the prefix and dynamic parts are at the end:
SYSTEM_PROMPT = "You are a code review assistant. Follow these guidelines..." # Fixed prefix, does not change between requestsTOOL_SCHEMA = load_tool_schema() # Fixed tool definitionsdef build_messages(history: list[dict], user_input: str) -> list[dict]:# Fixed content first, dynamic content last, to maximize prefix reusereturn [{"role": "system", "content": SYSTEM_PROMPT},{"role": "system", "content": TOOL_SCHEMA},*history,{"role": "user", "content": user_input},]
6. Choose Invocation Strategies by Workload Characteristics
Workloads with high reuse, long context, and fixed templates, such as internal code review and document-processing pipelines. The cost of these workloads is mainly determined by the cache-hit price and output length. It is worth investing effort in prefix engineering and continuously monitoring the hit rate.
Workloads with low reuse where prompts differ each time, such as occasionally used personal tools or one-off analysis tasks. These workloads can hardly rely on caching to reduce cost, so more attention should be paid to output limits, model size selection, and task decomposition. Adding a local result cache at the application layer can also reduce repeated requests.
A practice that applies to all workloads is to build a cost dashboard. Record the token counts for cache-miss input, cache-hit input, and output by functional module, calculate the unit cost of each module, and identify the most expensive call chains. Without itemized data, it is impossible to judge optimization.
