AI Applications
Streaming LLM Responses: Why and How
Streaming shows a reply while it is being written, but only if every step between the model and the screen passes it on. Measured on a local model: when the first words appear, how a buffering proxy erases that, what happens when the user leaves, and what checks on streamed text show or withhold.
On this page · 13 sections
The support assistant from Article #18 (opens in a new tab) drafts its reply to a customer email with three questions in about six seconds on a laptop. For those six seconds, a person looking at the screen sees nothing. Then the whole reply appears at once.
Streaming changes what that person sees: the reply appears word by word as the model writes it. Many chat interfaces do this, and it’s often described as making responses faster. It doesn’t make the model faster. It changes when the first words become visible, and only if every step between the model and the screen passes them on.
This article starts from what the user sees and works back through the system:
- What the user waits for, and how much of that wait streaming can hide.
- Where streams get stuck on their way to the screen.
- What happens when the user leaves before the reply is finished.
- What streaming does to checks that need the whole reply.
The companion demo measures all four on the same support assistant.
Try to Understand It This Way
Ask a colleague a question about a long document. First they read the document, and then they answer.
You can wait for a written memo, which you get when they have finished everything. Or they can tell you the answer out loud as they work it out, starting the moment they have read the document. Talking as they go doesn’t make them read any faster. If the document is long, you still wait while they read. What you gain is the time they would have spent writing the memo.
That’s streaming. Reading the document is prefill, the model processing the prompt. Talking is decode, generating the reply token by token. Article #10 (opens in a new tab) measured both. Streaming can show the reply during decode, but it can’t hide prefill.
The analogy also misses something. Between the colleague and you there may be a messenger who waits for the whole answer before passing anything on. And if you walk away, nobody may tell the colleague to stop talking.
The Path to the Screen
The demo answers the same 40 customer emails as Article #18 and streams each reply along this path:
client ──► [proxy] ──► app server ──► Ollama (llama3.2:3b)
server-sent events newline-delimited JSON,
to the client one line per chunk
- The model server produces the reply in small chunks, usually one token each. Ollama sends them as one line of JSON per chunk.
- The app server retrieves the excerpts, builds the prompt, and passes each chunk on to the client as soon as it arrives, using server-sent events:
- a standard format for a stream of text events over one HTTP response, with the content type
text/event-stream; - each event is a few
field: valuelines followed by a blank line.
- a standard format for a stream of text events over one HTTP response, with the content type
- A proxy may sit in between, such as a reverse proxy, a load balancer, or a gateway. The demo inserts one for the second experiment.
- The client reads the bytes as they arrive, puts the events back together, and shows the text.
Every step can either pass the text on immediately or hold it. Streaming works only if all of them pass it on.
The Experiment
The inputs, the settings, and the measurements were written down and committed before any run:
- Inputs: Article #18’s 40 emails and five documents.
- The four experiments, and how each one is measured.
Nothing was tuned on these runs.
Every timestamp is recorded by the program that sees the event: the client, the app server, or the proxy. All of them use the same machine’s clock. So it is possible to say exactly when the model produced its first token and when the client showed it. Each experiment ran once in Python and once in JavaScript.
- The same everywhere: the counts, the outcomes, and every complete reply.
- Different: the timings, which vary from run to run.
The model is a small local one, and the replies are short: 17 to 218 tokens, with a median of 45 to 96 depending on the email. The numbers describe this setup, not a hosted model or long answers.
Implementation note: the exact setup
Machine: Apple M4 Pro (24 GB), macOS 26.6.2, on mains power
Runtime: Ollama 0.40.16; /api/chat with and without streaming
Model: llama3.2:3b, ID a80c4f17acd5 (Q4_K_M), temperature 0, seed 1, num_ctx 8192
Retrieval: as in Article #18; prompts of 1,300 to 3,600 tokens
Transport: server-sent events from the app server, one event per Ollama chunk,
written and flushed at once; HTTP on 127.0.0.1
Blocks: each mode is a separate block of requests, starting with the model
unloaded and a warm-up request; nothing else was connected to Ollama
Languages: Python 3.12.13 and Node.js 24.16.0, each with its own app server, proxy,
and client: identical counts, outcomes, and complete replies
Date: 2026-11-15
Before the experiments, a separate check confirmed how to read Ollama’s server log, which the third experiment uses to count the tokens the model generated.
What the User Waits For
The first question is how much of the wait streaming takes away. In each streamed request, the demo recorded when the client showed the first words and when the reply was complete.
The first words appeared a median of 0.75 seconds before the reply was complete (0.21 to 3.32 seconds). That is about 20% of the total wait: time the user had something to read while the model kept writing. Streaming doesn’t make the first word arrive before the model has processed its prompt; its benefit here is that the user can read while generation continues. This experiment doesn’t establish whether streaming changes the total generation time, for a reason explained at the end of this section.
Unless stated otherwise, the detailed timings in this article are from the Python run. The JavaScript run produced the same counts, outcomes, and complete replies, with small differences in timing; the companion demo’s README has both.
Most of the wait came before the first word:
| Emails | Prompt tokens | Reply tokens | First words | Complete |
|---|---|---|---|---|
| 1 question | 1,334 | 45 | 1.83 s | 2.32 s |
| 2 questions | 2,424 | 70 | 3.46 s | 4.28 s |
| 3 questions | 3,474 | 96 | 5.24 s | 6.41 s |
(Python medians; the JavaScript run’s first words came at 1.89, 3.60, and 5.27 seconds.) An email with more questions brings more retrieved excerpts, so its prompt is longer, and the model has to read all of it before writing the first word. Question count and prompt length rise together here, so the table can’t separate them. These replies are short, so there was little writing time for streaming to hide. A reply ten times as long would leave much more of the wait after the first word, and streaming would hide a larger share.
When nothing was in the way, the first words reached the client at most 1.2 milliseconds after the model produced them (2.5 in the JavaScript run). The app server and the transport added nothing a user could notice.
One comparison in this experiment doesn’t hold up, and it’s worth seeing why. The plan also compared a block of non-streaming requests with a block of streaming ones.
- Observed medians: non-streaming replies were complete after 3.74 seconds, streamed ones after 4.23 seconds.
- What that doesn’t mean: that streaming is slower. Ollama’s own timing for reading the same prompt was slower in the streaming block for 37 of the 40 emails, by a median of 0.4 seconds, in both the Python and the JavaScript run.
- What differed between the blocks: the first started after the machine had been idle for a minute, and the second started straight after the first. The difference is consistent with the machine slowing under sustained load, but temperature wasn’t measured, so the cause is unknown.
Two separate blocks of runs measured minutes apart are not a controlled comparison. Comparing the first word with the complete reply within the same request avoids the block-order confound: it shows how much earlier the user can see text, not whether streaming changes the total generation time.
Where Streams Get Stuck
The second question is what happens between the app server and the client. The demo put a proxy between them in three modes, for the first 20 emails:
| Proxy | Delay from the model’s first token to the client’s first words | Replies the client received in one piece |
|---|---|---|
| pass-through | 0.00 s | 0 of 20 |
| buffer everything | 0.57 s (0.19 to 3.02) | 20 of 20 |
| buffer 4,096 bytes | 0.59 s (0.19 to 0.83) | 15 of 20 |
(Python; in the JavaScript run, the two buffering delays were 0.65 and 0.70 seconds.) With buffer-everything, the model streamed and the app server streamed, but the user saw nothing until the reply was complete, and then all of it at once. With the 4,096-byte buffer, some longer replies arrived in several bursts. The app server’s records show every event sent on time. Only the client’s timestamps show when the events actually arrived.
The fixed-size buffer is the subtler case. It passes data on each time it has collected 4,096 bytes. Most of these replies were smaller than that, including the event framing, so for 15 of the 20 the buffer filled only when the reply ended. For short replies, a 4,096-byte buffer behaves exactly like buffering everything. The longest reply, about 13,000 bytes, arrived in four bursts: the first words after 2.5 seconds, the rest at 5.0 seconds. This describes replies of this length; longer replies would arrive in more bursts.
The proxy in the demo is a small program that imitates these behaviours. Real ones exist:
- nginx: its documentation says it buffers responses from the server behind it by default (
proxy_buffering on), in buffers of one memory page, 4K or 8K depending on the platform. - Turning it off: a response can disable this for itself with the header
X-Accel-Buffering: no. Then, in the documentation’s words, nginx passes the response on “synchronously, immediately as it is received”. - Compression: it can hold back output in the same way. That is a common cause worth testing, but it wasn’t measured here.
When the User Leaves
The third question: if the user closes the page halfway through, does the model stop? In the demo, the client read 20 chunks of each reply and then closed the connection. One email’s whole reply was only 17 tokens, so it never reached 20 and never disconnected. That left nine disconnects per mode.
The app server noticed each disconnect the next time it tried to write: 22 milliseconds later in the Python run, 12 in the JavaScript run. What happened next depended entirely on what it did then:
| App server | Chunks it still received from the model | Tokens the model generated (Ollama’s log) |
|---|---|---|
| keeps reading | 6 to 43 more per email | the whole reply, in each of the nine disconnects |
| closes its connection to the model | none | 23 (22 in the JavaScript run), in each of the nine disconnects |
When the app server kept reading, the model wrote every reply to the end for a user who was gone. Closing the client’s connection didn’t cancel anything by itself. Generation stopped only when the app server closed its own connection to the model, two or three tokens after the 20 the client had received.
Here the waste was small, because the replies were short and only one request ran at a time. On a busy server, every reply generated for a departed user takes compute that others are waiting for.
Streaming and Checks
The last question connects back to Article #18. Its value check compares every number, code, and email address in a reply with the retrieved excerpts and the customer’s email, and it needs the reply to be complete. Streaming shows the reply before it is complete. So when does the check run?
The demo tried three policies with the same check:
- Check, then show: collect the whole reply, check it, and show it only if it passes.
- Show, then check: stream as usual; check at the end, and if the check fails, remove the reply and show a notice.
- Check each sentence: hold each sentence until it is complete, check it, and show it if it passes; at the first sentence that fails, stop showing anything.
One of the 40 replies failed the check. It promised the customer “99.9% per month” uptime, which no document mentions. The three policies handled it very differently:
| Policy | What the customer saw | Delay before the first words, across all 40 emails |
|---|---|---|
| Check, then show | nothing | 0.79 s (0.22 to 2.84) |
| Show, then check | the whole reply, including the false promise, for 0.52 s before it was removed | 0.00 s |
| Check each sentence | “Hi Priya Nair,” and then a notice that the rest was withheld | 0.08 s (0.03 to 0.66) |
The delays are measured within each request, from the model’s first token to the first words on screen (Python run). The JavaScript run had the same outcomes, and the false promise was on screen for 0.62 seconds.
- Check, then show gives up streaming entirely: every customer waits for the whole reply.
- Show, then check keeps streaming, but the customer reads the false claim before it disappears. On a page that a customer can copy, screenshot, or act on, half a second may be enough.
- Checking each sentence cost 0.08 seconds before the first words, the time to complete the first sentence. Only the greeting was shown before the failing sentence.
It has its own limits:
- Sentence boundaries: the demo ends a sentence at “.”, “!”, or “?” followed by a space, or at a line break, and “e.g.” breaks that rule.
- Partial replies: a reply can stop halfway through, which may confuse more than a short delay would.
- What the check covers: a sentence that passes is not shown to be true. As Article #18 found, this kind of check tests where a value came from, not whether the answer is right.
One failing reply shows how the policies behave. It says nothing about how often the check would catch an error.
What Breaks
Everything here happened in the demo, except where marked:
- A buffering step erases streaming. The model and the app server both streamed, and the user still saw the reply only when it was complete.
- A fixed-size buffer behaves like a full one for short replies. 15 of 20 replies fit inside 4,096 bytes.
- Server-side measurements look fine while the user waits. The model’s first token was on time; only the client saw the delay.
- A departed user’s reply is still generated. That happens unless the app server cancels its own request to the model.
- Streamed text can’t be unshown. Retracting a failed reply removes it from the page, not from the reader’s mind.
- Prefill can’t be streamed. With long prompts and short replies, most of the wait comes before the first word.
- A comparison across separate runs can mislead. Ollama took longer to read the same prompts in the second block than in the first.
- A client that reads one network read as one event will break. Events can be split across reads, and so can a multi-byte character. The demo’s clients reassemble both; a naive client will fail on this sooner or later. That is general engineering knowledge, not a failure measured here.
Engineering Decisions
1. Measure when the user sees the words, not when the model writes them
The time to the model’s first token is a server-side number. What matters is when the first words appear on the user’s screen, and only a measurement at the client shows that. In this demo, the two were within a few milliseconds of each other without a proxy, and up to 3.0 seconds apart with one (5.0 in the JavaScript run).
2. Make every step pass the stream on, and test through the real path
Streaming from the model is not enough. Every step between it and the browser has to forward each event as it arrives:
- the app server’s framework;
- any compression;
- reverse proxies, load balancers, and CDNs.
Turn off buffering for event streams, for example with nginx’s X-Accel-Buffering: no. Then test through the same chain of proxies as production, not only against the app server directly.
3. Cancel the model’s work when the user leaves
Detect the disconnect, and close the request to the model. Closing the user’s connection doesn’t do it by itself, as the demo showed. If the model is behind a paid API, the reply generated after the user left may still be billed; check what your provider documents.
4. Decide when unchecked text becomes visible
If a check needs the whole reply, streaming forces a choice:
- delay everything;
- show and retract;
- release text in checked pieces.
Choose by what a wrong sentence costs once a reader has seen it, not by what is easiest to build.
5. Shorten the wait before the first token separately
Streaming doesn’t touch prefill. If users wait several seconds before the first word, the fixes are on the input side, and Article #10 covers the mechanics:
- fewer or shorter retrieved excerpts;
- a shorter system prompt;
- prompt caching for a prefix that is repeated.
6. Parse the stream as a stream
Decode bytes incrementally, so a character split across two reads isn’t corrupted. Split events on the blank line that ends each one, not on each network read. Treat a stream that ends without its final event as incomplete, not as a finished reply.
What to Take Away
- Streaming changes when the reply becomes visible. Here the first words appeared a median of 0.75 seconds before the reply was complete, about 20% of the wait. This run doesn’t show whether it changes the total time.
- The wait before the first word is prefill, and streaming can’t hide it. It grew from 1.8 to 5.2 seconds as the prompts grew.
- Every step between the model and the user must pass the stream on. A buffering proxy delivered all 20 replies in one piece, after the model had finished.
- Leaving doesn’t stop the model. It generated every reply to the end until the app server cancelled the request.
- Streaming makes you decide when unchecked text is shown. Checking each sentence cost 0.08 seconds; showing first left a false promise on screen for half a second.
Think About These Use Cases
-
A chat assistant behind a CDN and a load balancer: Users say replies “arrive all at once”. The model server’s logs show the first token after 300 milliseconds. Where would you look first, and what would you measure?
-
A reply that must not show personal data: A filter needs to see a whole phone number before deciding to redact it. Which of the three check policies would you use, and what happens to a number split across two chunks?
-
A report generator that runs for two minutes: The user closes the tab after ten seconds. What should stop, and how would you know it did?
-
A mobile app on a slow network: Each chunk is a separate small packet. Would you send every token as its own event, or batch them? What would you measure to decide?
-
A product manager asks to “turn on streaming to make it faster”: The prompts are 6,000 tokens and the answers are one sentence. What would you tell them, and what would you change instead?
If you want to work through one of these, take it to GPT along with this article. Ask GPT ↗️
Related Sources
- streaming-responses: The companion demo, in Python and JavaScript. It covers the app server, the three proxy modes, cancellation with Ollama’s log as evidence, and the three check policies, with the frozen settings and both runs.
- Server-sent events (WHATWG HTML Standard): The format and the browser’s
EventSourceinterface. - Using server-sent events (MDN): How a page receives an event stream, with examples of the format.
- ngx_http_proxy_module: proxy_buffering (nginx documentation): Buffering of proxied responses, on by default, and the
X-Accel-Bufferingheader. - Ollama: Streaming: Ollama’s streamed responses as newline-delimited JSON.
What’s Next?
Each reply in this article stood on its own: one email, one answer. Real conversations don’t. The customer replies, asks a follow-up, refers to “the second point”. The application has to decide what to remember and what to send back to the model each time.
In the next article, we’ll look at “Managing Conversation State in AI Applications (opens in a new tab)”: what “memory” and state really mean in an application, and what they cost.