mcpField
S
streaming

streaming

github.com/thelobbi开发2143 次安装3 天前更新
代码仓库

关于 streaming

用于通过 Server-Sent Events 实现 Claude API 实时流式响应的技能,涵盖事件处理、工具调用与扩展思考流、重试、连接设置及 UI 集成。

主要功能

  1. 01处理 Server-Sent Events
  2. 02流式传输文本和工具调用数据
  3. 03传输扩展思考事件
  4. 04处理错误并重试请求
  5. 05配置超时与连接池
  6. 06将流式输出发送到 UI

适用场景

  1. 01在实时界面逐步显示 Claude 响应
  2. 02待流式 JSON 完整后处理工具输入
  3. 03从过载或可重试错误中恢复

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/thelobbi/claude --skill streaming

安装后在 Claude Code 中输入/streaming确认已加载。

详细介绍

流式传输技能

使用服务器发送事件(SSE)实现 Claude API 的实时流式响应。

何时使用此技能

  • 实时用户界面
  • 长时间运行的生成
  • 渐进式输出显示
  • 流式工具调用
  • 扩展思考可视化

SSE 事件流

message_start
    → content_block_start
        → content_block_delta (repeated)
    → content_block_stop
    → (more blocks...)
→ message_delta
→ message_stop

核心实现

基本文本流式传输(Python)
import anthropic

client = anthropic.Anthropic()

with client.messages.stream(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short story."}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
基于事件的流式传输
with client.messages.stream(
    model="claude-sonnet-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}]
) as stream:
    for event in stream:
        if event.type == "content_block_delta":
            if event.delta.type == "text_delta":
                print(event.delta.text, end="")
            elif event.delta.type == "input_json_delta":
                # Tool input (accumulate, don't parse yet!)
                tool_input_buffer += event.delta.partial_json
        elif event.type == "content_block_stop":
            # Now safe to parse tool input
            if tool_input_buffer:
                tool_input = json.loads(tool_input_buffer)
TypeScript 流式传输
import Anthropic from '@anthropic-ai/sdk';

const client = new Anthropic();

const stream = client.messages.stream({
    model: 'claude-sonnet-5',
    max_tokens: 1024,
    messages: [{ role: 'user', content: 'Write a story.' }]
});

for await (const event of stream) {
    if (event.type === 'content_block_delta' &&
        event.delta.type === 'text_delta') {
        process.stdout.write(event.delta.text);
    }
}

const finalMessage = await stream.finalMessage();

事件类型参考

事件时机数据
message_start开始时消息元数据
content_block_start内容块开始时内容块类型、索引
content_block_delta内容块片段增量内容
content_block_stop内容块结束时-
message_delta消息更新时停止原因、用量
message_stop完成时-

增量类型

增量类型内容时机
text_delta.text文本内容
input_json_delta.partial_json工具输入
thinking_delta.thinking扩展思考
signature_delta.signature思考签名

工具调用流式传输

关键规则:切勿在流传输过程中解析 JSON!
# WRONG - Will fail on partial JSON!
for event in stream:
    if event.delta.type == "input_json_delta":
        tool_input = json.loads(event.delta.partial_json)  # FAILS!

# CORRECT - Accumulate then parse
tool_json_buffer = ""
for event in stream:
    if event.delta.type == "input_json_delta":
        tool_json_buffer += event.delta.partial_json
    elif event.type == "content_block_stop":
        if tool_json_buffer:
            tool_input = json.loads(tool_json_buffer)  # Safe now!
            tool_json_buffer = ""
完整的工具流式传输模式
def stream_with_tools(client, messages, tools):
    current_block = None
    tool_input_buffer = ""

    with client.messages.stream(
        model="claude-sonnet-5",
        max_tokens=4096,
        messages=messages,
        tools=tools
    ) as stream:
        for event in stream:
            if event.type == "content_block_start":
                current_block = event.content_block
                tool_input_buffer = ""

            elif event.type == "content_block_delta":
                if event.delta.type == "text_delta":
                    yield {"type": "text", "content": event.delta.text}
                elif event.delta.type == "input_json_delta":
                    tool_input_buffer += event.delta.partial_json

            elif event.type == "content_block_stop":
                if current_block.type == "tool_use":
                    yield {
                        "type": "tool_call",
                        "id": current_block.id,
                        "name": current_block.name,
                        "input": json.loads(tool_input_buffer)
                    }

扩展思考流式传输

thinking_content = ""
signature = ""

with client.messages.stream(
    model="claude-sonnet-5",
    max_tokens=16000,
    thinking={"type": "adaptive"},
    messages=[{"role": "user", "content": "Solve this complex problem..."}]
) as stream:
    for event in stream:
        if event.type == "content_block_delta":
            if event.delta.type == "thinking_delta":
                thinking_content += event.delta.thinking
                # Optionally display thinking in UI
            elif event.delta.type == "signature_delta":
                signature = event.delta.signature
            elif event.delta.type == "text_delta":
                print(event.delta.text, end="")

错误处理

可重试的错误
import time

RETRIABLE_ERRORS = [529, 429, 500, 502, 503]

def stream_with_retry(client, **kwargs):
    max_retries = 3
    base_delay = 1

    for attempt in range(max_retries):
        try:
            with client.messages.stream(**kwargs) as stream:
                for event in stream:
                    yield event
            return
        except anthropic.APIStatusError as e:
            if e.status_code in RETRIABLE_ERRORS and attempt < max_retries - 1:
                delay = base_delay * (2 ** attempt)
                time.sleep(delay)
            else:
                raise
静默处理过载错误
# CRITICAL: Check for error events even at HTTP 200!
for event in stream:
    if event.type == "error":
        if event.error.type == "overloaded_error":
            # Retry with backoff
            pass

连接管理

保持活动配置
import httpx

# Proper timeout configuration
http_client = httpx.Client(
    timeout=httpx.Timeout(
        connect=10.0,    # Connection timeout
        read=120.0,      # Read timeout (long for streaming!)
        write=30.0,      # Write timeout
        pool=30.0        # Pool timeout
    )
)

client = anthropic.Anthropic(http_client=http_client)
连接池
http_client = httpx.Client(
    limits=httpx.Limits(
        max_keepalive_connections=20,
        max_connections=100,
        keepalive_expiry=30.0
    )
)

最佳实践

应做:
  • 将读取超时设为至少 60 秒
  • 累积工具 JSON,并在 block_stop 后解析
  • 即使 HTTP 状态码为 200,也要处理错误事件
  • 重试时使用指数退避
不应做:
  • 在流式传输过程中解析不完整的 JSON
  • 使用较短的超时时间
  • 忽略 overloaded_error 事件
  • 让连接空闲超过 5 分钟

UI 集成模式

async def stream_to_ui(websocket, prompt):
    """Stream Claude response to WebSocket client"""
    async with client.messages.stream(
        model="claude-sonnet-5",
        max_tokens=4096,
        messages=[{"role": "user", "content": prompt}]
    ) as stream:
        async for text in stream.text_stream:
            await websocket.send_json({
                "type": "chunk",
                "content": text
            })

        await websocket.send_json({
            "type": "complete",
            "usage": stream.get_final_message().usage
        })

另请参阅

  • [[llm-integration]] - API 基础知识
  • [[tool-use]] - 工具调用
  • [[extended-thinking]] - 深度推理

评论

支持 Markdown