Home / The Claude API for builders
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.
The sequence of a stream, from opening to stop
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 this establishes: This log establishes that the response text arrived in several distinct fragments and that the cumulative token count was updated at least twice before the stream stopped.
What this does not establish: It does not establish the exact final token count, since that value is read in the usage field of the last message_delta and not in the simple count of events received.
The three most common miscalibrations
- Too broad This log proves that every stream from the API always produces exactly fourteen text fragments before stopping.
- Too narrow This log says nothing about how streaming mode works, since a single request never allows a conclusion about the protocol.
- Off target This log shows that the HTTP connection between the machine and the API stayed stable throughout the exchange.
- 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.
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.
Every datable claim in this lesson links here to the public text behind it. A source that does not open proves nothing.
- Anthropic, streaming mode for the Messages API consultée le 2026-09-02