KV Cache Compression › KV Cache Compression

Michael BrenndoerferJanuary 9, 20264 min read
Language AI HandbookAll chapters
Part 42 of 67Inference Optimization
Chapter 316KV Cache Compression › KV Cache Compression
Reading section 1 of 13 · KV Cache Compression · 4 minSection 1 of 13 · 4 min

Book contents

Section 1 of 13 · 4 min read

KV Cache Compression › KV Cache CompressionOne section · Sections in this chapter1 / 13 ↓
Reading tools Definitions & inline links

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

As we explored in the previous chapters on KV cache fundamentals and memory analysis, the cache grows linearly with sequence length and can consume tens of gigabytes for long contexts. A model generating a 32,768-token response might accumulate several gigabytes of cached key and value tensors across all layers and attention heads. Paged attention addresses memory fragmentation and allocation overhead, but the total memory footprint of the cache itself remains substantial. KV cache compression tackles this problem directly by reducing the amount of data stored, either through selective eviction of less important cache entries or by quantizing the cached values to lower numerical precision.

The key insight driving compression research is that not all cached tokens contribute equally to generation quality. Attention patterns in transformers are highly non-uniform: certain tokens receive concentrated attention while others are nearly ignored across hundreds of generation steps. This sparsity creates an opportunity. If we can identify and preserve the high-value cache entries while discarding the rest, we can dramatically reduce memory usage with minimal impact on output quality. The central challenge is doing this identification accurately and efficiently, without expensive per-step computation that would offset the memory savings.

Think of the KV cache as a research library. When a scholar writes a paper, they do not continuously consult every book they have ever read. Instead, they repeatedly return to a small set of highly relevant sources, occasionally glance at recent acquisitions, and largely ignore the large middle ground of books that happened to be present on the shelves. A librarian who understood the scholar's research patterns could remove most of those rarely-consulted books without affecting the quality of the scholar's work. KV cache compression is the process of being that librarian: identifying which cached tokens are the books our model keeps returning to and discarding the rest.

This chapter covers the three main approaches to KV cache compression: eviction-based strategies that remove entire token representations from the cache, attention sink preservation that protects necessary initial tokens from eviction, and cache quantization that reduces the numerical precision of stored values. We examine the H2O (Heavy-Hitter Oracle) algorithm in detail as a representative eviction method, then explore how these techniques combine for maximum compression. We also analyze the quality implications of each approach and discuss the practical trade-offs involved in deploying compressed caches in production.

Understanding these techniques requires familiarity with attention mechanics from Part XIII and the KV cache fundamentals covered earlier in this inference optimization section. We will also build on the attention sink discussion from Part XVIII to explain why naive eviction strategies fail and how the research community solved this problem. By the end of this chapter, you will have a thorough understanding of how to reduce KV cache memory by more than 30x while preserving generation quality for the large majority of practical use cases.

Historical Context

KV cache compression emerged as a practical research area around 2023, when the deployment of large language models at scale made memory efficiency a pressing concern. The StreamingLLM paper (Xiao et al., 2023) formalized the concept of attention sinks and introduced the sink-plus-window eviction strategy. This enables to long sequences long generation within bounded memory. The H2O paper (Zhang et al., 2023) appeared around the same time. This provides the heavy-hitter oracle formulation and showing that tracking cumulative attention significantly outperforms pure recency-based eviction. Cache quantization has deeper roots in the general model quantization literature, with GPTQ (Frantar et al., 2022) and similar works motivating the application of post-training quantization to activation caches rather than just weights. These concurrent developments reflect how memory pressure became the dominant constraint for LLM deployment at the frontier, shifting research focus from pure accuracy improvements to efficiency without quality sacrifice.