/v1/messages), the response is completely different from OpenAI compatible mode: the answer is a typed content block array, and streaming uses Anthropic’s named-event SSE protocol. This page explains how to parse both modes.
The request side (endpoint,
anthropic-version header, x-api-key auth, effort / thinking params) is covered in Claude API Basics and the Claude Effort & Thinking Guide. This page is purely about the response side. Examples use the lightweight model claude-haiku-4-5-20251001.Non-streaming response
The top level is amessage object, and the answer lives in the content array, split into blocks by type:
content array — you can’t read a single string field like in OpenAI:
stop_reason values: end_turn (normal), max_tokens (cut off by max_tokens — the text may be empty; raise the limit), stop_sequence, tool_use (wants to call a tool). With thinking on, the content array gains a type: "thinking" block placed before the text block.Streaming response (named-event SSE)
Claude streaming uses the Anthropic event protocol: each message has anevent: name plus a data: payload, and you dispatch by event type rather than treating every chunk identically as in OpenAI.
The core is accumulating the
text_delta inside content_block_delta:
With thinking (adaptive thinking) on, a
type: "thinking" block appears first; its increments are thinking_delta, and a signature_delta (thinking-block signature) appears before the block closes. To display thinking, render thinking_delta and text_delta separately. See the Claude Effort & Thinking Guide.Key differences from OpenAI compatible mode
Usage and billing
- Non-streaming:
usagecomes back with the result, includinginput_tokens,output_tokens,cache_creation_input_tokens,cache_read_input_tokens. - Streaming:
input_tokensis inmessage_start, and the finaloutput_tokensis inmessage_delta— merge both. - For the cache-hit field (
cache_read_input_tokens) discount and usage, see Claude Cache Billing.
Related links
- Same group: Claude API Basics · Claude Cache Billing · Claude Effort & Thinking Guide
- Compatible-format counterpart: OpenAI Compatible Mode: Handling Responses
- Get / manage tokens:
https://api.apiyi.com/token