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
建構/工具

使用提示快取的工具

在多個回合中快取工具定義,並了解什麼會使您的快取失效。

本頁涵蓋工具定義的提示快取:在何處放置 cache_control 斷點、defer_loading 如何保留您的快取,以及什麼會使其失效。如需一般提示快取,請參閱提示快取。

工具定義上的 cache_control

在您的 tools 陣列中的最後一個工具上放置 cache_control: {"type": "ephemeral"}。這會快取整個工具定義前綴,從第一個工具到標記的斷點:

{
  "tools": [
    {
      "name": "get_weather",
      "description": "Get the current weather in a given location",
      "input_schema": {
        "type": "object",
        "properties": {
          "location": { "type": "string" }
        },
        "required": ["location"]
      }
    },
    {
      "name": "get_time",
      "description": "Get the current time in a given time zone",
      "input_schema": {
        "type": "object",
        "properties": {
          "timezone": { "type": "string" }
        },
        "required": ["timezone"]
      },
      "cache_control": { "type": "ephemeral" }
    }
  ]
}

對於 mcp_toolset,cache_control 斷點位於集合中的最後一個工具上。您無法控制 MCP 工具集內的工具順序,因此請在 mcp_toolset 項目本身上放置斷點,API 會將其應用於最終展開的工具。

defer_loading 和快取保留

延遲工具不包含在系統提示前綴中。當模型通過工具搜尋發現延遲工具時,定義會作為 tool_reference 區塊內聯附加到對話歷史記錄中。前綴保持不變,因此提示快取得以保留。

這意味著通過工具搜尋動態添加工具不會破壞您的快取。您可以使用一小組始終載入的工具(已快取)開始對話,讓模型根據需要發現其他工具,並在每個回合中保持相同的快取命中。

defer_loading 也獨立於嚴格模式的語法構造而起作用。無論哪些工具被延遲,語法都從完整工具集構建,因此當工具動態載入時,提示快取和語法快取都得以保留。

什麼會使您的快取失效

快取遵循前綴層級結構(tools → system → messages),因此一個層級的更改會使該層級及其之後的所有內容失效:

更改使其失效
修改工具定義整個快取(工具、系統、訊息)
切換網路搜尋或引用系統和訊息快取
更改 tool_choice訊息快取
更改 disable_parallel_tool_use訊息快取
切換圖像存在/不存在訊息快取
更改思考參數訊息快取

如果您需要在對話中途改變 tool_choice,請考慮在變化點之前放置快取斷點。

每個工具的互動表

工具快取考量
網路搜尋啟用或停用會使系統和訊息快取失效
網路擷取啟用或停用會使系統和訊息快取失效
程式碼執行容器狀態獨立於提示快取
工具搜尋發現的工具作為 tool_reference 區塊載入,保留前綴快取
電腦使用螢幕截圖存在會影響訊息快取
文字編輯器標準客戶端工具,無特殊快取互動
Bash標準客戶端工具,無特殊快取互動
記憶標準客戶端工具,無特殊快取互動

後續步驟

提示快取

了解完整的提示快取模型,包括 TTL 和定價。

工具搜尋

按需載入工具而不破壞您的快取。

工具參考

瀏覽所有可用工具及其參數。

Was this page helpful?

  • 工具定義上的 cache_control
  • defer_loading 和快取保留