Topic 1 of 5 · 4 min read
Measuring tokens per second and time to first token
Goal: Measure prompt and generation speed for a local model and report them in a comparable form.
Local speed is two numbers, not one: prompt processing speed (reading your input) and generation speed (writing the answer), each in tokens per second. A single "it does 40 tokens per second" hides which one you measured.
Check yourself · 1 of 3
A benchmark prints rows named pp512 and tg128. What does tg128 measure?
Why it matters
A chat with a short question is dominated by generation. A request that pastes a long file or a retrieved document is dominated by prompt processing. A model that feels fast in one case can feel slow in the other. If you want to compare machines, quants or settings, you need both numbers, measured the same way.
What the two numbers mean
- Prompt processing (pp): the model reads all input tokens in parallel. This is limited mostly by compute, so a GPU does it far faster than a CPU.
- Generation (tg, token generation): the model writes one token at a time, and every token needs the model weights read from memory. This is limited mostly by memory bandwidth, which is why a smaller quant is faster.
- Time to first token (TTFT): the wait before the first word appears. It is roughly queue time plus prompt processing time, so it grows with prompt length.
How to measure
Use a tool built for it rather than a stopwatch. In llama.cpp, llama-bench runs a fixed prompt length and a fixed output length and repeats the test:
llama-bench -m model-Q4_K_M.gguf -ngl 99 -p 512 -n 128 -r 5
It prints one row for pp512 and one for tg128, each as a mean with a standard deviation over the repetitions. The first load reads the weights from disk, so the very first run is slower than the rest. Do a warm-up run, or discard it, before you record anything.
Before and after
Weak: "Llama on my laptop gets 12 tokens/s." Which model, which quant, how long was the prompt, was the GPU used, and was it the first run?
Better: "Model X 8B Q4_K_M, all layers on the GPU, pp512 900 t/s and tg128 38 t/s, mean of 5 runs after a warm-up, llama.cpp build with CUDA." Someone else can now repeat it and compare.
What goes wrong
- Timing the first run. Disk loading and cache setup inflate it, so the number is lower than steady state.
- Mixing the two metrics. Quoting generation speed for a task that spends most of its time reading a long prompt.
- Different lengths. Comparing
pp512on one machine with a 4,000-token prompt on another. - One run only. A single run can sit on a noisy moment; the spread across repetitions tells you whether a difference is real.
Examples
A reproducible benchmark line
llama-bench -m qwen-8b-Q4_K_M.gguf -ngl 99 -p 512,4096 -n 128 -r 5
The two prompt lengths produce pp512 and pp4096 rows. Generation speed should barely change between them, but time to first token will: reading eight times more input takes roughly eight times longer when processing speed stays flat.
Reading a result
A run reports pp512 = 1,100 t/s and tg128 = 9 t/s. Prompt reading is fast, generation is slow. That pattern points at memory bandwidth or a part of the model living in system RAM, not at weak compute. Check offload next.
Key points
- Report prompt processing and generation speed separately; they have different bottlenecks.
- Prompt processing is mostly compute-bound, while generation is mostly memory-bandwidth-bound.
- Time to first token grows with prompt length because the whole prompt must be read first.
- Discard or warm up the first run, since it includes loading weights from disk.
- A comparable number names the model, quant, prompt and output length, offload, backend and repetitions.
Common mistakes
- Quoting a single tokens-per-second figure without saying whether it is prompt or generation speed.
- Recording the first run after launch, which includes loading the model from disk.
- Comparing runs with different prompt or output lengths.
- Trusting one run instead of a mean and spread over several repetitions.
Check yourself · 2 of 3
Why is the first benchmark run after starting a model usually slower than later runs?
Check yourself · 3 of 3
A user pastes a 20,000-token document into a local chat and waits a long time before any text appears. Which metric explains the wait?
Practice items, not counted towards your mastery.
Start here
- llama-bench (llama.cpp) (opens in a new tab)DocsFree
The standard tool for repeatable prompt and generation speed numbers, with options for offload, flash attention and repetitions.
- llama.cpp server README (opens in a new tab)DocsFree
Lists the flags for context size, GPU layers, KV cache types, flash attention and draft models.
- llama.cpp (opens in a new tab)ToolFree
The runtime behind many local tools; build instructions show which backends (CUDA, Metal, Vulkan) a binary includes.