Two numbers, not one
A non-streaming call has one latency: the time until the whole answer arrives. A streaming call has two, and they behave completely differently.
Time to first token (TTFT) measures when the first output arrives. Network delay, queueing, input processing and caching can affect it. Measure the actual model and request sizes rather than assuming a fixed relationship between prompt length and delay.
Tokens per second is how fast text then appears. It varies with the model, request, service load and delivery path.
For an illustrative 500-token answer delivered at 60 tokens per second, generation takes about 8.3 seconds. If the first token arrives after 0.4 seconds, streaming lets the reader begin while the remaining text arrives. A non-streaming interface waits for the complete answer.
Measure TTFT, output rate and total completion time for the intended use. Compare shorter prompts and different models with the same quality checks. Reading speed and the need to inspect earlier text also affect the experience.
What streaming looks like on the wire
It is Server-Sent Events: one long-lived HTTP response, chunks separated by blank lines, each chunk a small JSON delta.
q = "Explain what a context window is in two sentences."
with client.messages.stream(
model="claude-sonnet-5-5", max_tokens=1024,
messages=[{"role": "user", "content": q},
]) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
final = stream.get_final_message()Two things people miss. Text deltas need not line up with complete words: you will receive " refu" then "nd". Never split, trim or regex a delta in isolation; accumulate first. And the final message, not the concatenated text, is where usage and stop_reason live. If you throw the stream object away you have thrown away your cost data.
What streaming costs you
This is the part the tutorials skip, and it is where real systems break.
Complete-object validation needs a complete object. Buffer a streamed JSON response before parsing and validating it as a whole. A partial parser may expose fields earlier, but those fields still need appropriate checks and a clear partial state. Transport streaming and displaying raw partial output are separate choices.
Checks need the material they inspect. If a check needs the complete answer, hold the answer until that check finishes. Checks on buffered sentences or partial fields have a narrower scope. Design the display policy around what has actually been verified.
Errors arrive mid-body. The HTTP status was 200 four seconds ago. A failure now shows up as an error event inside the stream, or as a socket that stops. Your client needs to handle a partial answer as a real state, and your UI has to say something honest about it.
Cancellation does not always stop the meter. The user closes the tab, your server aborts the fetch: whether the provider stops generating and stops charging depends on the provider. Test it, and assume you pay for what was generated.
Two infrastructure traps
Buffering proxies. A proxy or compression layer can delay delivery. Check the deployed platform’s streaming and buffering configuration, then measure the public route. For an Nginx deployment, X-Accel-Buffering: no is one setting to investigate.
Timeout policy. A long-lived stream may need both an inactivity timeout and a total time budget. Choose them for the task, provider and infrastructure. Distinguish a lack of incoming data from reaching the maximum duration your application permits.
Choose transport and display separately
A machine consumer can use streaming internally, collect the final message and then validate the result. The Python SDK supports stream.get_final_message() for this purpose, including long requests. A human-facing interface can display checked portions earlier when that suits the task. Handle incomplete streams explicitly in both cases. SDK streaming and final messages.
Check your understanding
A team streams raw JSON to a browser. What must it do before using that response as a complete schema-validated object?
Pick the one you would defend. Your answer stays private.
Your place and first choices stay on this browser. Classroom credit is recorded separately.
Every lesson here is open. Your progress is recorded once you pass the Python, from Zero, for AI exam.
Stuck on something?Ask Sayali, your AI tutor