Streaming and Non-Streaming Responses
This page explains response handling for Claude native calls (/v1/messages); integration methods are covered in Claude Native Call Basics.
Non-Streaming Responses
{ "id": "msg_xxx", "type": "message", "role": "assistant", "content": [{ "type": "text", "text": "……" }], "stop_reason": "end_turn", "usage": { "input_tokens": 24, "output_tokens": 180 }}- The main content is in the
contentarray, organized by blocks (text / tool_use / thinking), and should be iterated block by block before concatenation; stop_reason:end_turnindicates normal completion,max_tokensindicates truncation, andtool_userequests a tool call.
Streaming Responses (SSE)
Add "stream": true to the request, and events are delivered by type:
| Event | Meaning |
|---|---|
message_start | Session starts, includes input token count |
content_block_start | A content block starts (text/tool_use/thinking) |
content_block_delta | Incremental content; concatenate delta.text directly |
content_block_stop | Current content block ends |
message_delta | Trailing metadata (stop_reason, output token count) |
message_stop | Everything ends |
with client.messages.stream( model="claude-sonnet-5", max_tokens=1024, messages=[{"role": "user", "content": "Write a short poem about the sea"}],) as stream: for text in stream.text_stream: print(text, end="", flush=True)Notes
- When using the official Anthropic SDK, these events are already encapsulated; only handwritten SSE needs event-by-event handling;
- Token usage is based on
message_start(input) +message_delta(output) and matches billing; - For parsing the
thinkingblock in thinking mode, see Thinking Mode.