跳转到内容

工具

工具让智能体能够执行操作——获取数据、调用外部 API、执行代码,甚至操作计算机。JavaScript/TypeScript SDK 支持七个类别:

在阅读智能体、并明确哪个智能体应负责该任务后,再阅读本页,为它赋予能力。如果你仍在决定委派模式,请参阅智能体编排

  1. OpenAI 托管工具——与模型一起在 OpenAI 服务器上运行。(Web 搜索、文件搜索、Code Interpreter、图像生成、工具搜索)
  2. 内置执行工具——由 SDK 提供、在模型之外执行的工具。(计算机操作和 apply_patch 在本地运行;shell 可以在本地或托管容器中运行)
  3. 函数工具——用 JSON schema 封装任何本地函数,使 LLM 能够调用它。
  4. Agents as tools——将整个智能体公开为可调用工具。
  5. MCP 服务器——挂载一个 Model Context Protocol 服务器(本地或远程)。
  6. 沙盒能力——将工作区作用域的 shell、文件系统、技能、记忆或压缩工具挂载到 SandboxAgent
  7. 实验性:Codex 工具——将 Codex SDK 封装为函数工具,用于运行具备工作区感知能力的任务。

本指南剩余部分会先介绍每个工具类别,然后总结跨工具的工具选择和提示编写建议。

使用 OpenAIResponsesModel 时,你可以添加以下内置工具:

工具类型字符串用途
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 提供了返回托管工具定义的辅助函数:

辅助函数说明
webSearchTool(options?)适合 JS 的选项,例如 searchContextSizeuserLocationfilters.allowedDomains
fileSearchTool(ids, options?)第一个参数接受一个或多个向量存储 ID,另可传入 maxNumResultsincludeSearchResultsrankingOptions 和过滤器等选项。
codeInterpreterTool(options?)未提供 container 时,默认使用自动管理的容器。
imageGenerationTool(options?)支持图像生成配置,例如 modelsizequalitybackgroundinputFidelityinputImageMaskmoderationoutputCompressionpartialImages 和输出格式。
toolSearchTool(options?)添加内置 tool_search 辅助工具。可与设置了 deferLoading: true 的延迟函数工具或托管 MCP 工具搭配使用。默认支持托管执行,也支持通过 execution: 'client'execute 进行客户端执行。

这些辅助函数会将 JavaScript/TypeScript 友好的选项名称映射到底层 OpenAI Responses API 工具载荷。完整工具 schema 以及排序选项或语义过滤器等高级选项,请参阅官方 OpenAI 工具指南;当前内置工具搜索流程和模型可用性,请参阅官方工具搜索指南


这些工具内置于 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 }),
],
});

computerTool() 接受以下任一形式:

  • 具体的 Computer 实例。
  • 每次运行创建一个 Computer 的初始化函数。
  • 当你需要运行作用域的设置和清理时,使用带有 { create, dispose } 的提供方对象。

要使用 OpenAI 当前的计算机操作路径,请设置支持计算机能力的模型,例如 gpt-5.4。当请求模型显式指定时,SDK 会发送 GA 内置 computer 工具形态。如果有效模型仍来自已存储提示或其他旧集成,SDK 会保留旧版 computer_use_preview 传输形态以保持兼容,除非你通过 modelSettings.toolChoice: 'computer' 显式选择 GA 路径。

GA 计算机调用可以在单个 computer_call 中包含批量 actions[]。SDK 会按顺序执行它们,针对每个操作评估 needsApproval,并将最终截图作为工具输出返回。如果你基于 interruption.rawItem 构建审批 UI,请在存在 actions 时读取它,并对旧版预览项回退到 action

当高影响的计算机操作应暂停并交由用户审核时,请使用 needsApproval;当你想确认或拒绝计算机调用报告的待处理安全检查时,请使用 onSafetyCheck。有关模型侧指导和迁移细节,请参阅官方 OpenAI 计算机操作指南及其迁移说明

shellTool() 有两种模式:

  • 本地模式:提供 shell,并可选提供 environment: { type: 'local', skills },以及用于自动审批处理的 needsApprovalonApproval
  • 托管容器模式:提供 type: 'container_auto'type: 'container_reference'environment

