Decode phase

Also known as: decode stage

On this page 7

The decode phase, also called the decode stage, is the part of large language model inference in which the model generates output tokens one at a time, each new token depending on all tokens before it. In AI infrastructure, it follows the prefill phase and usually accounts for most of the wall-clock time of a long response.

Decode is memory-bandwidth bound rather than compute bound: for every token, the accelerator must read the full model weights and the growing KV cache from memory while doing comparatively little arithmetic. This property shapes how serving systems batch requests, size GPUs, price tokens and measure latency.

How the decode phase generates tokens

The decode phase runs an autoregressive loop. After prefill has processed the prompt and produced the first token, the model feeds that token back in, computes attention against all cached keys and values, samples the next token and repeats until it emits an end-of-sequence token or reaches a length limit.

Each step processes only one new token per sequence, so matrix operations shrink to matrix-vector products. The key and value vectors of every earlier token are stored in the KV cache, which avoids recomputing them but grows linearly with sequence length and batch size.

The steps below show what happens in a single decode iteration.

StepOperationMain resource used
EmbeddingConvert the latest token into a vectorNegligible
Weight readsLoad every layer’s weights from memoryMemory bandwidth
Attention over cacheCompare the new query with cached keys and valuesMemory bandwidth
Cache appendStore the new key and value vectorsMemory capacity
SamplingSelect the next token from the output distributionNegligible
One decode iteration

Decode phase compared with the prefill phase

Prefill and decode are the two phases of LLM inference, and they stress hardware in opposite ways. Prefill processes the whole prompt in parallel and is compute bound; decode processes one token at a time and is bandwidth bound.

PropertyPrefill phaseDecode phase
Tokens per stepEntire prompt at onceOne per sequence
Arithmetic intensityHighLow
BottleneckCompute throughputMemory bandwidth
Latency metricTime to first tokenTime per output token
GPU utilizationTypically highOften low without batching
KV cache roleWritten for all prompt tokensRead in full every step
Prefill and decode characteristics

Because the phases compete for the same GPU, a long prefill can stall ongoing decodes and cause latency spikes. Chunked prefill splits long prompts into pieces interleaved with decode steps, and disaggregated serving runs the two phases on separate GPU pools so each can be tuned independently.

Why decode is memory-bandwidth bound

Decode speed is limited by how fast bytes move from memory, not by how fast the chip multiplies. A model with 70 billion parameters stored in 16-bit precision occupies about 140 GB, and generating one token for a single sequence requires reading roughly all of it.

As a rough upper bound, single-sequence decode speed equals memory bandwidth divided by bytes read per token. On an accelerator with about 3 TB/s of bandwidth, a 140 GB read gives at most around 20 tokens per second for one sequence, ignoring the cache. Real systems split such models across several GPUs with tensor parallelism, which raises aggregate bandwidth.

Batching is the main remedy. When many sequences share one weight read, the cost of loading the weights is amortized and arithmetic intensity rises. The limit then shifts to KV cache capacity, since each sequence adds its own cache.

The KV cache size can be estimated directly. For each token, the cache stores one key and one value vector per layer and per key-value head. A model with 80 layers, 8 key-value heads of dimension 128 and 16-bit storage needs about 320 KB per token, so a single sequence with 32,000 tokens of context consumes roughly 10 GB of accelerator memory. This arithmetic explains why long-context workloads reduce the maximum batch size so sharply and why cache compression matters as much as weight compression.

Mixture-of-experts models change the picture only partly. Each token activates a subset of expert weights, which lowers the bytes read per token, but large batches tend to touch most experts anyway, so total memory footprint still drives hardware sizing.

Techniques that accelerate decoding

Most inference optimizations target the decode phase because it dominates cost per token for long outputs. The table lists the most common techniques and the constraint each one relaxes.

TechniqueWhat it changesMain benefit
Continuous batchingAdds and removes sequences at every stepHigher throughput and utilization
PagedAttentionStores the KV cache in fixed-size blocksLess memory fragmentation
Speculative decodingA draft model proposes tokens verified in parallelSeveral tokens per weight read
Weight quantizationUses 8-bit or 4-bit weightsFewer bytes read per token
KV cache quantizationCompresses cached keys and valuesLarger batches and contexts
Grouped-query attentionShares key and value heads across queriesSmaller KV cache
Decode optimizations

These techniques combine. A typical production stack, such as one built on vLLM, uses continuous batching with paged KV cache, quantized weights and optional speculative decoding, while monitoring time per output token to protect interactive latency.

Metrics and trade-offs in the decode phase

Decode performance is measured mainly by time per output token (TPOT), also called inter-token latency, and by aggregate tokens per second across all users. For interactive chat, a TPOT of roughly 20 to 50 milliseconds feels fluid, since it exceeds typical reading speed.

The central trade-off is throughput versus per-user latency. Larger batches raise total throughput and lower cost, but each step takes longer, so every user sees slower streaming. Operators often set a latency target and fill batches only up to that limit, an approach measured as goodput.

  • Long contexts enlarge the KV cache and slow every decode step.
  • Memory exhaustion forces preemption, cache offloading or request rejection.
  • Output length is unknown in advance, which complicates scheduling and capacity planning.

The short version

When a chatbot answers, it first reads the whole question at once. Then it writes the answer one word piece at a time. That writing part is the decode phase.

Each new piece needs the model to look through its whole memory again. So the speed depends mostly on how fast data moves, not on raw calculating power. It is like a librarian who must walk the full library for every single word. Serving many readers on the same walk makes the trip far more efficient.

Where it makes a difference

A company runs an internal coding assistant on a 13-billion-parameter model using four GPUs. Developers complain that answers stream slowly at peak hours. Monitoring shows that time to first token is fine, but time per output token climbs to 120 milliseconds when many users are active, because long code prompts interrupt ongoing decodes.

The infrastructure team enables chunked prefill, switches weights to 8-bit quantization and adds speculative decoding with a small draft model. Average time per output token drops to about 35 milliseconds, and the same hardware serves roughly twice as many concurrent developers. The fix required no new GPUs, only a serving configuration that respects the bandwidth-bound nature of decoding.