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 限制。如需非嚴格模式的 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?