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 NameKey-Value Cache
CreatedUsed 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

  1. Accelerating LLM text generation in production inference servers
  2. Reducing latency in real-time conversational AI applications
  3. Enabling efficient batch inference with continuous batching
  4. Long-context processing with memory-efficient KV cache compression
  5. Serving multiple concurrent users with shared prefix caching

Example

loading...
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.

Related Tools

Related Terms

Attention Mechanism

Attention Mechanism is a differentiable operation that computes data-dependent weights over permitted representations and uses them to form an output. Its behavior depends on the Query, Key, and Value projections, masking, normalization, learned parameters, and the surrounding model.

Transformer

Transformer is a neural-network architecture first described in the 2017 paper "Attention Is All You Need." Its common variants combine attention, feed-forward layers, residual connections, normalization, and positional information to process token or other sequence representations.

Context Window

Context Window is the maximum number of tokens that a large language model can process in a single interaction, encompassing both the input prompt and the generated output, which determines how much information the model can consider when generating responses.

Quantization

Quantization is a model compression technique that reduces the precision of neural network weights and activations from higher bit representations (like 32-bit floating point) to lower bit formats (like 8-bit or 4-bit integers), significantly decreasing model size and inference costs while maintaining acceptable accuracy. For large language models (LLMs), quantization has become the primary method for making billion-parameter models accessible on consumer hardware, with specialized formats such as GPTQ, AWQ, and GGUF enabling efficient inference on devices ranging from NVIDIA gaming GPUs to Apple Silicon laptops and even smartphones.

Related Articles