Streaming the Reply

streaming output

Part of: Persona Chatbot

Watch Claude in a browser: words appear one chunk at a time, like live typing. That's streaming , and it's the difference between a user thinking "is this thing broken?" and "it's thinking, and I can read along." What streaming is Streaming means receiving and displaying the reply in small chunks as it's generated , instead of waiting for the whole thing. Without it, you call the API and stare at a blank screen for several seconds, then the full reply dumps at once. With it, the first words show up almost immediately and keep flowing. The subtle part: streaming barely changes the total time. What it changes is time-to-first-token , how long until something appears. Time-to-first-token is what humans perceive as speed. How it works with the SDK The SDK gives you a streaming context manager . You loop over text chunks from stream.text stream as they arrive: Two details make this work. print(text, end="", flush=True) prints each chunk with no newline and forces it to the screen immediately; without flush, Python buffers stdout and you lose the live effect. And get final message() hands you the complete assembled reply once the stream ends. Stream to show, store to remember Streaming i

Challenge: Reassemble the Stream