Shrinking the KV Cache: The Core Thread in Modern LLM Architecture Evolution
A practical overview of KV Cache in large language models: why it becomes the leading bottleneck for GPU memory and throughput, how it is compressed in practice, and how to make engineering tradeoffs. The article explains the role of KV Cache in autoregressive decoding, walks through the main compression techniques—cross-layer KV sharing, grouped-query attention (MQA/GQA), low-rank compression, quantization, and sparsity/eviction strategies—and discusses the real constraints faced by on-device, MoE, and dense models. It closes with guidance for long-context and AI Agent workloads and concrete recommendations for model selection, memory budgeting, and quality regression. Readers will gain a complete mental model for evaluating and deploying models under memory and context-length pressure.
Introduction: A Theme That Keeps Coming Up
Over the past year or two, mainstream LLM architectures have evolved along different paths, but one thread keeps resurfacing: shrinking the KV Cache. Whether the target is an on-device small model, a Mixture-of-Experts (MoE) model, or a server-side deployment built for maximum inference efficiency, architects are all looking for ways to compress the KV Cache. The reason is not complicated: under the combined pressure of inference cost and context length, the KV Cache is often the primary source of GPU memory pressure and throughput bottlenecks.
This article uses three questions as its main thread—why the KV Cache matters, how it gets compressed, and how to make engineering tradeoffs—to lay out the common techniques in this area and help developers build a complete mental model.
1. What the KV Cache Really Is, and Where the Memory Bottleneck Comes From
1.1 Starting from autoregressive decoding
Large language models generate text autoregressively: they predict one token at a time, append it to the input sequence, and then predict the next. The Transformer attention mechanism needs the Key and Value vectors between the current token and every historical token. Recomputing the K and V of all past tokens at every step would introduce massive redundant computation.
So engineering practice introduces the KV Cache: store the Key and Value already computed at each layer, and reuse them in subsequent steps. This turns the repeated O(n²) computation per step into incremental computation, and it is the key to running long-form generation efficiently.
1.2 Where the memory overhead comes from
The GPU memory used by the KV Cache can be approximated as:
KV Cache memory ≈ 2 (K and V) × num_layers × num_heads × head_dim × sequence_length × batch_size × bytes_per_dtype
The factor of 2 accounts for the two caches, Key and Value. As the formula shows, the memory cost is proportional to the number of layers, the number of heads, the sequence length, and the batch size. Under long context and higher concurrency (larger batches), this term grows quickly—sometimes exceeding the model weights themselves. This is exactly why shrinking the KV Cache deserves serious attention.
2. Cross-Layer Reuse: From Per-Layer Isolation to Cross-Layer Sharing
2.1 The redundancy in the traditional approach
In a standard Transformer, every layer independently maintains its own Key and Value cache. Although different layers represent different semantics, the K and V states extracted by adjacent layers are often highly correlated. Storing a separate copy at every layer introduces obvious redundancy.
2.2 Reusing KV tensors across layers
A representative change in the latest generation of open-source model architectures is the introduction of a cross-layer shared KV Cache mechanism: later layers directly reuse the Key-Value tensors already computed by earlier layers, instead of each layer computing and caching them separately.
This idea had groundwork in academia—papers had already proposed schemes such as Cross-Layer Attention, showing that sharing K and V across layers is feasible with controllable accuracy loss. The value of the new mainstream open-source architectures is that, for the first time, they brought this kind of idea to large-scale, reusable model families spanning different sizes, from on-device to server-side.
The core benefits of cross-layer reuse include:
- Lower memory usage: shared layers no longer cache K and V individually, so the total KV Cache shrinks as sharing becomes more aggressive.
- Bandwidth and memory-access gains: a large portion of decoding time is spent reading the cache, and a smaller cache directly reduces memory-access overhead.
- Friendliness to long context: the longer the sequence, the more absolute memory you save.
A schematic implementation of the logic looks like this (pseudocode that only illustrates the reuse idea):
# Assume KV is shared once every K layers, layer_kv_stride = Kshared_kv = Nonefor layer_id, layer in enumerate(layers):if layer_id % K == 0:# This layer actually computes K and V and caches them as the sharing sourcek, v = layer.compute_kv(hidden_states)shared_kv = (k, v)else:# Later layers directly reuse the shared KV from the previous group, without separate cachingk, v = shared_kvattn_out = layer.attention(hidden_states, k, v)hidden_states = layer.ffn(attn_out)
3. Other Mainstream Routes for Compressing the KV Cache
Cross-layer reuse is just one route. In practice, compressing the KV Cache usually means stacking several techniques together, forming a combination.
3.1 Grouped-query attention (MQA / GQA)
In multi-head attention, each query head has its own Key and Value head, so the cache size grows linearly with the number of heads. Multi-query attention (MQA) lets all query heads share a single set of K and V; grouped-query attention (GQA) takes a middle ground, splitting query heads into several groups that share K and V within each group. With fewer K and V heads, the cache shrinks proportionally, while the accuracy loss is usually controllable. GQA has become a common default configuration in current open-source and commercial models.
3.2 Low-rank compression (compressing KV into latent space)
Another route is to project K and V into a lower-dimensional latent space for storage, then reconstruct them when needed. Through low-rank decomposition, latent vectors much smaller than the original dimension carry the key-value information, sharply compressing the cache size per token. These schemes offer significant gains in memory and bandwidth, but introduce additional projection and reconstruction computation.
3.3 Quantization
Quantizing the KV Cache from high precision (such as FP16/BF16) to low-bit representations (such as INT8 or INT4) is a direct way to reduce cache size. It follows the same thinking as model weight quantization, but you need to watch for attention’s sensitivity to numerical precision; it is typically combined with per-channel or per-group scaling strategies to keep accuracy stable.
3.4 Sparse and eviction strategies
For very long contexts, there is also the approach of “not keeping all history”: caching only a recent window, important tokens (such as high-attention-score tokens), or tokens sampled at fixed intervals, combined with strategies like sliding windows and attention recomputation to balance memory against quality.
4. Real Constraints for On-Device and Small-Size Models
Models of different sizes and positioning are highly sensitive to the KV Cache in very different ways. New-generation open-source model families are usually tiered by use case:
- On-device / embedded small models: memory is extremely tight, so any KV Cache compression directly determines whether a model can run smoothly on a phone. Cross-layer sharing and compact caching are especially critical here.
- Mid-sized Mixture-of-Experts (MoE) models: they trade a small number of activated parameters for high-quality output and are well suited to efficient local inference; however, scheduling sparse activations and post-training are more difficult.
- Dense large models: they pursue quality and ease of post-training, since dense structures are more mature for fine-tuning and tuning, making them the first choice for many teams doing customization.
For developers, model selection should not look at parameter count alone. You also need to see which KV compression strategies a model uses, and whether those strategies match your deployment environment (memory ceiling, target context length, concurrency).
5. Why This Matters for Long Context and Agent Scenarios
Why does shrinking the KV Cache matter so much right now? Because reasoning models and AI Agent scenarios place demanding requirements on context length and concurrency:
- Long context: when processing entire documents or long multi-turn conversations, sequence length pushes cache size up linearly, and without compression you quickly hit the memory wall.
- High-concurrency serving: the larger the batch size, the higher the KV Cache usage, which directly determines how much concurrency and throughput a single machine can handle.
- Multi-turn Agent calls: tool calls, retrieval results, and accumulated histories keep piling up, so cache management directly affects response speed and cost.
In these scenarios, KV Cache compression is no longer just a “nice to have”—it is a key engineering variable that determines whether a model can go into production and whether the cost is acceptable.
6. Practical Recommendations
If you are evaluating or deploying large models, keep these points in mind:
- Prefer models that support mechanisms like GQA or cross-layer sharing: these architectures offer clear memory advantages under long context.
- Set a clear memory budget: roughly estimate cache usage with
layers × heads × head_dim × context length × concurrencyto avoid out-of-memory errors after launch. - Combine compression techniques: cross-layer sharing, GQA, and quantization usually take effect in combination, so joint tuning yields the biggest gains.
- Watch for accuracy regression: every compression scheme needs a quality regression test on your own business datasets to confirm the loss stays within an acceptable range.
- Use general cloud capabilities to validate: you can combine container platforms to host inference services, use monitoring and alerting to track memory and latency metrics, and use logging services to trace long-context request behavior, thereby completing stress testing and capacity planning on cloud platforms such as Baidu AI Cloud.
Conclusion
From cross-layer KV tensor reuse to grouped-query attention, low-rank compression, and quantization, shrinking the KV Cache has become a clear thread in LLM architecture evolution. Understanding its motivation, principles, and tradeoffs not only helps you make more sensible choices when selecting models, but also lets you spend limited memory where it matters most in the era of long context and Agents. As demand for reasoning and agents keeps growing, architecture innovation around the KV Cache will remain a direction worth watching for some time to come.
