RazorAttention: Efficient KV Cache Compression Through Retrieval Heads

Hanlin Tang, Yang Lin, Jing Lin, Qingsen Han, Danning Ke, Shikuan Hong, Yiwu Yao, Gongyi Wang

TL;DR

RazorAttention is a training-free compression algorithm that reduces the Key-Value (KV) cache of Large Language Models (LLMs) by up to 70% with virtually no loss in accuracy. By identifying a small subset of "retrieval heads" that handle global context and compressing the rest using a novel "compensation token," it enables efficient inference for ultra-long sequences (up to 1 million tokens) while remaining fully compatible with FlashAttention.

What Problem Does This Paper Address?

As LLMs move toward massive context windows—scaling to 32k, 128k, or even 1 million tokens—the KV cache becomes a massive bottleneck. The KV cache is the memory used to store previous tokens' keys and values to avoid re-computation. For long sequences, this cache can actually exceed the size of the model weights themselves, leading to Out-of-Memory (OOM) errors and sluggish inference speeds.

Existing solutions often rely on "token-dropping" (seen in methods like H2O or SnapKV), which identifies and deletes "unimportant" tokens. The problem with this approach is that importance is highly subjective and context-dependent. A token that seems irrelevant to the current query might be the exact "needle" required for a query three turns later in a conversation. Once a token is dropped, that information is gone forever, leading to catastrophic failures in multi-turn or multi-fact retrieval.

Comparison of RazorAttention against token-dropping methods on a multi-needle retrieval task.

Methods: A Head-Wise Approach to Compression

The authors introduce RazorAttention, which shifts the focus from which tokens to drop to which attention heads matter for long-range recall. Their approach is built on two core pillars:

1. Retrieval Head Identification

The researchers discovered that LLMs operate on a "retrieve and process" basis. Only about 15% of attention heads—dubbed Retrieval Heads—actually look at the whole history to find specific facts. These specialized heads primarily include Echo heads (which look for tokens identical to the current one) and Induction heads (which look for sequential patterns like A-B...A->B). The remaining 85% of heads primarily focus on local context and do not need the full sequence history.

Illustration of Echo and Induction heads, the primary drivers of long-range retrieval.

2. Differential Caching & Compensation Tokens

Based on this insight, RazorAttention applies a differential caching strategy:

  • For Retrieval Heads: The model maintains a 100% full KV cache, ensuring no "needles" are lost.
  • For Non-Retrieval Heads: The model keeps only the most recent tokens and the "attention sinks" (the very first tokens).

Crucially, instead of just deleting the rest of the tokens in non-retrieval heads, RazorAttention compresses all discarded tokens into a single "compensation token" (calculated as the mean of the dropped K and V values). This preserves a mathematical "summary" of the discarded information, which helps maintain the model's internal attention distribution and prevents performance degradation.

Because the pruning happens at the head level rather than the token level, the method is "plug-and-play" and fully compatible with FlashAttention. This is a significant advantage, as FlashAttention is notoriously difficult to integrate with token-dropping methods that require constant, irregular attention-map updates.

Key Insights and Results

The researchers evaluated RazorAttention across several demanding long-context benchmarks, including LongBench, Needle In A Haystack, RULER, and InfiniteBench, testing models like Qwen1.5, Llama-2, Llama-3, Baichuan2, and GLM-9B.

  • The "Retrieve and Process" Hypothesis: The most striking insight is that LLMs don't use all heads equally for memory. Most heads are "local workers," while a few "specialists" act as the model's long-term memory retrieval system. Giving these specialists all the tokens they need is far more efficient than trying to find globally "important" tokens.
  • 3x Compression: RazorAttention consistently achieved a 70% reduction in KV cache size across various models.
  • Near-Lossless Performance: On the Needle In A Haystack task, RazorAttention maintained nearly 100% accuracy even at 1 million tokens. In contrast, competing methods like H2O saw performance collapse as sequence length increased.
  • Massive Throughput Gains: On 8 Ascend 910B NPUs, the authors reported up to a 71.8% improvement in decoding throughput for 256k context lengths. The compressed cache allows for much larger batch sizes, directly translating to hardware efficiency.
  • Training-Free: Unlike compression methods that require fine-tuning or specific calibration data, RazorAttention can be applied to existing models instantly.

Limitations and Future Work

While highly effective, the authors note a few areas for future exploration. First, the underlying reason why specific heads evolve to take on these retrieval roles during training isn't fully understood. Second, the current selection of retrieval heads is static; future work could explore dynamically shifting which heads are "protected" based on the input prompt. Finally, while a 70% reduction is impressive, the authors believe even higher compression ratios are possible with more sophisticated "compensation" strategies beyond simple averaging.

Summary updated

BibTeX
@inproceedings{tang2025razorattention,
  author = {Hanlin Tang and Yang Lin and Jing Lin and Qingsen Han and Danning Ke and Shikuan Hong and Yiwu Yao and Gongyi Wang},
  title = {{RazorAttention: Efficient KV Cache Compression Through Retrieval Heads}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=tkiZQlL04w},
}
Figure