What is KV Cache? Memory Scaling in Long-Context Inference
KV-cache is why LLMs need so much more VRAM than their weight size suggests. Learn how it scales with context length and how to optimize it.
What is KV Cache?
During autoregressive text generation, the LLM processes one token at a time. To avoid re-computing attention for every previous token, the model caches the Key and Value tensors from previous steps ā this is the KV-cache. Without KV-cache, generating N tokens would require O(N²) compute. With KV-cache, each step is O(N) because previous K/V vectors are reused. The tradeoff: KV-cache grows linearly with context length, batch size, and number of layers, consuming significant VRAM that could otherwise hold model weights.
KV-Cache Memory Formula
KV-cache size = 2 Ć num_layers Ć num_heads Ć head_dim Ć context_length Ć batch_size Ć precision_bytes. For Llama 3.3 70B at FP16: 2 Ć 80 layers Ć 8 heads Ć 128 dim Ć 32k context Ć 1 batch Ć 2 bytes = ~8 GB. At 128k context: ~32 GB. At batch=32 with 32k context: ~256 GB. The multiplier is dramatic: going from 8k to 128k context increases KV-cache by 16x. This is why single-GPU 128k inference requires the H200 (141 GB) or B200 (192 GB).
KV-Cache Optimization Techniques
PagedAttention (vLLM): allocates KV-cache in non-contiguous pages, reducing internal fragmentation from ~60% to ~4%. This alone can 2x your effective batch size. FlashAttention-3: fuses attention computation to reduce peak KV-cache memory by 20-40% through kernel-level optimization. Grouped Query Attention (GQA): reduces KV head count from num_heads to num_kv_heads (e.g., 8 KV heads vs 64 attention heads in Llama 3), cutting KV-cache by 8x. Quantized KV-cache: storing K/V in INT8 or INT4 reduces KV-cache memory by 50-75% with minimal quality loss.
Context Length vs VRAM: Practical Limits
RTX 4090 (24 GB): 7B FP16 at 128k (~4 GB KV-cache), 70B INT4 at 8k (~0.5 GB KV-cache). L40S (48 GB): 32B FP8 at 32k (~2 GB KV-cache), 70B INT4 at 16k (~4 GB KV-cache). H100 (80 GB): 70B FP8 at 32k (~16 GB KV-cache), 70B FP8 at 128k (OOM ā needs H200). H200 (141 GB): 70B FP8 at 128k (~16 GB KV-cache, 53 GB headroom). B200 (192 GB): 70B FP16 at 128k (~32 GB KV-cache, 90 GB headroom). The H200 is the minimum for single-GPU 128k inference at 70B scale.