Skip to content
Inference Academy

Prefill vs decode: why the first token is slow and the rest are fast

Every LLM request runs in two phases with opposite bottlenecks. Prefill is limited by compute, decode by memory bandwidth. Worked numbers for an 8B model on an H100 at prompt lengths from 500 to 32,000 tokens, why attention makes long prompts grow faster than linearly, how context length and batch size change decode, a latency budget for a real request, and which optimisations help which phase.

Key takeaways

  • Prefill processes the whole prompt at once and is compute bound; it sets time to first token.
  • Decode produces one token per step and is memory-bandwidth bound; it sets tokens per second.
  • An 8B BF16 model on an H100 has a decode ceiling of about 210 tokens/s for one user, and an 8K-token prompt takes about 0.3 s to prefill.
  • At 32K tokens, attention adds about half again to prefill time, so time to first token grows faster than prompt length.
  • Quantisation mostly speeds up decode; batching is nearly free for decode; chunked prefill and disaggregation stop long prompts stalling other users.

By

8 min read

Share
On this page

Send a long prompt to an LLM and you'll notice a pause before anything appears, then a steady stream of tokens. The pause and the stream are two different kinds of work, limited by two different parts of the GPU. Once you see that, a surprising amount of LLM performance makes sense.

This guide explains the two phases, works through the numbers for a real model and GPU, and maps every common optimisation to the phase it helps.

#The two phases

Prefill processes the whole prompt at once. Every prompt token goes through every layer in parallel, as one big matrix multiply per layer. It produces the KV cache for the prompt (KV cache explained) and the first output token.

Decode then produces output tokens one at a time. Each step takes the single newest token through every layer, reading the cached keys and values of everything before it.

The time until prefill finishes is the time to first token (TTFT). The time per step after that is the time per output token (TPOT), whose inverse is the tokens per second you see streaming.

#A tiny trace

Prompt: "The capital of France is". Five tokens.

  1. Prefill: all five tokens go through all layers together. Each layer computes and caches keys and values for all five. The last position's output predicts " Paris".
  2. Decode step 1: only " Paris" goes through the layers. Each layer computes its key and value, appends them to the cache (now six entries), and attends over all six. The output predicts ".".
  3. Decode step 2: only "." goes through. The cache grows to seven entries. And so on.

Prefill did five tokens' worth of work in one go; each decode step does one token's worth, but must still read every weight.

#Why they have different bottlenecks

The key quantity is arithmetic intensity: FLOPs done per byte read from memory. Every GPU has a balance point; below it, the GPU waits on memory, above it, on compute. For an H100 it's roughly 989×1012/3.35×1012≈295989 \times 10^{12} / 3.35 \times 10^{12} \approx 295 FLOPs per byte for BF16 maths.

  • Decode at batch 1 reads every weight once (2 bytes each in BF16) and does 2 FLOPs with it (a multiply and an add). That's about 1 FLOP per byte: hundreds of times below the balance point. Decode is memory-bandwidth bound.
  • Prefill of 2,000 tokens reads each weight once and uses it for all 2,000 tokens: about 2,000 FLOPs per byte, well above the balance point. Prefill is compute bound.
The roofline: where does decode sit?
Interactive
balance ≈ 295 FLOPs/byte18645124096arithmetic intensity (FLOPs per byte, log)1101001000

Achieved

3 TFLOPs

Step time

4.8 ms

Tokens/s (all)

209

Bound by

memory

Decode with BF16 weights does 2 FLOPs per weight byte per sequence, so arithmetic intensity ≈ batch size. KV-cache reads are ignored, which flatters large batches. Dense BF16 peak.

Same model, same GPU, same weights, but one phase is starved for bytes and the other for FLOPs.

#Worked numbers: an 8B model on an H100

Take a Llama-style 8B model in BF16 (about 16 GB of weights).

Decode, one user. Each token reads 16 GB. At 3.35 TB/s:

