Why the KV Cache exists
When a Transformer generates text token by token (autoregressive mode), each new token must "look at" all previous tokens via the attention mechanism. Without a cache, that means recomputing the keys (K) and values (V) for all previous tokens at each step. For the 1000th token, you'd have to recompute K/V for the 999 tokens before it. That's absurd waste.
The solution: the KV Cache. You store each token's K and V at the moment it's processed, and reuse them for all subsequent tokens. Each new token only costs the computation of its own K/V, plus the attention operation with the existing cache.
And why isn't the query stored? Because attention is causal: the current token's query is used once, at the moment the token is processed, and never again; the keys and values of the whole past, on the other hand, are re-read at every new step. You cache what gets re-read, not what is written once, the detailed explanation is here.
In terms of complexity, you go from per token to for the K/V computation (the attention itself remains ), being the number of tokens in the context and the vector dimension: the cost of computing K/V no longer depends on the length of the text.

Imagine you're looking for a book in a library of 10,000 volumes. Without a cache, for each question you go back to the shelves to find all the relevant books. With a cache, you keep the useful books stacked on your table. Next question? You look at your stack directly. Enormously faster.
Real KV Cache size: it blows up fast
Let's do the math for LLaMA 2 7B with an 8K token context in FP16 (2 bytes per value):
The factor of 2 at the beginning counts both keys AND values (never the queries, which are used only once). 4 GB just for the cache of a single request.
For a model at the scale of GPT-3 175B (96 layers, 96 heads of dimension 128) with 8K tokens, the same arithmetic gives about 38 GB. On an A100 GPU with 80 GB, once the model is loaded and the cache allocated, there's almost nothing left to process other requests in parallel.

