The KV cache is a memory buffer that stores the key and value tensors from previously processed tokens during autoregressive decoding. By caching these intermediate representations, the model avoids recomputing them for the entire sequence at each generation step, reducing the computational complexity of attention from quadratic to linear with respect to sequence length.
Glossary
KV Cache

What is KV Cache?
A memory optimization technique that stores pre-computed key and value tensors to eliminate redundant computation during autoregressive text generation.
The cache size grows linearly with batch size, sequence length, number of layers, and key/value dimensions, making it a primary bottleneck for long-context inference. Techniques like Grouped Query Attention (GQA) and Multi-Query Attention (MQA) reduce this memory pressure by sharing key-value heads across multiple query heads, trading a small amount of model quality for significant memory savings.
Core Characteristics of the KV Cache
The KV cache is a defining optimization for autoregressive transformer inference. It trades memory capacity for a dramatic reduction in redundant computation, fundamentally shaping the throughput and latency profile of large language model serving.
Linear Memory Growth
The cache size scales linearly with sequence length. For a model with L layers and H key/value heads of dimension d, storing a single token requires 2 * L * H * d elements. For a 70B parameter model using 16-bit precision, a 4096-token sequence can consume over 2 GB of memory. This growth is the primary bottleneck for long-context generation, directly limiting the maximum batch size a GPU can serve.
Autoregressive Reuse Mechanism
During generation, the model processes only the single newest token through the full network. The Key and Value projections for all previous tokens are fetched from the cache, eliminating the need to recompute them. This transforms the computational complexity of each generation step from quadratic in the total sequence length to linear in the total sequence length, as the attention operation for the new query must still be computed against all historical keys.
Prefill-Decode Disparity
Inference is split into two distinct phases:
- Prefill Phase: The input prompt is processed in parallel. All intermediate Keys and Values are computed and written to the cache. This phase is compute-bound.
- Decode Phase: Tokens are generated one by one. Each step loads the entire cache and performs a single-token matrix multiply. This phase is memory-bound, with GPU utilization often limited by the speed of high-bandwidth memory (HBM).
Quantization for Compression
To mitigate the massive memory footprint, the cached tensors can be stored at lower precision than the model's computation precision. KV cache quantization applies techniques like per-channel or per-token INT8/FP8 quantization to the Key and Value tensors before storage. A key challenge is handling outlier channels in the Key tensors, which can cause significant accuracy degradation if not handled with specialized calibration or mixed-precision strategies.
Multi-Query Attention (MQA) & GQA
Standard Multi-Head Attention (MHA) stores a separate Key and Value for each attention head. Multi-Query Attention (MQA) radically reduces the cache size by sharing a single Key-Value head across all Query heads. Grouped Query Attention (GQA) provides a practical trade-off by using a small number of Key-Value groups, achieving most of the memory savings of MQA while recovering the quality degradation observed in MQA.
KV Cache Optimization Strategies
Comparison of techniques for reducing the memory footprint and computational overhead of the key-value cache during autoregressive generation
| Feature | Multi-Query Attention | Grouped Query Attention | KV Cache Quantization |
|---|---|---|---|
Core mechanism | Shares single KV head across all query heads | Groups query heads into clusters sharing one KV head per cluster | Compresses stored KV tensors to lower bit precision |
Memory reduction vs MHA | Up to 8x reduction | 2-4x reduction (configurable) | 2-4x reduction (e.g., FP16 to INT4) |
Quality impact | Moderate degradation on recall tasks | Minimal degradation with optimal group count | Negligible at 8-bit; measurable at 4-bit |
Requires retraining | |||
Compatible with FlashAttention | |||
Hardware support | Standard GPU kernels | Standard GPU kernels | Requires INT4/INT8 tensor core support |
Typical use case | Low-latency streaming inference | Balanced throughput-quality tradeoff | High-throughput batch inference on memory-bound hardware |
Frequently Asked Questions
Clear, technically precise answers to the most common questions about the key-value cache mechanism used to accelerate autoregressive text generation in large language models.
A KV Cache (Key-Value Cache) is a memory buffer that stores the pre-computed key and value tensors from all previously generated tokens during autoregressive text generation. In a standard transformer, every new token generation step recomputes the keys and values for the entire sequence, leading to quadratic computational cost. The KV cache eliminates this redundancy by saving the key and value projections from each token as it is produced. When generating token t+1, the model only computes the query, key, and value for the new token, then appends the new key and value to the cache. The attention mechanism then operates over the cached keys and values from all prior tokens, reducing the per-step complexity from O(n²) to O(n). This is the primary optimization that makes real-time, token-by-token streaming generation feasible on modern hardware.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Understanding the KV cache requires familiarity with the attention mechanisms it accelerates and the architectural variants designed to optimize it.
Grouped Query Attention (GQA)
An attention mechanism that uses a smaller number of key-value head groups than query heads, interpolating between multi-head and multi-query attention. GQA directly reduces the KV cache size by sharing a single KV pair across multiple query heads, dramatically lowering memory bandwidth requirements during autoregressive decoding without a proportional loss in model quality.
Multi-Head Attention (MHA)
The standard attention mechanism where each head computes independent queries, keys, and values. In MHA, the KV cache stores a full set of key-value tensors per head, resulting in a memory footprint that scales linearly with the number of heads. This is the baseline against which cache-reduction techniques like GQA and Multi-Query Attention are measured.
Attention Sink
A phenomenon where the first few tokens of a sequence receive disproportionately high attention scores from all heads. These initial tokens act as a resting place for attention mass not allocated to semantically relevant tokens. Streaming LLM exploits this by always retaining the attention sink tokens in the KV cache, enabling theoretically infinite sequence lengths without cache growth.
Rotary Position Embedding (RoPE)
A position encoding method that applies a rotation matrix to query and key vectors based on their absolute positions. RoPE ensures the attention dot product depends only on relative distance between tokens. This property is critical for KV cache efficiency because it allows cached key tensors to remain valid regardless of how many new tokens are appended to the sequence.
QK and OV Circuits
The QK circuit (query-key pathway) determines which previous tokens an attention head attends to by computing attention scores. The OV circuit (output-value pathway) determines what information is copied from the source token. The KV cache stores the outputs of both the key and value projections, effectively freezing the OV circuit's input for past tokens while the QK circuit continues to operate on new queries.
PagedAttention
A memory management algorithm that partitions the KV cache into fixed-size blocks, or pages, that can be stored non-contiguously in memory. This eliminates fragmentation and enables memory sharing across sequences. PagedAttention is the core innovation behind vLLM, achieving near-zero waste in GPU memory and enabling high-throughput serving with continuous batching.

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.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us