Originally published at Trida AI.

The answer was typing itself onto the screen, sentence by sentence, the way users now expect. Then, at what looked like the last line, it stopped. No error, no spinner, nothing. The text on screen read as finished — a complete, confident paragraph — except it wasn't. The connection had died somewhere between the provider and the browser, and the final section, the one with the caveat in it, never arrived.

Nobody had done anything exotic. The team had turned on streaming because a non-streamed answer took long enough to appear that the blank screen was losing people. What they hadn't done was decide what their own code should believe about a stream that ends early. So the interface believed the simplest thing: whatever text it held when the bytes stopped was the answer.

That is the gap this post is about. Streaming changes the shape of a response from one object that either arrives or doesn't into a sequence of events that can stop anywhere. Production code has to know the difference.

What actually comes down the wire

Both major APIs stream the same way at the transport level: server-sent events over a held-open HTTP connection. You ask for it in the request (OpenAI's streaming responses guide describes setting stream=true on the Responses endpoint) and instead of one JSON body you get a flow of small, typed events, each with a name and a JSON payload.

The names differ between providers, but the lifecycle is the same shape everywhere. A start event opens the response. Delta events carry the increments: a few characters of text, or a fragment of a tool call's arguments. Then a terminal event closes the whole thing. Anthropic's version adds block-level events in between, closing each piece (a text block, a tool call) before the message itself closes. Anthropic's streaming messages documentation lays out the sequence: message_start, a series of content blocks each opened and closed, message_delta, and finally message_stop.

Two details in those docs deserve more attention than they usually get.

First, errors can arrive inside the stream. A request that would have failed with an HTTP status code in a non-streamed call can instead open successfully and then deliver an error event partway through. Anthropic gives the example of an overloaded_error, the streaming counterpart of a 529. If your handler only checks the status code at connection time, it will treat that failure as content.

Second, a stream can simply stop. No error event, no terminal event: the connection closes, or goes quiet, and your code is holding a prefix of an answer. Anthropic's stream also includes ping events, which give a handler a way to tell silence from slowness: if neither content nor pings are arriving, something is wrong. A handler that can't distinguish "finished" from "stopped arriving" will eventually present a prefix as a result.

The events in one streamed answer, in order, and the three ways the same stream can end without completing

The events in one streamed answer, in order, and the three ways the same stream can end without completing

Four ways a stream ends, and only one is success

Strip away provider vocabulary and every streamed run ends in one of four states:

  • Completed. The terminal event arrived. You have the full output, the usage figures, and the stop reason. This is the only state in which the answer exists.
  • Failed in-stream. An error event arrived after some content. The partial output is evidence about a failure, not a partial answer to be used.
  • Cut off. The connection closed with no terminal event. You cannot know whether generation finished and the ending was lost, or died mid-sentence. Treat it as failed, and say so.
  • Abandoned. Your own client went away: the user closed the tab, the request timed out at your edge, a mobile app backgrounded. Generation may still be running upstream, producing tokens nobody will read. Somebody should cancel it, or you pay for completions you throw away.

Most demo code handles the first state and lets the other three fall through to the same rendering path. The fix is mostly a matter of making the end state explicit in your own code: a stream handler should finish by recording which of the four happened, next to the run, the same way you'd record an HTTP status. When a user later asks why an answer looks truncated, "the run record says cut off at 14:02, no terminal event" is an answer. "It just stopped" is not.

Rules we hold for streamed output

Buffer tool calls; never parse fragments. Tool-call arguments arrive as partial JSON. Anthropic streams them as input_json_delta fragments, and OpenAI streams function-call arguments the same way. A fragment is not malformed JSON you can repair; it's a prefix. Parse only when the block-closing event says the arguments are complete. Acting on a half-received argument set (a transfer amount missing its last digits, a file path cut in half) is the streaming version of executing a sentence before reading its ending.

Partial text is display, not data. Showing text as it arrives is the entire point of streaming to a human. But nothing downstream should consume the deltas as if they were the answer: no storing them as the record, no feeding them to the next agent in a chain, no running checks against them. Downstream consumers get the completed blocks or the completed message. The display layer is the one place where a prefix is allowed to exist, and even there it should be labelled as still arriving until the terminal event lands.

Hold consequential actions until completion. OpenAI's streaming guide notes plainly that streaming makes moderation harder, because partial completions are harder to evaluate than complete ones. The same logic applies to your own guardrails. A check that would run on a finished answer (a policy scan, a citation check, a human approval) cannot run meaningfully on the first forty percent of one. Stream the reading experience if you like; gate the doing on the completed object.

Expect event types you don't know. Anthropic's docs warn that new event types may be added under its versioning policy and that code should handle unknown types gracefully. That's a general property of these APIs, not a quirk: a handler that throws on an unrecognised event turns a provider's minor addition into your outage. Skip what you don't understand, log that you skipped it, and keep reading.

Example: the events of one streamed run that stops mid-tool-call, with the handler action taken for each event

Example: the events of one streamed run that stops mid-tool-call, with the handler action taken for each event

Where streaming is the wrong tool

Streaming buys perceived latency. It does not buy total latency, since the last token arrives when it arrives, and it costs you a stateful handler, an end-state model, and a harder moderation problem. Plenty of agent work has no human watching the tokens land: background runs, batch jobs, eval harnesses, agent-to-agent calls. For those, take the completed message in one piece. Anthropic's SDK even offers the honest version of this pattern: stream internally to keep the connection healthy, but hand your code the assembled final message and work with that.

There's also a real argument for not streaming in high-stakes interfaces. If the answer will be acted on rather than read (a figure that feeds a payment, a recommendation a clinician signs), the typewriter effect lends the text a confidence it hasn't earned yet, because the reader starts trusting it before the caveats, which tend to live at the end, have arrived. In those interfaces we would rather show a deliberate wait and then a complete, checked answer than a fast-arriving partial one. Reasonable teams will draw that line differently. Draw it on purpose, though, not by default.

The short version

A stream is a promise that an answer is coming, delivered in pieces. Your code's job is to know, at every moment, whether the promise was kept, broken, or is still open — and to keep everything downstream of the display layer from treating an open promise as a kept one. Get the end states right and streaming is what it should be: the same answer, arriving sooner. Get them wrong and you've built an interface that presents fragments as conclusions, which is worse than making people wait.

Work with us

If your agents stream answers to people who act on them, and the partial, the cut-off, and the quietly failed cases aren't handled yet, that's exactly the kind of production detail we work on. Tell us about the deployment through Trida AI's intake and we'll talk about what finishing the job looks like.