Guide

Prefill and decode

One token at a time

A model writes in a loop: make one token, add it to the text, run again. This is autoregressive generation. One run through every layer is a pass, and each new output token needs one more pass. A 200-token answer takes 200 passes.

Prefill

The first pass, prefill, reads the whole prompt at once. All the prompt's tokens are worked on side by side, because they are all known already. That is a lot of math in one go, so prefill is compute-bound: the GPU's math power is the limit. Prefill ends by making the first output token.

Decode

Every later step is decode, and it makes one new token. One token is little math, but each step must still read all the weights from VRAM, about 14 GB for a 7B model. Reading memory is the limit, so decode is memory-bound. It is like driving to a warehouse to fetch one screw.

Two different limits

The two phases hit different bottlenecks: prefill is limited by math, decode by memory. So "tokens per second" can tell two stories. Prompt tokens read in prefill can be counted in thousands per second. One user's output tokens in decode come far slower. Always ask which tokens a number counts.

What makes each one longer

  • A long prompt makes prefill longer, so the time to first token grows.
  • A long answer means more decode steps, so the stream runs longer.

Summing up a long report is mostly prefill. Writing a long story from a short idea is mostly decode.

Remember

  • One pass per output token.
  • Prefill: whole prompt at once, compute-bound.
  • Decode: one token per step, reads all weights, memory-bound.
  • Ask which tokens a "tokens per second" number counts.
  • Long prompt, late first token; long answer, long stream.