ttoken≈16 GB3350 GB/s≈4.8 ms(about 210 tokens/s)t_{\text{token}} \approx \frac{16 \ \text{GB}}{3350 \ \text{GB/s}} \approx 4.8 \ \text{ms} \quad (\text{about } 210 \text{ tokens/s})

That's a ceiling; real engines land below it.

Prefill, a 2,000-token prompt. The work is about 2×parameters×tokens=2×8×109×2000=3.2×10132 \times \text{parameters} \times \text{tokens} = 2 \times 8 \times 10^{9} \times 2000 = 3.2 \times 10^{13} FLOPs. At 989 TFLOP/s of BF16 tensor-core peak, and assuming you reach half of it in practice:

tprefill≈3.2×10130.5×989×1012≈65 mst_{\text{prefill}} \approx \frac{3.2 \times 10^{13}}{0.5 \times 989 \times 10^{12}} \approx 65 \ \text{ms}

So a 2,000-token prompt costs about as much time as 13 output tokens.

#Longer prompts grow faster than linearly

The formula above counts only the weight matrix multiplies. Attention adds work that grows with the square of the prompt length, because every token attends to every earlier token. For this model (32 layers, hidden size 4,096) with causal masking, attention adds roughly 2×layers×N2×hidden size2 \times \text{layers} \times N^2 \times \text{hidden size} FLOPs:

Prompt length Matrix multiplies only Including attention Attention's share of the extra
500 tokens about 16 ms about 16 ms 1%
2,000 tokens about 65 ms about 67 ms 3%
8,192 tokens about 0.27 s about 0.30 s 13%
32,768 tokens about 1.06 s about 1.63 s 53%

All at half of the H100's BF16 peak. At short prompts, attention is noise; at 32K tokens it adds half again. That's why long-context work leans so hard on fast attention kernels (FlashAttention explained) and prefix caching.

#Longer contexts slow decode, slowly

Each decode step also reads the cached keys and values of every earlier token: 128 KiB per token for this model in BF16. At a 32,768-token context, that's about 4.3 GB extra per step on top of the 16 GB of weights, so the step rises from about 4.8 ms to about 6.1 ms. Decode slows gently with context; prefill time rises steeply with prompt length.

#Batching changes decode, not prefill

At batch BB, a decode step reads the weights once and uses each weight BB times, so arithmetic intensity rises to about BB FLOPs per byte. Decode stays bandwidth-bound until the step holds roughly as many tokens as the GPU's balance point: around 300 tokens per step on an H100 in BF16. Below that, adding users is nearly free; above it, steps start to slow in proportion (continuous batching explained).

Prefill is already compute-bound, so batching several prompts together gains little; the work just adds up.

#A latency budget for a real request

A chat request with a 1,500-token prompt (system prompt, history and question) and a 400-token answer, on a busy server where each decode step takes about 15 ms because 60 users share the batch:

Part Time
Queueing before prefill depends on load: a few ms to seconds
Prefill of 1,500 tokens about 50 ms
First token appears (TTFT) about 50 ms plus queueing
400 decode steps at 15 ms about 6 seconds
Whole answer (end to end) about 6 seconds

For this request, decode is over 99% of the total time. For a RAG request with a 20,000-token prompt and a 100-token answer, the balance flips, and prefill dominates. Know which kind of traffic you have before choosing optimisations (serving metrics).

#What this explains

  • Long prompts slow the first token, not the stream. TTFT grows with prompt length; TPOT barely changes.
  • 4-bit models decode faster. They read a quarter of the bytes per token. Prefill speeds up less, since it's compute bound and the maths often still runs at 16 bits.
  • Batching is nearly free for decode. At batch 1 the GPU's compute is mostly idle. Serving 32 users at once reads the weights once per step for all of them, so throughput rises almost 32 times while each step gets only a little slower.
  • A GPU with more FLOPs but the same bandwidth won't decode faster. Check bandwidth for decode, FLOPs for prefill (H100 vs H200 vs B200).
  • Reasoning models are decode-heavy. Thousands of thinking tokens per answer make decode cost dominate, and make TPOT the metric users feel.
  • One long prefill can stall everyone's stream. If a 30K-token prompt arrives, the step that runs its prefill takes much longer, and every other user's next token waits. That's what chunked prefill and disaggregation solve.

