Skip to main content

Minimal request

{
  "model": "claude-sonnet-4-20250514",
  "max_tokens": 1024,
  "stream": true,
  "messages": [
    {
      "role": "user",
      "content": [{ "type": "text", "text": "Explain SSE streaming while streaming the answer." }]
    }
  ]
}

cURL example

curl https://api.anthropic.com/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -N \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 1024,
    "stream": true,
    "messages": [
      {
        "role": "user",
        "content": [{ "type": "text", "text": "Explain SSE streaming while streaming the answer." }]
      }
    ]
  }'

Python example

from anthropic import Anthropic

client = Anthropic(api_key="<ANTHROPIC_API_KEY>")

with client.messages.stream(
    model="claude-sonnet-4-20250514",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": [{"type": "text", "text": "Explain SSE streaming while streaming the answer."}],
        }
    ],
) as stream:
    for text in stream.text_stream:
        print(text, end="")

Node.js example

import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

const stream = await client.messages.stream({
  model: "claude-sonnet-4-20250514",
  max_tokens: 1024,
  messages: [
    {
      role: "user",
      content: [{ type: "text", text: "Explain SSE streaming while streaming the answer." }]
    }
  ]
});

for await (const text of stream.textStream) {
  process.stdout.write(text);
}

Best practices

  • Claude returns SSE events rather than one final JSON payload
  • Preserve content-block boundaries when parsing streamed output
  • If you also enable tools or thinking, your event parser must support more than plain text deltas