Inference, Serving & Production·Question 2 of 76BasicAI-75

What is the KV cache, why is it needed, and how big does it get? Ballpark it for a 70B-class model at 128K context.

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

At each decode step, the new token's query attends to the keys and values of all previous tokens. Without caching you'd recompute every past token's K/V projections at every step - O(n²) redundant work per generated sequence. The KV cache stores each layer's K and V vectors for every processed token, making each decode step incremental. It's what makes autoregressive generation computationally feasible; the price is memory.

Full worked answer

At each decode step, the new token's query attends to the keys and values of all previous tokens. Without caching you'd recompute every past token's K/V projections at every step - O(n²) redundant work per generated sequence. The KV cache stores each layer's K and V vectors for every processed token, making each decode step incremental. It's what makes autoregressive generation computationally feasible; the price is memory.

Size formula:

KV bytes/token = 2 (K and V) × n_layers × n_kv_heads × head_dim × bytes_per_elem

Worked example, Llama-3-70B shape: 80 layers, 8 KV heads (GQA), head_dim 128, FP16:

2 × 80 × 8 × 128 × 2  ≈ 320 KB per token
8K context   → ~2.6 GB per sequence
128K context → ~41 GB per sequence

Note that's with GQA - with full multi-head attention (64 KV heads) it would be 8× larger, which is exactly why GQA/MQA/MLA exist.

Why it matters operationally: on an 80 GB GPU, after ~40 GB of 4-bit weights, the KV cache is the budget that determines how many sequences you can serve concurrently - i.e., your batch size, and therefore your throughput. A single 128K-context request can evict dozens of short-context users' worth of cache. Mitigations: fewer KV heads (architecture), KV-cache quantization (FP8 halves it), paged allocation (PagedAttention, to stop fragmentation waste), prefix sharing, and sliding-window attention for some layers.

What a good answer covers

  • At each decode step, the new token's query attends to the keys and values of all previous tokens
  • Without caching you'd recompute every past token's K/V projections at every step - O(n²) redundant work…
  • The KV cache stores each layer's K and V vectors for every processed token, making each decode step…
  • It's what makes autoregressive generation computationally feasible; the price is memory
  • Note that's with GQA - with full multi-head attention (64 KV heads) it would be 8× larger, which is…

Interviewer follow-ups to expect

  1. Why does GQA shrink the KV cache but not the weight count meaningfully?

  2. What happens in vLLM when the KV cache fills up mid-generation?

  3. How would the math change for a mixture-of-experts model?

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