在本地模式中,environment.skills 允许你按 namedescription 和文件系统 path 挂载本地技能。

在托管容器模式中,使用以下任一方式配置 shellTool({ environment })

  • type: 'container_auto':为本次运行创建托管容器。
  • type: 'container_reference':通过 containerId 复用现有容器。

托管的 container_auto 环境支持:

  • networkPolicy,包括带有 domainSecrets 的允许列表。
  • fileIds,用于挂载已上传文件。
  • memoryLimit,用于容器规格设置。
  • skills,可通过 skill_reference 或内联 zip 包提供。

托管 shell 环境不接受 shellneedsApprovalonApproval,因为执行发生在托管容器环境中,而不是你的本地进程中。

端到端用法请参阅 examples/tools/local-shell.tsexamples/tools/container-shell-skill-ref.tsexamples/tools/container-shell-inline-skill.ts

applyPatchTool() 复用了 shellTool() 的本地审批流程:当需要在文件编辑前暂停时使用 needsApproval;当你希望应用层回调自动批准或拒绝时使用 onApproval


你可以使用 tool() 辅助函数将任何函数变成工具。

使用 Zod 参数的函数工具
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.`;
},
});
字段必填描述
name默认为函数名(例如 get_weather)。
description显示给 LLM 的清晰、人类可读描述。
parametersZod schema 或原始 JSON schema 对象。Zod 参数会自动启用严格模式。
stricttrue(默认)时,如果参数未通过验证,SDK 会返回模型错误。设置为 false 可进行模糊匹配。
execute(args, context, details) => string | unknown | Promise<...>——你的业务逻辑。非字符串输出会为模型序列化。context 是可选的 RunContextdetails 包含 toolCallresumeStatesignal 等元数据。
errorFunction自定义处理器 (context, error) => string,用于将内部错误转换为用户可见的字符串。
timeoutMs每次调用的超时时间,单位为毫秒。必须大于 0 且小于或等于 2147483647
timeoutBehavior超时模式:error_as_result(默认)返回模型可见的超时消息,raise_exception 抛出 ToolTimeoutError
timeoutErrorFunctiontimeoutBehaviorerror_as_result 时,自定义处理器 (context, timeoutError) => string 用于生成超时输出。
customDataExtractor回调 (context) => Record<string, unknown> | null | undefined,用于将仅供 SDK 使用的元数据附加到发出的 RunToolCallOutputItem.customData。这些数据不会传回模型。
needsApproval执行前要求人工审批。请参阅人机协作指南
isEnabled按运行条件暴露工具;接受布尔值或谓词。
inputGuardrails在工具执行前运行的护栏;可以拒绝或抛出异常。请参阅护栏
outputGuardrails在工具执行后运行的护栏;可以拒绝或抛出异常。请参阅护栏

当你的应用需要在工具结果旁附加渲染提示、内部 ID 或其他 JSON 兼容元数据时,请使用 customDataExtractor。该回调会接收运行上下文、工具定义、模型工具调用、解析后的输入、输出,以及克隆后的原始输出项。返回的数据会存储在 RunToolCallOutputItem.customDataRunState 中,但会从 history 和模型重放中排除。

使用 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 时禁用严格模式:

非严格 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;
},
});

工具搜索让模型能够在运行时只加载所需的工具定义,而不是预先发送所有 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,就将 toolSearchTool() 添加到同一个 tools 数组中。
  • modelSettings.toolChoice 保持为 'auto'。SDK 会拒绝按名称强制调用内置 tool_search 工具或延迟函数工具。
  • 默认使用托管执行。如果你设置 toolSearchTool({ execution: 'client', execute }),标准 run() 循环仅支持内置 { paths: string[] } 客户端查询形态;自定义客户端侧 schema 需要你自己的 Responses 循环。
  • 命名空间可以混合立即成员和延迟成员。立即成员无需工具搜索即可调用,而同一命名空间中的延迟成员会按需加载。
  • 延迟函数工具和 toolNamespace() 仅适用于 Responses。Chat Completions 会拒绝它们,AI SDK 适配器也不支持延迟 Responses 工具加载流程。

有时你希望一个智能体辅助另一个智能体,而不是完全交接对话。使用 agent.asTool()

如果你仍在 agent.asTool()handoff() 之间选择,请比较智能体指南智能体编排中的模式。

Agents as tools
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,并在函数执行期间用它运行该智能体。如果你想提供 runConfigrunOptions 的任何属性,可以将它们传给 asTool() 方法来自定义 runner 的行为。

你也可以通过 asTool() 选项在智能体工具上设置 needsApprovalisEnabled,以集成人工干预流程和条件化工具可用性。

customOutputExtractor 内部,使用 result.agentToolInvocation 检查当前 Agent.asTool() 调用。在该回调中,结果始终来自 Agent.asTool(),因此 agentToolInvocation 始终已定义,并公开 toolNametoolCallIdtoolArguments。使用 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' 则在序列化上下文保持不变的情况下恢复。

智能体工具可以将所有嵌套运行事件流式传回你的应用。请选择适合你构建工具方式的钩子风格:

流式智能体工具
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_eventrun_item_stream_eventagent_updated_stream_event
  • onStream 是最简单的”兜底”方式,在你内联声明工具(tools: [agent.asTool({ onStream })])时效果很好。如果你不需要按事件路由,请使用它。
  • on(eventName, handler) 允许你选择性订阅(或使用 '*'),当你需要更细粒度处理,或想在创建后附加监听器时最合适。
  • 如果你提供了 onStream 或任何 on(...) 处理器,agent-as-tool 会自动以流式模式运行;如果没有提供,它会保持非流式路径。
  • 处理器会并行调用,因此缓慢的 onStream 回调不会阻塞 on(...) 处理器(反之亦然)。
  • 当工具通过模型工具调用被调用时,会提供 toolCallId;直接 invoke() 调用或提供方差异可能会省略它。

你可以通过 Model Context Protocol (MCP) 服务器公开工具,并将其挂载到智能体。例如,可以使用 MCPServerStdio 启动并连接到 stdio MCP 服务器:

本地 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 集成中的生命周期指导。


@openai/agents-extensions/experimental/codex 提供 codexTool(),这是一个函数工具,可将模型工具调用路由到 Codex SDK,使智能体能够自主运行工作区作用域任务(shell、文件编辑、MCP 工具)。此接口为实验性,可能会变更。

请先安装依赖:

Terminal window
npm install @openai/agents-extensions @openai/codex-sdk

快速开始:

实验性 Codex 工具
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 }
  • 安全:将 sandboxModeworkingDirectory 搭配使用;如果目录不是 Git 仓库,请设置 skipGitRepoCheck
  • 线程:useRunContextThreadId: true 会在 runContext.context 中读取/存储最新 thread id,这对于在你的应用状态中跨轮复用很有用。
  • Thread ID 优先级:工具调用 threadId(如果你的 schema 包含它)优先,其次是运行上下文 thread 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 搜索),便于你记录日志或追踪进度。
  • 输出:工具结果包含 responseusagethreadId,Codex token 用量会记录在 RunContext 中。
  • 结构:outputSchema 可以是描述符、JSON schema 对象或 Zod 对象。对于 JSON 对象 schema,additionalProperties 必须为 false

运行上下文线程复用示例:

Codex 运行上下文线程复用
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;

有关控制模型何时以及如何必须使用工具(modelSettings.toolChoicetoolUseBehavior 等),请参阅智能体指南


  • 简短、明确的描述——说明工具做什么以及何时使用它
  • 验证输入——尽可能使用 Zod schema 进行严格 JSON 验证。
  • 避免在错误处理器中产生副作用——errorFunction 应返回有用的字符串,而不是抛出异常。
  • 每个工具一个职责——小型、可组合的工具有助于模型更好地推理。

  • 智能体:定义带工具的智能体并控制 toolUseBehavior
  • 智能体编排:决定何时使用 Agents as tools,何时使用交接。
  • 运行智能体:了解执行流程、流式传输和对话状态。
  • 模型:了解托管 OpenAI 模型配置和 Responses 传输选择。
  • 护栏:验证工具输入或输出。
  • 深入查看 tool() 以及各种托管工具类型的 TypeDoc 参考。