Loading...
    • 开发者指南
    • API 参考
    • MCP
    • 资源
    • 发布说明
    Search...
    ⌘K

    第一步

    Claude 简介快速开始

    模型与定价

    模型概览选择模型Claude 4.5 的新功能迁移到 Claude 4.5模型弃用定价

    使用 Claude 构建

    功能概览使用 Messages API上下文窗口提示词最佳实践

    功能

    提示词缓存上下文编辑扩展思考流式消息批处理引用多语言支持Token 计数嵌入向量视觉PDF 支持Files API搜索结果Google Sheets 插件

    工具

    概述如何实现工具使用令牌高效的工具使用细粒度工具流式传输Bash 工具代码执行工具计算机使用工具文本编辑器工具Web fetch 工具网络搜索工具记忆工具

    代理技能

    概述在 API 中开始使用 Agent Skills技能创作最佳实践通过 API 使用 Agent Skills

    Agent SDK

    概览TypeScript SDKPython SDK

    指南

    流式输入处理权限会话管理托管 Agent SDK修改系统提示词SDK 中的 MCP自定义工具SDK 中的子代理SDK 中的斜杠命令SDK 中的代理技能跟踪成本和使用情况待办事项列表SDK 中的插件

    API 中的 MCP

    MCP 连接器远程 MCP 服务器

    Claude 在第三方平台上

    Amazon BedrockVertex AI

    提示词工程

    概述提示词生成器使用提示模板提示词改进器保持清晰和直接使用示例(多示例提示)让 Claude 思考(思维链)使用XML标签给Claude分配角色(系统提示)预填充 Claude 的响应链式复杂提示长文本技巧扩展思考技巧

    测试与评估

    定义成功标准开发测试用例使用评估工具减少延迟

    加强防护措施

    减少幻觉提高输出一致性缓解越狱handle-streaming-refusals减少提示词泄露保持Claude的角色特征

    管理和监控

    Admin API 概述使用量和成本 APIClaude Code 分析 API
    Console
    工具

    细粒度工具流式传输

    了解如何使用细粒度工具流式传输来减少延迟并实时接收工具参数。

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

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

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

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

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

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

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

    Shell
    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'
    Python
    import anthropic
    
    client = anthropic.Anthropic()
    
    response = client.beta.messages.stream(
        max_tokens=65536,
        model="claude-sonnet-4-5",
        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?"
        }],
        betas=["fine-grained-tool-streaming-2025-05-14"]
    )
    
    print(response.usage)
    TypeScript
    import Anthropic from '@anthropic-ai/sdk';
    
    const anthropic = new Anthropic();
    
    const message = await anthropic.beta.messages.stream({
      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?" 
      }],
      betas: ["fine-grained-tool-streaming-2025-05-14"]
    });
    
    console.log(message.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
    © 2025 ANTHROPIC PBC

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

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

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    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

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

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

    Products

    • Claude
    • Claude Code
    • Max plan
    • Team plan
    • Enterprise plan
    • Download app
    • Pricing
    • Log in

    Features

    • Claude and Slack
    • Claude in Excel

    Models

    • Opus
    • Sonnet
    • Haiku

    Solutions

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

    Claude Developer Platform

    • Overview
    • Developer docs
    • Pricing
    • Amazon Bedrock
    • Google Cloud’s Vertex AI
    • Console login

    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

    Help and security

    • Availability
    • Status
    • Support center

    Terms and policies

    • Privacy policy
    • Responsible disclosure policy
    • Terms of service: Commercial
    • Terms of service: Consumer
    • Usage policy
    © 2025 ANTHROPIC PBC