• 构建
  • 管理
  • 模型与定价
  • 客户端 SDK
  • API 参考
Search...
⌘K
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 合规性。

Was this page helpful?

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

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

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

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

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

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

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

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

快速开始

响应格式: 在 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 和数据保留。

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)