Claude With The Anthropic Api
← All lessons
Lesson 12Claude With The Anthropic Api

Response streaming

Summary audio

No audio recap for this lesson.

Study notes

The Problem with Standard Requests

  • User messages sent to Claude can take 10-30 seconds to receive a response
  • Showing only a spinner during this wait creates poor user experience
  • Users expect to see response text appear almost immediately

How Streaming Works

  • Server sends initial user message to Claude
  • Claude immediately sends back an initial response (no text content yet) to signal it's starting generation
  • Server then receives a stream of events containing chunks of the generated response
  • Each event contains a small piece of the overall message, not necessarily just one word
  • Server can extract text from each event and send it to the client immediately
  • Result: text appears chunk-by-chunk on screen as it's generated

Event Types in Streaming

  • Multiple event types are sent back: message_start, content_block_start, content_block_delta, content_block_stop, message_delta, message_stop
  • content_block_delta is the most important event type—it contains the actual generated text
  • Standard sequence: message_start → content_block_start → multiple content_block_deltas → content_block_stop → message_delta → message_stop

Using the Anthropic SDK for Streaming

  • Use client. messages. stream() wrapped in a with block instead of client. messages. create() with stream=true
  • Access text chunks via stream. text_stream() to get only the text content without processing raw events
  • This simplifies code by automatically extracting text from content_block_delta events

Collecting Full Messages

  • After streaming completes, use stream. get_final_message() to assemble all chunks into one complete message
  • Allows storing the entire conversation in a database while still streaming chunks to the user in real-time

Takeaways

  • Streaming sends response text chunk-by-chunk to the client as Claude generates it, eliminating long wait times and improving user experience
  • The content_block_delta event type contains the actual generated text chunks that should be displayed to users
  • Use client. messages. stream() with stream. text_stream() in the Anthropic SDK to easily extract and display text chunks without handling raw events
  • After streaming completes, call stream. get_final_message() to get the complete assembled message for storage or further processing
Flashcards 10 cards
Question
click to reveal · ←/→
Answer
click to flip back
Export to Anki (.tsv) ↓
Knowledge check 5 questions