An AI chat reply fills in word by word because the app shows the text as it arrives, not because the model types. Making the reply and delivering it are two separate jobs, and delivery is the app's choice.
By default, the OpenAI API generates the whole output first and sends it back in one HTTP response. With long outputs that wait adds up. Turn streaming on and the API sends the reply as a series of small events while it is still being produced, so the app can print the beginning before the end exists. In the Claude Messages API the setting is "stream": true. In the OpenAI Responses API it is stream=True. Both use server-sent events, a text format for a long-lived HTTP response where a blank line ends each event.
One thing to get right: a streamed piece is not always a whole word. The Claude docs sample carries a text_delta of "ello frien". Treat each piece as a string to append, exactly as it came, with no added spaces. Tool input is split the same way, as partial JSON strings you accumulate and parse once content_block_stop arrives.
So how does the app know the reply is finished? The completion event is provider specific. A Claude Messages API stream runs in this order: message_start, then content blocks (content_block_start, any number of content_block_delta, content_block_stop), then one or more message_delta events, then a final message_stop. The stop_reason in message_delta says what happened: end_turn means the turn finished, tool_use means the model wants a tool, max_tokens means the reply was cut at the token limit. The OpenAI Responses API names its events differently, with response.completed among the common ones.
A client also has to read the stream correctly. One network read can end halfway through an event or hold three whole events, so parse line by line: skip lines starting with a colon, split other lines at the first colon, and dispatch on a blank line. A frame with no data dispatches nothing. Decode UTF-8 as a stream, because a character can split across two reads.
Then there is the middle of the route. A reverse proxy that buffers responses can hold the bytes back, so the text arrives in lumps or all at once. Nginx has proxy_buffering on by default. To find where the delay sits, log the time of the first content_block_delta on your server and again in the client, with both clocks synchronized.
The failure to test first is a partial reply shown as finished. If the connection closes before message_stop, the reply stops mid-sentence and looks done, so mark it incomplete and offer a retry or continuation. An error event mid-stream means something failed, so show the error next to the partial text and never mark it done.
Our take: when someone is waiting for the answer, seeing the first words early is often worth the parser and the extra handling. If nobody watches the text appear, let the SDK collect the whole message.
Read the full article on System Design Simple: https://systemdesignsimple.com/why-ai-replies-in-pieces-streaming/
Top comments (0)