Pidoku

KV Cache Research

Module 24 Lesson 24.c 1h Difficulty 4/5

Prerequisites 12.e, 12.f, 13.h, 13.i, 24.b


Before you start: words you need#

WordPlain meaning
KV cacheStored keys and values for every earlier token, so they are not recomputed (12.e).
EvictionThrowing away the KV of tokens judged unimportant, to save memory. The model can never see them again.
KV quantizationStoring the cache in fewer bits (FP8, FP4, 3-bit), which keeps every token but makes each one less exact (13.h).
Tiering / offloadMoving KV out of GPU memory into CPU memory, local SSD or remote storage, and bringing it back when needed (14.k).
Prefix cachingReusing the KV of a prompt start that an earlier request already computed (12.m).
Non-prefix reuseReusing cached KV for text that is not at the start of the prompt, such as a retrieved document in the middle. Not exact without a correction step.
TTL (time to live)How long a cache entry is kept before it may be thrown away.
Agent turnOne model call inside an agent loop. Between turns the agent runs a tool, often for seconds.

1. What is it?#

The KV cache of one sequence is

KV bytes = 2 × layers × KV heads × head size × bytes per number × tokens

Research attacks each term, and the system around it:

What is attackedApproachStatus, November 2026
Wasted memoryPaging (PagedAttention)ESTABLISHED
Duplicate workPrefix caching, radix treesESTABLISHED
KV headsGQA, MLA, shared K=V (24.b)ESTABLISHED
Bytes per numberFP8 KVESTABLISHED
FP4 / 3–4-bit KV (NVFP4, TurboQuant)EMERGING: shipping, validate per workload
LayersCross-layer sharing (CLA, YOCO)EMERGING: few production models
Tokens keptEviction (H2O, SnapKV and many more)SPECULATIVE for general use: still not a default
Tokens readTrained sparse attention plus offloading the unselected KVEMERGING (24.b)
Where KV livesTiers: CPU, SSD, remote; cluster-wide storesESTABLISHED in engines, still fast-moving
Agent sessionsKeep-or-drop during tool calls, KV moved back to prefillEMERGING
Non-prefix reuseCacheBlend-style partial recomputeEMERGING

Facts checked on 12 November 2026. Lesson 13.i teaches how to use the established techniques; this lesson is about which open questions are moving.


2. Why does it matter?#

Agents and reasoning models made sessions long and repetitive (1.e). Each agent turn re-sends almost the same growing prompt, so whether the old KV is still somewhere fast decides time to first token. Research in 2026 found that for multi-turn agent traffic, loading cached KV from storage is often the bottleneck, not the arithmetic (DualPath), and that coding agents spend a large share of their time regenerating KV that was thrown away during tool calls (SAGA reports about 38% on SWE-bench agents).


3. Simple analogy#

A library with a small reading room (GPU memory) and a big basement (CPU memory, SSD, remote storage).

  • Quantization prints the books in smaller type: everything fits, slightly harder to read.
  • Eviction throws books away because nobody has opened them lately. Fine until someone asks for one.
  • Tiering moves books to the basement and fetches them on request. Fine if the stairs are fast.
  • TTL during tool calls: a reader steps out for ten minutes. Do you keep their desk, box their books in the basement, or clear the desk and make them start over?

4. Tiny example: stacking reductions#

A 48-layer model, 128k-token contexts, 100 GiB of GPU memory left for the cache:

StackSequences that fit
Full multi-head attention, BF161
+ GQA (8 KV heads)4
+ FP8 KV8
+ cross-layer sharing in pairs16
+ NVFP4 KV29

Each step multiplies. Section 6 prints the table.


5. Technical explanation#

Quantization: FP8 settled, 4-bit and below in testing#

The key finding, reproduced many times: keys are more sensitive than values, because keys pass through the softmax’s exponential while values are averaged. So K gets more bits or per-channel scales, V gets per-token scales (KIVI, KVQuant; 13.h).

