Why LLM Inference Costs Keep Falling: A Look at KV Cache Mechanics
This article breaks down the three components behind large language model inference cost—compute, memory, and bandwidth—and explains why high cache hit rates, not launch discounts, are what keep prices low over time. It covers redundant computation in autoregressive generation, how key-value caching works, a formula and worked example for estimating cache memory usage, what drives high hit rates, and practical steps for controlling cost.
1. What a Price Cut Reveals About Inference Cost
When a large model service announces something like “permanently reduced to one quarter of the previous price,” most people focus on the number. For developers, the more useful question is what the cost structure behind that number looks like.
Inference cost breaks down into three parts:
- Compute — the floating-point operations required to generate each token.
- Memory — the capacity needed to hold weights and cache.
- Bandwidth — the overhead of moving data between memory tiers.
Optimize any one of these significantly, and there is room for the quoted price to drop.
The pattern observed across the industry over the past two years is consistent: a “discount on launch” is a market decision, while what earns broad acceptance after a few days of use is a real reduction in unit cost driven by very high cache hit rates. When repeated prefixes are reused at scale, the same hardware effectively produces several times as many usable tokens. That is the technical basis for prices that stay low over the long term.
2. Redundant Computation in Autoregressive Generation
Text generation is autoregressive: the model emits one token, appends it to the end of the sequence, and generates the next.
Step 1: input [today weather] -> output trueStep 2: input [today weather true] -> output goodStep 3: input [today weather true good] -> output ...
Under the attention mechanism, every token produces three vectors: query (Q), key (K), and value (V). The problem is that in Step 2, the K and V vectors for the first three tokens are identical to Step 1, yet they are computed again. The longer the sequence, the higher the proportion of this duplicated work, and the more compute is wasted on the historical portion.
3. How Key-Value Caching Works
Key-value caching (KV Cache) works like this: after each new token is computed, its K and V are written to memory. On the next inference pass, the K and V of historical tokens are read directly from cache; only the newest token needs a full Q, K, V computation. The new Q is then used with all cached K to compute attention scores, and the cached V is applied as the weighting.
This reduces the work per step from “process the entire sequence” to “process one token,” changing complexity from something proportional to sequence length to constant time. The tradeoff is memory for compute: the cache itself takes up space.
4. Estimating Cache Memory Usage
Cache size can be estimated with a general formula:
KV Cache = 2 × num_layers × num_heads × head_dim × sequence_length × bytes_per_value
For 32 layers, 32 heads, head dimension 128, and FP16 (2 bytes):
Per token, per layer: 2 × 32 × 128 × 2 = 16384 bytes = 16 KBSequence of 4096: 16 KB × 32 layers × 4096 ≈ 2 GB10 concurrent requests: about 20 GB
The conclusion is straightforward: with long contexts or high concurrency, the cache can consume more memory than the model weights and become the real bottleneck. That makes cache compression, quantization, tiered offloading, and eviction policies mandatory engineering questions.
5. Where High Cache Hit Rates Come From
High hit rates depend on two things: a stable prefix and fine-grained reuse. When the system prompt, knowledge base snippets, tool definitions, and few-shot examples stay identical across requests, that prefix can be served from cache repeatedly. When the differences between requests are confined to the user input at the end, the reuse ratio approaches saturation.
In practice this is implemented with prefix hash indexing, chunked caching, and concurrency scheduling. The higher the hit rate, the lower the effective compute cost.
6. Practical Recommendations
- Fix stable content and place it at the front of the prompt; keep volatile content at the end.
- Control context length so irrelevant history does not dilute the value of the cache.
- Chunk and summarize long documents to reduce the size of repeated prefixes.
- Monitor hit rate, time to first token, and memory usage through alerting and logging.
- Use elastic container platforms and message queues to smooth out traffic spikes so cache resources are fully utilized.
In related work at Baidu AI Cloud, these capabilities are typically assembled from general-purpose cloud product categories rather than a single toggle. Understanding the caching mechanism delivers more long-term cost benefit than chasing any one price cut.
