During decode, why is only Q computed for the new token while full K and V come from cache?
Each decode step makes one new Q row that reads all past K, V once and is never reread. K and V are read by every future query, so only K and V get cached.
Picture a long meeting where every minute someone new walks in, asks one question, listens to everything everyone has ever said, writes down one sentence, then leaves the room forever. The questions are throwaway: nobody else needs to look at the question someone asked five minutes ago. But the things everyone else said? Every future newcomer will replay those. So you bother to remember (cache) the said things, and you happily redo the throwaway question each time. That is exactly what a transformer does during decode with Q versus K and V.
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.
Trace one decode step end to end, explain Q is read once and K, V are reread, derive the cache memory formula, connect to GQA and MLA as the modern compressions.
Real products, models, and research that use this idea.
- vLLM's PagedAttention manages KV-cache pages explicitly because KV is the dominant memory budget at long context for Llama 4 Maverick serving.
- DeepSeek V4's MLA (Multi-head Latent Attention) compresses the KV cache further by sharing a low-rank latent across heads.
- Llama 4 uses GQA with H_q = 64, H_{kv} = 8 specifically to keep the KV-cache size manageable at 128k context.
- TensorRT-LLM and SGLang both expose KV-cache reuse across requests (prefix sharing) because reprojecting K and V is the costly part to avoid.
- Claude Opus 4.7 long-context serving relies on aggressive KV-cache pinning and offload to keep multi-100k-token sessions affordable.
What an interviewer would ask next. Try answering before peeking at the approach.
QWalk through what changes about KV-cache behavior during prefill versus decode.
QWhy does GQA shrink only K and V heads but not Q heads?
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.
Assuming Q is cheaper than K or V or that caching past queries would save work. The asymmetry is about reuse: Q is read once, K and V are reread every step.
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.