KV Cache Explained: The Secret Behind Cutting LLM Inference Costs to a Quarter
A technical breakdown of how KV Cache works, why it drives down inference costs, how to calculate its memory footprint, and the optimization strategies (prefix reuse, quantization, GQA/MQA, PagedAttention) that make near-99% cache hit rates possible—plus practical tips for structuring requests to reuse cached prefixes.
A major LLM service recently announced that it is permanently cutting API prices to a quarter of the previous level, sparking widespread discussion. On the surface this looks like ordinary price competition, but what developers should really pay attention to is that the price cut reflects a genuine drop in the unit cost of inference. One of the core technical drivers behind this is a large improvement in KV Cache hit rates. According to many developers, the service’s cache hit rate in real usage approaches 99%, meaning a large amount of repeated computation is skipped outright—effectively pushing the marginal cost close to zero.
This article starts from the underlying principles and unpacks the whole thing.
Why Inference Is Expensive: The Repeated-Computation Dilemma of Autoregressive Generation
To understand the value of caching, you first need to understand how an LLM generates text.
A language model generates text through an autoregressive process: it produces one token at a time, appends that new token to the end of the sequence, and then generates the next token.
For example, suppose the model needs to continue “The weather today is”:
- Step 1: input “The weather today is”, the model computes the next token “nice”
- Step 2: input “The weather today is nice”, the model computes the next token, and so on
- Step 3: input “The weather today is nice”, continue computing the next token…
The problem is right here: at every step, the attention computation must be redone over the entire sequence.
In the Transformer attention mechanism, each token produces three vectors: Q (Query), K (Key), and V (Value). In Step 2, the input becomes “The weather today is nice”, and the model needs to recompute the K and V for the three tokens “The”, “weather”, and “nice”—but these K and V values were already fully computed in Step 1, and the results are exactly the same.
In other words, without a caching mechanism, the same computation is repeated dozens or even hundreds of times as generation progresses. This is a massive waste of compute that directly drives up inference costs.
KV Cache: Turning Repeated Computation into Memory Reads
KV Cache (Key-Value Cache) is a memory-optimization technique aimed at exactly this problem. Its core idea is simple:
Every time a new token is computed, store its K and V in GPU memory. On the next inference step, the K and V of historical tokens are read directly from the cache instead of being recomputed.
This way, each step only needs to:
- Fully compute the Q, K, and V of the single latest token;
- Compute attention scores using the new Q against all cached historical K;
- Take a weighted sum of the cached V using those attention scores.
The computation per step drops from “processing the entire sequence” to “processing only the latest token”, a qualitative leap in inference speed that also substantially lowers the unit cost.
The Cost of KV Cache: Memory Usage You Can Calculate
There is no free lunch. KV Cache trades memory space for compute time, and the memory it occupies follows a clear formula:
KV Cache size = 2 × num_layers × num_heads × head_dim × sequence_length × bytes_per_value
The factor of 2 is because K and V each take up one copy.
Let’s plug in a typical set of parameters: assume a model with 32 layers, 32 attention heads, a head dimension of 128, a sequence length of 4096, and FP16 precision (2 bytes):
- Per-token, per-layer KV size: 2 × 32 heads × 128 dim × 2 bytes = 16,384 bytes, i.e. 16KB
- Total KV Cache for the full sequence: 16KB × 32 layers × 4096 tokens ≈ 2GB
If you serve 10 requests simultaneously (batch size = 10), the cache alone consumes about 20GB of GPU memory. For long-context or high-concurrency scenarios, the memory occupied by KV Cache can exceed the model parameters themselves, becoming a new bottleneck.
This is also why some platforms in real-world engineering offer online estimation tools that let users quickly compute cache usage from a model configuration—such calculations are essentially just applying the formula above.
From “Computing Fast” to “Storing Smart”: Advanced Caching Strategies
Once you understand the memory bottleneck of KV Cache, the various optimization directions the industry has pursued around it make sense:
1. Improving the cache hit rate
This is the key to the price cut. In scenarios like multi-turn conversations, Agent calls, and code completion, requests often share a large amount of repeated prefix content (such as system prompts or prior conversation context). If the inference system can identify and reuse the cached common prefix instead of recomputing it every time, you get the “hit rate approaching 99%” phenomenon described earlier—repeated prefill computation is almost entirely eliminated, and the marginal cost approaches zero.
2. Reducing the size of each cache copy
Common industry approaches include quantizing the KV Cache (storing K and V at lower precision), grouped-query attention (GQA, which reduces the number of KV heads), and multi-query attention (MQA). These architecture-level improvements all directly shrink the “num_heads × head_dim × bytes_per_value” term in the formula, compressing memory overhead at the source.
3. Optimizing cache management strategies
For example, PagedAttention-style approaches borrow the idea of paging from operating systems, splitting the cache into fixed-size blocks and allocating them on demand, significantly reducing memory fragmentation and waste while increasing the number of concurrent requests a single GPU can handle.
Takeaways for Developers
Look past the price cut to the technology: When a model service is priced far below comparable offerings, don’t just write it off as marketing. It often means there is real cost optimization at the architecture level (such as cache reuse or MoE sparse activation), and it is worth studying its technical report or published papers.
Design request structure to leverage caching: In real engineering, keep stable, unchanging content (system prompt, tool definitions, long-context material) at the very beginning of the request and keep it consistent, while placing volatile content at the end. This significantly improves the chance of reusing the prefix cache.
Pay attention to cache capabilities in enterprise deployments: If you deploy or call LLM services on a cloud platform (such as Baidu AI Cloud), check whether it offers general capabilities like inference acceleration and context caching—these directly affect your final bill.
Conclusion
A quarter of the price is not charity; it is the combined result of “a cache hit rate approaching 99% + architectural sparsification + extreme engineering optimization”. As the most fundamental and critical piece of all, KV Cache is a topic every developer who cares about LLM inference costs should master. Understand it, and you understand the underlying logic of LLM price cuts; use it well, and you can bring down costs in your own applications too.
