嚴格工具使用
透過語法約束取樣,強制 Claude 的工具輸入符合 JSON Schema。
在工具定義上設定 strict: true,可透過將模型的 token 取樣限制為符合 schema 的輸出(一種稱為「grammar-constrained sampling」(語法約束取樣)的技術),保證 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-5-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-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-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 保護措施的保障。
後續步驟
從特定 URL 擷取並讀取內容,將即時網頁內容帶入 Claude 的上下文中。
跨回合快取工具定義,以降低成本與延遲。
使用相同的語法約束取樣取得經驗證的 JSON 回應。
指定工具 schema、撰寫有效的描述,並控制 Claude 何時呼叫您的工具。
可用性
Claude API
- API
- Messages API
- 模型
- 所有模型
Amazon Bedrock
- API
- Messages API
- 模型
- 所有模型
Amazon Bedrock (bedrock-runtime)
- API
- Messages API
- 模型
- 所有模型
Claude Platform on AWS
- API
- Messages API
- 模型
- 所有模型
Claude Models on Google Cloud (Anthropic managed)
- API
- Messages API
- 模型
- 所有模型
Claude Models on Google Cloud (Google managed)
- API
- Messages API
- 模型
- 所有模型
Microsoft Foundry
- API
- Messages API
- 模型
- 所有模型
資料處理
資料處理概覽這個頁面有幫助嗎?