#Which optimisations help which phase

Technique Prefill Decode
Weight quantisation (INT4, FP8) Some (FP8 maths is faster) Large (fewer bytes per token)
Batching more users Little (already compute bound) Large (amortises weight reads)
Speculative decoding None Large at low batch (explained)
Prefix caching Large (skip shared prefixes entirely) None
FlashAttention Large at long prompts Moderate (decode-specific kernels matter more)
KV cache quantisation Little Moderate at long context (fewer cache bytes per step)
Tensor parallelism Spreads compute across GPUs Spreads weight reads across GPUs
Chunked prefill Spreads it out Protects TPOT from long prompts
Disaggregated serving Runs on its own GPUs Runs on its own GPUs, undisturbed

#Chunked prefill and disaggregation

Because the phases interfere, engines separate them in two ways:

  • Chunked prefill splits a long prompt into pieces (say 512 or 2,048 tokens) and mixes each piece into a batch with ongoing decodes. No single step gets huge, so streams stay smooth at a small cost to TTFT.
Chunked prefill: keep running users smooth while a long prompt arrives
Animated

No chunking: the whole prompt in one step

Longest wait between tokens: –

Chunked prefill: 2,000-token chunk plus all running decodes

Longest wait between tokens: –

decode stepdecode plus prompt chunkwhole prompt at oncetoken delivered to a running user

Everyone is streaming

Several users are generating. Each step is a quick decode pass (about 6 ms), and every user gets one token per step.

Illustrative timings: a decode-only step is about 6 ms, a step with a 2,000-token chunk about 65 ms, and one step for an 8,000-token prompt about 260 ms.

  • Disaggregated prefill and decode runs the two phases on different GPUs and ships the KV cache between them, so each pool can be sized and tuned for its own bottleneck (disaggregated serving explained).

#How to measure each phase

  • Single user, local: llama-bench reports prompt processing (pp) and text generation (tg) separately. Ollama's --verbose prints "prompt eval rate" and "eval rate".
  • Server under load: benchmark tools such as vllm bench serve report TTFT, TPOT and inter-token latency percentiles. Vary the prompt length and output length separately to see each phase move (how to benchmark an LLM server).

#Common mistakes

  • Quoting one "tokens per second" number without saying whether it's prefill, decode, per user or total.
  • Load testing with short prompts when real traffic has long ones. TTFT and memory use will both surprise you later.
  • Expecting quantisation to fix TTFT. It mostly fixes decode.
  • Comparing GPUs on FLOPs alone for a chat workload dominated by decode.
  • Ignoring queueing in TTFT. Under load, waiting for a prefill slot often costs more than the prefill itself.

#FAQ

Why is the first token sometimes fast even with a long prompt? Prefix caching: if the start of the prompt was seen recently, its KV cache is reused and only the new part is prefilled.

Does streaming make generation faster? No; it shows tokens as they're made. Total time is the same, but users see progress after TTFT instead of waiting for the end.

Why do APIs charge more for output tokens? Because decode uses the GPU far less efficiently per token than prefill (cost per million tokens).

#Go deeper

The roofline lesson works through these calculations for any model and GPU, and is free. Units and vocabulary covers FLOPs, bytes and balance points from scratch. Both open the Inference Engineering course.

prefilldecodefundamentalslatencyinference-engineering

Found this useful?

Send it to someone who is learning the same thing.

Share this article

Next: learn it step by step, free

Prefill, decode and the roofline

Why decode is memory-bound, with worked numbers for real GPUs. The idea the rest builds on. A free lesson with step-through diagrams and a quiz.

Get new articles by email

One short email when a new guide is out. Unsubscribe any time.