LLM API Cost Accounting: Why Cache Hit Rate Is the Real Variable Behind the True Price
When estimating LLM API costs, most teams multiply the listed price per million input tokens by their expected volume—but this overlooks a critical variable: the cache hit rate. This article explains how context caching and KV cache reuse change the effective price per token, walks through a concrete cost calculation, examines the technical architecture behind high cache hit rates, and offers practical steps developers can take to measure and optimize hit rates for real cost savings.
1. Why the “List Price” Misleads Your Cost Budget
When evaluating the cost of an LLM API, most teams’ first instinct is to open the pricing page, find the “price per million input tokens” column, and multiply it by the estimated call volume. This approach seems reasonable, but it overlooks a widely ignored variable: the cache hit rate.
Modern LLM inference services commonly implement context caching. The core logic is straightforward: if the prefix of the current request (such as the system prompt, few-shot examples, or long document segments) is exactly identical to a previous request, the inference service can directly reuse the already-computed KV Cache instead of re-running the forward pass over the entire prefix. Because this eliminates a large amount of compute, tokens that hit the cache are billed at a price far below the standard rate.
This leads to a counterintuitive conclusion: two models both listed at 4 yuan per million tokens can differ in actual cost by a factor of 3–5, depending entirely on the cache hit rate. For scenarios with long system prompts, frequent multi-turn conversations, or batch processing of similar content, this gap widens even further.
2. Doing the Math: How Cache Hit Rate Changes the Effective Unit Price
Let’s calculate the actual input cost for a typical scenario. Suppose a model’s public pricing is 4 yuan per million input tokens, cached tokens are billed at 0.7 yuan per million (about 17.5% of the standard price), and the cache hit rate is 90%. The actual cost of processing 1 million input tokens would be:
Cache-hit portion:1,000,000 × 90% = 900,000 tokens900,000 × (0.7 yuan / 1M) = 0.63 yuanCache-miss portion:1,000,000 × 10% = 100,000 tokens100,000 × (4 yuan / 1M) = 0.40 yuanActual total cost:0.63 + 0.40 = 1.03 yuan per million input tokens
Compared with the standard price of 4 yuan, the actual cost is roughly one quarter. More generally, this can be expressed with the following formula:
def actual_cost(std_price, cache_price, hit_rate, tokens_m):"""Calculate the actual input cost after accounting for cache hit rate"""return tokens_m * (hit_rate * cache_price + (1 - hit_rate) * std_price)# Example: 90% hit rate, cache price 0.7 yuan/M, standard price 4 yuan/Mprint(actual_cost(4, 0.7, 0.9, 1)) # Outputs approximately 1.03 yuanprint(actual_cost(4, 0.7, 0.0, 1)) # 4 yuan when hit rate is 0%
The formula shows that every 10-percentage-point increase in hit rate moves the actual cost significantly closer to the cache price. When the hit rate rises from 0% to 90%, the difference in effective input price approaches 4x. This is why, when comparing prices across providers, looking only at list prices can lead to completely wrong conclusions.
3. The Technical Logic Behind High Hit Rates: KV Cache and Inference Architecture
Cache hit rate is not purely a business strategy—it reflects the capability of the underlying inference architecture. Understanding why hit rates vary so much across platforms requires looking at several technical components:
1. Prefix Matching and KV Cache Reuse
In the Transformer architecture, autoregressive generation requires every token to attend to the Key-Value cache of all preceding tokens. For a fixed system prompt and context prefix, the KV Cache is fully reusable. If an inference service can persist these intermediate results and quickly match prefixes when new requests arrive, it can skip the redundant computation.
2. Distributed Cache Pooling
The key to a high hit rate lies in cache storage and scheduling capability. A state-of-the-art approach in the industry is to free the KV Cache from a single machine’s GPU memory and build a cross-node distributed cache pool: GPU memory holds hot data, while host memory and SSD storage hold warm data, forming a tiered caching system. System design in this direction has also become a hot research topic at academic conferences in the computer systems field, and related work has driven improvements in cache reuse efficiency across the industry, with some system designs open-sourced for community reference.
3. Request Scheduling and Affinity
Routing requests with identical prefix characteristics to nodes that hold the corresponding cache is another important means of improving hit rates. The scheduler must balance load distribution against cache affinity, which directly affects the actual hit rate in long-context scenarios.
4. How Developers Should Evaluate Costs and Optimize in Practice
Based on the analysis above, we recommend the following workflow for model selection and cost accounting:
Step 1: Obtain real hit rate data. Ask the service provider for cache hit rate statistics under typical scenarios and their measurement methodology, or instrument your own tracking using the cache-hit fields in API responses (most platforms report the number of cached tokens in the response’s usage object).
Step 2: Estimate hit rates by business scenario. Scenarios with fixed system prompts, multi-turn sessions, and fixed knowledge base prefixes in RAG typically achieve high hit rates, while scenarios with highly random prefixes or fully changing contexts each time will see limited hit rates. You can verify this as follows:
# Calculate the actual hit rate from the usage field in the responsehit = usage.get("prompt_tokens_details", {}).get("cached_tokens", 0)hit_rate = hit / usage["prompt_tokens"]
Step 3: Optimize request construction to improve the hit rate. Place stable content (system prompts, tool definitions, knowledge base summaries) at the front and variable content (user input, timestamps) at the back, ensuring the prefix is byte-for-byte identical across requests. Avoid embedding random IDs or dynamic timestamps in the system prompt, as these break prefix matching.
Step 4: Model costs using the “effective unit price,” not the “list price.” In your budget spreadsheet, split input costs into cache-hit and cache-miss portions priced separately. Also pay attention to output token pricing and cache write fees (some platforms charge separately for cache storage or writes) to arrive at the true all-in cost.
5. Conclusion
The gap between the 4-yuan “list price” and the roughly 1-yuan “effective price” is not a marketing discount—it is a technical chasm spanning cache reuse, distributed storage, and compute scheduling. For teams with heavy token consumption, understanding caching mechanisms, evaluating real hit rates, and optimizing request structure often saves more real money than simply negotiating the standard price. The next time you evaluate models, take one extra step beyond the price table: how much of the caching dividend can your business scenario actually capture?
