Across which axes is the KV cache stored, per layer, per head, per batch?
The KV cache spans all three axes: per layer, per KV head, and per batch request. Each combination stores its own K and V tensor over the sequence.
Imagine a multistory library where every floor (a transformer layer) has its own card catalog. On each floor, several librarians (the heads) keep separate drawers of cards. And every visitor (a request in the batch) brings their own set of bookmarks. To find a book, you need the right floor, the right librarian's drawer, and your personal bookmark stack. The KV cache works the same way: layers, heads, and requests are all separate axes, and any one of them being missed will make your memory math wrong by an order of magnitude.
Detailed answer & concept explanation~6 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
3m: the three independent axes (layer, KV head, batch), how MHA vs GQA vs MQA changes the head axis only, the per-token formula, and how prefix sharing partially compresses the batch axis.
Real products, models, and research that use this idea.
- HuggingFace transformers' `past_key_values` is a tuple of length L, each entry holding K and V of shape (batch, H_kv, T, d_h).
- vLLM's PagedAttention treats this cache as paged memory with per-request page tables and prefix sharing.
- Llama 4 Maverick and Claude Opus 4.7 both use GQA, so num_kv_heads is much smaller than num_heads in their config files.
- DeepSeek V4's MLA compresses K and V into a shared low rank latent that still spans the layer and batch axes.
- llama.cpp stores the cache as a single contiguous buffer indexed by (layer, head, position, dim) and supports per-axis quantization.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does PagedAttention's prefix sharing change the batch-axis story?
QWhy is cross-layer KV sharing not yet a production default?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Pretending the cache is per layer only, then sizing memory as if heads or batch share storage. Each axis is genuinely independent and contributes a multiplicative factor.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.