Inferensys

Glossary

KV Cache

A memory mechanism in transformer-based generative models that stores previously computed key and value tensors to avoid redundant computation during autoregressive token generation.
ML engineer working on model compression and quantization, laptop showing performance benchmarks, technical workspace.
TRANSFORMER INFERENCE OPTIMIZATION

What is KV Cache?

A memory mechanism in transformer-based generative models that stores previously computed key and value tensors to avoid redundant computation during autoregressive token generation.

KV Cache is a memory optimization technique that stores the previously computed Key and Value tensors from past attention operations during autoregressive text generation. Without caching, a transformer would recompute the attention scores for all preceding tokens at each new step, resulting in quadratic computational complexity relative to sequence length.

By retaining these tensors in GPU memory, the model only computes the query, key, and value for the single new token and appends them to the cache. This reduces the per-step computation from O(n²) to O(n). Efficient KV cache management, such as PagedAttention in vLLM, is critical for maximizing throughput and minimizing latency in high-concurrency serving environments.

MEMORY MECHANISM

Key Characteristics of KV Cache

The KV Cache is a critical inference optimization that stores previously computed Key and Value tensors, eliminating redundant computation during autoregressive token generation and dramatically reducing latency.

01

Autoregressive Redundancy Elimination

During text generation, each new token requires attention over all previous tokens. Without caching, the model would recompute Keys and Values for the entire sequence at every step, resulting in O(n²) computational complexity. The KV Cache stores these tensors in GPU memory, reducing per-step computation to O(n) by only calculating attention for the newest token against the cached history. This transforms generation from quadratic to linear time complexity relative to sequence length.

02

Memory Capacity as a Bottleneck

The cache grows linearly with batch size, sequence length, number of layers, and hidden dimensions. For a 70B parameter model serving 64 concurrent requests at 4096 tokens each, the KV Cache can consume hundreds of gigabytes of GPU memory. This memory pressure often becomes the primary constraint on serving throughput, not compute. Techniques like PagedAttention and multi-query attention directly target this bottleneck by reducing per-token memory footprint and enabling efficient memory sharing across sequences.

100+ GB
Typical KV Cache Size (70B Model)
2-4x
Memory Reduction via Multi-Query Attention
03

PagedAttention and Virtual Memory

Traditional KV Cache implementations allocate contiguous memory blocks per sequence, leading to fragmentation and underutilization. PagedAttention, introduced by the vLLM project, treats the cache like virtual memory in operating systems:

  • Partitions cache into fixed-size blocks that can be mapped non-contiguously
  • Enables memory sharing across sequences for identical prompt prefixes
  • Reduces waste from overallocation by up to 80%
  • Allows near-optimal GPU memory utilization under dynamic batching workloads
04

Prefix Caching for Shared Prompts

When multiple requests share a common system prompt or few-shot examples, the KV tensors for that prefix are identical. Prefix-aware caching computes and stores these once, then reuses them across all requests. This technique:

  • Eliminates redundant computation for shared prefixes
  • Reduces time-to-first-token for common prompt templates
  • Is especially impactful for chat applications with long system instructions
  • Requires careful cache keying based on token-level prefix matching
05

Quantized KV Cache

To further reduce memory pressure, the cached Key and Value tensors can be stored at lower precision than the model weights. KV Cache quantization typically uses:

  • INT8 or FP8 formats instead of FP16/BF16
  • Per-channel or per-token quantization granularity
  • Calibration-based scaling factor determination This can reduce cache memory by 50% with negligible accuracy degradation, enabling larger batch sizes or longer context windows on the same hardware. Libraries like TensorRT-LLM and llama.cpp implement this natively.
50%
Memory Reduction via INT8 Quantization
< 0.1%
Typical Accuracy Impact
06

Sliding Window and Eviction Policies

For extremely long sequences exceeding available memory, sliding window attention restricts the cache to only the most recent tokens. Key strategies include:

  • Fixed window: Retain only the last W tokens, discarding older context
  • Sink token preservation: Keep initial tokens as attention sinks while sliding the window
  • StreamingLLM: Maintains stable perplexity by preserving a small set of initial tokens alongside the sliding window These policies trade full-context recall for bounded memory usage, enabling theoretically infinite-length generation.
KV CACHE

Frequently Asked Questions

Clear, technical answers to the most common questions about the key-value cache mechanism in transformer-based generative models, covering its function, memory dynamics, and optimization strategies.

A KV cache is a memory mechanism in transformer-based generative models that stores previously computed Key (K) and Value (V) tensors to avoid redundant computation during autoregressive token generation. During the self-attention calculation, every new token must attend to all preceding tokens. Without a cache, the model would recompute the K and V projections for the entire sequence at each step, resulting in quadratic computational complexity. The KV cache works by appending the newly computed K and V tensors for the latest token to a stored cache in GPU memory, so the model only computes attention for the single new query token against the accumulated history. This transforms the inference from a compute-bound problem to a memory-bound one, dramatically reducing FLOPs but creating significant memory pressure, especially for long sequences and large batch sizes.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.