嚴格工具使用
透過文法約束取樣,強制 Claude 的工具輸入符合 JSON Schema。
在工具定義上設定 strict: true,可透過將模型的 token 取樣限制為符合 schema 的輸出(一種稱為「grammar-constrained sampling」(文法約束取樣)的技術),保證 Claude 的工具輸入符合您的 JSON Schema。本頁說明嚴格模式為何對代理(agent)很重要、如何啟用它,以及常見的使用案例。關於支援的 JSON Schema 子集,請參閱 JSON Schema 限制。關於非嚴格 schema 的指引,請參閱定義工具。
「Strict tool use」(嚴格工具使用)會驗證工具參數,確保 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-5",
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
{
"type": "tool_use",
"name": "get_weather",
"input": {
"location": "San Francisco, CA"
}
}保證:
- 工具的
input嚴格遵循input_schema - 工具的
name一律有效(來自所提供的工具或伺服器工具)
運作方式
定義您的工具 schema
為您工具的
input_schema建立 JSON schema。此 schema 使用標準 JSON Schema 格式,但有一些限制(請參閱 JSON Schema 限制)。加入 strict: true
在您的工具定義中,將
"strict": true設為頂層屬性,與name、description和input_schema並列。處理工具呼叫
當 Claude 使用該工具時,
tool_use區塊中的input欄位會嚴格遵循您的input_schema,且name一律有效。
電腦使用與瀏覽器使用工具集項目(computer_toolset_20260801 和 browser_toolset_20260801)不接受 strict: true;在任一項目上設定它的請求將會被拒絕。
常見使用案例
確保工具參數完全符合您的 schema:
client = Anthropic()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Search for flights to Tokyo departing June 1, 2026",
}
],
tools=[
{
"name": "search_flights",
"strict": True,
"input_schema": {
"type": "object",
"properties": {
"destination": {"type": "string"},
"departure_date": {"type": "string", "format": "date"},
"passengers": {
"type": "integer",
"enum": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
},
"required": ["destination", "departure_date"],
"additionalProperties": False,
},
}
],
)
print(response)以有保證的工具參數建構可靠的多步驟代理:
client = Anthropic()
response = client.messages.create(
model="claude-opus-5",
max_tokens=1024,
messages=[
{
"role": "user",
"content": "Help me plan a trip from New York to Paris for 2 people, departing June 1, 2026",
}
],
tools=[
{
"name": "search_flights",
"strict": True,
"input_schema": {
"type": "object",
"properties": {
"origin": {"type": "string"},
"destination": {"type": "string"},
"departure_date": {"type": "string", "format": "date"},
"travelers": {"type": "integer", "enum": [1, 2, 3, 4, 5, 6]},
},
"required": ["origin", "destination", "departure_date"],
"additionalProperties": False,
},
},
{
"name": "search_hotels",
"strict": True,
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string"},
"check_in": {"type": "string", "format": "date"},
"guests": {"type": "integer", "enum": [1, 2, 3, 4]},
},
"required": ["city", "check_in"],
"additionalProperties": False,
},
},
],
)
print(response)資料保留
嚴格工具使用會使用與結構化輸出相同的管線,將工具的 input_schema 定義編譯為文法。工具 schema 會自最後一次使用起暫時快取最多 24 小時。提示與回應在 API 回應之外不會被保留。
嚴格工具使用符合 HIPAA 資格,但工具 schema 定義中不得包含受保護的健康資訊(PHI)。API 會將編譯後的 schema 與訊息內容分開快取,而這些快取的 schema 不會獲得與提示和回應相同的 PHI 保護。請勿在 input_schema 的屬性名稱、enum 值、const 值或 pattern 正規表示式中包含 PHI。PHI 應僅出現在訊息內容(提示與回應)中,在那裡它受到 HIPAA 保障措施的保護。
關於所有功能的 ZDR 與 HIPAA 資格,請參閱 API 與資料保留。
後續步驟
從特定 URL 擷取並讀取內容,將即時網頁內容帶入 Claude 的上下文中。
跨輪次快取工具定義,以降低成本與延遲。
使用相同的文法約束取樣取得經驗證的 JSON 回應。
指定工具 schema、撰寫有效的描述,並控制 Claude 何時呼叫您的工具。
Was this page helpful?