Skip to content
Main Site News Console

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 content array, organized by blocks (text / tool_use / thinking), and should be iterated block by block before concatenation;
  • stop_reason: end_turn indicates normal completion, max_tokens indicates truncation, and tool_use requests a tool call.

Streaming Responses (SSE)

Add "stream": true to the request, and events are delivered by type:

EventMeaning
message_startSession starts, includes input token count
content_block_startA content block starts (text/tool_use/thinking)
content_block_deltaIncremental content; concatenate delta.text directly
content_block_stopCurrent content block ends
message_deltaTrailing metadata (stop_reason, output token count)
message_stopEverything 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 thinking block in thinking mode, see Thinking Mode.