AI / Claude Models Basics Interview Questions
What is streaming in Claude API responses and how do you use it?
Streaming allows you to receive Claude's response token by token as it is generated, rather than waiting for the complete response. This dramatically reduces the time to first token and creates a more responsive user experience for chat applications.
# Streaming with the Python SDK with client.messages.stream( model="claude-opus-4-8", max_tokens=1024, messages=[{"role": "user", "content": "Write a short story."}] ) as stream: for text in stream.text_stream: print(text, end="", flush=True) # print each token as it arrives # Or using the raw SSE event stream with client.messages.stream(...) as stream: for event in stream: if event.type == "content_block_delta": print(event.delta.text, end="")
| Event | When it fires |
|---|---|
| message_start | Once at the beginning — includes usage metadata |
| content_block_start | When a new content block (text, tool_use) begins |
| content_block_delta | For each token chunk — contains the text delta |
| content_block_stop | When a content block finishes |
| message_delta | When stop_reason or usage is updated |
| message_stop | Once when the response is fully complete |
Streaming is supported on all current Claude models. Fine-grained tool streaming (streaming tool call arguments as they are generated) is generally available on Sonnet 4.6 and later models with no beta header required.
More Related questions...