Autoregressive inference is limited by memory bandwidth, not by compute power. For each generated token, the GPU must read the entire KV cache from HBM (High Bandwidth Memory, the card's main memory). For 4 GB of cache on an A100 at 2 TB/s bandwidth, that takes about 2 ms per token (4 ÷ 2000). The GPU may be incredibly powerful in compute, but it spends its time waiting for data to arrive from memory.
KV Cache compression techniques
Cache quantization
The most direct approach: reduce the precision of stored K/V.
INT8: instead of FP16 (2 bytes), you store in INT8 (1 byte). The cache is cut in half. Degradation is minimal for most tasks.
INT4: 4x reduction, but trickier. You need to quantize by groups to preserve precision.
FP8: a good compromise that preserves FP16's dynamic range while halving the size.
Keys and values have different statistical distributions. Keys tend to be uniformly distributed, while values exhibit outliers (extreme values). Asymmetric quantization (with a zero-point) works better than symmetric for values. A detail that matters in practice.
Smart eviction: dropping what's no longer useful
Instead of keeping everything, you can intelligently remove cache entries that contribute little to attention.
Heavy Hitter Oracle (H2O): identifies tokens that receive the most attention (the "heavy hitters") and only keeps those, plus a local window of recent tokens. Keeping roughly 20% of the tokens divides the cache by 5.
Sink Tokens: systematically keeps the very first tokens (typically the system prompt) in addition to the local window. Essential for tasks that depend on an initial instruction.
Sliding Window: a radical approach used by Mistral. You only keep the last tokens in the cache. Ultra memory-efficient, but only works if the recent context is sufficient for the task.
Structural compression: rethinking the architecture
Multi-Head Latent Attention (MLA), introduced by DeepSeek, is the most aggressive compression. Instead of computing and then storing each head's K and V, you cache a small latent vector from which they can be reconstructed:
In plain terms: is the representation of token at the entrance of the attention layer. A learned matrix squeezes it into a small latent vector , and the cache stores only that. When you need K and V to compute attention, you reconstruct them on the fly with two decompression matrices, also learned. The full mechanism (and its delicate coexistence with positional encoding) is detailed in the MLA lesson.
DeepSeek-V2 reports a 93.3% KV cache reduction with MLA. This is far more aggressive than GQA (Grouped Query Attention) while being more expressive: K and V share a rich representation in latent space, rather than simply sharing heads.

CSA + HCA: the logical sequel to MLA (DeepSeek V4, April 2026)
DeepSeek V4 goes further with a hybrid compressed attention. Instead of compressing uniformly (MLA) or evicting (H2O), V4 alternates two kinds of compression layer by layer:
- Compressed Sparse Attention (CSA): a learned compressor merges the K/V of every group of consecutive tokens into a single cache entry, and each query then consults only the most relevant compressed entries.
- Heavily Compressed Attention (HCA): much more aggressive compression (far larger groups), in the alternating layers.
The effect at 1M tokens, per the technical report: V4-Pro (a 1.6T-parameter mixture of experts, with 49 billion active per token) runs with only 10% of the KV cache of V3.2 and 27% of the inference FLOPs per token (FLOPs, floating-point operations, count the compute work). Concretely: a whole novel in context, served at a fraction of the previous generation's cost.
The key idea: KV cache compression is no longer uniform, it is learned and hierarchical, like a multi-level memory. V4 confirms that the future of long context runs through layer-specific compression, not a single trick applied everywhere.
Implementation optimizations
PagedAttention (vLLM)
vLLM's brilliant idea: manage the KV cache like an OS's virtual memory.
- The cache is divided into pages (fixed-size blocks, typically 16 tokens)
- Pages are dynamically allocated as needed, not pre-allocated
- Multiple requests with the same prefix (system prompt) share the same pages
The result: near-zero memory fragmentation. Where naive allocation wasted 60-80% of GPU memory, PagedAttention achieves near-optimal utilization.
Prefix Caching
If 100 requests share the same 2000-token system prompt, you compute the KV cache for that prompt once and share it across all requests. The savings are massive in production deployments where the system prompt is fixed.
Speculative Decoding
Instead of generating one token at a time (very bandwidth-inefficient), a small fast model generates several candidate tokens, then the large model verifies them in a single batch. The cache is updated in batch rather than token by token, which makes much better use of GPU bandwidth.
The direct economic impact
Reducing KV cache per request means being able to process more requests in parallel on the same GPU:
Halving the cache = doubling the batch size = doubling the throughput = cutting the cost per token in half. For a production LLM service, KV cache size is directly linked to profitability.
Cache quantization, GQA, MLA, eviction, PagedAttention: these are not marginal optimizations to squeeze out a few percentage points of performance. They are fundamental elements of economic viability. Without them, serving LLMs at scale would be financially impossible.
TurboQuant: quantizing the KV cache to around 3 bits (Google Research / DeepMind and NYU)
TurboQuant is a training-free compression algorithm (from researchers at Google Research, Google DeepMind and New York University) that quantizes the KV cache to about 3 bits per coordinate: per the paper, 3.5 bits are enough for intact quality, and 2.5 bits for marginal degradation. The process relies on two complementary steps:
-
A random rotation, then a precomputed quantizer: each K/V vector receives a random rotation that makes the distribution of every coordinate known in advance (a so-called Beta distribution, the same for all vectors). You can therefore precompute the optimal level assignment once (the Lloyd-Max algorithm) and reuse it everywhere, which eliminates the costly per-block normalization of classical quantizers.
-
QJL (Quantized Johnson-Lindenstrauss): uses 1 additional bit per dimension to correct the first stage's residual error, only the sign of the residual (+1 or −1) is stored. This single bit is enough to make the dot product estimate mathematically unbiased: no more systematic error, only noise that averages out.
In total, the first stage uses all bits but one, and QJL the last. At 3.5 bits per coordinate instead of FP16's 16 bits, cache memory is divided by roughly 4.5 (16 ÷ 3.5).
Results are validated on Llama-3.1-8B with the LongBench and Needle in a Haystack benchmarks (finding a piece of information hidden inside a very long text). The compressed cache maintains near-perfect fidelity, including on those long-context retrieval tasks.
No training or fine-tuning is needed: TurboQuant applies directly to any existing model in a single pass.
TurboQuant: Online Vector Quantization with Near-optimal Distortion RateWith context windows now exceeding a million tokens (Gemini, Claude), the KV cache becomes the primary limiting factor. A cache 4 to 6 times smaller means serving correspondingly longer contexts on the same hardware, or correspondingly more requests in parallel. TurboQuant makes serving very long contexts economically viable without quality compromise.
In summary
The KV Cache is what makes autoregressive inference practical, but it quickly becomes a bottleneck in memory and bandwidth. Compression techniques (quantization, eviction, MLA) and optimization techniques (PagedAttention, prefix caching) are pillars of modern LLM infrastructure. This is a very active area of research, directly tied to the profits and viability of LLM services.
1. In the KV cache size formula, the factor 2 counts the keys and the values. Why is there no factor 3 including the queries?
2. During token-by-token generation, what actually limits the GPU?
3. With DeepSeek's Multi-Head Latent Attention, what actually sits in the cache?
4. What is the idea behind PagedAttention, vLLM's flagship optimization?
5. Why does halving the KV cache size per request roughly halve the cost per token?