Skip to content
Mastering Claude

Home / The Claude API for builders

The Claude API for builders6 minApplication

Receiving the response as it streams

Streaming mode keeps a single HTTP connection open and delivers text in fragments as it is generated, and the cumulative token count is read in the last message_delta event received, never in the final message_stop event.

A response from the Messages API normally arrives as a single block, once the model has finished generating all the text. Streaming mode changes this behaviour: the same HTTP connection stays open, and the server delivers the text in fragments, as the model produces it, without waiting for generation to finish.

The exact sequence of events

A stream begins with a message_start event, which carries a message object whose content field is still empty at this stage. For each content block generated, a content_block_start event opens the block, one or more content_block_delta events carry the text fragments received, then a content_block_stop event closes that block. Once every block in the response is finished, one or more message_delta events arrive, followed by a message_stop event that closes the connection. ping events can appear at any point in the stream without carrying any useful information, and an error event signals an incident that occurred server side.

Where the token count lives

The token count consumed by the request is cumulative from one message_delta event to the next, and it lives in the usage field of that event, never in message_stop itself. Code that waits for the final event to read this count is therefore looking in the wrong place: the last message_delta received, just before the stream stops, has to be kept, and its usage field read at that point. This nuance is what separates an implementation that shows a correct token count from one that shows a count stuck at zero or missing.

The following snippet reads these two types of events from a request built like the API's single entry point. It builds its own short request and does not depend on any external file to run.

import anthropic

client = anthropic.Anthropic()

texte_recu = ""
jetons_cumules = 0

with client.messages.stream(
    model="claude-sonnet-5",
    max_tokens=200,
    messages=[{"role": "user", "content": "Ecris un haiku sur la pluie."}],
) as flux:
    for evenement in flux:
        if evenement.type == "content_block_delta" and evenement.delta.type == "text_delta":
            texte_recu += evenement.delta.text
            print(evenement.delta.text, end="", flush=True)
        if evenement.type == "message_delta":
            jetons_cumules = evenement.usage.output_tokens

print("\njetons de sortie cumules :", jetons_cumules)

The text displays fragment by fragment as it is received, and the jetons_cumules variable only settles at its final value after the last message_delta, before message_stop ends the loop. An implementation that read this counter only on message_stop would find an event with no usage field there. The snippet also checks the exact type of the delta before reading its text, since a content_block_delta can carry a signature_delta rather than a text_delta when the model produces a reasoning block, a setting active by default on Claude Sonnet 5.

Figure 1

The sequence of a stream, from opening to stop

01
Stream opens
The message_start event arrives first, with a message object whose content field is still empty.
02
Text fragments
For each content block, content_block_start opens the block, one or more content_block_delta carry the generated text, then content_block_stop closes it.
03
Cumulative updates
One or more message_delta events then arrive, each with a usage field carrying the cumulative token count.
04
Connection ends
The message_stop event closes the stream, without itself carrying a new token count.
05
Events outside the sequence
A ping event can occur at any time with no useful information, and an error event signals an incident server side.
The figure shows the actual order of events received on the connection and the exact event that carries the cumulative token count.
Calibrate it yourself

A developer turns on streaming mode for a request sent from their machine and logs every event received in order. They count one message_start, fourteen content_block_delta, one content_block_stop, then two message_delta, just before message_stop arrives.

Write, in one sentence, what this situation establishes, and in one sentence what it does not establish.

What to remember
  • Streaming mode keeps a single HTTP connection open and delivers text in fragments, instead of waiting for the model's complete response.
  • The sequence starts with message_start, alternates blocks of content_block_start, content_block_delta and content_block_stop, then ends with one or more message_delta followed by message_stop.
  • The cumulative output token count is read in the usage field of the last message_delta event received, never in message_stop.
  • A ping event can appear at any point with no useful information, and an error event signals an incident that occurred server side.
Do this now

Write a short script that turns on streaming mode for a request you build yourself, display each text fragment as it arrives, and note in which message_delta event the cumulative token count appears just before the stream stops.

Check the source

Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.