Claude and the Anthropic SDK · Chapter 4 of 21

Streaming

By the end of this chapter you can ask Claude for a streamed reply, read the events it arrives as and the order they come in, print the answer as it is written with the SDK's stream helper, still get the whole Message at the end, and measure the time to the first words against the total time. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: The Messages API
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.

Why you need this

A family asks Sahay a question on WhatsApp and waits. With client.messages.create, nothing comes back until Claude has written the whole answer: a five-paragraph note on Opus 5.5 takes about four and a half seconds in this lab, and all that time the family sees nothing. Kamla says families give up and call the desk.

Streaming sends the reply while it is written: the first words arrive in under a second, and the rest follows. It is also the only way to ask for a very long reply: chapter 3's "Streaming is required" ValueError, for max_tokens=64000, is fixed by exactly this.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    raw_events.py: the raw events of a stream

    Send NOTE to "claude-opus-5-5" (max_tokens 1024) with stream=True, and loop over the events with for. Print the event types in order, one line per run of the same type: the type alone, or TYPE xN when it came N times in a row.

    Then print stop_reason: STOP and output tokens: N, both from the message_delta event (event.delta.stop_reason, event.usage.output_tokens).

  2. Task 2

    stream_text.py: stream the answer to the terminal

    Stream NOTE from "claude-opus-5-5" (max_tokens 1024) with "with client.messages.stream(...) as stream:", and print each piece of stream.text_stream as it arrives, with print(piece, end="", flush=True).

    When the stream ends, print a new line, then tokens: I in, O out from stream.get_final_message().

  3. Task 3

    first_token.py: time to the first token

    Define timed(model, question) -> dict: stream question from model (max_tokens 1024) with client.messages.stream, and return {"first_ms": ..., "total_ms": ..., "text": ...}: the milliseconds from just before the request to the first piece of text, and to when get_final_message() returns (time.perf_counter()), and the pieces joined.

    The check runs it at the lab model's real pace.

  4. Task 4 · interview question

    Interview: long_answer.py

    "The SDK raises 'Streaming is required...' for our max_tokens 64000 job. Why, and how do you fix it, still getting one Message?" Send REPORT to "claude-opus-5-5" with max_tokens 64000 as a stream (client.messages.stream) and take stream.get_final_message().

    Print stop_reason: STOP, output tokens: N and text: C characters (the text blocks, joined).

What you will learn

  • stream=True: the reply as server-sent events instead of one JSON body.
  • The events, in order: message_start, the content blocks and their deltas, message_delta, message_stop.
  • client.messages.stream(): stream.text_stream and stream.get_final_message().
  • Where usage is: the input in message_start, the final output in message_delta.
  • Time to first token against total time, measured at the lab's real pace.

More in The Messages API

Build "Streaming" yourself

Parchi, a Jaipur startup, helps families claim health insurance. You build Sahay, a claims assistant that reads bills and policies with Claude, and measure what caching, models and effort cost. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Claude and the Anthropic SDK are free. This one comes with the all labs plan.