Kolkata ·  
AI engineering
foundations
deep · 5 min

Streaming

Sending tokens to the client as they come out, over server-sent events.

updated 23 Sept 2026

Send Claude the word "Hello" with one extra flag, "stream": true, and this is what comes back (trimmed a little):

event: message_start
data: {"type": "message_start", "message": {"content": [], "usage": {"input_tokens": 25, "output_tokens": 1}, ...}}

event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}

event: ping
data: {"type": "ping"}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}

event: content_block_stop
data: {"type": "content_block_stop", "index": 0}

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn"}, "usage": {"output_tokens": 15}}

event: message_stop
data: {"type": "message_stop"}

The reply is just "Hello!", spread over two lines in the middle. Everything else is bookkeeping. Let's work out why the answer arrives like this, and what our code has to do with it.

Why send it in pieces at all

Without that flag, a call to the chat API is one request and one response. The provider waits until the whole reply is written, then sends it. For a long answer, the user stares at a spinner the entire time.

But the model never writes its answer all at once anyway. It reads the prompt, then produces the reply one token at a time (the two stages are in prefill and decode). Streaming just passes each piece along as it's made.

So does streaming make the model faster? No. The whole reply takes as long as it always did. What changes is when the first words reach the person waiting.

Reading the wire

That response has the content type text/event-stream. It's server-sent events (SSE), a format from the HTML standard, and it's plain UTF-8 text. Each event is a few lines like event: ... and data: ..., ended by a blank line. Lines starting with a colon are comments.

Now look at the order of Claude's events again. It always goes:

  1. message_start, with an empty message.
  2. One or more content blocks, each a content_block_start, some content_block_delta events, then content_block_stop. A block's index is its spot in the final message, so a text block and a tool call can be tracked side by side.
  3. message_delta with the stop reason and token usage.
  4. message_stop.

ping can show up anywhere. Deltas have types: text_delta for text, input_json_delta for a tool call's arguments, thinking_delta for thinking. To show the reply, we glue the text pieces together in order.

Is this the same everywhere? The envelope is. SSE is the format all the big providers use. What's inside differs:

ClaudeOpenAI ResponsesOpenAI Chat Completions
Event namesYes (event: lines)Yes (in type)No
Text piecestext_deltaresponse.output_text.deltachoices[0].delta.content
End signalmessage_stopresponse.completeddata: [DONE]

On the older Chat Completions, token usage only shows up if you ask for it with stream_options: {"include_usage": true}. And Gemini, in a 2024 test, only sent SSE when asked with alt=sse, in much bigger chunks. So the parser is provider-specific, or you let the SDK do it.

"The browser already has an SSE client, right?"

It does. EventSource parses the format, reconnects by itself, and even tells the server where it left off with a Last-Event-ID header. Looks perfect.

Look at what it accepts, though: a URL and one option, withCredentials. No method, no body, no headers. So it can only make GET requests, and LLM APIs want a POST with a JSON body of messages. It can't call them.

What we do instead:

  • Read the stream with fetch(). Make the POST, get a reader on the body, decode bytes to text, split on blank lines, pull out the data: lines. Decode with { stream: true }, so a character split across two chunks comes out whole.
  • Put your own server in between. It calls the provider (which keeps the API key off the client) and passes the stream on. The SDKs do the parsing there, and we only write the browser side.

When the stream goes wrong

Here's where streaming code usually breaks.

A 200 doesn't mean it worked. Once the stream starts, the status code is already sent, so errors come as events inside it. On Claude, an overload that would be an HTTP 529 without streaming arrives as an error event partway through. Watch for it.

Deltas aren't words. A Claude text delta can be "ello frien". Append them and work on the accumulated text, never on single pieces.

JSON arrives broken. A tool call's arguments come in as partial strings like {"location": "San Fra. Collect them and parse when the block closes (or use a partial-JSON parser). As of 2026-09 Claude sends tool input one complete key and value at a time, so the stream can pause while the model works. Streamed structured output is the same: half an object until the end.

Usage numbers are running totals on Claude. Don't add them up.

New event types turn up over time. Skip the ones you don't know instead of crashing.

There's no resume. SSE has a mechanism for it, but these APIs don't use it. If the connection drops, keep the text you already have and send a new request asking the model to carry on. How changed with model versions: up to Claude 4.5 you put the partial text in an assistant message; from 4.6 you send it in a user message with an instruction to continue. Tool calls and thinking can't be resumed halfway, only text.

The network in between interferes. Some proxies drop connections that sit quiet too long; the SSE spec suggests a comment line about every 15 seconds to keep them open. It also warns that HTTP chunking done by a layer that doesn't know about the stream can hurt. If you proxy LLM streams through your own server, that server is one of those layers, so test streaming through your real hosting setup early.

Checking output gets harder. If you moderate replies before showing them, partial text is hard to judge. On OpenAI, the moderation scores you can request arrive only after the full output.

And sometimes we have to stream even when nobody's watching. For requests with a large max_tokens, Anthropic's SDKs require it to avoid HTTP timeouts, and give you a helper that streams underneath and hands back the finished message.

Back to that wall of events from the start. The two lines that carried "Hello" and "!" are the easy part. The rest is what lets us know when a block starts and stops, why the reply ended, and what it cost, so we read it like a small state machine: start, deltas, stop, errors, and anything unknown. Turning those deltas into something pleasant on screen is a problem of its own: streaming UI.

Further reading

  • Streaming messages, Anthropic, undated (checked 2026-09-23). The full Claude event flow, errors mid-stream, and how to recover a dropped stream.
  • Streaming API responses, OpenAI, undated (checked 2026-09-23). The Responses API's typed events, and the moderation trade-off.
  • HTML Living Standard: Server-sent events, WHATWG, living standard. The wire format itself. The authoring notes on proxies are short and worth it.
  • How streaming LLM APIs work, Simon Willison, 2024. Raw curl output from three providers side by side, plus a fetch() parser you can copy.