To operate an inference service you need a precise picture of what happens between a request arriving and the last token streaming out. This lesson follows one request through tokenization, queueing, prefill, the decode loop and streaming, explains what model weights are and where they sit, and shows why prefill and decode stress the hardware in opposite ways. Almost every later topic, from batching to disaggregation, is built on that difference.
- Explain tokens and why every cost, limit and speed is measured in them
- Trace a request through tokenize, queue, prefill, decode, stream and stop
- Contrast prefill (compute-bound) with decode (memory-bound)
- Relate prompt length and output length to latency and cost
- Name the sampling and stop parameters that change how long a request runs
Tokens and weights
Models do not read characters or words; they read tokens, chunks of text from a fixed vocabulary learned by a tokenizer. Common words are often one token, rarer words split into several, and code, other languages and numbers can take many more. As a loose rule of thumb, English prose averages around three-quarters of a word per token, but it varies by tokenizer, so always count with the model's own tokenizer.
Everything in inference is measured in tokens: the context window (maximum prompt plus output), prices, rate limits, throughput (tokens per second) and memory (KV cache per token). A request has input tokens (the prompt, including system prompt, history, retrieved documents and tool results) and output tokens (what the model generates).
The weights are the model's learned parameters: large matrices for every layer. A model with 8 billion parameters in BF16 is about 16 GB of numbers that must sit in GPU memory and be read for every forward pass. Weights are fixed during inference; what changes per request is the token sequence and the per-request KV cache built from it.
The request lifecycle
- Receive and tokenize. The server applies the model's chat template to the messages and converts the text to token IDs on the CPU.
- Queue. The scheduler admits the request when there is room: KV-cache memory and a slot in the running batch. Time spent here is pure waiting.
- Prefill. The model processes all prompt tokens in one forward pass, builds the KV cache for them, and produces the first output token.
- Decode loop. One forward pass per new token: read the weights, attend over the KV cache, pick the next token, append its keys and values to the cache.
- Stream. Each new token is detokenized and sent to the client as it is produced.
- Stop. The loop ends at an end-of-sequence token, a stop string, or the max-tokens limit; the KV cache is freed.
tokens = tokenize(apply_chat_template(messages))
kv_cache, logits = model.prefill(tokens) # one pass over the whole prompt
output = []
while len(output) < max_tokens:
next_id = sample(logits, temperature=0.7, top_p=0.9)
if next_id == EOS or hits_stop_string(output, next_id):
break
output.append(next_id)
stream(detokenize(next_id))
logits = model.decode_step(next_id, kv_cache) # one pass per new tokenPrefill versus decode
Prefill handles hundreds or thousands of prompt tokens at once. Each weight read from memory is used for every prompt token, so the work is large matrix-matrix multiplication and the GPU's Tensor Cores are the limit: prefill is compute-bound. Its duration grows with prompt length, and it decides the time to first token.
Decode produces one token per request per step. Each step reads every weight but does very little arithmetic with it, so the GPU waits on memory: decode is memory-bandwidth-bound. Its duration grows with output length, and it decides the gap between streamed tokens. Batching many requests' decode steps together is what turns this wasteful pattern into efficient throughput.
| Prefill | Decode | |
|---|---|---|
| Tokens per pass | Whole prompt | One per request |
| Bottleneck | Compute (Tensor Cores) | Memory bandwidth |
| Scales with | Input length | Output length |
| User feels it as | Time to first token | Streaming speed |
| Helped by | More compute, prefix caching | Batching, faster memory, smaller weights |
A RAG app with 6,000-token prompts and 200-token answers is prefill-heavy; a writing assistant with short prompts and 2,000-token answers is decode-heavy. Profile your real input and output length distribution before tuning anything.
Parameters that change the work
- max_tokens caps output length and therefore worst-case decode time and KV-cache use. Always set it.
- Stop sequences end generation early when a delimiter appears, saving tokens.
- temperature and top_p change which token is sampled, not the cost per token; but they can change how long answers run.
- n (several completions per prompt) shares one prefill but multiplies decode work.
- Structured output constrains tokens to a schema; engines implement it efficiently, but it adds per-step overhead.
- Long system prompts are paid in prefill on every request unless the engine caches the shared prefix.