2026 state:

  • FP8 KV is the safe default. vLLM made it the default for GLM-5.x on Blackwell (SM100) with --kv-cache-dtype auto in v0.32.0.
  • NVFP4 KV arrived in vLLM v0.32.0 on SM8x and SM12x GPUs. Promising, not yet a default.
  • TurboQuant (3–4-bit KV) was measured by the vLLM team in May 2026, a good example of an independent check. Their findings: 4-bit keys and values with norm correction gave up to 3.4x KV capacity for 1–4 points of accuracy loss on most tests. 3-bit variants dropped about 20 points on one reasoning model and about 30% relative on long-context retrieval for another. Every TurboQuant variant cost 20–34% offline throughput against BF16, while FP8 matched BF16. Their advice: FP8 by default, 4-bit only when memory-bound and after validation, 3-bit not in production.

The lesson: a bit-count saving is only a win if dequantization does not eat the time it saves.

Eviction: still not a default#

H2O (2023), SnapKV, PyramidKV, StreamingLLM and dozens more keep only “important” tokens. Benchmarks look good because most test answers sit in recent or highly-attended text. The failure is a fact from the middle of a long document, asked about only at the end: it got little attention during prefill, was evicted, and the model makes something up. The failure is silent.

What would change the verdict: a method with a guarantee about what is kept, a detector for “I needed something I evicted”, tests that deliberately build the failure, and a large provider publishing results. As of November 2026, none of these is in place.

Two things moved the problem anyway:

  • StreamingLLM’s real contribution, attention sinks, became training practice, and gated attention (24.b) largely removes sinks.
  • Trained sparse attention gets most of eviction’s benefit without deleting anything: the model reads only selected tokens, while the rest stay in memory or are moved to CPU and can be fetched back. vLLM’s Hybrid HiSparse does exactly this for GLM-5.3. That is the 2026 answer to “can we touch fewer tokens safely”: train the model to choose, and keep everything recoverable.

Tiering: from research to engine feature#

Mooncake (2024) framed KV as data in a distributed store. In 2026 this is product, not paper: vLLM’s tiered offload to host memory, filesystems and object stores; LMCache; Mooncake Store; NVIDIA NIXL for transfers (14.k, 14.l). Research moved on to:

  • Hierarchical caches for long contexts: Strata (OSDI 2026) manages GPU, CPU and storage tiers for long-context serving in SGLang.
  • Storage bandwidth: DualPath (February 2026) loads cached KV through both prefill and decode machines’ network links to beat the storage-bandwidth limit.
  • CXL memory pools: Beluga (Alibaba, late 2025) puts KV in a shared CXL pool and reports up to 4.8x throughput over an RDMA-based Mooncake baseline. A research prototype; see 24.i.

Agents: keeping KV across tool calls#

When an agent calls a tool, its request leaves the GPU. Throwing the KV away means recomputing the whole history on return; keeping it blocks memory others could use.

  • Continuum (ICLR 2026) pins a paused agent’s KV with a time-to-live set from the reload cost and the predicted tool duration, and frees it when the TTL expires.
  • MARS and SAGA (2026) decide per session, or per whole agent workflow.
  • Bi-directional KV transfer (vLLM, October 2026): in prefill/decode disaggregation, the decode node keeps the conversation’s KV (default 480 s) and the prefill node pulls it back on the next turn instead of recomputing. Reported time to first token fell by up to 3.3x on GLM-5-FP8 at long prompts, on one prefill and one decode node.

24.h does the keep-versus-drop arithmetic.

Non-prefix reuse#

Retrieval and agent prompts often contain the same document in different positions. Reusing its KV is not exact, because each token’s KV depends on everything before it. CacheBlend recomputes a small fraction of tokens to repair the cross-attention; LMCache applies it to agents. EMERGING: real gains, quality depends on the task.

Cross-layer sharing#

CLA (adjacent layers share KV) and YOCO (one global KV) compose with GQA and quantization. Sound results since 2024, little uptake in 2026 frontier models, which put their effort into MLA, compression and sparsity instead. Still EMERGING.


6. Under the hood: stacking the reductions#

Go
// kvstack.go — stack KV-cache reductions and see how many 128k-token sequences fit.
package main

import (
	"fmt"
	"math"
)

