Sub topic 2
Why Streaming Improves Perceived Latency
This chapter assumes you know basic HTTP requests and responses, what a token is, and how to call an LLM API. It covers why streaming helps and where its benefit stops.
Two latencies, not one
A model produces output one token at a time. The full response is ready only after the last token. That gives two numbers to track:
- Time to first token (TTFT): the delay between sending the request and receiving the first piece of output.
- Total generation time: the delay until the last token arrives.
Without streaming, the server holds the whole response until generation ends. The user sees nothing for the full generation time. With streaming, the server sends each token as it is produced. The user sees output after the TTFT and then watches the text grow.
Streaming does not make the model faster. Tokens are produced at the same rate either way. The total time is the same, or slightly longer because of per-event overhead. What changes is when the user first sees something.
A worked example
Suppose a model needs 0.8 seconds to start and then produces 50 tokens per second. A reply of 500 tokens takes this long:
ttft = 0.8
tokens_per_second = 50
reply_tokens = 500
generation_time = reply_tokens / tokens_per_second
total_time = ttft + generation_time
print(f"Non-streaming: first text appears at {total_time:.1f}s")
print(f"Streaming: first text appears at {ttft:.1f}s")
print(f"Streaming: last text appears at {total_time:.1f}s")The output:
Non-streaming: first text appears at 10.8s
Streaming: first text appears at 0.8s
Streaming: last text appears at 10.8sThe last token lands at 10.8 seconds in both cases. With streaming, the user starts reading at 0.8 seconds. People read at roughly 4 to 8 tokens per second, which is slower than most models generate. So a reader can keep up with the stream and finish shortly after the last token arrives.
What streaming does not fix
- Slow starts. Streaming cannot shorten the TTFT. A long prompt, a queue at the provider, or a reasoning phase before visible output all delay the first token.
- Total time. If your code needs the complete answer before it can act, such as parsing a full JSON result, streaming gains you nothing.
- Complexity. You take on parsing, error handling after the response has begun, and cancellation. The next chapters cover these.
When to stream
Stream when a person reads the output as it arrives, as in chat interfaces and writing assistants. Skip streaming for background jobs, batch processing, and calls whose output feeds directly into other code that needs the whole result.
Exercise
A model has a TTFT of 1.5 seconds and generates 40 tokens per second. For a 200-token reply, when does the first text appear and when does the last text appear, with streaming and without?
Show answer
Generation takes 200 / 40 = 5 seconds, so the last token arrives at 1.5 + 5 = 6.5 seconds in both modes. With streaming, the first text appears at 1.5 seconds. Without streaming, the first text appears at 6.5 seconds, when the whole response arrives.