For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主要導覽

工具搜尋

在執行階段載入延後載入的工具,讓模型只匯入所需的定義。

工具搜尋可讓模型視需要動態搜尋工具,並將工具載入模型的上下文。這樣就不必一開始就將所有工具定義載入模型的上下文,且 可能有助於減少整體 Token 用量與成本。為了最佳化成本與延遲,工具搜尋的設計會 保留模型的快取。當模型找到新工具時,這些工具會被插入上下文視窗的末尾。

在 Responses API 中,只有 gpt-5.4 及更新的模型支援 tool_search

以下組態與範例使用 Responses API。如需瞭解以工作階段為基礎的函式載入與自動探索 MCP 工具的功能,請參閱 Agents API

若要在 Responses API 中啟用工具搜尋,必須完成以下兩項設定:

  1. tool_search 作為工具加入 tools 陣列。
  2. 如果使用函式,請以 defer_loading: true 標記要延後載入的函式。如果使用 MCP 伺服器,請在 MCP 伺服器的工具定義中設定 defer_loading: true

盡可能使用命名空間

工具搜尋可搭配延後載入的函式命名空間MCP 伺服器使用,但建議盡可能使用命名空間或 MCP 伺服器。我們的模型主要針對這兩種工具組織方式進行搜尋訓練,採用這些方式通常也能更顯著地節省 Token。

對於命名空間,defer_loading 適用於其中的函式,而非命名空間物件本身。

請求開始時,模型仍會看到所有可搜尋項目的名稱與描述。對於命名空間或 MCP 伺服器,這表示模型一開始只會看到命名空間或伺服器的名稱與描述,直到工具搜尋工具載入其中的個別函式後,才會看到這些函式的詳細資訊。對於個別延後載入的函式,模型仍會看到函式名稱與描述,因此實際上,工具搜尋主要是延後載入參數結構描述。

為了盡可能節省 Token,建議將延後載入的函式分組至命名空間或 MCP 伺服器,並提供清楚的概括描述,讓模型充分掌握其中的內容,進而有效搜尋並只載入相關函式。最佳做法是盡量讓每個命名空間包含的函式少於 10 個,以提升 Token 使用效率與模型效能。

{
    "tools": [
      {
        "type": "namespace",
        "name": "crm",
        "description": "CRM tools for customer lookup and order management.",
        "tools": [
          {
            "type": "function",
            "name": "list_open_orders",
            "description": "List open orders for a customer ID.",
            "defer_loading": true,
            "parameters": {
              "type": "object",
              "properties": {
                "customer_id": { "type": "string" }
              },
              "required": ["customer_id"],
              "additionalProperties": false
            }
          }
        ]
      },
      {
        "type": "tool_search"
      }
    ]
  }

命名空間可以同時包含延後載入與非延後載入的工具。未設定 defer_loading: true 的工具可立即呼叫,同一命名空間中延後載入的工具則透過工具搜尋載入。

工具搜尋類型

你可以選擇以下兩種工具搜尋類型:

  • 託管式工具搜尋: OpenAI 會搜尋你在請求中宣告的延後載入工具,並在同一個回應中傳回已載入的工具子集。
  • 用戶端執行的工具搜尋: 模型會產生 tool_search_call,由你的應用程式執行查詢,再由你傳回對應的 tool_search_output

如果建立請求時已知候選工具有哪些,請先使用託管式工具搜尋。如果工具探索取決於專案狀態、租用戶狀態,或你的應用程式控制的其他系統,則使用用戶端執行的工具搜尋。

如果你已掌握要讓模型搜尋的所有函式命名空間MCP 伺服器,託管式工具搜尋是最簡單的方式。只要事先宣告這些項目,加入 {"type": "tool_search"},再讓 API 決定要載入哪些工具即可。

設定託管式工具搜尋
from openai import OpenAI

client = OpenAI()

crm_namespace = {
    "type": "namespace",
    "name": "crm",
    "description": "CRM tools for customer lookup and order management.",
    "tools": [
        {
            "type": "function",
            "name": "get_customer_profile",
            "description": "Fetch a customer profile by customer ID.",
            "parameters": {
                "type": "object",
                "properties": {
                    "customer_id": {"type": "string"},
                },
                "required": ["customer_id"],
                "additionalProperties": False,
            },
        },
        {
            "type": "function",
            "name": "list_open_orders",
            "description": "List open orders for a customer ID.",
            "defer_loading": True,
            "parameters": {
                "type": "object",
                "properties": {
                    "customer_id": {"type": "string"},
                },
                "required": ["customer_id"],
                "additionalProperties": False,
            },
        },
    ],
}

response = client.responses.create(
    model="gpt-6-astra",
    input="List open orders for customer CUST-12345.",
    tools=[
        crm_namespace,
        {"type": "tool_search"},
    ],
    parallel_tool_calls=False,
)

