メインコンテンツへスキップ

Documentation Index

Fetch the complete documentation index at: https://docs.apigo.ai/llms.txt

Use this file to discover all available pages before exploring further.

推奨エンドポイント

最低限のお願い

{
  "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の例

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 の例

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 の例

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);
}

ベストプラクティス

  • クロードは、最終的な JSON ペイロードではなく、SSE イベントを返します。
  • ストリーミング出力を解析するときにコンテンツ ブロックの境界を保持する
  • ツールまたは思考も有効にする場合、イベント パーサーはプレーン テキスト デルタ以上のものをサポートする必要があります。