KV Cache
The KV cache stores intermediate key and value tensors from previous tokens during autoregressive generation, enabling subquadratic computation by avoiding redundant re-encoding of the full context.
The KV cache (Key-Value cache) is a memory-based optimization essential to efficient transformer inference. During autoregressive text generation, each new token's attention scores must be computed against every previous token in the sequence. Without caching, this would require recomputing the key and value projections for the entire past context on every step-a quadratic cost in sequence length. The KV cache stores the key and value matrices from each earlier time step in GPU memory, so that only the new token's query needs to be projected. This reduces attention computation from O(n^2) to O(n) per token, and the total generation cost becomes roughly O(n^2) over the full sequence (since cache grows linearly). However, the KV cache imposes a significant memory footprint: for a 70B parameter model with 80 layers and 8-head group, the cache can consume over 30 GB for a 4096-token sequence. This often becomes the bottleneck for long-context inference, as it scales linearly with batch size and sequence length. Techniques like PagedAttention (used in vLLM) and sliding window attention manage cache more efficiently by storing it in non-contiguous blocks and evicting stale entries. In quantized inference with ExL2 or GPTQ, the KV cache is typically kept in FP16 to maintain numerical precision, further increasing memory pressure. AI builders must carefully profile KV cache size when selecting GPU hardware-models with high token-to-parameter ratios (e.g., MoE) can be especially demanding. Newer architectures like Mamba attempt to eliminate the KV cache entirely, but at the cost of reduced output quality for certain tasks.
cache_size = 2 * batch_size * sequence_length * num_layers * num_key_value_heads * head_dim * bytes_per_elementThe KV cache is not a hold-all for raw token embeddings; it only stores the linear projection outputs used by the attention mechanism, not the input embeddings themselves.
Related terms
Concepts you'll usually encounter alongside this one.
The context window defines the maximum number of input tokens a transformer model can process at once, determining the length of conversation history or document it can reason over.
FlashAttention is an I/O-aware algorithm that computes exact attention without materializing the full attention matrix, reducing memory usage from O(n^2) to O(n) and improving speed 2-10x on GPUs.
The number of parallel data channels on a PCI Express bus connecting GPUs and CPUs, determining bandwidth for inter-device communication.
Used in benchmarks
Workloads on MyAI Bench that involve KV Cache.
Mentioned in tutorials
Hands-on guides that put this concept to work.
Stand up a fully functional local LLM stack on Ubuntu 22.04, NVIDIA drivers, CUDA, Ollama, and your first model, in under 30 minutes.
Run local LLMs on Windows 11 the right way, WSL2, the WSL CUDA toolkit, and Ollama with full GPU acceleration. Native installer covered too.
Apple Silicon is shockingly good at LLM inference. Get Ollama running with unified memory acceleration on any M-series Mac in 15 minutes.
DeepSeek's reasoning model rivals GPT-4 on math and code. Run the distilled 7B/14B/32B variants on consumer hardware in 30 minutes.
See how this term affects your memory budget.
Real numbers, real chips, every workload.
Picks for builders, by use case.
Stay Ahead of the AI Curve
Get weekly AI hardware news, benchmark updates, and deals in your inbox. Founding-subscriber list, be one of the first.