Derive the KV cache memory formula and compute it for a concrete model.
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
Derivation from what's stored: for each token, at each layer, the cache holds that token's key and value vectors for every KV head. A key (or value) vector per head has head_dim elements. So:
bytes per token = 2 × n_layers × n_kv_heads × head_dim × bytes_per_element
Full worked answer with diagram
Derivation from what's stored: for each token, at each layer, the cache holds that token's key and value vectors for every KV head. A key (or value) vector per head has head_dim elements. So:
bytes per token = 2 × n_layers × n_kv_heads × head_dim × bytes_per_element
(2 for K and V; multiply by seq_len for a sequence, and by batch size for a serving node.) Note it's kv heads - with GQA the query head count is irrelevant to the cache.
Concrete: a Llama-3-70B-shaped model - 80 layers, 8 KV heads (GQA), head_dim 128, fp16 (2 bytes):
bytes_per_token = 2 * 80 * 8 * 128 * 2 # = 327,680 ≈ 320 KB/token
seq_128k = bytes_per_token * 131_072 # ≈ 42 GB for one sequence
seq_8k = bytes_per_token * 8_192 # ≈ 2.7 GB
So a single 128k-context conversation consumes ~40+ GB of GPU memory beyond the ~140 GB of fp16 weights. With full MHA (64 KV heads) it would be 8× worse: ~2.6 MB/token, ~340 GB at 128k - completely unservable. That one comparison is the entire justification for GQA.
Engineering consequences to draw out: (1) batch capacity - cache memory, not weights, caps concurrent users, so cache size directly sets cost per request; (2) PagedAttention (vLLM) exists because naively pre-allocating max-length contiguous cache wastes most of it - paging allocates on demand in blocks, like virtual memory; (3) KV quantization to fp8/int8 halves or quarters this again with modest quality cost; (4) prefix caching shares the cache of common prompt prefixes (system prompts, few-shot examples) across requests - huge savings for agentic workloads that repeatedly re-send growing conversations; (5) speculative decoding, beam search, and parallel sampling all multiply cache pressure.
Sanity-check habit: memorise the formula, not model numbers - interviewers vary the architecture and want to watch you compute.
Worth sketching. Writing the multiply chain out means you can recompute it live when the interviewer changes the architecture on you.
flowchart LR
T["one token"] --> L["x 80 layers"]
L --> H["x 8 KV heads (not query heads)"]
H --> D["x 128 head_dim"]
D --> KV["x 2, one each for K and V"]
KV --> B["x 2 bytes, fp16"]
B --> R["320 KB per token<br/>~42 GB for one 128k sequence"]
What a good answer covers
- Derivation from what's stored
- A key (or value) vector per head has head_dim elements
- bytes per token = 2 × n_layers × n_kv_heads × head_dim × bytes_per_element
- (2 for K and V; multiply by seq_len for a sequence, and by batch size for a serving node.) Note it's kv…
- So a single 128k-context conversation consumes ~40+ GB of GPU memory beyond the ~140 GB of fp16 weights
Key terms: kv · PagedAttention · KV quantization · prefix caching
In the glossary: GQA · MHA · PagedAttention · Prefix caching · Speculative decoding · Beam search
Interviewer follow-ups to expect
How does PagedAttention reduce waste, and what problem does fragmentation cause?
Engineering consequences to draw out: (1) batch capacity - cache memory, not weights, caps concurrent users, so cache size directly sets cost per request; (2) PagedAttention (vLLM) exists because naively pre-allocating max-length contiguous cache wastes most of it - paging allocates on demand in blocks, like virtual memory; (3) KV quantization to fp8/int8 halves or quarters this again with modest quality cost; (4) prefix caching shares the cache of common prompt prefixes (system prompts, few-shot examples) across requests - huge savings for agentic workloads that repeatedly re-send growing conversations; (5) speculative decoding, beam search, and parallel sampling all multiply cache pressure.
On an 8×80 GB node serving this model, how many concurrent 32k-token sequences fit after the weights?
How does sliding-window attention change the formula?
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