Memory & Efficiency
KV Cache
FundamentalThe KV cache makes token generation fast, and it's also why inference runs short of memory. This page starts with the basic idea and works up to how production systems spread it across HBM, DRAM, NVMe and pooled storage.
Prerequisite: this page assumes you know what keys and values are. If not, read Attention Mechanisms (and Tokenization) first.
Why a cache at all?
Each new token has to attend to every previous token. Without a cache, the model recomputes the keys and values for the whole sequence at every step, which is O(n²) work. The KV cache stores those keys and values once and reuses them, so generation becomes linear instead of quadratic. You pay in memory: the cache grows with every token, every layer and every concurrent user.
Models have no memory
A is stateless. It keeps nothing between steps; give it the same tokens and it does exactly the same work again. To write token N it has to attend to all N−1 prior tokens, so if nothing were saved it would recompute every token's keys and values from scratch on every step. The KV cache is the workaround.
A language model has no memory
Each call is a pure function: the full token sequence goes in, one next token comes out, and nothing is kept for the next call. So every call gets the whole prefix again and processes all of it.
Input this call
model
f(tokens)
kept for next call
nothing
Next token
catThe first call sees one token. Everything it works out about "The" is thrown away when it returns.
Because the model is stateless, the keys and values for past tokens come out identical at every step, so there is no reason to recompute them. The KV cache stores each token's K/V once and reuses it. That's all it does. The rest of this page is about what it costs you in memory.
What exactly gets cached
Keys and values are worth keeping because of how self-attention works. Each new token forms a query, scores it against every prior token's key, then blends their values. A past token's K and V never change, so you can compute them once and reuse them for the rest of the sequence.
Self-attention: score, softmax, blend
The newest token forms a Query, scores it against every token's Key (Q·K / √d), converts the scores to weights with softmax, then mixes their Values by those weights. All numbers are toy 4-d vectors, for illustration only.
| Token | K | Q·K/√d | Softmax weight | V |
|---|---|---|---|---|
Cache cached · frozen | 0.80 | 0.49 score 0.80 | ||
the cached · frozen | −0.41 | 0.14 score −0.41 | ||
keys new · computed now | 0.55 | 0.37 score 0.55 |
Strongest link: "Cache", 2 tokens back, even though "the" is right next to it. The weights come from what the Q and K vectors encode. Distance between tokens doesn't enter into it.
Blend: output = Σ weight × V(bars zoomed in)
The blended vector is what this token carries forward. Only its Query and its own K and V were new this step. Every other K and V row came straight from the cache.
attn(Q, K, V) = softmax(Q·Kᵀ / √d) · V
A past token's K and V depend only on that token and the weights. Nothing that comes later changes them. They are frozen the moment the token is processed, so you can compute them once and keep them. Those stored rows are the KV cache.
Quadratic work, or linear?
Without the recompute, generation drops from O(n²) to O(n) work. By a thousand tokens, the no-cache path is doing hundreds of times as much attention work.
The work triangle: recompute vs cache
Each row is one decode step. Each square is one prefix token whose keys and values that step needs. Without a cache the whole row is recomputed, so the total work is the area of the triangle. With a cache only the new token (the diagonal) is computed and the rest is read back.
Step 1: no cache computed 1 tokens this step, cached computed 1. Totals so far 1 vs 1.
No cache
O(n²)This step: 1 computed
With KV cache
O(n)This step: 1 computed
Cumulative work
fixed axesNo cache has done 1.0× the work
The gap widens with every token. At a 1,000-token context the no-cache path does about 501× the attention work of the cached path. No production decoder runs without a KV cache, and the cost moves from compute to memory.
The KV cache doesn't fit, so it tiers
is fast but small. At scale the KV cache is far bigger, so serving stacks push cold KV down a hierarchy: , local , then pooled network storage. Each step down gives ~10–100× more capacity and is ~10× slower. You want hot KV in HBM and the rest demoted, so it can come back later instead of being thrown away and recomputed.
KV cache hierarchy: demote, then promote back
Each lettered block is one user's KV context. New requests land in HBM. When a tier is full, the least-recently-used block moves one tier down and stays there until it's needed. Click any block to make that user return.
The cache is empty.
80–288 GB / GPU · 0/3 used
1–2 TB / node · 0/4 used
~30–100 TB / node · 0/6 used
PB-scale · 0/8 used
Empty hierarchy
Every tier has free slots. Press play to watch traffic arrive, or add a request yourself.
Tiered KV systems exist so that blocks get demoted instead of evicted. If a returning user's context is still in DRAM, NVMe or pooled storage, it's read back and promoted again, with no recompute from scratch. Turn off local NVMe to send cold blocks straight to the pooled network tier.
Size it: the tiering & capacity planner
The planner follows the way a KV cache builds up. A real 2026 model sets the bytes per token (dense , and sparse/ differ a lot), the workload mix sets tokens per session, and users plus retention set how many sessions you hold. Then you pick a GPU fleet and split it into prefill and decode. The KV fills decode HBM and CPU DRAM first, and the rest lands on the shared tier. The last step shows how long prefill and decode take, and how fast the shared tier has to be for reloading KV to beat recomputing it.
KV tiering & capacity planner
The steps follow the order a KV cache is built in. The model sets bytes per token, the workload sets tokens per session, users and retention set how many sessions you hold, and your fleet sets where it all lands: HBM first, then CPU DRAM, then the shared tier.
1Model
MLA latent (512 + 64 RoPE) per layer - tiny cache for a 671B model.
2Workload
What a session holds. Tokens per session set the KV per session.
Chat50%
conversation history
RAG30%
question + retrieved documents
Agentic / coding20%
long tool-use sessions
capped at the model's 160K
Images0%
prompt + images
DeepSeek R1 doesn't take images input
Video0%
prompt + video
DeepSeek R1 doesn't take video input
Audio0%
prompt + speech
DeepSeek R1 doesn't take audio input
Average session: 59K tokens = 2.1 GB of KV on DeepSeek R1. Per session: Chat 589 MB · RAG 2.3 GB · Agentic / coding 5.8 GB
3Users and retention
How many sessions' KV you hold at once.
Real time: a session's KV is dropped when it goes idle, so a returning user's context is recomputed from scratch. Pick a retention window to keep it: idle KV moves down to CPU DRAM and the shared tier, where it can be reloaded.
4Hardware
Decode nodes hold the session KV; prefill nodes process prompts and hand the KV over.
Hopper baseline - 80GB HBM3 per GPU, 900 GB/s NVLink.
Aggregated serving: all 4 nodes run prefill and decode and hold session KV.
5Where the KV lands
Model weights
671.0 GB
per copy · 2 decode copies, 2 nodes each
KV per session
2.1 GB
59K tokens on average
Total KV demand
2.08 TB
1,000 live sessions, real time
On the shared tier
-
what's left after HBM and DRAM
Live sessions need 2.08 TB of KV in decode HBM, but only 962.0 GB is free after the weights. About 451 of 1,000 sessions can decode at once. The rest wait, or are swapped out and back in.
Where the KV cache lands
demand 2.08 TB
Each tier across the 4 decode nodes, drawn to its own capacity
50% of 2.00 TB host memory per node
Pooled storage every node can read, so a session's KV can be reloaded on any node. It has no fixed capacity, so it's drawn open-ended. Light: what spilled; solid: what is stored after VAST's ~1.2:1 data reduction on KV data.
HBM fills; 1.14 TB spills to CPU DRAM
45% of the KV cache fits in HBM after the weights; the other 55% lives in DRAM, one hop away. Nothing reaches the shared tier.
6Compute time
Prefill and decode on one model copy (16 H100 GPUs): peak FLOP/s and HBM bandwidth scaled by measured efficiency, or your own measurements.
Prefill, average session
2.24 s
59K tokens from scratch · 1,691 tok/s per GPU
Reload beats recompute above
973 MB/s
reading a session's 2.1 GB of KV into the model copy is faster than prefilling it again
Decode, per token
54 ms
19 tok/s per user · 226 live sessions per copy
Decode is bound by
Memory
reading weights and KV from HBM each step
Prefill per session: Chat 194 ms · RAG 1.62 s · Agentic / coding 8.29 s. Attention is 68% of prefill FLOPs at the average prompt, and it grows with prompt length.
Optional. Replaces the theoretical prefill time with what you measured.
Optional. Replaces the theoretical time per output token.
Defaults come from measured serving runs: prefill typically reaches 30-55% of peak FLOP/s (about 30% for MoE models), and decode 55-75% of HBM bandwidth on one or two GPUs (less across more GPUs or at small MoE batches). Engine, precision and parallelism move real numbers by 25% or more, so enter a measurement when you have one. Prefill is a whole session from scratch, as when a returning user's KV was dropped.
Each decode node group holds one copy of the weights plus ~10% activation headroom, and the KV cache gets the rest of HBM. Live sessions are placed first, then idle sessions kept for retention, then burst headroom. The shared tier is pooled network storage (VAST, NVIDIA CMX and similar) that every node can read, so a session can be reloaded on any node. Figures are illustrative.
KV per session by model
KV cache for one session on every model in the planner, each at its usual KV precision. The attention design sets the size: the largest needs 104× the smallest at the same context.
Session length
Log scale. Models with a shorter maximum context are shown at that maximum. Pure state-space models keep a fixed-size state and have no per-token KV cache.
Software that stretches the cache
Before you add hardware, modern serving engines get a lot more out of the HBM you already have. Every production LLM stack uses these four techniques.
PagedAttention
2–4× throughputvLLM
Stores KV in fixed-size pages, like OS virtual memory, so a request no longer needs one contiguous block. That removes the 60–80% fragmentation waste of pre-allocating max-length buffers, and far more requests fit in HBM at once.
RadixAttention
5–6× on shared prefixesSGLang
Keeps cached prefixes in a radix tree, so requests that share a system prompt or few-shot examples reuse the same KV blocks. A big win for chat and agent workloads, where every request starts with the same instructions.
Continuous batching
Higher GPU utilizationOrca / vLLM
Finished sequences leave the batch and new ones join at every decode step, so nobody waits for the slowest request. The GPU stays busy even when output lengths vary wildly.
12 requests finished in 12 steps, 1 idle slot-step
A static batch finishes 7 in the same time (illustrative lengths).
Prefix / KV reuse
TTFT 11s → 1.5sLMCache · Mooncake
Prefill once, reuse many times. You cache the KV of a long document or shared context and serve it to many requests from a fast tier, so prefill doesn't run again for each one. In benchmarks, VAST + LMCache cut time-to-first-token ~7× on 128K contexts.
Time to first token, 128K-token context
The shared context's KV is already stored, so the request loads it with no recompute: ~7× faster to first token. (from a VAST + LMCache benchmark)
Prefix caching a hybrid model
assumes the cache can be cut at any token. That holds for KV, which is stored in small pages. such as Qwen3.5, Kimi K3 and Nemotron 3 also carry a fixed-size recurrent state per sequence, which can only be resumed from a snapshot taken at a block boundary. A request that diverges between snapshots recomputes from the last one. One snapshot weighs as much as thousands of tokens of KV, and it is kept in fp32.
Prefix caching a hybrid model: KV pages vs state snapshots
Two requests share a 6,144-token prompt, then the second one diverges. How much of the shared part can it skip?
Hybrid model
69 KDA : 24 MLA layers
State snapshot every
768 tokens is the LMCache Kimi K3 recipe
Full-attention model
reuse 3,696 · recompute 4
KV cached in 16-token pages; reuse runs to the last page before the split
Hybrid: Kimi K3
reuse 3,072 · recompute 628
Recurrent state resumes only from a snapshot; everything after it replays
One state snapshot
414 MiB
per sequence, kept in fp32. That's about 15.7K tokens of this model's KV (27 KiB/token)
Attention KV for the prompt
162 MiB
6,144 tokens x 27 KiB, sliceable at any page
Keeping every snapshot
3.23 GiB
8 snapshots x 414 MiB. Finer snapshots give more reuse and take more storage
Engine support
- vLLM: prefix caching for hybrids through
mamba_cache_mode="align", which snapshots state on block boundaries; still experimental. - SGLang: MambaRadixCache keeps states and KV pages in separate LRU pools, with extra state slots per request for snapshots.
- LMCache: the Kimi K3 recipe stores the KDA state as an opaque page, snapshotted every 768 tokens.
Takeaway: a tiered KV store serving hybrid models has to hold whole state blobs (hundreds of MiB each, all or nothing) next to the usual token pages. Prompt length and page size are illustrative.
Throughput figures come from each project's paper or benchmarks and depend on the workload. See Inference & Serving for prefill/decode and disaggregated serving.
How engines manage KV cache
These two demos walk through the first two ideas above. Paging controls how KV is laid out in HBM. Prefix reuse controls how much of it has to be computed at all.
PagedAttention: KV in fixed-size blocks, like OS pages
The same 48 slots of KV memory under two allocators. Reserving a contiguous max-length region per request leaves much of it empty. PagedAttention treats KV as non-contiguous fixed-size blocks allocated on demand, like OS virtual memory pages, so fragmentation disappears and more sequences fit in HBM.
Contiguous, reserved up front
3 of 4 requests fitOne 16-slot region per request, sized for the longest it might get.
- Tokens stored
- 9
- Reserved but empty
- 39 slots
Paged, allocated on demand
3 of 4 requests fit12 blocks of 4 slots in one shared pool; a block table maps each request.
Block tables
- Tokens stored
- 9
- Allocated but empty
- 3 slots
36 slots still free for more requests.
Waste in the paged pool is at most one part-filled block per request, while contiguous reservations sit mostly empty. The engine uses the reclaimed memory to batch more sequences at once.
Illustrative sizes: 48 slots, 4-token blocks, 16-token reservations.
Prefix caching and RadixAttention: reuse the KV prompts share
Four prompts arrive one after another. All start with the same system prompt, and two pairs also share a document. Compare how much gets prefilled with no reuse, with one cached prefix, and with a radix tree.
A radix tree of cached prefixes, so partially overlapping prompts share whatever KV they have in common automatically.
Prompts, in arrival order
Radix tree of cached prefixes
Prefilled
0
tokens
Reused
0
tokens
Work saved
0%
of prefill
Over all four prompts: no reuse prefills 7,000 tokens, caching the system prompt 5,800, and the radix tree 3,400. The tree also finds the shared documents without anyone declaring them.
Illustrative token counts: system prompt 400, each document 1,200, each question 150 (question bars drawn wider than scale so they stay readable).
KV cache acceleration projects
Outside the engines, a set of projects pushes KV reuse and offload further, especially for long context and RAG. For how LMCache, KVBM and NIXL fit together on shared storage, see Context Memory.
Where each project moves the KV cache
Three projects extend the cache down the memory hierarchy. CacheBlend widens which cached chunks can be reused at all. Tap a project to read it.
the small subset of tokens CacheBlend recomputes to reuse a chunk
LMCache
Adds: Multi-tier KV layer: offloads KV to CPU DRAM / disk and shares it across instances.
Helps when: Long contexts and repeated prefixes that overflow HBM. 3–10× latency cut with vLLM.
Multipliers come from each project's paper or benchmarks and depend on the hardware and workload.
Quantizing the cache itself
The KV cache is just numbers, and like weights it can be stored at lower precision. Dropping KV from (2 bytes) to (1 byte) halves the cache footprint and the bandwidth read per decode step, with minimal quality loss on most workloads. It works with every attention variant and every tiering option above.
Same cache, half the bytes
Eight cached K/V values, stored at two precisions.
Bytes in memory
8 values × 1 B = 8 Bdashed cells are freed
per request, every layer
HBM bandwidth spent
Takeaway: half the bytes per value halves both bars. The cache needs half the memory, and every decode step streams half as much from HBM.
In the planner above, switch KV precision between BF16 and FP8 and the spill shrinks. Half the bytes per token means half the total cache, which can pull a scenario back up a whole tier.
FP8 is the production default. Blackwell adds KV (4-bit values with an FP8 scale per 16) at about 56% of FP8's footprint. In benchmarks on Blackwell (Sep 2026) it raised decode throughput by 26-30% at the same concurrency and by 37-78% when memory was the limit, with prefill unchanged; accuracy held on Qwen3.5-397B but Qwen3.8-27B dropped 1-1.6 points on reasoning and coding tests, so validate it on your own workload. DeepSeek V4.1-Flash goes further and stores its KV in FP4 natively. It also keeps it on SSD for days, as DeepInfer's Inference Stack shows.
See Quantization & Precision for how FP8/FP4 formats actually work.
GPU HBM at a glance
HBM capacity and bandwidth cap how much KV can stay hot. The planner sizes against this fleet.
| Node | HBM / GPU | GPUs | Total HBM | Bandwidth | NVLink |
|---|---|---|---|---|---|
| 8× H100 (DGX/HGX) | 80 GB | 8 | 640 GB | ~3.35 TB/s | 900 GB/s |
| 8× H200 | 141 GB | 8 | 1,128 GB | ~4.8 TB/s | 900 GB/s |
| 8× B200 | 180 GB | 8 | 1,440 GB | ~8 TB/s | 1800 GB/s |
| 8× B300 | 288 GB | 8 | 2,304 GB | ~8 TB/s | 1800 GB/s |
| GB200 NVL72 | 186 GB | 72 | 13,392 GB | ~8 TB/s | 1800 GB/s |
| GB300 NVL72 | 288 GB | 72 | 20,736 GB | ~8 TB/s | 1800 GB/s |
| Vera Rubin NVL72 | 288 GB | 72 | 20,736 GB | ~19.2 TB/s | 3000 GB/s |
Specs are approximate and vendor-published. NVLink figures are per-GPU intra-domain bandwidth; NVL72 systems pool all 72 GPUs into a single domain.
The other way out: have no KV cache at all
Everything on this page tackles a KV cache that grows linearly with context. State-space models (Mamba) avoid it altogether. Instead of caching every past token's keys and values, they carry one fixed-size recurrent state that's updated token by token, so memory stays constant however long the context gets. The cost is lossy recall, so frontier models go hybrid (mostly SSM layers, a few attention layers).
When the cache outgrows the cluster: pooled, persistent KV
At 100K concurrent users with 64K-token contexts and multi-day retention, the KV cache reaches tens of petabytes. No local NVMe holds that. A pooled, networked KV tier (VAST, NVIDIA CMX, Mooncake) makes it practical: KV persists across requests and nodes, returning users skip prefill entirely, and data reduction shrinks the footprint further. The cache stops being a per-GPU scratchpad and becomes shared cluster infrastructure.