Streaming the Reply
Streaming
Part of: Build a Chatbot
Watch Claude in a browser: words appear one chunk at a time, like someone typing live. This is called streaming , and it is 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 model's 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 while the entire reply generates, then it dumps all at once. With it, the first words show up almost immediately and keep flowing. Here's 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 Anthropic SDK gives you a streaming context manager . You loop over text chunks from stream.text stream as they arrive over the connection: 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 typing effect entirely. And get final message() han
Challenge: Time to First Token