工具
工具让智能体能够执行操作——获取数据、调用外部 API、执行代码,甚至使用计算机。JavaScript/TypeScript SDK 支持七个类别:
当你已经确定哪个智能体应负责该任务,并希望赋予它能力后,请在智能体之后阅读本页。如果你仍在选择委派模式,请参见智能体编排。
- OpenAI 托管工具——与模型一起在 OpenAI 服务器上运行。(Web 搜索、文件搜索、Code Interpreter、图像生成、工具搜索)
- 内置执行工具——由 SDK 提供、在模型之外执行的工具。(计算机操作和 apply_patch 在本地运行;shell 可以在本地或托管容器中运行)
- 函数工具——用 JSON schema 包装任意本地函数,以便 LLM 调用。
- Agents as tools——将整个智能体暴露为可调用工具。
- MCP 服务器——附加一个 Model Context Protocol 服务器(本地或远程)。
- 沙盒能力——将工作区作用域的 shell、文件系统、技能、记忆或压缩工具附加到
SandboxAgent。 - 实验性:Codex 工具——将 Codex SDK 包装为函数工具,用于运行感知工作区的任务。
本指南剩余部分会先介绍每个工具类别,然后总结跨类别的工具选择和提示建议。
1. 托管工具(OpenAI Responses API)
Section titled “1. 托管工具(OpenAI Responses API)”使用 OpenAIResponsesModel 时,你可以添加以下内置工具:
| Tool | Type string | Purpose |
|---|---|---|
| Web 搜索 | 'web_search' | 互联网搜索。 |
| 文件 / 检索搜索 | 'file_search' | 查询托管在 OpenAI 上的向量存储。 |
| Code Interpreter | 'code_interpreter' | 在沙盒环境中运行代码。 |
| 图像生成 | 'image_generation' | 基于文本生成图像。 |
| 工具搜索 | 'tool_search' | 在运行时加载延迟加载的函数工具、命名空间或可搜索的 MCP 工具。 |
import { Agent, codeInterpreterTool, fileSearchTool, imageGenerationTool, webSearchTool,} from '@openai/agents';
const agent = new Agent({ name: 'Travel assistant', tools: [ webSearchTool({ searchContextSize: 'medium' }), fileSearchTool('VS_ID', { maxNumResults: 3 }), codeInterpreterTool(), imageGenerationTool({ size: '1024x1024' }), ],});SDK 提供了一些辅助函数,用于返回托管工具定义:
| Helper function | Notes |
|---|---|
webSearchTool(options?) | 对 JS 友好的选项,例如 searchContextSize、userLocation 和 filters.allowedDomains。 |
fileSearchTool(ids, options?) | 第一个参数接受一个或多个向量存储 ID,并支持 maxNumResults、includeSearchResults、rankingOptions 和过滤器等选项。 |
codeInterpreterTool(options?) | 未提供 container 时,默认使用自动管理的容器。 |
imageGenerationTool(options?) | 支持图像生成配置,例如 model、size、quality、background、inputFidelity、inputImageMask、moderation、outputCompression、partialImages 和输出格式。 |
toolSearchTool(options?) | 添加内置的 tool_search 辅助工具。将它与设置了 deferLoading: true 的延迟加载函数工具或托管 MCP 工具配合使用。默认支持托管执行,也支持通过 execution: 'client' 加 execute 进行客户端执行。 |
这些辅助函数会将 JavaScript/TypeScript 友好的选项名映射到底层 OpenAI Responses API 工具负载。完整工具 schema 以及排序选项或语义过滤器等高级选项,请参见官方 OpenAI 工具指南;当前内置工具搜索流程和模型可用性,请参见官方工具搜索指南。
2. 内置执行工具
Section titled “2. 内置执行工具”这些工具内置于 SDK,但执行发生在模型响应本身之外:
- 计算机操作——实现
Computer接口并将其传给computerTool()。它始终针对你提供的本地Computer实现运行。 - Shell——提供一个本地
Shell实现,或通过shellTool({ environment })配置托管容器环境。 - 应用补丁——实现
Editor接口并将其传给applyPatchTool()。它始终针对你提供的本地Editor实现运行。 - 沙盒 shell 和文件系统工具——当这些操作应在沙盒工作区内运行时,在
SandboxAgent上使用shell()、filesystem()、skills()、memory()或compaction()。
工具调用仍由模型请求,但由你的应用或配置的执行环境完成实际工作。
沙盒能力工具不同于进程级内置工具:它们绑定到当前 SandboxAgent 运行的实时沙盒会话。当工具应在智能体的隔离工作区而不是你的应用进程中运行时,请使用沙盒智能体的快速入门。
import { Agent, applyPatchTool, computerTool, shellTool, Computer, Editor, Shell,} from '@openai/agents';
const computer: Computer = { environment: 'browser', dimensions: [1024, 768], screenshot: async () => '', click: async () => {}, doubleClick: async () => {}, scroll: async () => {}, type: async () => {}, wait: async () => {}, move: async () => {}, keypress: async () => {}, drag: async () => {},};
const shell: Shell = { run: async () => ({ output: [ { stdout: '', stderr: '', outcome: { type: 'exit', exitCode: 0 }, }, ], }),};
const editor: Editor = { createFile: async () => ({ status: 'completed' }), updateFile: async () => ({ status: 'completed' }), deleteFile: async () => ({ status: 'completed' }),};
const agent = new Agent({ name: 'Local tools agent', model: 'gpt-5.4', tools: [ computerTool({ computer }), shellTool({ shell, needsApproval: true }), applyPatchTool({ editor, needsApproval: true }), ],});Computer 工具细节
Section titled “Computer 工具细节”computerTool() 接受以下任一项:
- 一个具体的
Computer实例。 - 一个为每次运行创建
Computer的初始化函数。 - 当你需要按运行作用域进行设置和清理时,使用带有
{ create, dispose }的 provider 对象。
要使用 OpenAI 当前的计算机操作路径,请设置支持计算机操作的模型,例如 gpt-5.4。当请求模型被显式指定时,SDK 会发送 GA 内置 computer 工具形态。如果有效模型仍来自已存储的提示或其他较旧的集成,SDK 会保留旧版 computer_use_preview 传输形态以保持兼容性,除非你通过 modelSettings.toolChoice: 'computer' 显式选择 GA 路径。
GA 计算机调用可以在单个 computer_call 中包含批量 actions[]。SDK 会按顺序执行它们,针对每个 action 评估 needsApproval,并将最终截图作为工具输出返回。如果你基于 interruption.rawItem 构建审批 UI,请在存在 actions 时读取它,并针对旧版 preview 项回退到 action。
当高影响的计算机操作需要暂停以供用户审阅时,请使用 needsApproval;当你想确认或拒绝计算机调用报告的待处理安全检查时,请使用 onSafetyCheck。有关模型侧指导和迁移细节,请参见官方 OpenAI 计算机操作指南及其迁移说明。
Shell 工具细节
Section titled “Shell 工具细节”shellTool() 有两种模式:
- 本地模式:提供
shell,并可选提供environment: { type: 'local', skills },以及用于自动审批处理的needsApproval和onApproval。 - 托管容器模式:提供
environment,其中type为'container_auto'或'container_reference'。
在本地模式下,environment.skills 允许你按 name、description 和文件系统 path 挂载本地技能。
在托管容器模式下,使用以下任一项配置 shellTool({ environment }):
type: 'container_auto':为本次运行创建一个托管容器。type: 'container_reference':通过containerId复用现有容器。
托管的 container_auto 环境支持:
networkPolicy,包括带有domainSecrets的允许列表。fileIds,用于挂载已上传文件。memoryLimit,用于配置容器大小。skills,可通过skill_reference或内联 zip 包提供。
托管 shell 环境不接受 shell、needsApproval 或 onApproval,因为执行发生在托管容器环境中,而不是你的本地进程中。
端到端用法请参见 examples/tools/local-shell.ts、examples/tools/container-shell-skill-ref.ts 和 examples/tools/container-shell-inline-skill.ts。
Apply-patch 工具细节
Section titled “Apply-patch 工具细节”applyPatchTool() 与 shellTool() 的本地审批流程一致:在文件编辑前使用 needsApproval 暂停;当你需要应用级回调来自动批准或拒绝时,使用 onApproval。
3. 函数工具
Section titled “3. 函数工具”你可以使用 tool() 辅助函数将任意函数转换为工具。
import { tool } from '@openai/agents';import { z } from 'zod';
const getWeatherTool = tool({ name: 'get_weather', description: 'Get the weather for a given city', parameters: z.object({ city: z.string() }), async execute({ city }) { return `The weather in ${city} is sunny.`; },});| Field | Required | Description |
|---|---|---|
name | No | 默认为函数名(例如 get_weather)。 |
description | Yes | 展示给 LLM 的清晰、易读描述。 |
parameters | Yes | 可以是 Zod schema,也可以是原始 JSON schema 对象。Zod 参数会自动启用严格模式。 |
strict | No | 当为 true(默认)时,如果参数验证失败,SDK 会返回模型错误。设为 false 可进行模糊匹配。 |
execute | Yes | (args, context, details) => string | unknown | Promise<...>——你的业务逻辑。非字符串输出会被序列化后提供给模型。context 是可选的 RunContext;details 包含 toolCall、resumeState 和 signal 等元数据。 |
errorFunction | No | 自定义处理器 (context, error) => string,用于将内部错误转换为用户可见的字符串。 |
timeoutMs | No | 每次调用的超时时间(毫秒)。必须大于 0 且小于或等于 2147483647。 |
timeoutBehavior | No | 超时模式:error_as_result(默认)返回模型可见的超时消息,raise_exception 抛出 ToolTimeoutError。 |
timeoutErrorFunction | No | 自定义处理器 (context, timeoutError) => string,用于在 timeoutBehavior 为 error_as_result 时生成超时输出。 |
needsApproval | No | 执行前需要人工审批。请参见人机协作。 |
isEnabled | No | 按运行条件暴露工具;接受布尔值或谓词。 |
inputGuardrails | No | 工具执行前运行的护栏;可以拒绝或抛出错误。请参见护栏。 |
outputGuardrails | No | 工具执行后运行的护栏;可以拒绝或抛出错误。请参见护栏。 |
函数工具超时
Section titled “函数工具超时”使用 timeoutMs 限制每次函数工具调用的时长。
timeoutBehavior: 'error_as_result'(默认)向模型返回Tool '<name>' timed out after <timeoutMs>ms.。timeoutBehavior: 'raise_exception'抛出ToolTimeoutError,你可以将其作为运行异常的一部分捕获。timeoutErrorFunction允许你在error_as_result模式下自定义超时文本。- 超时会中止
details.signal,因此长时间运行的工具在监听取消信号时可以及时停止。
如果你直接调用函数工具,请使用 invokeFunctionTool,以强制执行与正常智能体运行相同的超时行为。
非严格 JSON schema 工具
Section titled “非严格 JSON schema 工具”如果你需要模型猜测无效或部分输入,可以在使用原始 JSON schema 时禁用严格模式:
import { tool } from '@openai/agents';
interface LooseToolInput { text: string;}
const looseTool = tool({ description: 'Echo input; be forgiving about typos', strict: false, parameters: { type: 'object', properties: { text: { type: 'string' } }, required: ['text'], additionalProperties: true, }, execute: async (input) => { // because strict is false we need to do our own verification if (typeof input !== 'object' || input === null || !('text' in input)) { return 'Invalid input. Please try again'; } return (input as LooseToolInput).text; },});基于工具搜索的延迟工具加载
Section titled “基于工具搜索的延迟工具加载”工具搜索让模型能够在运行时只加载它需要的工具定义,而不是预先发送每个 schema。在 SDK 中,这就是你使用延迟加载的顶层函数工具、toolNamespace() 组以及配置了 deferLoading: true 的托管 MCP 工具的方式。
仅在 GPT-5.4 以及更新且在 Responses API 中支持工具搜索的模型版本中使用工具搜索。
import { Agent, tool, toolNamespace, toolSearchTool } from '@openai/agents';import { z } from 'zod';
const customerIdParams = z.object({ customerId: z.string().describe('The customer identifier to look up.'),});
// Keep a standalone deferred tool at the top level when it represents a// single searchable capability that does not need a shared namespace.const shippingLookup = tool({ name: 'get_shipping_eta', description: 'Look up a shipment ETA by customer identifier.', parameters: customerIdParams, deferLoading: true, async execute({ customerId }) { return { customerId, eta: '2026-03-07', carrier: 'Priority Express', }; },});
// Group related tools into a namespace when one domain description should// cover several deferred tools and let tool search load them together.const crmTools = toolNamespace({ name: 'crm', description: 'CRM tools for customer profile lookups.', tools: [ tool({ name: 'get_customer_profile', description: 'Fetch a basic customer profile.', parameters: customerIdParams, deferLoading: true, async execute({ customerId }) { return { customerId, tier: 'enterprise', }; }, }), ],});
const agent = new Agent({ name: 'Operations assistant', model: 'gpt-5.4', // Mixing namespaced and top-level deferred tools in one request is supported. tools: [shippingLookup, ...crmTools, toolSearchTool()],});该示例有意混合两种风格:
shippingLookup保持顶层,因为它是一个独立的可搜索能力。crmTools使用toolNamespace(),因为相关的 CRM 工具共享一个高层级标签和描述。- 支持在同一请求中混合带命名空间和顶层延迟加载工具;工具搜索可以加载命名空间路径(例如
crm)和顶层路径(例如get_shipping_eta)。
使用工具搜索时:
- 使用
deferLoading: true标记每个延迟加载的函数工具。 - 当多个相关工具应共享一个领域描述并作为一组加载时,使用
toolNamespace({ name, description, tools })。 - 当工具是单一独立能力,且工具名称本身就是良好的搜索目标时,让该工具保持顶层。
- 只要任何延迟加载的函数工具或托管 MCP 工具使用
deferLoading: true,就在同一个tools数组中添加toolSearchTool()。 - 将
modelSettings.toolChoice保持为'auto'。SDK 会拒绝按名称强制使用内置tool_search工具或延迟加载的函数工具。 - 默认使用托管执行。如果你设置
toolSearchTool({ execution: 'client', execute }),标准run()循环只支持内置的{ paths: string[] }客户端查询形态;自定义客户端 schema 需要你自己的 Responses 循环。 - 一个命名空间可以混合立即加载成员和延迟加载成员。立即加载成员无需工具搜索即可调用,而同一命名空间中的延迟加载成员会按需加载。
- 延迟加载的函数工具和
toolNamespace()仅适用于 Responses。Chat Completions 会拒绝它们,AI SDK 适配器也不支持延迟加载的 Responses 工具加载流程。
4. Agents as tools
Section titled “4. Agents as tools”有时你希望一个智能体协助另一个智能体,而不是完全交接对话。使用 agent.asTool():
如果你仍在 agent.asTool() 和 handoff() 之间选择,请比较智能体和智能体编排中的模式。
import { Agent } from '@openai/agents';
const summarizer = new Agent({ name: 'Summarizer', instructions: 'Generate a concise summary of the supplied text.',});
const summarizerTool = summarizer.asTool({ toolName: 'summarize_text', toolDescription: 'Generate a concise summary of the supplied text.',});
const mainAgent = new Agent({ name: 'Research assistant', tools: [summarizerTool],});在底层,SDK 会:
- 创建一个带有单个
input参数的函数工具。 - 当工具被调用时,使用该输入运行子智能体。
- 返回最后一条消息,或返回由
customOutputExtractor提取的输出。
当你将智能体作为工具运行时,Agents SDK 会使用默认设置创建一个 runner,并在函数执行期间用它运行该智能体。如果你想提供 runConfig 或 runOptions 的任何属性,可以将它们传给 asTool() 方法,以自定义 runner 的行为。
你还可以通过 asTool() 选项在智能体工具上设置 needsApproval 和 isEnabled,以集成人工干预流程和条件化工具可用性。
在 customOutputExtractor 内,使用 result.agentToolInvocation 检查当前 Agent.asTool() 调用。在该回调中,执行结果始终来自 Agent.asTool(),因此 agentToolInvocation 始终有定义,并暴露 toolName、toolCallId 和 toolArguments。使用 result.runContext 获取常规应用上下文和 toolInput。这些元数据限定于当前嵌套调用的作用域,并不会序列化进 RunState。
import { Agent } from '@openai/agents';
const billingAgent = new Agent({ name: 'Billing Agent', instructions: 'Handle billing questions and subscription changes.',});
const billingTool = billingAgent.asTool({ toolName: 'billing_agent', toolDescription: 'Handles customer billing questions.', customOutputExtractor(result) { console.log('tool', result.agentToolInvocation.toolName); // Direct invoke() calls may not have a model-generated tool call id. console.log('call', result.agentToolInvocation.toolCallId); console.log('args', result.agentToolInvocation.toolArguments);
return String(result.finalOutput ?? ''); },});
const orchestrator = new Agent({ name: 'Support Orchestrator', instructions: 'Delegate billing questions to the billing agent tool.', tools: [billingTool],});agent.asTool() 的高级结构化输入选项:
inputBuilder:将结构化工具参数映射为嵌套智能体输入负载。includeInputSchema:在嵌套运行中包含输入 JSON schema,以获得更强的 schema 感知行为。resumeState:控制恢复嵌套的已序列化RunState时的上下文协调策略:'merge'(默认)将实时审批/上下文状态合并到序列化状态中,'replace'改用当前运行上下文,'preferSerialized'则使用未更改的序列化上下文恢复。
来自智能体工具的流式事件
Section titled “来自智能体工具的流式事件”智能体工具可以将所有嵌套运行事件以流式方式传回你的应用。请选择适合你构建工具方式的钩子风格:
import { Agent } from '@openai/agents';
const billingAgent = new Agent({ name: 'Billing Agent', instructions: 'Answer billing questions and compute simple charges.',});
const billingTool = billingAgent.asTool({ toolName: 'billing_agent', toolDescription: 'Handles customer billing questions.', // onStream: simplest catch-all when you define the tool inline. onStream: (event) => { console.log(`[onStream] ${event.event.type}`, event); },});
// on(eventName) lets you subscribe selectively (or use '*' for all).billingTool.on('run_item_stream_event', (event) => { console.log('[on run_item_stream_event]', event);});billingTool.on('raw_model_stream_event', (event) => { console.log('[on raw_model_stream_event]', event);});
const orchestrator = new Agent({ name: 'Support Orchestrator', instructions: 'Delegate billing questions to the billing agent tool.', tools: [billingTool],});- 事件类型与
RunStreamEvent['type']匹配:raw_model_stream_event、run_item_stream_event、agent_updated_stream_event。 onStream是最简单的”兜底”方式,并且在你以内联方式声明工具时效果很好(tools: [agent.asTool({ onStream })])。如果你不需要按事件路由,请使用它。on(eventName, handler)允许你选择性订阅(或使用'*'),最适合需要更细粒度处理,或希望在创建后附加监听器的场景。- 如果你提供了
onStream或任何on(...)处理器,agent-as-tool 会自动以流式模式运行;如果不提供,它会保持非流式路径。 - 处理器会并行调用,因此较慢的
onStream回调不会阻塞on(...)处理器(反之亦然)。 - 当工具通过模型工具调用被调用时,会提供
toolCallId;直接invoke()调用或 provider 的特殊行为可能会省略它。
5. MCP 服务器
Section titled “5. MCP 服务器”你可以通过 Model Context Protocol (MCP) 服务器暴露工具,并将它们附加到智能体。例如,你可以使用 MCPServerStdio 启动并连接到 stdio MCP 服务器:
import { Agent, MCPServerStdio } from '@openai/agents';
const server = new MCPServerStdio({ fullCommand: 'pnpm exec mcp-server-filesystem ./sample_files',});
await server.connect();
const agent = new Agent({ name: 'Assistant', mcpServers: [server],});完整示例请参见 filesystem-example.ts。此外,如果你正在寻找 MCP 服务器工具集成的全面指南,请参阅 MCP 集成了解详情。管理多个服务器(或部分失败)时,请使用 connectMcpServers,并遵循 MCP 集成中的生命周期指导。
6. 实验性:Codex 工具
Section titled “6. 实验性:Codex 工具”@openai/agents-extensions/experimental/codex 提供 codexTool(),这是一个函数工具,会将模型工具调用路由到 Codex SDK,使智能体能够自主运行工作区作用域内的任务(shell、文件编辑、MCP 工具)。该接口面仍处于实验阶段,可能会发生变化。
先安装依赖:
npm install @openai/agents-extensions @openai/codex-sdk快速开始:
import { Agent } from '@openai/agents';import { codexTool } from '@openai/agents-extensions/experimental/codex';
export const codexAgent = new Agent({ name: 'Codex Agent', instructions: 'Use the codex tool to inspect the workspace and answer the question. When skill names, which usually start with `$`, are mentioned, you must rely on the codex tool to use the skill and answer the question.', tools: [ codexTool({ sandboxMode: 'workspace-write', workingDirectory: '/path/to/repo', defaultThreadOptions: { model: 'gpt-5.4', networkAccessEnabled: true, webSearchEnabled: false, }, }), ],});注意事项:
- 身份验证:提供
CODEX_API_KEY(推荐)或OPENAI_API_KEY,也可以传入codexOptions.apiKey。 - 输入:严格 schema——
inputs必须包含至少一个{ type: 'text', text }或{ type: 'local_image', path }。 - 安全:将
sandboxMode与workingDirectory配合使用;如果目录不是 Git repo,请设置skipGitRepoCheck。 - 线程处理:
useRunContextThreadId: true会在runContext.context中读取/存储最新线程 ID,这有助于在你的应用状态中跨轮次复用。 - 线程 ID 优先级:工具调用
threadId(如果你的 schema 包含它)优先,其次是运行上下文线程 ID,最后是codexTool({ threadId })。 - 运行上下文键:对于
name: 'codex',默认为codexThreadId;对于name: 'engineer'这样的名称,默认为codexThreadId_<suffix>(规范化后为codex_engineer)。 - 可变上下文要求:启用
useRunContextThreadId时,请将可变对象或Map作为run(..., { context })传入。 - 命名:工具名称会被规范化到
codex命名空间(engineer会变为codex_engineer),同一智能体中的重复 Codex 工具名称会被拒绝。 - 流式传输:
onStream会镜像 Codex 事件(推理、命令执行、MCP 工具调用、文件变更、Web 搜索),因此你可以记录日志或追踪进度。 - 输出:工具执行结果包含
response、usage和threadId,Codex token 用量会记录在RunContext中。 - 结构:
outputSchema可以是描述符、JSON schema 对象或 Zod 对象。对于 JSON 对象 schema,additionalProperties必须为false。
运行上下文线程复用示例:
import { Agent, run } from '@openai/agents';import { codexTool } from '@openai/agents-extensions/experimental/codex';
// Derived from codexTool({ name: 'engineer' }) when runContextThreadIdKey is omitted.type ExampleContext = { codexThreadId_engineer?: string;};
const agent = new Agent<ExampleContext>({ name: 'Codex assistant', instructions: 'Use the codex tool for workspace tasks.', tools: [ codexTool({ // `name` is optional for a single Codex tool. // We set it so the run-context key is tool-specific and to avoid collisions when adding more Codex tools. name: 'engineer', // Reuse the same Codex thread across runs that share this context object. useRunContextThreadId: true, sandboxMode: 'workspace-write', workingDirectory: '/path/to/repo', defaultThreadOptions: { model: 'gpt-5.4', approvalPolicy: 'never', }, }), ],});
// The default key for useRunContextThreadId with name=engineer is codexThreadId_engineer.const context: ExampleContext = {};
// First turn creates (or resumes) a Codex thread and stores the thread ID in context.await run(agent, 'Inspect src/tool.ts and summarize it.', { context });// Second turn reuses the same thread because it shares the same context object.await run(agent, 'Now list refactoring opportunities.', { context });
const threadId = context.codexThreadId_engineer;工具策略和最佳实践
Section titled “工具策略和最佳实践”工具使用行为
Section titled “工具使用行为”有关如何控制模型必须在何时以及以何种方式使用工具(modelSettings.toolChoice、toolUseBehavior 等),请参见智能体。
- 简短、明确的描述——描述工具会做什么以及何时使用它。
- 验证输入——尽可能使用 Zod schema 进行严格 JSON 验证。
- 避免在错误处理器中产生副作用——
errorFunction应返回有帮助的字符串,而不是抛出错误。 - 每个工具一个职责——小而可组合的工具有助于模型更好地推理。