Sub topic 10
Latency versus throughput
This chapter assumes you can call an LLM API, read basic Python, and know what a token is. It covers two speed measures that often pull in different directions, and how model size affects each.
Two different questions
Latency is how long one request takes. Throughput is how much work the system finishes per unit of time, such as requests per second or tokens per second across all users.
A system can have good throughput and poor latency. A server that batches many requests together finishes more total work, but each request waits longer. The reverse also happens: a lightly loaded server answers one request fast but sits mostly idle.
Breaking latency into parts
For a streaming chat response, latency has two parts you should measure separately:
- Time to first token (TTFT): the delay before the first token arrives. It includes network time, queueing, and processing the whole prompt.
- Inter-token time: the gap between later tokens. Output is generated one token at a time, so total generation time grows with output length.
def total_latency(ttft_s: float, tokens_per_s: float, output_tokens: int) -> float:
"""Estimated seconds for one streamed response."""
return ttft_s + output_tokens / tokens_per_s
print(f"short answer: {total_latency(0.5, 50, 100):.1f} s")
print(f"long answer: {total_latency(0.5, 50, 1000):.1f} s")
# short answer: 2.5 s
# long answer: 20.5 sThe numbers are illustrative. Measure your own.
A long prompt raises TTFT. A long answer raises total time. Chat interfaces care most about TTFT because users see text appear. Batch jobs care about total time.
How model size affects speed
A larger model does more computation per token and needs more memory to read for each one. So it usually has higher TTFT and lower tokens per second on the same hardware. A small model is often several times faster.
This is one reason the guideline "pick the smallest model that meets requirements" matters. Speed is a quality of the product, not only a cost.
Hosted versus self-hosted
With a hosted API, the provider manages batching and capacity. Your latency can vary with their load, and you may hit rate limits that cap your throughput. You can often request higher limits or pay for reserved capacity.
When you self-host, you control the trade-off. Batching more requests raises throughput and lowers cost per token, but each user waits longer. Serving tools such as vLLM batch requests continuously for this reason. Your hardware sets the ceiling.
| Goal | What to tune | Cost of the choice |
|---|---|---|
| Lower latency per request | Smaller model, shorter prompt, less batching | Lower quality or less efficient hardware use |
| Higher throughput | Larger batches, more replicas | Longer waits per request |
Practical steps
- Set a target, such as "first token within 1 second for interactive use".
- Measure TTFT and tokens per second for each candidate with realistic prompts.
- Test under expected concurrent load, not one request at a time.
- Shorten prompts and cap output length where you can.
Exercise
A model streams at 40 tokens per second with a 0.6 s time to first token. About how long does a 400-token answer take?
Show answer
0.6 + 400 / 40 = 10.6 seconds.
Exercise
Why can raising batch size improve throughput while making individual users unhappier?
Show answer
Batching lets the server process many requests at once, so more total tokens are produced per second. Each request shares the hardware and may wait for the batch, so its own latency rises.