KV Cache Compression

A conceptual wide-format banner in a green and lime color scheme focused on optimization. A massive, complex data structure is shown being structurally compacted and reduced into a tight, dense, glowing crystalline cube, representing memory efficiency and model quantization.
image: gemini.google.com

KV Cache Compression: What It Is and When It’s Worth It

The short version: the KV cache is the part of inference memory that grows with every token. The model decides its shape — layers, KV heads, head dimension — but it does not ship with a KV cache precision. The inference engine exposes the storage formats, and you (or the engine’s default) choose the level. Compression buys longer context or more concurrency; the first 2× is usually cheap, the next 2× is where quality gets opinionated.

Use the VRAM Fit Calculator to see what your context window actually costs.

What the KV cache is

Every transformer layer stores a key and a value vector for every token it has seen. That’s the KV cache. It lets the model attend to earlier tokens without recomputing them, which is why generation is fast after the first token.

The size is roughly:

2 × layers × KV heads × head dim × bytes per element × tokens

A Llama 3.1 8B — 32 layers, 8 KV heads, 128-dim, FP16 — costs about 128 KiB per token. At 8K context that’s ~1 GiB; at 128K it’s ~16 GiB, roughly twice the model’s weights. A 70B model with 80 layers and 8 KV heads is ~320 KiB per token: ~40 GiB at 128K.

That’s the memory wall: weights are fixed, but the cache grows with context and batch size.

Who decides the compression level?

The model does not ship with a KV cache precision. It ships with a cache shape — how many layers, how many KV heads, how wide each head is, and whether the architecture uses MLA or sliding-window attention. That shape sets the baseline size. It does not decide whether those values are stored as FP16, FP8, INT8, or INT4, and it does not decide whether tokens get evicted.

That is a runtime decision. The inference engine implements the storage format, and the user configuration — or the engine’s default — selects it.

DecisionDetermined byCan you change it?
Cache shape: layers, KV heads, head dim, MLA latent, sliding windowModel architectureNo
KV storage precision: FP16/BF16, FP8, INT8, INT4Inference engine + user configYes, if supported
Eviction/sparsity policyEngine or research featureRarely exposed
Paging / CPU-SSD offloadEngineSometimes
Weight quantizationUser / model fileYes — separate knob

In practice:

  • vLLM: --kv-cache-dtype fp8
  • llama.cpp: --cache-type-k / --cache-type-v with q8_0, q4_0, etc.
  • LMDeploy / TensorRT-LLM: their own INT4/INT8/FP8 KV options.

If the engine doesn’t support a format, you can’t enable it. Weight quantization and KV quantization are independent: you can run Q4 weights with FP16 KV, or FP16 weights with Q8 KV. Most engines default to FP16/BF16 KV; some auto-select FP8 when the hardware supports it.

Eviction is different. It’s usually an engine-level feature, not a simple user setting, and most local runtimes don’t expose it at all. The model doesn’t “contain” an eviction policy either — the engine implements it.

Why compression is needed

Three reasons:

  1. Context — long documents, codebases, transcripts, and agents with big histories.
  2. Concurrency — every simultaneous request needs its own cache; batching multiplies memory.
  3. Headroom — the cache competes with weights, activations, and the OS. Running out means OOM or a smaller context than the model supports.

The four families

FamilyWhat it doesTypical savingWhere you see it
QuantizationStore K/V at FP8, INT8, or INT4 instead of FP162× (8-bit), 4× (4-bit)vLLM FP8, LMDeploy INT4/INT8, llama.cpp Q8/Q4 KV
Eviction / sparsityKeep only “important” tokens; drop the rest2–10×+H2O, SnapKV, StreamingLLM, PyramidKV — mostly research or specialized serving
ArchitectureFewer KV heads or bounded attention4–8×+ vs MHAGQA/MQA, MLA, sliding-window, hybrid attention, DSA
Paging / offloadMove cache to CPU/SSD or reuse prefixesCapacity, not compressionvLLM paged attention, prefix caching, CPU/SSD offload

Only the architecture row is model-determined. The other three are engine features or user choices.

What it does to quality

  • FP8/INT8 KV: usually near-lossless. This is the safe first step. vLLM’s FP8 path runs the attention math in FP8 and reports meaningful throughput gains; LMDeploy calls INT8 “almost lossless.”
  • INT4 KV: can be noticeable on long-context retrieval, exact recall, and some reasoning tasks. It’s usable, but test it.
  • Eviction: great for chat and streaming; risky for tasks that need the exact middle of a long document. StreamingLLM keeps attention sinks and a sliding window — it can run “forever” but forgets.
  • Architecture: GQA/MLA/sliding window are baked into the model. They’re not a setting you toggle; they’re why modern models have smaller caches than old MHA models.

What it does to speed

Quantization reduces memory traffic, which is the bottleneck for decode. That can lower inter-token latency and raise throughput — vLLM reports a 54% lower ITL slope and ~15% higher output throughput at concurrency 8 for Llama-3.1-8B with FP8 KV. But dequantization overhead can eat the gain, especially on older hardware. Eviction reduces attention compute but adds selection overhead. Offloading saves memory but is slower.

Rule of thumb: 8-bit KV is usually a speed win or neutral; 4-bit is a memory win with a quality and sometimes speed cost.

Is it common?

  • Architectural reduction: standard. GQA/MLA/sliding-window/hybrid attention are in most current open models.
  • 8-bit KV quantization: common in serving stacks (vLLM, LMDeploy, TensorRT-LLM) and in llama.cpp and apps built on it.
  • 4-bit KV: common among memory-constrained local users, not the default; quality varies by model and task.
  • Eviction/sparsity: mostly research or specialized serving; not a checkbox in most local tools.
  • Offloading/paging: standard in high-throughput serving; less common on a single desktop.

How to choose

  1. Start with FP8/INT8 KV. It’s the best quality-per-byte. Check whether your engine supports it before assuming it does.
  2. Only go 4-bit if you must. Test long-context retrieval, exact quotes, and code.
  3. Use eviction for chat/streaming, not for exact recall.
  4. Check the architecture first. A GQA/MLA model may already have a smaller cache than a quantization tweak can give you.
  5. Measure. Use the VRAM calculator for the memory math, then run your own task twice.

Myths

  • “The model has 4-bit KV built in.” No — the model has a cache shape; the engine stores the values at whatever precision you configure.
  • “KV cache quantization is the same as weight quantization.” No — different tensors, different failure modes, separate settings.
  • “4-bit KV is free.” No — it’s the first place long-context quality shows damage.
  • “Eviction is always safe.” No — it is literally forgetting tokens.
  • “More context always helps.” Only if the cache can hold it and the model can use it.

Bottom line: the model sets the shape; the inference engine and your configuration set the precision. Start with 8-bit KV, check what your engine actually supports, and only reach for 4-bit or eviction when the memory math forces you to.