What Is KV Cache? Speeding Up LLM Inference 2026
KV cache stores key-value pairs from previous tokens to avoid redundant LLM computation. How it works, memory, optimization.
KV cache (Key-Value cache) is a memory optimization technique that stores the computed key and value tensors from previous tokens during large language model inference. Instead of recomputing the attention mechanism for every token from scratch, the model reuses cached key-value pairs from earlier tokens. This reduces the computational complexity of generating long sequences from quadratic to linear, making real-time LLM inference practical for production applications.
For engineering teams building AI-powered products, understanding KV cache is essential because it directly affects inference speed, memory requirements, and the cost of running LLMs at scale.
How Attention Works Without KV Cache
To understand KV cache, you first need to understand how the attention mechanism works in transformer models.
The Attention Mechanism
When a transformer model processes a sequence of tokens, each token produces three vectors through learned linear projections:
- Query (Q): What this token is looking for
- Key (K): What this token offers to other tokens
- Value (V): The actual content of this token
The attention score between two tokens is computed as the dot product of the query from one token and the key from another, scaled by the square root of the key dimension. This score determines how much attention one token should pay to another.
The Problem
Without caching, generating each new token requires recomputing attention for the entire sequence. For a sequence of length N:
- Token 1: Compute attention for 1 token
- Token 2: Compute attention for 2 tokens
- Token 3: Compute attention for 3 tokens
- ...
- Token N: Compute attention for N tokens
The total computation is O(N^2) because each token must attend to all previous tokens, and the sequence grows with each generation step.
How KV Cache Solves This
KV cache stores the key and value tensors computed for each token during the forward pass. When generating a new token, the model only needs to:
- Compute Q, K, V for the new token only
- Retrieve cached K and V tensors for all previous tokens
- Compute attention using the new token's Q against all cached K and V tensors
This reduces the computation for each new token from O(N) to O(1) for the key-value operations, making the total generation complexity O(N) instead of O(N^2).
Practical Example
Consider generating a 1,000-token response:
Without KV cache: 1 + 2 + 3 + ... + 1000 = 500,500 attention computations With KV cache: 1000 attention computations (one per token, each using cached values)
The computational savings are massive for long sequences.
Memory Requirements
KV cache has a direct memory cost. Understanding this cost is critical for production deployments.
Memory Formula
The memory required for KV cache is:
KV_cache_memory = 2 * num_layers * num_heads * head_dim * sequence_length * precision_bytes
Where:
- 2 accounts for both K and V tensors
- num_layers: Number of transformer layers
- num_heads: Number of attention heads
- head_dim: Dimension of each attention head
- sequence_length: Number of tokens in the sequence
- precision_bytes: 4 for float32, 2 for float16/bfloat16
Real-World Numbers
For a 7B parameter model (like Llama 2 7B):
- 32 layers, 32 heads, 128 head dimension
- float16 precision (2 bytes per value)
- At 2,048 tokens: ~1 GB KV cache
- At 4,096 tokens: ~2 GB KV cache
- At 8,192 tokens: ~4 GB KV cache
For a 70B parameter model:
- 80 layers, 64 heads, 128 head dimension
- float16 precision
- At 2,048 tokens: ~8 GB KV cache
- At 4,096 tokens: ~16 GB KV cache
This is why KV cache management is critical for production deployments. The cache can easily consume more memory than the model weights themselves.
KV Cache Optimization Strategies
1. Multi-Query Attention (MQA)
MQA shares key and value heads across all attention heads. Instead of each head having its own K and V, all heads share a single K and V projection.
Memory reduction: num_heads / shared_heads (typically 8x reduction) Quality trade-off: Slight quality reduction for faster inference
Used in: PaLM, Falcon, some versions of Llama
2. Grouped-Query Attention (GQA)
GQA is a middle ground between standard attention and MQA. Key and value heads are grouped, with each group sharing K and V projections.
Memory reduction: proportional to group size Quality trade-off: Minimal quality reduction
Used in: Llama 2 70B, Mistral, most modern models
3. PagedAttention
PagedAttention (used in vLLM) manages KV cache like virtual memory pages. Instead of allocating contiguous memory for each sequence, the cache is divided into fixed-size pages that can be allocated dynamically.
Benefits:
- Reduces memory waste from fragmentation
- Enables efficient memory sharing between sequences
- Supports longer sequences without OOM errors
4. Sliding Window Attention
Instead of attending to the entire sequence, each token only attends to a fixed window of recent tokens. This caps the KV cache size at the window length.
Memory reduction: Constant regardless of sequence length Quality trade-off: Loses long-range dependencies
Used in: Mistral, Longformer
5. KV Cache Quantization
Reduce the precision of cached key-value tensors. Instead of float16, use int8 or int4 for the cache while keeping computation in float16.
Memory reduction: 2-4x Quality trade-off: Minimal for int8, noticeable for int4
6. Token Eviction
For very long sequences, evict older tokens from the cache when memory pressure is high. The model retains recent tokens that are more relevant for continuing the sequence.
Strategies:
- Evict oldest tokens first (simple, effective)
- Evict tokens with lowest attention scores (more sophisticated)
- Use a sliding window with periodic full recomputation
Production Implications
Throughput
KV cache directly affects inference throughput. With caching:
- Prefill phase: Process the entire input prompt (computation-heavy)
- Decode phase: Generate tokens one at a time using cached values (memory-bound)
The decode phase is typically the bottleneck. More memory for KV cache means higher throughput because you can serve more concurrent requests.
Concurrency
Each concurrent request requires its own KV cache. For a 7B model with 4K context:
- Single request: ~1 GB KV cache
- 10 concurrent requests: ~10 GB KV cache
- 100 concurrent requests: ~100 GB KV cache
This is why production LLM serving requires significant GPU memory, often more than the model weights themselves.
Cost
KV cache memory requirements directly affect deployment cost:
- More memory = more expensive GPUs
- More concurrent requests = more GPUs needed
- Longer sequences = more memory per request
Optimizing KV cache usage reduces inference cost proportionally.
Implementation Considerations
Framework Support
Major inference frameworks handle KV cache automatically:
vLLM: PagedAttention for efficient memory management TensorRT-LLM: Optimized KV cache with FlashAttention llama.cpp: CPU-friendly KV cache with quantization support Hugging Face Transformers: Basic KV cache management
Monitoring
Monitor these metrics for KV cache health:
- Cache utilization: Percentage of allocated cache that is in use
- Cache eviction rate: How often tokens are evicted from cache
- Memory pressure: GPU memory usage relative to available memory
- Sequence length distribution: How long sequences are in your workload
Configuration
Key parameters to configure:
- max_sequence_length: Maximum tokens per request (affects cache allocation)
- batch_size: Number of concurrent requests (multiplied by cache per request)
- kv_cache_dtype: Precision for cached tensors (float16, int8, int4)
- enable_paged_attention: Use PagedAttention for memory efficiency
Frequently Asked Questions
What is the difference between KV cache and regular caching?
Regular caching stores computed results to avoid recomputation. KV cache specifically stores the key and value tensors from transformer attention layers. Regular caching might store database query results or API responses. KV cache is a model-level optimization that accelerates the attention mechanism during token generation.
How much memory does KV cache use for a 7B model?
A 7B parameter model (like Llama 2 7B) uses approximately 1 GB of KV cache memory per request at 2,048 tokens, or 2 GB at 4,096 tokens. The formula is 2 * num_layers * num_heads * head_dim * sequence_length * 2 bytes (for float16). The cache grows linearly with sequence length.
Can I reduce KV cache memory without losing quality?
Yes. Multi-Query Attention (MQA) and Grouped-Query Attention (GQA) reduce KV cache memory by sharing key-value projections across attention heads. These techniques are built into modern models like Llama 2 70B and Mistral. KV cache quantization (using int8 instead of float16) also reduces memory with minimal quality loss.
Why is KV cache important for production LLM deployment?
KV cache directly affects inference speed, memory requirements, and cost. Without KV cache, each token generation would require recomputing attention for the entire sequence, making inference impractically slow. With KV cache, the decode phase becomes memory-bound rather than compute-bound, enabling real-time generation at scale.
What is PagedAttention and why does it matter?
PagedAttention (used in vLLM) manages KV cache like virtual memory pages. Instead of allocating contiguous memory for each sequence (which wastes memory due to fragmentation), PagedAttention divides the cache into fixed-size pages that can be allocated dynamically. This reduces memory waste, enables higher concurrency, and supports longer sequences without out-of-memory errors.
Related Resources
- What is KV Cache Explained — technical deep-dive on KV cache
- LLM Retrieval Guide — RAG and retrieval optimization
- How to Build an AI Agent — build agents that use LLMs