在创建消息时,您可以设置 "stream": true 来使用服务器发送事件 (SSE)增量流式传输响应。

使用SDK进行流式传输

我们的 PythonTypeScript SDK提供了多种流式传输方式。Python SDK同时支持同步和异步流。详情请参阅各SDK的文档。

事件类型

每个服务器发送事件都包含一个命名的事件类型和相关的JSON数据。每个事件都会使用SSE事件名称(例如 event: message_stop),并在其数据中包含匹配的事件 type

每个流使用以下事件流程:

  1. message_start:包含一个带有空 contentMessage 对象。
  2. 一系列内容块,每个块都有一个 content_block_start、一个或多个 content_block_delta 事件和一个 content_block_stop 事件。每个内容块都有一个 index,对应于其在最终消息 content 数组中的索引。
  3. 一个或多个 message_delta 事件,表示最终 Message 对象的顶层更改。
  4. 最后一个 message_stop 事件。

Ping事件

事件流中可能还包含任意数量的 ping 事件。

错误事件

我们可能偶尔会在事件流中发送错误。例如,在高使用率期间,您可能会收到 overloaded_error,这在非流式上下文中通常对应于HTTP 529:

错误示例
event: error
data: {"type": "error", "error": {"type": "overloaded_error", "message": "Overloaded"}}

其他事件

根据我们的版本控制政策,我们可能会添加新的事件类型,您的代码应该能够优雅地处理未知的事件类型。

Delta类型

每个 content_block_delta 事件包含一个 delta,用于更新给定 index 处的 content 块。

文本delta

text 内容块delta看起来像这样:

文本delta
event: content_block_delta
data: {"type": "content_block_delta","index": 0,"delta": {"type": "text_delta", "text": "ello frien"}}

输入JSON delta

tool_use 内容块的delta对应于块的 input 字段的更新。为了支持最大粒度,delta是_部分JSON字符串_,而最终的 tool_use.input 始终是一个_对象_。

您可以累积字符串delta,并在收到 content_block_stop 事件后解析JSON,可以使用像Pydantic这样的库进行部分JSON解析,或使用我们的SDK,它们提供了访问已解析增量值的辅助工具。

tool_use 内容块delta看起来像这样:

输入JSON delta
event: content_block_delta
data: {"type": "content_block_delta","index": 1,"delta": {"type": "input_json_delta","partial_json": "{\"location\": \"San Fra"}}}

注意:我们当前的模型仅支持一次从 input 中发出一个完整的键和值属性。因此,在使用工具时,模型工作时可能会在流式事件之间出现延迟。一旦累积了 input 键和值,我们会将它们作为多个带有分块部分json的 content_block_delta 事件发出,这样格式就可以自动支持未来模型中的更细粒度。

原始HTTP流响应

我们强烈建议在使用流式模式时使用我们的客户端SDK。但是,如果您正在构建直接的API集成,则需要自己处理这些事件。

流响应由以下部分组成:

  1. 一个 message_start 事件
  2. 可能有多个内容块,每个块包含: a. 一个 content_block_start 事件 b. 可能有多个 content_block_delta 事件 c. 一个 content_block_stop 事件
  3. 一个 message_delta 事件
  4. 一个 message_stop 事件

响应中可能还分散着 ping 事件。有关格式的更多详细信息,请参见事件类型

基本流式请求

请求
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-3-5-sonnet-20241022",
  "messages": [{"role": "user", "content": "Hello"}],
  "max_tokens": 256,
  "stream": true
}'
响应
event: message_start
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-5-sonnet-20241022", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}

event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}

event: ping
data: {"type": "ping"}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}

event: content_block_stop
data: {"type": "content_block_stop", "index": 0}

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null}, "usage": {"output_tokens": 15}}

event: message_stop
data: {"type": "message_stop"}

使用工具的流式请求

在这个请求中,我们要求Claude使用工具来告诉我们天气情况。

请求
  curl https://api.anthropic.com/v1/messages \
    -H "content-type: application/json" \
    -H "x-api-key: $ANTHROPIC_API_KEY" \
    -H "anthropic-version: 2023-06-01" \
    -d '{
      "model": "claude-3-5-sonnet-20241022",
      "max_tokens": 1024,
      "tools": [
        {
          "name": "get_weather",
          "description": "获取指定位置的当前天气",
          "input_schema": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "城市和州,例如 San Francisco, CA"
              }
            },
            "required": ["location"]
          }
        }
      ],
      "tool_choice": {"type": "any"},
      "messages": [
        {
          "role": "user",
          "content": "旧金山的天气如何?"
        }
      ],
      "stream": true
    }'
响应
event: message_start
data: {"type":"message_start","message":{"id":"msg_014p7gG3wDgGV9EUtLvnow3U","type":"message","role":"assistant","model":"claude-3-haiku-20240307","stop_sequence":null,"usage":{"input_tokens":472,"output_tokens":2},"content":[],"stop_reason":null}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: ping
data: {"type": "ping"}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"好的"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"让我们"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"查看"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"旧金山"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"的"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"天气"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"情况"}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":":"}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: content_block_start
data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_01T1x1fJ34qAmk2tNTrN7Up6","name":"get_weather","input":{}}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":""}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"location\":"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"San"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Francisc"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"o,"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" CA\""}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":", "}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"unit\": \"fah"}}

event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"renheit\"}"}}

event: content_block_stop
data: {"type":"content_block_stop","index":1}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":89}}

event: message_stop
data: {"type":"message_stop"}