Topic 2 of 5 · 5 min read
Prefill, decode and time to first token
Goal: Explain from a latency log why a long prompt delays the first token and a long answer stretches the total time.
A model call has two phases: prefill, where it reads your whole prompt at once, and decode, where it writes the answer one token at a time. This explains why a long prompt delays the first word, and why a long answer takes a long time even after it starts.
Check yourself · 1 of 3
Which phase processes the whole prompt at once?
Why it matters
Speed and price both follow these two phases. Time to first token (TTFT) is how long you wait before anything appears. After that, tokens stream at a steady rate. If you know which phase is slow, you know what to change.
The two phases
- Prefill. All the input tokens are processed together, which graphics cards do well. The work grows with prompt length. A 100,000-token document makes TTFT longer than a 200-token question.
- Decode. The model produces one token, adds it to the text, and then produces the next. Each step depends on the one before, so it cannot be done in parallel. Total time is roughly the number of output tokens divided by the tokens-per-second speed.
Many providers price input and output tokens separately, and output tokens usually cost more per token. Check your provider's price page rather than assuming a ratio.
Reading a latency log
If TTFT is high and the stream is quick, the prompt is the problem: trim it, cache it or send less. If TTFT is low but the whole call is slow, the answer is long: ask for a shorter output, set a sensible output limit, or use a faster model.
Before and after
Before: "The app feels slow. Let's switch to a bigger server."
After: "The log shows 0.4 seconds to first token and 40 seconds of streaming. The answers are 900 tokens long. We asked for an essay when a 3-line summary was enough. Shorten the requested output."
What goes wrong
- Tuning the wrong phase: shrinking the prompt when the answer is the slow part.
- Hiding the wait. Without streaming, users stare at a blank screen for the full decode time.
- Letting "think step by step" or long reasoning inflate output tokens without noticing; those tokens are billed and take time.
- Judging speed from one call. Load on the provider changes the numbers hour by hour.
Examples
Two logs, two fixes
Call A: ttft=6.2s stream=3.1s in=90000 tok out=150 tok
Call B: ttft=0.5s stream=38.0s in=300 tok out=1400 tok
Call A is slow at the start: the long input is the problem, so trim or cache it. Call B starts fast and then drags: the long output is the problem, so ask for less text or cap the output length.
Streaming does not make decode faster
A chat app turns on streaming. The total time is the same, but the first words appear in under a second, so it feels faster. The fix is perception, not speed. To cut the real time, the answer must get shorter or the model faster.
Key points
- Prefill reads the whole prompt in parallel; decode writes the answer one token at a time.
- Time to first token grows with prompt length because prefill must finish first.
- Total time after the first token is roughly output length divided by the streaming speed.
- Output tokens are usually priced higher than input tokens, so long answers cost more than long questions of the same size.
- Reading a latency log tells you whether to trim the prompt or shorten the answer.
Common mistakes
- Assuming slow calls always mean a slow model rather than a long prompt or a long answer.
- Optimising prompt length when the output tokens dominate the time.
- Ignoring that output tokens usually cost more than input tokens.
- Skipping streaming and leaving users with a blank screen.
Check yourself · 2 of 3
A call shows a first token after 0.4 seconds, then streams for 45 seconds. What is the best first fix?
Check yourself · 3 of 3
Why do long answers take so long even on a fast model?
Practice items, not counted towards your mastery.
Start here
- Optimizing LLMs for speed and memory (Hugging Face Transformers docs) (opens in a new tab)DocsFree
Covers the KV cache, quantization and faster decoding with runnable settings and plain explanations.
- Mixture of Experts Explained (Hugging Face) (opens in a new tab)ArticleFree
A clear walk through experts, routers, and why total size and active size differ.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
A vendor's own rules for cacheable prefixes, what breaks a cache hit, and how cached reads are billed.