Loading...
    • 开发者指南
    • API 参考
    • MCP
    • 资源
    • 发布说明
    Search...
    ⌘K
    快速开始
    Claude 简介快速入门
    模型与定价
    模型概览选择模型Claude 4.5 新功能迁移到 Claude 4.5模型弃用定价
    使用 Claude 构建
    功能概览使用 Messages API上下文窗口提示词最佳实践
    能力
    提示词缓存上下文编辑扩展思考工作量流式消息批量处理引用多语言支持Token 计数嵌入视觉PDF 支持Files API搜索结果结构化输出
    工具
    概览如何实现工具使用细粒度工具流式传输Bash 工具代码执行工具程序化工具调用计算机使用工具文本编辑器工具Web 获取工具Web 搜索工具内存工具工具搜索工具
    Agent Skills
    概览快速入门最佳实践在 API 中使用 Skills
    Agent SDK
    概览快速入门TypeScript SDKTypeScript V2(预览版)Python SDK迁移指南
    API 中的 MCP
    MCP 连接器远程 MCP 服务器
    第三方平台上的 Claude
    Amazon BedrockMicrosoft FoundryVertex AI
    提示词工程
    概览提示词生成器使用提示词模板提示词改进器清晰直接使用示例(多轮提示)让 Claude 思考(CoT)使用 XML 标签给 Claude 一个角色(系统提示词)预填充 Claude 的响应链接复杂提示词长上下文提示扩展思考提示
    测试与评估
    定义成功标准开发测试用例使用评估工具降低延迟
    加强防护栏
    减少幻觉提高输出一致性缓解越狱流式拒绝减少提示词泄露保持 Claude 的角色
    管理和监控
    Admin API 概览使用和成本 APIClaude Code Analytics API
    Console
    Log in
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...
    Loading...

    Solutions

    • AI agents
    • Code modernization
    • Coding
    • Customer support
    • Education
    • Financial services
    • Government
    • Life sciences

    Partners

    • Amazon Bedrock
    • Google Cloud's Vertex AI

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Company

    • Anthropic
    • Careers
    • Economic Futures
    • Research
    • News
    • Responsible Scaling Policy
    • Security and compliance
    • Transparency

    Learn

    • Blog
    • Catalog
    • Courses
    • Use cases
    • Connectors
    • Customer stories
    • Engineering at Anthropic
    • Events
    • Powered by Claude
    • Service partners
    • Startups program

    Help and security

    • Availability
    • Status
    • Support
    • Discord

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    工具

    细粒度工具流式传输

    工具使用现在支持参数值的细粒度流式传输,允许开发者在不缓冲/JSON验证的情况下流式传输工具使用参数,减少接收大型参数的延迟。

    工具使用现在支持参数值的细粒度流式传输。这允许开发者在不缓冲/JSON验证的情况下流式传输工具使用参数,减少接收大型参数的延迟。

    细粒度工具流式传输可通过Claude API、AWS Bedrock、Google Cloud的Vertex AI和Microsoft Foundry获得。

    细粒度工具流式传输是一项测试功能。请确保在生产环境中使用前评估您的响应。

    请使用此表单提供关于模型响应质量、API本身或文档质量的反馈——我们迫不及待地想听到您的意见!

    使用细粒度工具流式传输时,您可能会收到无效或不完整的JSON输入。请确保在您的代码中考虑这些边界情况。

    如何使用细粒度工具流式传输

    要使用此测试功能,只需将测试版标头fine-grained-tool-streaming-2025-05-14添加到工具使用请求中并打开流式传输。

    以下是如何使用API进行细粒度工具流式传输的示例:

    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" \
      -H "anthropic-beta: fine-grained-tool-streaming-2025-05-14" \
      -d '{
        "model": "claude-sonnet-4-5",
        "max_tokens": 65536,
        "tools": [
          {
            "name": "make_file",
            "description": "Write text to a file",
            "input_schema": {
              "type": "object",
              "properties": {
                "filename": {
                  "type": "string",
                  "description": "The filename to write text to"
                },
                "lines_of_text": {
                  "type": "array",
                  "description": "An array of lines of text to write to the file"
                }
              },
              "required": ["filename", "lines_of_text"]
            }
          }
        ],
        "messages": [
          {
            "role": "user",
            "content": "Can you write a long poem and make a file called poem.txt?"
          }
        ],
        "stream": true
      }' | jq '.usage'

    在此示例中,细粒度工具流式传输使Claude能够将长诗的行流式传输到工具调用make_file中,而无需缓冲来验证lines_of_text参数是否为有效的JSON。这意味着您可以看到参数流在到达时的样子,而无需等待整个参数缓冲和验证。

    使用细粒度工具流式传输时,工具使用块开始流式传输的速度更快,通常更长,包含的换行符更少。这是由于分块行为的差异。

    示例:

    不使用细粒度流式传输(15秒延迟):

    块1:'{"'
    块2:'query": "Ty'
    块3:'peScri'
    块4:'pt 5.0 5.1 '
    块5:'5.2 5'
    块6:'.3'
    块8:' new f'
    块9:'eatur'
    ...

    使用细粒度流式传输(3秒延迟):

    块1:'{"query": "TypeScript 5.0 5.1 5.2 5.3'
    块2:' new features comparison'

    因为细粒度流式传输在不缓冲或JSON验证的情况下发送参数,所以无法保证生成的流将以有效的JSON字符串完成。特别是,如果达到停止原因max_tokens,流可能在参数中途结束,可能不完整。您通常需要编写特定的支持来处理何时达到max_tokens的情况。

    处理工具响应中的无效JSON

    使用细粒度工具流式传输时,您可能会从模型收到无效或不完整的JSON。如果您需要在错误响应块中将此无效JSON传回模型,您可以将其包装在JSON对象中以确保正确处理(使用合理的键)。例如:

    {
      "INVALID_JSON": "<your invalid json string>"
    }

    这种方法帮助模型理解内容是无效的JSON,同时保留原始格式错误的数据用于调试目的。

    包装无效JSON时,请确保正确转义无效JSON字符串中的任何引号或特殊字符,以在包装器对象中维护有效的JSON结构。

    • 处理工具响应中的无效JSON