Loading...
    • 建構
    • 管理
    • 模型與定價
    • 客戶端 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 合規性。

    在工具定義上設定 strict: true 會使用文法約束採樣來保證 Claude 的工具輸入符合您的 JSON Schema。本頁涵蓋為什麼嚴格模式對代理很重要、如何啟用它,以及常見的使用案例。如需支援的 JSON Schema 子集,請參閱 JSON Schema 限制。如需非嚴格模式的 schema 指導,請參閱定義工具。

    嚴格工具使用驗證工具參數,確保 Claude 使用正確型別的引數呼叫您的函式。當您需要以下情況時,請使用嚴格工具使用:

    • 驗證工具參數
    • 建立代理工作流程
    • 確保型別安全的函式呼叫
    • 處理具有巢狀屬性的複雜工具

    為什麼嚴格工具使用對代理很重要

    建立可靠的代理系統需要保證 schema 合規性。沒有嚴格模式,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

      定義您的工具 schema

      為您的工具的 input_schema 建立 JSON schema。該 schema 使用標準 JSON Schema 格式,但有一些限制(請參閱 JSON Schema 限制)。

    2. 2

      新增 strict: true

      在您的工具定義中設定 "strict": true 作為頂級屬性,與 name、description 和 input_schema 一起。

    3. 3

      處理工具呼叫

      當 Claude 使用該工具時,工具使用區塊中的 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?