The KV cache is a memory buffer that stores the previously computed Key and Value tensors during the autoregressive decoding phase of a transformer model. By caching these intermediate representations, the model avoids recomputing them for all prior tokens at each new generation step, transforming the quadratic computational complexity of attention into a linear cost per step.
Glossary
KV Cache

What is KV Cache?
A memory buffer that stores computed key and value tensors from previous tokens during autoregressive generation to prevent redundant computation.
This mechanism is essential for achieving interactive latency in large language models. Without a KV cache, generating the N-th token would require a full forward pass over all N-1 preceding tokens. The cache grows linearly with sequence length, making its memory footprint a primary bottleneck for long-context serving, which is addressed by techniques like PagedAttention and Multi-Query Attention.
Core Characteristics of KV Cache
The KV Cache is a defining architectural optimization for autoregressive transformers. It trades memory capacity for a dramatic reduction in redundant computation, fundamentally enabling the real-time generation of long sequences.
Mechanism of Autoregressive Caching
During autoregressive decoding, each new token must attend to all previous tokens. Without a cache, the Key and Value projections for the entire sequence would be recomputed at every step, resulting in quadratic complexity. The KV Cache stores these computed tensors in GPU memory.
- Write Phase: When a new token is generated, its Key and Value tensors are computed and appended to the cache.
- Read Phase: The attention mechanism reads all historical Keys and Values directly from the cache, computing only the single new Query vector.
- Result: Reduces the computational complexity of a single decoding step from quadratic to linear relative to the sequence length.
Memory Capacity and Bottleneck
The cache size scales linearly with batch size, sequence length, number of layers, and hidden dimension. For large models, this creates a significant memory bottleneck.
- Calculation:
cache_size = 2 * batch_size * seq_len * num_layers * hidden_dim * precision_bytes - Example: A 70B parameter model serving a single 4096-token sequence can require over 2.5 GB of memory just for the KV Cache.
- Impact: Memory bandwidth, not compute, often becomes the primary limiter of throughput, a phenomenon known as memory-bound inference.
PagedAttention and Fragmentation
Traditional KV Caches allocate one large contiguous block of memory per sequence, leading to internal fragmentation and limiting total throughput. PagedAttention solves this by partitioning the cache into non-contiguous blocks.
- Block-Based Management: Memory is allocated in fixed-size blocks, similar to virtual memory paging in operating systems.
- Zero Waste: Blocks can be shared across sequences for parallel sampling or beam search, eliminating redundant storage.
- Result: This enables a serving system to handle a significantly higher number of concurrent requests by dynamically mapping logical KV caches to physical GPU memory blocks.
Prefix Caching for Shared Prompts
Many applications use a long, static system prompt or few-shot examples. Prefix Caching identifies and reuses the KV Cache for these shared prefixes across multiple distinct requests.
- Mechanism: The serving engine hashes the prompt prefix tokens. If a match is found in a persistent cache, the pre-computed Keys and Values are loaded instantly.
- Benefit: Eliminates the computationally expensive prefill phase for the shared portion, drastically reducing Time To First Token (TTFT) and freeing compute for unique suffixes.
- Use Case: Critical for chatbots with large system instructions or Retrieval-Augmented Generation (RAG) applications with a fixed retrieved context.
Trade-off: Latency vs. Throughput
The KV Cache embodies a fundamental trade-off in inference serving. Saving compute by caching tensors consumes scarce high-bandwidth memory (HBM).
- Latency-Optimized: Keeping the entire cache in GPU HBM ensures the fastest possible token generation speed.
- Throughput-Optimized: Offloading less-frequently accessed cache blocks to CPU RAM or NVMe storage allows for serving extremely long contexts or more concurrent users, at the cost of higher per-token latency.
- Quantization: Applying KV Cache Quantization (e.g., FP8 or INT8) is a common strategy to compress the cache, increasing effective memory capacity with minimal accuracy loss.
Multi-Query and Grouped-Query Attention
Architectural changes to the attention mechanism directly reduce the size of the KV Cache. Multi-Query Attention (MQA) uses a single Key-Value head shared across all Query heads.
- MQA: Drastically shrinks the cache size, but can lead to quality degradation.
- Grouped-Query Attention (GQA): A compromise where a small number of Key-Value heads (e.g., 4) serve a larger number of Query heads (e.g., 32).
- Impact: GQA achieves near-MHA quality while significantly reducing the memory footprint of the KV Cache, enabling larger batch sizes and longer context windows on the same hardware.
KV Cache vs. Related Caching Mechanisms
A technical comparison of the KV Cache against other caching strategies used during large language model inference to reduce redundant computation and memory overhead.
| Feature | KV Cache | Prefix Caching | Semantic Caching |
|---|---|---|---|
Cached Data Structure | Key and value tensors per token | KV Cache for shared prompt prefixes | Embedding vectors or raw text of prompts |
Primary Objective | Eliminate recomputation of previous tokens | Eliminate recomputation for identical prompt starts | Eliminate inference entirely for semantically similar queries |
Granularity | Per-token within a single sequence | Per-prefix across multiple requests | Per-request across a session or fleet |
Storage Location | GPU VRAM (high-bandwidth memory) | GPU VRAM or CPU RAM | External database or in-memory store (e.g., Redis) |
Exact Match Required | |||
Cache Hit Condition | Autoregressive step > 1 | Identical token sequence at prompt start | Vector similarity above threshold |
Memory Overhead | Linear with sequence length (O(n)) | Linear with prefix length, shared across users | Constant per entry, scales with number of entries |
Risk of Staleness |
Frequently Asked Questions
Explore the mechanics of the Key-Value cache, the critical memory buffer that makes autoregressive text generation computationally feasible by eliminating redundant matrix multiplications.
A KV Cache is a memory buffer that stores the computed Key and Value tensors from previous tokens during autoregressive generation. In a standard Transformer, the attention mechanism computes Query, Key, and Value projections for every token. Without a cache, each new token generation would require recomputing these projections for the entire preceding sequence, resulting in quadratic computational complexity. The KV Cache works by saving these pre-computed Keys and Values in GPU memory. When generating token t+1, the model only computes the Query for the new token and retrieves all previous Keys and Values from the cache, concatenating them for the attention calculation. This transforms the generation step from O(n²) to O(n), dramatically accelerating inference.
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
The KV cache is a critical inference optimization that sits at the intersection of memory management, attention mechanisms, and serving infrastructure. These related concepts define how the cache is structured, scaled, and accelerated in production deployments.
Prefix Caching
An optimization that stores and reuses the computed KV cache for a shared prompt prefix across multiple generation requests, avoiding redundant computation of identical token sequences.
- System Prompt Reuse: Caches the KV tensors for static system instructions applied to every request
- Few-Shot Example Sharing: Reuses cached examples across different user queries
- Radix Tree Matching: Efficiently identifies the longest common prefix among incoming requests
Prefix caching can reduce Time To First Token (TTFT) by up to 90% when serving applications with long, shared system prompts or conversation histories.
Grouped-Query Attention (GQA)
An attention mechanism that uses a single set of key-value heads for multiple query heads, directly reducing the size of the KV cache. GQA balances the memory efficiency of multi-query attention with the quality of multi-head attention.
- Configuration: Typically 8 query heads share 1 KV head (e.g., Llama 2 70B)
- Cache Reduction: Reduces KV cache size proportionally to the query-to-KV head ratio
- Quality Retention: Maintains >99% of multi-head attention quality on standard benchmarks
GQA is the default attention mechanism in Llama 2, Llama 3, Mistral, and Gemma models, making it a foundational design choice for modern LLM inference.
Multi-Query Attention (MQA)
An extreme variant of attention where all query heads share a single key-value head, minimizing the KV cache to its theoretical minimum. This trades some model quality for maximum memory efficiency during autoregressive decoding.
- Single KV Head: Regardless of the number of query heads (e.g., 96 query heads, 1 KV head)
- Memory Savings: Reduces KV cache size by the number of query heads compared to multi-head attention
- Quality Tradeoff: Small degradation on tasks requiring fine-grained attention differentiation
MQA was popularized by Google's PaLM and is used in models where inference speed and memory constraints outweigh marginal quality improvements.
Flash Attention
An IO-aware exact attention algorithm that minimizes high-bandwidth memory (HBM) reads and writes by tiling the attention computation. While not a caching technique itself, FlashAttention dramatically accelerates the computation that populates the KV cache.
- Tiling: Decomposes the attention matrix into blocks that fit in SRAM, avoiding materializing the full N×N matrix in HBM
- Recomputation: Recomputes attention statistics in the backward pass rather than storing them, reducing memory overhead
- Speedup: Achieves 2-4x wall-clock speedup over standard attention implementations
FlashAttention-2 and FlashAttention-3 extend these principles to achieve up to 70% of theoretical peak FLOPs on H100 GPUs, making it the de facto standard attention kernel in modern inference stacks.
Continuous Batching
A serving technique that dynamically appends new sequences to a running batch instead of waiting for the entire batch to complete before starting new requests. This maximizes GPU utilization by keeping the batch full at all times.
- Iteration-Level Scheduling: New requests join the batch at the next token generation step
- KV Cache Coexistence: Multiple sequences at different generation stages share GPU memory simultaneously
- Throughput Gains: Achieves 5-10x higher throughput compared to static batching
Continuous batching is a core feature of modern inference engines like vLLM, TensorRT-LLM, and TGI, directly complementing PagedAttention's memory management for maximum serving efficiency.

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