Sub topic 2
Why streaming improves perceived latency
This chapter assumes you know basic HTTP request and response concepts, can read Python and JavaScript, and understand what a token is. It covers why streaming makes an LLM application feel faster, what it does not change, and how to measure the difference. Later chapters cover the wire format and the code.
The problem: a long wait with nothing on screen
A language model produces its answer one token at a time. A typical answer of 400 tokens, generated at 50 tokens per second, takes about 8 seconds. Without streaming, the server holds the whole response until the last token exists. The user sees a spinner for those 8 seconds and then the full text appears at once.
With streaming, the server sends each piece as soon as it is generated. The user sees the first words after a fraction of a second and reads while the rest is produced. The model does the same work in the same time. Only the delivery changes.
Two kinds of latency
Latency here means the time between a user's action and what they see. Two different clocks matter.
- Time to first token (TTFT) is the time from sending the request until the first generated token arrives at the client.
- Total generation time is the time from sending the request until the last token arrives.
Perceived latency is how long the wait feels. It tracks TTFT much more than total time, because once text is moving the user is reading, not waiting.
What streaming changes and what it does not
Streaming moves the moment the user first sees output from "after the last token" to "after the first token". It does not make the model produce tokens faster.
| Metric | Without streaming | With streaming |
|---|---|---|
| Time to first visible output | About total generation time | About TTFT |
| Total generation time | T | About T, often slightly more |
| Tokens per second from the model | Unchanged | Unchanged |
| Cost per request | Same tokens billed | Same tokens billed |
| Client and server complexity | Low | Higher |
Total time is often slightly higher with streaming. Each event carries framing overhead, and the client does more work per token. For a normal chat response the difference is small, but it exists.
A worked timeline
Suppose a request has these properties:
- Network and queueing before generation starts: 0.3 s.
- Prompt processing (the model reads your input): 0.5 s.
- Generation: 400 tokens at 50 tokens per second, which is 8 s.
The first token arrives at about 0.3 + 0.5 + 0.02 = 0.82 s. The last token arrives at about 8.8 s.
| Approach | User first sees text | User sees complete answer |
|---|---|---|
| Non-streaming | About 8.8 s | About 8.8 s |
| Streaming | About 0.8 s | About 8.8 s |
The user waits under one second for the start instead of nearly nine. Because people read at roughly 4 to 5 words per second, and 50 tokens per second is about 35 words per second, the stream outpaces the reader. The reader is never starved after the first words appear.
Why the wait feels shorter
Three effects explain the difference.
- Feedback. Text appearing is proof the system is working. A spinner gives no information about progress, so uncertainty makes the wait feel longer.
- Overlap. Reading and generation happen at the same time. The user finishes reading shortly after the model finishes writing, instead of waiting and then reading.
- Early exit. If the first sentences show the answer is off track, the user can stop and rephrase without paying for the rest. Cancellation is covered in a later chapter.
What dominates TTFT
Streaming only helps as much as TTFT is small. TTFT is the sum of several parts, and streaming does not shorten any of them.
- Network round trip and TLS setup.
- Queueing at the provider when demand is high.
- Prompt processing. A longer prompt takes longer to process before the first token. A prompt of 50,000 tokens can add seconds.
- Your own backend work, such as retrieval or database calls before the model call starts.
If your TTFT is 6 seconds because of slow retrieval, streaming the model output will not fix the first 6 seconds. Measure before you assume streaming is the answer.
Measuring it
Measure both numbers on the client, not only on the server, because the user's experience includes the network and any proxies. The following Python script times a streamed request against a generic OpenAI-compatible endpoint. Replace MODEL_NAME with a model you can use.
import time
from openai import OpenAI
client = OpenAI()
start = time.perf_counter()
first_token_at = None
count = 0
stream = client.chat.completions.create(
model="MODEL_NAME",
messages=[{"role": "user", "content": "Write two paragraphs about rivers."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
if first_token_at is None:
first_token_at = time.perf_counter()
count += 1
end = time.perf_counter()
print(f"Time to first chunk: {first_token_at - start:.2f} s")
print(f"Total time: {end - start:.2f} s")
print(f"Content chunks: {count}")The count is of content chunks, which usually but not always equals tokens. Treat it as an approximation. Run the script several times, because both numbers vary with load. Report percentiles such as p50 and p95 rather than a single run.
Edge cases and common mistakes
- Streaming that is not really streaming. A proxy or hosting platform may buffer the response and deliver it all at once. The user then sees no benefit, even though your code is correct. A later chapter covers this.
- Very short answers. If the answer is 10 tokens long, the whole response takes a fraction of a second. Streaming adds complexity for almost no gain.
- Tasks that need the whole answer. If your code must parse a full JSON object before doing anything, the user gains nothing from partial text. You may still stream for a progress signal, but the real value is small.
- Rendering that is too aggressive. Updating the page on every token can cause flicker or heavy layout work. Batching updates, for example once per animation frame, keeps the interface smooth.
- Moderation and post-processing. If you must check the whole output before showing it, streaming exposes unchecked text. Decide whether to delay display, filter in windows, or accept the risk.
- Measuring only total time. A benchmark that reports only total duration will show streaming as slightly worse. Track TTFT separately.
When to stream
Stream when the response is long enough for the wait to be noticeable, the user reads it directly, and the interface can show partial text sensibly. Chat, writing assistants, and code explanation fit well. Batch jobs, short classification calls, and calls whose output feeds straight into code often do not.
Exercise
A request takes 0.9 s before the first token, and then the model generates 300 tokens at 60 tokens per second. Compute the time to first visible output and the time to the complete answer, with and without streaming. Ignore overhead. Then say what streaming changed.
Show answer
Generation takes 300 / 60 = 5 s, so the complete answer arrives at about 0.9 + 5 = 5.9 s in both cases. Without streaming, the first visible output is also at about 5.9 s. With streaming, it is at about 0.9 s (plus one token, 1/60 s, so roughly 0.92 s). Streaming changed when the user first sees text, not when the answer completes.
Exercise
Your streamed responses still show a 7 second blank wait before any text. Name three places to look that streaming does not improve.
Show answer
Any three of: prompt processing time for a very long prompt, queueing at the provider, network and connection setup, your backend work before the model call (such as retrieval), hidden model reasoning before the first visible token, and a proxy that buffers the response.