Sub topic 2
Where latency comes from: queueing, prefill, and decoding
This chapter assumes you have called an LLM API, know what tokens are, and can read basic Python. Cost per token was covered in the previous chapter. Here the focus is time.
The three phases of a request
A request to a hosted model spends time in three places:
- Queueing. The request waits for network transfer, rate-limit checks, and a free slot on a GPU. This time grows when the provider is busy.
- Prefill. The model reads the whole prompt in one parallel pass and builds its internal state (the key-value cache). Prefill time grows with input length.
- Decoding. The model produces output one token at a time. Each new token needs a pass through the model, so decoding time grows with output length.
Two numbers that matter
Time to first token (TTFT) is the delay before the first output token arrives. It is roughly queueing plus prefill.
Total generation time is the time until the last token arrives. It is roughly TTFT plus the number of output tokens multiplied by the time per output token.
A rough model:
total ≈ queueing + prefill(input_tokens) + output_tokens × time_per_tokenTypical hosted models decode tens of tokens per second, so a 500-token answer can take several seconds even when TTFT is short. Prefill is much faster per token than decoding, so a long prompt usually adds less delay than a long answer of the same size.
| Symptom | Likely cause | First lever to try |
|---|---|---|
| Long wait before anything appears | Queueing or a very long prompt | Trim the prompt, check provider load |
| Output appears, but finishes slowly | Many output tokens | Limit and shape the output |
| Slow only at busy hours | Queueing | Retries, other regions, or batch for non-urgent work |
| Slow on every request | Model too large for the task | Choose a smaller model |
Seeing the split in code
Streaming returns tokens as they are produced. That lets you time the first chunk separately from the last one.
import time
def time_stream(chunks):
"""Measure time to first chunk and total time for any iterable of streamed chunks."""
start = time.perf_counter()
first = None
count = 0
for _ in chunks:
if first is None:
first = time.perf_counter() - start
count += 1
total = time.perf_counter() - start
return {"ttft_s": first, "total_s": total, "chunks": count}Pass it the iterator returned by your client's streaming call. Because it starts the clock before the first chunk is pulled, ttft_s includes queueing and prefill. The difference total_s - ttft_s is mostly decoding.
Common errors
- Timing a non-streaming call and calling it TTFT. Without streaming you only see the total. You cannot separate prefill from decoding.
- Blaming prompt length for slow answers. If TTFT is low and total time is high, the output is the problem.
- Measuring once. Queueing varies with load. Collect many requests and look at the median and the slowest few percent, not one run.
- Ignoring hidden output. Some models produce reasoning tokens before the visible answer. These count as decoding time and as output tokens.
Exercise
A request has a TTFT of 0.4 seconds and a total time of 12 seconds. The prompt is 300 tokens and the answer is 600 tokens. Which phase dominates, and which change is likely to help most?
Show answer
Decoding dominates, since about 11.6 of the 12 seconds come after the first token. Shortening or limiting the 600-token answer will help most. Trimming the 300-token prompt would save little, because TTFT is already small.