LLMscore
KV cache
The stored keys and values of every past token, so generation doesn’t recompute them.
What it is and why it exists
During generation each new token attends to all previous tokens. Instead of recomputing their keys and values every step, the server keeps them in GPU memory — the KV cache. It makes decoding fast but its size grows with sequence length × batch size, which is why long contexts and many concurrent users run out of GPU memory.
Quick prototype
Invisible — the provider handles it.
In production, at scale
When self-hosting, KV-cache memory decides how many requests fit on a GPU: per token it is 2 × layers × KV heads × head dim × bytes. Grouped-query attention, FP8 KV cache, PagedAttention and prefix sharing all exist to shrink or reuse it.
Vendors and tools
| vLLM | PagedAttention: KV cache in blocks, near-zero fragmentation, prefix sharing |
|---|---|
| SGLang | RadixAttention: reuses cached prefixes across requests |
| TensorRT-LLM | Paged KV cache and FP8/INT8 KV quantisation |
On AWS: On SageMaker or EC2 GPU instances you size the KV cache yourself (e.g. via vLLM’s gpu_memory_utilization); on Bedrock it’s managed.
How it fails
- OOM under load because max_model_len × concurrency was never budgeted
- Throughput collapses when long prompts evict everyone else’s cache
What the interviewer asks next
Compute the KV cache for an 8B model with 32 layers, 8 KV heads, head dim 128, FP16, at 8k tokens.
2 × 32 × 8 × 128 × 2 bytes = 128 KB per token; × 8,192 ≈ 1 GB per sequence.
Why does grouped-query attention matter for serving?
Several query heads share one KV head, so the cache shrinks by that factor (e.g. 32 → 8 heads = 4× smaller), which means more concurrent sequences per GPU.