print(response.output)

如果模型判斷需要某個延後載入的工具,回應會在最終的函式呼叫之前額外包含兩個輸出項目:

  • tool_search_call,用於記錄託管式搜尋步驟。
  • tool_search_output,包含已載入且可供呼叫的工具子集。
託管式工具搜尋回應
[
  {
    "type": "tool_search_call",
    "execution": "server",
    "call_id": null,
    "status": "completed",
    "arguments": {
      "paths": ["crm"]
    }
  },
  {
    "type": "tool_search_output",
    "execution": "server",
    "call_id": null,
    "status": "completed",
    "tools": [
      {
        "type": "namespace",
        "name": "crm",
        "description": "CRM tools for customer lookup and order management.",
        "tools": [
          {
            "type": "function",
            "name": "list_open_orders",
            "description": "List open orders for a customer ID.",
            "defer_loading": true,
            "parameters": {
              "type": "object",
              "properties": {
                "customer_id": { "type": "string" }
              },
              "required": ["customer_id"],
              "additionalProperties": false
            }
          }
        ]
      }
    ]
  },
  {
    "type": "function_call",
    "name": "list_open_orders",
    "namespace": "crm",
    "call_id": "call_abc123",
    "arguments": "{\"customer_id\":\"CUST-12345\"}"
  }
]

在託管模式下,execution 設為 server,而 call_id 設為 null

對於較複雜的任務,模型也可以在同一個 tool_search_call 中載入多個命名空間或 MCP 伺服器。例如,如果需要不同命名空間中的函式才能完成一項任務,模型可能會選擇先一併搜尋並載入這些命名空間,再進行後續的函式呼叫。

用戶端執行的工具搜尋可讓你的應用程式完全掌控工具探索的運作方式。如果可用工具取決於不適合在初始 tools 清單中宣告的資訊,這種方式就很實用。

tool_search 工具設定 execution: "client",並提供結構描述,定義應用程式預期接收的搜尋引數:

設定用戶端執行的工具搜尋
from openai import OpenAI

client = OpenAI()

first_response = client.responses.create(
    model="gpt-6-astra",
    input="Find the shipping ETA tool first, then use it for order_42.",
    tools=[
        {
            "type": "tool_search",
            "execution": "client",
            "description": "Find the project-specific tools needed to continue the task.",
            "parameters": {
                "type": "object",
                "properties": {
                    "goal": {"type": "string"},
                },
                "required": ["goal"],
                "additionalProperties": False,
            },
        }
    ],
    parallel_tool_calls=False,
)

search_call = next(
    item for item in first_response.output if item.type == "tool_search_call"
)

loaded_tools = [
    {
        "type": "function",
        "name": "get_shipping_eta",
        "description": "Look up shipping ETA details for an order.",
        "defer_loading": True,
        "parameters": {
            "type": "object",
            "properties": {
                "order_id": {"type": "string"},
            },
            "required": ["order_id"],
            "additionalProperties": False,
        },
    }
]

second_response = client.responses.create(
    model="gpt-6-astra",
    input=[
        *first_response.output,
        {
            "type": "tool_search_output",
            "execution": "client",
            "call_id": search_call.call_id,
            "status": "completed",
            "tools": loaded_tools,
        },
    ],
)

print(second_response.output)

在第一個回合,模型會產生 tool_search_call,然後停止:

用戶端工具搜尋呼叫
[
  {
    "type": "tool_search_call",
    "execution": "client",
    "call_id": "call_abc123",
    "status": "completed",
    "arguments": {
      "goal": "Find the shipping ETA tool for order_42."
    }
  }
]

接著,你的應用程式會執行搜尋,並傳回包含所要載入工具的 tool_search_output

傳回 tool_search_output
[
  {
    "type": "tool_search_output",
    "execution": "client",
    "call_id": "call_abc123",
    "status": "completed",
    "tools": [
      {
        "type": "function",
        "name": "get_shipping_eta",
        "description": "Look up shipping ETA details for an order.",
        "defer_loading": true,
        "parameters": {
          "type": "object",
          "properties": {
            "order_id": { "type": "string" }
          },
          "required": ["order_id"],
          "additionalProperties": false
        }
      }
    ]
  }
]

在下一個回合,已載入的工具就能像一般函式一樣呼叫:

呼叫已載入的函式
[
  {
    "type": "function_call",
    "name": "get_shipping_eta",
    "namespace": "get_shipping_eta",
    "call_id": "call_xyz456",
    "arguments": "{\"order_id\":\"order_42\"}"
  }
]

在用戶端模式下,execution 設為 client,且 call_id 已定義。請在 tool_search_output 中原樣傳回 tool_search_call 中的 call_id

進階用法

清楚描述命名空間

命名空間的描述應清楚說明使用情境,因為模型會根據這段描述,決定何時載入該命名空間中的部分函式。描述不宜過長;更詳盡的資訊應放在延後載入的函式描述中,這些描述只會在需要時載入。

