What is KV Cache?
KV Cache (Key-Value Cache) is inference state that stores the attention Key and Value tensors already computed for prior tokens, so autoregressive decoding can reuse them instead of projecting the full prefix again.
Quick Facts
| Full Name | Key-Value Cache |
|---|---|
| Created | Used as incremental attention state in autoregressive Transformer decoding; implementations evolved after the 2017 Transformer architecture |
How It Works
During prefill, a causal Transformer processes prompt tokens and creates K/V state for each layer. During decode, it computes projections for the newest token, reads the historical cache, attends over it, and appends new K/V state. Caching removes repeated historical K/V projection, but it does not make attention constant-time: standard attention work and cache reads still grow linearly with cached sequence length per decode step. For an uncompressed decoder-only cache, a useful planning estimate is batch_size × sequence_length × n_layers × 2 × n_kv_heads × head_dim × bytes_per_element. GQA/MQA, sliding windows, quantization, paging, prefix sharing, offload, and implementation layout change the result; model weights, activations, workspaces, fragmentation, and metadata belong in the full VRAM budget.
Key Characteristics
- Caches Key and Value matrices from all previous tokens in attention layers
- Eliminates redundant computation during autoregressive generation
- Memory usage grows linearly with sequence length and model depth
- Requires careful memory management for long-context scenarios
- Compatible with various quantization techniques to reduce cache size
- Supports advanced variants like Multi-Query Attention (MQA) and Grouped-Query Attention (GQA)
Common Use Cases
- Accelerating LLM text generation in production inference servers
- Reducing latency in real-time conversational AI applications
- Enabling efficient batch inference with continuous batching
- Long-context processing with memory-efficient KV cache compression
- Serving multiple concurrent users with shared prefix caching
Example
Loading code...Frequently Asked Questions
What is KV Cache in Transformer models?
KV Cache (Key-Value Cache) is an inference optimization that stores the Key and Value matrices computed during previous generation steps. In autoregressive text generation, instead of recomputing attention keys and values for all previous tokens at each step, the cached values are reused. This avoids redundant computation and significantly speeds up token generation.
How does KV Cache affect memory usage?
For a basic decoder-only layout, estimate bytes as batch_size × sequence_length × n_layers × 2 (K and V) × n_kv_heads × head_dim × bytes_per_element. Use KV heads, not Query heads, for GQA/MQA models. This excludes weights, activations, temporary workspaces, allocator fragmentation, paging metadata, and implementation-specific layout, so validate the estimate against the exact checkpoint and serving engine.
What are Multi-Query Attention and Grouped-Query Attention?
MQA shares one Key-Value head across Query heads, while GQA shares a smaller set of KV heads among Query groups. Relative to full multi-head attention at the same dimensions and dtype, the cache reduction ratio is approximately Query heads divided by KV heads. Quality is a property of the trained architecture and workload, not a guaranteed minimal trade-off.
Can KV Cache be quantized to save memory?
Yes, when the exact checkpoint, serving engine, and hardware kernels support the selected cache dtype. The nominal bit-width ratio overstates realized savings because scales, padding, metadata, and layout add overhead. Quality and latency effects depend on model, layer sensitivity, calibration, and workload, so benchmark memory, throughput, and task quality together.
What is prefix caching and how does it relate to KV Cache?
Prefix caching reuses compatible K/V state for an exact or provider-defined shared prompt prefix across requests. Potential TTFT and compute savings depend on hit rate, prefix length, model and tokenizer identity, cache locality, eviction, and engine design. Tenant isolation, permissions, invalidation, and data-retention rules must prevent cross-user disclosure or stale reuse.