func main() {
	const freeGiB = 100.0          // HBM left for the KV cache after weights
	const tokens = 131072.0        // context per sequence
	const mhaBF16 = 768.0 * 1024.0 // bytes per token: 48 layers, 32 KV heads, head 128, BF16

	steps := []struct {
		name   string
		factor float64 // multiplies bytes per token
	}{
		{"MHA, BF16 (start)", 1},
		{"+ GQA, 8 KV heads", 8.0 / 32},
		{"+ FP8 KV cache", 0.5},
		{"+ cross-layer sharing (pairs)", 0.5},
		{"+ NVFP4 KV (4.5 bits incl. scales)", 4.5 / 8},
	}
	perToken := mhaBF16
	for _, s := range steps {
		perToken *= s.factor
		perSeqGiB := perToken * tokens / (1 << 30)
		fmt.Printf("%-36s %6.0f KiB/token  %6.2f GiB/seq  %4.0f sequences\n",
			s.name, perToken/1024, perSeqGiB, math.Floor(freeGiB/perSeqGiB))
	}
}

Output:

MHA, BF16 (start)                       768 KiB/token   96.00 GiB/seq     1 sequences
+ GQA, 8 KV heads                       192 KiB/token   24.00 GiB/seq     4 sequences
+ FP8 KV cache                           96 KiB/token   12.00 GiB/seq     8 sequences
+ cross-layer sharing (pairs)            48 KiB/token    6.00 GiB/seq    16 sequences
+ NVFP4 KV (4.5 bits incl. scales)       27 KiB/token    3.38 GiB/seq    29 sequences

NVFP4 stores 4-bit numbers plus one 8-bit scale per 16 values, so about 4.5 bits, not 4. The last row assumes NVFP4 costs no speed. The TurboQuant study shows why you must measure that: capacity went up, and throughput went down.


7. Performance implications#

  • Capacity and speed are different wins. Fewer KV bytes means more sequences per GPU and, at long context, faster decode. Dequantization and extra kernels can take some or all of that back.
  • Tier bandwidth sets the value of tiering. Fetching must beat recomputing (14.k); on coherent CPU links (NVLink-C2C) it nearly always does, on one NVMe drive often not under load (24.i).
  • Agent time to first token is now a cache-placement problem. Where the last turn’s KV is decides whether the next turn waits milliseconds or seconds.

8. Production implications#

  • FP8 KV first. It is one flag and nearly free.
  • 4-bit KV only after testing long-context retrieval and reasoning on your traffic, and after measuring throughput, not only capacity.
  • Do not deploy eviction for general traffic. For a narrow task, build a test that triggers its failure first.
  • For agents, measure how long tool calls take and how often sessions come back, then set cache TTLs and offload tiers from that data.
  • Expect KV formats to differ by engine and model. A standard KV interchange format across engines does not exist yet; connectors (19.b) bridge them case by case.

9. Common mistakes#

  1. Reporting KV capacity gains without throughput and latency.
  2. Validating KV compression on summarisation and perplexity only; they do not trigger the failure.
  3. Quantizing K and V the same way.
  4. Assuming eviction is “basically solved” because a paper showed small losses.
  5. Dropping an agent’s KV the moment its request pauses, then recomputing it seconds later.
  6. Counting 4-bit formats as exactly 4 bits; scales add overhead.

10. Hands-on exercise#

  1. Run kvstack.go. Replace cross-layer sharing with MLA (576 values per layer, BF16) and redo the stack. Which order of reductions gives the most sequences?
  2. Build an eviction failure test: put a fact early in a 64k-token document and ask about it at the end. Run it with one eviction method and with FP8 KV.
  3. Quantize only K to 4 bits, then only V, then both, and measure a retrieval task. Confirm K is more sensitive.
  4. Log an agent workload’s tool-call durations for a day. What TTL would keep 90% of returning sessions’ KV?
  5. Apply 24.a’s seven questions to one 2026 KV compression paper. Does it test the failure case?

11. Interview questions#

  1. Which KV-cache problems are solved and which are open in late 2026?
  2. Why is K more sensitive to quantization than V?
  3. Why has KV eviction still not become a default, and how did trained sparse attention change the question?
  4. A 4-bit KV format gives 3.4x capacity but lowers throughput. When would you still use it?
  5. How should a server treat an agent’s KV cache during a 10-second tool call?
  6. What is non-prefix KV reuse and why is it not exact?

12. Further reading#

Checked on 12 November 2026.

↑↓ navigate↵ openesc close

100% drag to pan · scroll or pinch to zoom · double-click to zoom in