瞭解載入的內容

tool_search_output.tools 包含模型動態載入的工具清單。模型可以在後續回合呼叫其中任何工具,因此在用戶端模式下,不必在不同回合重複載入相同工具。未列於此陣列的工具無法供模型使用。如果要停用已載入的工具,可以從定義已載入工具集的 tool_search_output 項目中移除該工具,但請注意,變更已載入的工具集會使模型從該位置起的快取失效。

進階插入方式

大多數整合會在請求的 tools 參數中宣告工具。用戶端執行的工具搜尋也支援更進階的方式,讓你的應用程式傳回原始請求中沒有的工具。請將此視為進階工作流程:仔細驗證傳回的結構描述,並且只提供可信任的工具定義。

工具搜尋與快取

所有工具都會載入至模型上下文視窗的末尾,託管式工具搜尋與用戶端執行的工具搜尋皆是如此。這樣就能在不同請求之間保留模型的快取,降低整體成本並提升速度。

在輸入中的特定位置加入工具

在進階工作流程中,可以使用 additional_tools 輸入項目,讓工具從對話中的特定位置起可供使用。如果你的應用程式在一般工具搜尋流程之外載入工具,或需要保留先前回應中所加入工具的順序,這種方式就很實用。

role 設為 developer,並在該項目的 tools 陣列中加入要新增的工具:

{
    "type": "additional_tools",
    "role": "developer",
    "tools": [
      {
        "type": "function",
        "name": "get_customer",
        "description": "Look up a customer by ID.",
        "parameters": {
          "type": "object",
          "properties": {
            "customer_id": { "type": "string" }
          },
          "required": ["customer_id"],
          "additionalProperties": false
        }
      }
    ]
  }

additional_tools 項目中的工具,只有在該項目出現在輸入中之後才可使用。手動往返傳遞對話項目時,請保留該項目的位置,讓模型在對話中的同一位置看到相同的工具。

Agents API

Agents API 預設會預先載入函式定義。若要延後載入特定函式,請在 agent.tools 中加入 { "type": "tool_search" },並為每個希望智慧體視需要探索的函式設定 defer_loading: true。加入 tool_search 並不會讓所有函式都延後載入。

工作階段請求仍須提供完整的函式定義,包括名稱、描述及引數結構描述。工具搜尋改變的是模型收到該定義的時機。找到函式後,應用程式會照常處理函式呼叫並傳回結果。如需瞭解結果處理方式,請參閱函式

執行此範例前,請先設定 OPENAI_API_KEY

僅在需要時載入函式工具
import OpenAI from "openai";
const client = new OpenAI();

const result = await client.beta.agents.sessions.create({
  agent: {
    model: "gpt-6-astra",
    tools: [
      {
        type: "tool_search",
      },
      {
        type: "function",
        name: "lookup_account",
        description: "Find an account by its account number.",
        parameters: {
          type: "object",
          properties: {
            account_id: {
              type: "string",
            },
          },
          required: ["account_id"],
          additionalProperties: false,
        },
        defer_loading: true,
      },
    ],
  },
  environment: {
    type: "none",
  },
  input: [
    {
      role: "user",
      content: [
        {
          type: "input_text",
          text: "Look up account 42.",
        },
      ],
    },
  ],
});
console.log(result.id);

選擇函式載入策略

策略組態適用情境取捨
預先載入省略 defer_loading,或將其設為 false函式數量較少,或大多數任務都需要使用這些函式。未使用的定義會占用上下文。變更定義可能會使已快取的前綴失效。
延後載入設定 defer_loading: true 並加入 tool_search函式目錄龐大,但每個任務只需要其中少數函式。探索工具會增加一個步驟,且必須能找到相關工具。

Agents API 支援在同一個工作階段中混用預先載入與延後載入的函式,但通常不建議這麼做。請為延後載入的函式提供清楚的名稱與描述。選擇預設策略前,請使用具代表性的請求,比較任務完成情況、輸入 Token 用量與延遲。

MCP 與外掛程式工具

當模型與供應商支援工具搜尋時,Agents API 中的 MCP 工具會使用自動探索功能。執行階段會延後載入 MCP 工具,並在有可搜尋的延後載入工具時加入工具搜尋。這適用於遠端 MCP、執行器 MCP,以及外掛程式提供的 MCP 工具。

你不需要專為 MCP 工具加入 { "type": "tool_search" },也不需要在 MCP 伺服器上設定函式層級的 defer_loading 旗標。請使用 MCP 連線來設定伺服器。本指南前面介紹的 Responses API 組態不適用於 Agents API 的 MCP 伺服器。

  • 使用函式呼叫來定義可呼叫的函式和自訂工具。
  • 請參閱使用工具,全面瞭解 Responses 中可用的工具。