Chapter 4 · Part 2

Stream the reply

When you use ChatGPT or Claude, the answer types itself out word by word. That's streaming — the model sends each token the moment it's generated instead of making you wait for the whole reply. It's the difference between a responsive app and one that feels frozen. The SDK makes it a one-line change.

From create to stream

Instead of client.messages.create(...), which blocks until the full reply is ready, use client.messages.stream(...) as a context manager and loop over its text_stream. Update the chat function in chatbot.py:

chatbot.py (updated chat)
def chat(user_text):
  messages.append({"role": "user", "content": user_text})

  print("Claude: ", end="", flush=True)
  with client.messages.stream(
      model="claude-opus-4-8",
      max_tokens=1024,
      messages=messages,
  ) as stream:
      for text in stream.text_stream:        # each chunk as it arrives
          print(text, end="", flush=True)    # print it immediately
      final = stream.get_final_message()     # the complete Message, once done

  print()                                    # newline after the reply
  answer = final.content[0].text
  messages.append({"role": "assistant", "content": answer})
  return answer

Two things to notice:

  • stream.text_stream yields the reply in small chunks. Printing each with end="", flush=True makes them appear live, on one line, instead of buffering.
  • stream.get_final_message() gives you the assembled Message after streaming ends — same shape as before — so you can still pull final.content[0].text and append it to the history. Streaming changes how the text arrives, not what you do with it.

Simplify the loop

Since chat now prints as it streams, the main loop just needs to feed it input:

chatbot.py (updated loop)
if __name__ == "__main__":
  print("Chat with Claude (type 'quit' to exit).")
  while True:
      user_text = input("\nYou: ")
      if user_text.lower() in {"quit", "exit"}:
          break
      chat(user_text)       # it prints the streamed reply itself
terminal
python chatbot.py

Run it and the reply now streams out live, token by token — the ChatGPT feel, in your own terminal. Your chatbot talks, remembers, and responds in real time. For the finale, let's give it the ability to do things.