The decode phase is the autoregressive, sequential generation stage of a transformer language model where it predicts the next token based on the entire preceding context. During this phase, the model relies heavily on its KV cache to avoid recomputing attention over past tokens, making each step a memory-bound operation focused on reading cached keys and values and performing a small amount of new computation. This iterative process continues until a stopping condition, like an end-of-sequence token, is met.
Glossary
Decode Phase

What is the Decode Phase?
The decode phase is the core token-by-token generation stage in transformer inference, directly responsible for the latency and throughput of text generation.
Optimizing the decode phase is the primary focus of inference latency reduction. Techniques like continuous batching, PagedAttention, and KV cache quantization are designed to maximize the efficiency of this phase by improving GPU utilization, reducing memory footprint, and minimizing I/O bottlenecks. The performance is often measured in metrics like time-to-first-token (TTFT) and tokens-per-second, which are dominated by the prefill and decode phases, respectively.
Key Characteristics of the Decode Phase
The decode phase is the token-by-token generation stage where the model uses its KV cache to efficiently compute attention over cached context and predict the next token autoregressively. Its performance is dominated by memory bandwidth constraints.
Autoregressive Token Generation
The decode phase operates autoregressively, meaning each generated token becomes the input for predicting the next. For a sequence of length L, this requires L serial forward passes. The model's output logits for the final token are used to sample the next token (e.g., via top-p or temperature sampling). This sequential dependency creates a fundamental latency bottleneck, as each step must wait for the previous one to complete, making techniques like speculative decoding critical for acceleration.
KV Cache Utilization
The primary optimization of the decode phase is the use of the KV cache. After the prefill phase computes keys and values for all prompt tokens, the cache is populated. During decoding:
- The query vector is computed only for the new token.
- The key and value vectors for all previous tokens are read from the cache.
- The attention scores (Query • Key^T) are computed, followed by the weighted sum of values. This avoids recomputing keys and values for the entire growing sequence, transforming the compute complexity of attention from O(n²) to O(n) per step, but creates a memory-bound workload where performance is limited by cache read speed.
Memory-Bound Performance Regime
Decoding is typically memory-bound, not compute-bound. The arithmetic intensity (FLOPs per byte of memory access) is very low because the primary operation is reading the large KV cache for each new token. Performance is limited by the GPU's memory bandwidth (e.g., H100: ~2 TB/s). The latency for a single decoding step can be approximated by: KV_Cache_Size / Memory_Bandwidth. Optimizations focus on:
- Reducing the size of the KV cache via Multi-Query Attention (MQA) or Grouped-Query Attention (GQA).
- Improving memory access patterns via PagedAttention.
- Using lower precision like KV cache quantization (FP8, INT8).
Continuous Batching & Scheduling
In production serving systems, the decode phase is executed with continuous batching (also known as iterative batching or dynamic batching). Unlike static batching, this allows:
- New requests to join the batch as others are still generating.
- Completed sequences to exit the batch, freeing resources.
- The batch size and composition to change every decoding step. This maximizes GPU utilization by keeping the hardware busy despite the variable and sequential nature of decoding. Efficient schedulers must manage KV cache memory allocation and context swapping across this dynamic request pool.
Context Window Limitations & Management
The maximum sequence length a model can process is defined by its context window, which is physically constrained by GPU memory capacity for the KV cache. For a model with n_layers, n_kv_heads, d_head, and precision b bytes, the cache size per token is: 2 * n_layers * n_kv_heads * d_head * b. Managing long contexts requires techniques like:
- KV Cache Eviction: Removing less important cached tokens (e.g., LRU).
- Sliding Window Attention: Only caching the most recent
Wtokens. - KV Cache Offloading: Moving older cache blocks to CPU/NVMe.
- Attention Sinks: As in StreamingLLM, keeping initial tokens to stabilize attention for infinite generation.
Hardware & Kernel Optimization
Low-level kernel optimizations are essential for decode phase performance. This includes:
- Operator Fusion: Combining the layer normalization, linear projection, and activation functions into a single kernel to reduce memory reads/writes.
- FlashAttention: While primarily for training, its principles inform decode-optimized attention kernels that minimize HBM accesses.
- TensorRT-LLM / vLLM: SDKs that provide highly optimized, fused decoding kernels and efficient PagedAttention memory management.
- Mixed Precision Inference: Using FP16/BF16 for compute while potentially quantizing the KV cache to INT8, requiring efficient dequantization kernels.
Decode Phase vs. Prefill Phase
A comparison of the two primary computational stages in autoregressive transformer inference, highlighting their distinct roles, performance characteristics, and system resource demands.
| Feature / Characteristic | Prefill Phase | Decode Phase |
|---|---|---|
Primary Function | Process the entire input prompt in parallel to compute the initial KV cache. | Generate output tokens sequentially, one per step, using the cached context. |
Computational Pattern | Massively parallel (across all prompt tokens). | Inherently sequential and autoregressive. |
Dominant Bottleneck | Compute-bound (FLOPs for matrix multiplications). | Memory-bound (bandwidth for reading the KV cache). |
KV Cache Role | Populated. The cache is written for all prompt tokens. | Utilized and extended. The cache is read for attention and appended with each new token. |
Latency Profile | Single, large operation. Latency scales with prompt length. | Many small, repeated operations. Latency scales with output length. |
Throughput Optimization | Large batch sizes to saturate GPU compute units. | Continuous batching to keep the GPU busy despite sequential steps. |
Typical GPU Utilization | High (compute cores active). | Often lower (frequent memory stalls). |
Key Optimization Techniques | Operator fusion (e.g., FlashAttention), efficient attention masking. | KV cache compression (quantization), PagedAttention, speculative decoding. |
Frequently Asked Questions
The decode phase is the token-by-token generation stage of transformer inference. This FAQ addresses common technical questions about its mechanics, optimization, and relationship to the KV cache.
The decode phase is the sequential, autoregressive stage of transformer inference where a language model generates output one token at a time, using its previously generated output as part of the input for the next step.
During this phase, the model's forward pass is dominated by reading the KV cache—the stored key and value tensors from all previous tokens—to compute attention and predict the next token's logits. This phase is inherently sequential and memory-bandwidth bound, as each new token generation depends on the full cached history, making its optimization critical for low-latency text generation.
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 decode phase is fundamentally defined by its interaction with the KV cache. These related concepts detail the specific mechanisms, constraints, and optimizations that govern this critical stage of transformer inference.
Prefill Phase
The compute-intensive initial stage of transformer inference where the model processes the entire input prompt in parallel. This phase populates the initial KV cache with the key and value tensors for all prompt tokens, enabling the subsequent, efficient autoregressive decoding. It is characterized by high FLOP utilization but occurs only once per sequence.
KV Cache
A memory buffer that stores the computed key (K) and value (V) tensors from previous tokens during the autoregressive decode phase. Its primary function is to eliminate redundant computation: instead of recomputing K and V for all prior tokens on each new decoding step, the model simply reads them from this cache. Its size grows linearly with sequence length and batch size, making it the primary bottleneck for long-context inference.
Continuous Batching
An inference scheduling paradigm that dynamically groups incoming requests into a shared batch. It is essential for high-throughput decode phases because it allows:
- New requests to join the batch as others are still processing.
- Completed sequences to exit the batch immediately, freeing resources.
- The KV cache for each request to be managed independently within the same batch. This maximizes GPU utilization compared to static batching, where the entire batch must finish before a new one can start.
PagedAttention
A memory management algorithm that organizes the KV cache into non-contiguous, fixed-size blocks or 'pages'. Popularized by vLLM, it solves the problem of internal fragmentation caused by variable-length sequences in continuous batching. Key benefits:
- Enables efficient sharing of physical memory across sequences.
- Allows for near-zero waste in KV cache memory.
- Supports advanced features like cache sharing for duplicated prompts and efficient cache eviction.
Multi-Query & Grouped-Query Attention
Attention variants designed to reduce the memory footprint of the KV cache.
- Multi-Query Attention (MQA): All query heads share a single key head and a single value head. This drastically reduces the size of the cached K and V tensors.
- Grouped-Query Attention (GQA): A hybrid where queries are grouped, and each group shares a single key and value head. It offers a tunable trade-off between the cache efficiency of MQA and the model quality of standard Multi-Head Attention. Both are critical for memory-efficient decoding of large models.
Speculative Decoding
An inference acceleration technique that reduces the number of costly autoregressive steps performed by the large target model. A small, fast draft model (or a simpler method) runs ahead during the decode phase to propose a sequence of candidate tokens. The large target model then verifies these tokens in a single, parallel forward pass. If most drafts are accepted, the effective latency per output token is reduced. It directly optimizes the time-to-first-token and throughput of the decode phase.

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