Architecture & Memory Analysis
What is KV Cache? Calculation, Formulas & VRAM Sizing
Understand how Key-Value caching accelerates autoregressive generation, why it causes unexpected CUDA Out of Memory errors on long contexts, and how to optimize it by 50% to 75%.
Interactive KV Cache Sizing Calculator
Dial in your context length, batch size, and attention mechanism to see exact VRAM consumption in real time.
32 layers · 32 query heads · 8 KV heads (4:1 GQA group)
Multi-user servers (like vLLM) multiply the KV cache by active parallel requests.
Formula: 2 (Keys + Values) × 32 layers × 8 KV heads × 128 head dim × 32,768 tokens × 2 bytes
Because this model uses GQA (8:1 ratio), this KV cache is 4x smaller than it would be with legacy MHA (which would have required 16.0 GiB).
1. Why Autoregressive Generation Requires a KV Cache
During transformer inference, tokens are generated one at a time. To generate token N, the model must compute attention across all previous N-1 tokens. Without a KV cache, keys and values for all past tokens would have to be recomputed at every single generation step, leading to quadratic O(N²) computational overhead.
By caching past Key and Value vectors in VRAM, computation stays linear O(N) per step. However, this trades compute for memory: every token you add to the context expands the KV cache in VRAM until generation finishes.
2. The Exact Mathematical Formula
The factor of 2 accounts for both Keys and Values. n_layers is transformer depth, n_heads_kv is the number of key/value attention heads, d_head is the head dimension, and seq_len is active context length.
Example: Llama 3 8B with GQA (8 KV heads, d_head=128, 32 layers) at 16k context with FP16 precision (2 bytes) requires: 2 x 32 x 8 x 128 x 16,384 x 1 x 2 = 2.15 GB VRAM for a single user sequence.
3. Architecture Evolution: MHA vs GQA vs MLA
4. How to Quantize the KV Cache (50% to 75% Savings)
By default, inference engines store KV cache in FP16 (2 bytes per value). By quantizing the cache to FP8 (1 byte) or INT4 (0.5 bytes), you immediately cut KV memory by 50% or 75% without touching the model weights.
In vLLM, add --kv-cache-dtype fp8. In llama.cpp, use -ctk q8_0 -ctv q8_0 or -ctk q4_0 -ctv q4_0 for dramatic memory savings on 32k+ conversations.
Frequently Asked Questions
What happens when the KV cache fills the remaining VRAM?
Does KV cache size depend on prompt length or generated output length?
Is quantizing the KV cache to FP8 safe for reasoning models like DeepSeek-R1?
How does KV cache scale with multiple concurrent users?
Why is DeepSeek MLA superior for long context inference?
Calculate your exact KV cache requirement
Use our interactive VRAM Calculator to test different context lengths and batch sizes across hundreds of models.
Knowledge & Deep Dives
Local LLM & VRAM Knowledge Hub
In-depth guides, architectural breakdowns, and hardware sizing manuals to help you build, run, and scale offline AI models with zero guesswork.