English
中文
English

Streaming Messages

中文

串流訊息

English

When creating a Message, you can set "stream": true to incrementally stream the response using server-sent events (SSE).

中文

建立 Message 時,你可以設定 "stream": true 來使用伺服器發送事件(SSE)逐步串流回應。

English

Streaming with SDKs

中文

使用 SDK 進行串流

English

Our Python and TypeScript SDKs offer multiple ways of streaming. The Python SDK allows both sync and async streams. See the documentation in each SDK for details.

中文

我們的 Python 和 TypeScript SDK 提供多種串流方式。Python SDK 支援同步和非同步串流。詳情請參閱各 SDK 的文件。

python
import anthropic

client = anthropic.Anthropic()

with client.messages.stream(
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
    model="claude-sonnet-4-5",
) as stream:
  for text in stream.text_stream:
      print(text, end="", flush=True)
typescript
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

await client.messages.stream({
    messages: [{role: 'user', content: "Hello"}],
    model: 'claude-sonnet-4-5',
    max_tokens: 1024,
}).on('text', (text) => {
    console.log(text);
});
English

Event types

中文

事件類型

English

Each server-sent event includes a named event type and associated JSON data. Each stream uses the following event flow:

中文

每個伺服器發送事件都包含具名的事件類型和相關的 JSON 資料。每個串流使用以下事件流程:

English
  • message_start: contains a Message object with empty content.
  • A series of content blocks, each with content_block_start, content_block_delta events, and content_block_stop.
  • One or more message_delta events, indicating top-level changes to the final Message object.
  • A final message_stop event.
中文
  • message_start:包含一個內容為空的 Message 物件。
  • 一系列內容區塊,每個區塊都有 content_block_start、content_block_delta 事件和 content_block_stop。
  • 一個或多個 message_delta 事件,表示對最終 Message 物件的頂層變更。
  • 最後一個 message_stop 事件。
English

Content block delta types

中文

內容區塊差異類型

English

Each content_block_delta event contains a delta of a type that updates the content block at a given index.

中文

每個 content_block_delta 事件包含一個差異,用於更新指定索引位置的內容區塊。

English
  • Text delta: Updates text content blocks with new text.
  • Input JSON delta: Updates tool_use content blocks with partial JSON strings.
  • Thinking delta: When using extended thinking, updates thinking content blocks.
中文
  • 文字差異:用新文字更新文字內容區塊。
  • 輸入 JSON 差異:用部分 JSON 字串更新 tool_use 內容區塊。
  • 思考差異:使用 extended thinking 時,更新思考內容區塊。
English

Basic streaming request

中文

基本串流請求

bash
curl https://api.anthropic.com/v1/messages \
     --header "anthropic-version: 2023-06-01" \
     --header "content-type: application/json" \
     --header "x-api-key: $ANTHROPIC_API_KEY" \
     --data '{
  "model": "claude-sonnet-4-5",
  "messages": [{"role": "user", "content": "Hello"}],
  "max_tokens": 256,
  "stream": true
}'
English

Error recovery

中文

錯誤復原

English

When a streaming request is interrupted due to network issues, timeouts, or other errors, you can recover by resuming from where the stream was interrupted.

中文

當串流請求因網路問題、逾時或其他錯誤而中斷時,你可以從中斷處恢復來進行錯誤復原。

English
  • Capture the partial response: Save all content that was successfully received before the error occurred.
  • Construct a continuation request: Create a new API request that includes the partial assistant response.
  • Resume streaming: Continue receiving the rest of the response from where it was interrupted.
中文
  • 擷取部分回應:儲存錯誤發生前成功接收的所有內容。
  • 建構延續請求:建立一個新的 API 請求,包含部分的助理回應。
  • 恢復串流:從中斷處繼續接收剩餘的回應。