LLM & Transformer Fundamentals·Question 13 of 109BasicAI-75

What is the KV cache and why does it make generation fast?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

During autoregressive generation, each new token's attention needs the keys and values of all previous tokens at every layer. Those K and V vectors depend only on the tokens that produced them (and their positions) - they don't change as generation proceeds. The KV cache stores them so each step computes Q/K/V only for the single new token, attends against cached K/V, and appends its own K/V to the cache.

Full worked answer with diagram

During autoregressive generation, each new token's attention needs the keys and values of all previous tokens at every layer. Those K and V vectors depend only on the tokens that produced them (and their positions) - they don't change as generation proceeds. The KV cache stores them so each step computes Q/K/V only for the single new token, attends against cached K/V, and appends its own K/V to the cache.

Without the cache you'd re-run the full forward pass over the whole prefix for every generated token: generating token n costs O(n) recomputation, so a full generation costs O(n²) forward-pass work (with the attention inside making it effectively cubic-ish in total FLOPs). With the cache, each step does one token's worth of projections plus attention reads over n cached entries - generation becomes linear in tokens generated, which is the difference between usable and unusable latency.

This creates the two-phase structure every serving stack is built around: prefill (process the whole prompt in parallel - compute-bound, populates the cache) and decode (one token at a time - memory-bandwidth-bound, because each step must read the entire cache from GPU memory while doing relatively few FLOPs). Time-to-first-token is prefill; inter-token latency is decode.

The cost is memory: per token you store 2 (K and V) × n_layers × n_kv_heads × head_dim elements. For a 70B-class model with GQA (80 layers, 8 KV heads, head_dim 128, fp16) that's ~320 KB per token - ~40 GB for one 128k-token sequence. That memory pressure is why GQA/MQA exist (fewer KV heads), why vLLM's PagedAttention manages cache in non-contiguous blocks, why prefix/prompt caching shares cache across requests, and why cache quantization (fp8/int8 KV) is common.

Worth sketching. The loop back into the cache is the point: draw it and the linear-versus-quadratic argument makes itself.

flowchart LR
    P["prefill: whole prompt in parallel<br/>compute-bound, sets TTFT"] --> C["KV cache: K and V for<br/>every token, every layer"]
    C --> D["decode step: project Q, K, V<br/>for one new token"]
    D --> A["attend over n cached entries,<br/>streaming the cache from HBM"]
    A --> AP["append this token's K and V"]
    AP --> D
    AP --> O["emit token, cache grows by one"]

What a good answer covers

  • During autoregressive generation, each new token's attention needs the keys and values of all previous…
  • Those K and V vectors depend only on the tokens that produced them (and their positions) - they don't…
  • The KV cache stores them so each step computes Q/K/V only for the single new token, attends against…
  • Without the cache you'd re-run the full forward pass over the whole prefix for every generated token
  • With the cache, each step does one token's worth of projections plus attention reads over n cached…

Key terms: prefill · decode

Interviewer follow-ups to expect

  1. Why is decode memory-bandwidth-bound rather than compute-bound?

  2. What is prefix caching and when does it save money?

  3. Why can't you cache the queries too?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep