Loading...
    • 构建
    • 管理
    • 模型与定价
    • 客户端 SDK
    • API 参考
    Search...
    ⌘K
    入门步骤
    Claude 简介快速入门
    使用 Claude 构建
    功能概览使用 Messages APIClaude API 技能处理停止原因
    模型能力
    扩展思考自适应思考努力程度任务预算(测试版)快速模式(测试版:研究预览)结构化输出引用流式消息批量处理搜索结果流式拒绝多语言支持嵌入
    工具
    概览工具使用原理教程:构建工具调用代理定义工具处理工具调用并行工具使用Tool Runner(SDK)严格工具使用工具使用与提示缓存服务器工具故障排除网页搜索工具网页抓取工具代码执行工具顾问工具记忆工具Bash 工具计算机使用工具文本编辑器工具
    工具基础设施
    工具参考管理工具上下文工具组合工具搜索程序化工具调用细粒度工具流式传输
    上下文管理
    上下文窗口压缩上下文编辑提示缓存Token 计数
    文件处理
    Files APIPDF 支持图像与视觉
    技能
    概览快速入门最佳实践企业级技能API 中的技能
    MCP
    远程 MCP 服务器MCP 连接器
    提示工程
    概览提示最佳实践Console 提示工具
    测试与评估
    定义成功标准并构建评估在 Console 中使用评估工具降低延迟
    加强防护栏
    减少幻觉提高输出一致性防范越狱减少提示泄露
    资源
    术语表
    发布说明
    Claude Platform
    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
    • 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
    • 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
    工具

    严格工具使用

    通过语法约束采样在 Claude 的工具输入上强制执行 JSON Schema 合规性。

    在工具定义上设置 strict: true 使用语法约束采样来保证 Claude 的工具输入与您的 JSON Schema 匹配。本页面介绍了为什么严格模式对代理很重要、如何启用它以及常见用例。有关支持的 JSON Schema 子集,请参阅 JSON Schema 限制。有关非严格模式的架构指导,请参阅 定义工具。

    严格工具使用验证工具参数,确保 Claude 使用正确类型的参数调用您的函数。在以下情况下使用严格工具使用:

    • 验证工具参数
    • 构建代理工作流
    • 确保类型安全的函数调用
    • 处理具有嵌套属性的复杂工具

    为什么严格工具使用对代理很重要

    构建可靠的代理系统需要保证架构一致性。在没有严格模式的情况下,Claude 可能会返回不兼容的类型("2" 而不是 2)或缺少必需的字段,从而破坏您的函数并导致运行时错误。

    严格工具使用保证类型安全的参数:

    • 函数每次都接收正确类型的参数
    • 无需验证和重试工具调用
    • 生产就绪的代理在规模上一致工作

    例如,假设预订系统需要 passengers: int。在没有严格模式的情况下,Claude 可能会提供 passengers: "two" 或 passengers: "2"。使用 strict: true,响应将始终包含 passengers: 2。

    快速开始

    client = anthropic.Anthropic()
    
    response = client.messages.create(
        model="claude-opus-4-7",
        max_tokens=1024,
        messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
        tools=[
            {
                "name": "get_weather",
                "description": "Get the current weather in a given location",
                "strict": True,  # Enable strict mode
                "input_schema": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "unit": {
                            "type": "string",
                            "enum": ["celsius", "fahrenheit"],
                            "description": "The unit of temperature, either 'celsius' or 'fahrenheit'",
                        },
                    },
                    "required": ["location"],
                    "additionalProperties": False,
                },
            }
        ],
    )
    print(response.content)

    响应格式: 在 response.content[x].input 中具有验证输入的工具使用块

    Output
    {
      "type": "tool_use",
      "name": "get_weather",
      "input": {
        "location": "San Francisco, CA"
      }
    }

    保证:

    • 工具 input 严格遵循 input_schema
    • 工具 name 始终有效(来自提供的工具或服务器工具)

    工作原理

    1. 1

      定义您的工具架构

      为您的工具的 input_schema 创建一个 JSON 架构。该架构使用标准 JSON Schema 格式,但有一些限制(请参阅 JSON Schema 限制)。

    2. 2

      添加 strict: true

      在您的工具定义中将 "strict": true 设置为顶级属性,与 name、description 和 input_schema 一起。

    3. 3

      处理工具调用

      当 Claude 使用该工具时,tool_use 块中的 input 字段将严格遵循您的 input_schema,name 将始终有效。

    常见用例

    数据保留

    严格工具使用将工具 input_schema 定义编译成语法,使用与结构化输出相同的管道。工具架构会临时缓存最多 24 小时(自上次使用以来)。提示和响应不会在 API 响应之外保留。

    严格工具使用符合 HIPAA 资格,但工具架构定义中不得包含 PHI。API 将编译的架构与消息内容分开缓存,这些缓存的架构不会获得与提示和响应相同的 PHI 保护。不要在 input_schema 属性名称、enum 值、const 值或 pattern 正则表达式中包含 PHI。PHI 应仅出现在消息内容(提示和响应)中,其中受到 HIPAA 保障措施的保护。

    有关所有功能的 ZDR 和 HIPAA 资格,请参阅 API 和数据保留。

    Was this page helpful?