运行智能体
智能体本身不会执行任何操作——你需要使用 Runner 类或 run() 工具来运行它们。
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Assistant', instructions: 'You are a helpful assistant',});
const result = await run( agent, 'Write a haiku about recursion in programming.',);console.log(result.finalOutput);
// Code within the code,// Functions calling themselves,// Infinite loop's dance.当你不需要自定义 runner 时,也可以使用 run() 工具,它会运行一个默认的单例 Runner 实例。
或者,你也可以创建自己的 runner 实例:
import { Agent, Runner } from '@openai/agents';
const agent = new Agent({ name: 'Assistant', instructions: 'You are a helpful assistant',});
// You can pass custom configuration to the runnerconst runner = new Runner();
const result = await runner.run( agent, 'Write a haiku about recursion in programming.',);console.log(result.finalOutput);
// Code within the code,// Functions calling themselves,// Infinite loop's dance.运行智能体后,你会收到一个 执行结果 对象,其中包含最终输出和完整的运行历史。
Runner 生命周期与配置
Section titled “Runner 生命周期与配置”当你在 Runner 中使用 run 方法时,需要传入起始智能体和输入。输入可以是字符串(会被视为用户消息),也可以是输入项列表,即 OpenAI Responses API 中的 items。
随后 runner 会执行循环:
- 使用当前输入调用当前智能体的模型。
- 检查 LLM 响应。
- 最终输出 → 返回。
- 交接 → 切换到新智能体,保留累计对话历史,回到 1。
- 工具调用 → 执行工具,将结果追加到对话中,回到 1。
- 达到
maxTurns时抛出MaxTurnsExceededError。
Runner 生命周期
Section titled “Runner 生命周期”在应用启动时创建一个 Runner,并在各请求间复用它。该实例会保存全局配置,例如模型 provider 和追踪选项。只有在需要完全不同配置时,才创建另一个 Runner。对于简单脚本,也可以直接调用 run(),它内部会使用默认 runner。
run() 方法的输入包括:用于启动运行的初始智能体、运行输入以及一组选项。
输入可以是字符串(会被视为用户消息)、input items 列表,或在构建人机协作智能体时传入 RunState 对象。
额外选项如下:
| Option | Default | Description |
|---|---|---|
stream | false | 若为 true,调用将返回 StreamedRunResult,并在模型返回时持续发出事件。 |
context | – | 传递给每个工具/护栏/交接的上下文对象。详见上下文管理指南。 |
maxTurns | 10 | 安全上限——达到后抛出 MaxTurnsExceededError。 |
signal | – | 用于取消的 AbortSignal。 |
session | – | 会话持久化实现。参见会话指南。 |
sessionInputCallback | – | 会话历史与新输入的自定义合并逻辑;在模型调用前执行。参见会话。 |
callModelInputFilter | – | 在调用模型前编辑模型输入(items + 可选 instructions)的钩子。参见调用模型输入过滤器。 |
toolErrorFormatter | – | 自定义返回给模型的工具审批拒绝消息的钩子。参见工具错误格式化器。 |
reasoningItemIdPolicy | – | 控制在将先前运行项转回模型输入时,是否保留 reasoning-item 的 id。参见Reasoning 项 ID 策略。 |
tracing | – | 单次运行的追踪配置覆盖项(例如导出 API key)。 |
errorHandlers | – | 支持的运行时错误处理器(当前为 maxTurns)。参见错误处理器。 |
conversationId | – | 复用服务器端会话(仅 OpenAI Responses API + Conversations API)。 |
previousResponseId | – | 在不创建会话的情况下,从上一次 Responses API 调用继续(仅 OpenAI Responses API)。 |
流式传输可让你在 LLM 运行时额外接收流式事件。流开始后,StreamedRunResult 将包含完整运行信息,包括所有新生成输出。你可以使用 for await 循环遍历流式事件。详见流式传输指南。
如果你要创建自己的 Runner 实例,可以传入 RunConfig 对象来配置 runner。
| Field | Type | Purpose |
|---|---|---|
model | string | Model | 为本次运行中的所有智能体强制指定模型。 |
modelProvider | ModelProvider | 解析模型名称——默认使用 OpenAI provider。 |
modelSettings | ModelSettings | 全局调优参数,会覆盖每个智能体自身设置。 |
handoffInputFilter | HandoffInputFilter | 执行交接时修改输入项(前提是交接本身未定义该过滤器)。 |
inputGuardrails | InputGuardrail[] | 应用于初始用户输入的护栏。 |
outputGuardrails | OutputGuardrail[] | 应用于最终输出的护栏。 |
tracingDisabled | boolean | 完全禁用 OpenAI 追踪。 |
traceIncludeSensitiveData | boolean | 从追踪中排除 LLM/工具输入与输出,但仍会发出 spans。 |
workflowName | string | 显示在 Traces 仪表盘中——有助于聚合相关运行。 |
traceId / groupId | string | 手动指定 trace 或 group ID,而不是由 SDK 自动生成。 |
traceMetadata | Record<string, string> | 附加到每个 span 的任意元数据。 |
tracing | TracingConfig | 单次运行追踪覆盖配置(例如导出 API key)。 |
sessionInputCallback | SessionInputCallback | 此 runner 上所有运行默认使用的历史合并策略。 |
callModelInputFilter | CallModelInputFilter | 每次模型调用前编辑模型输入的全局钩子。 |
toolErrorFormatter | ToolErrorFormatter | 自定义返回给模型的工具审批拒绝消息的全局钩子。 |
reasoningItemIdPolicy | ReasoningItemIdPolicy | 在将已生成项回放到后续模型调用时,保留或省略 reasoning-item id 的默认策略。 |
状态与对话管理
Section titled “状态与对话管理”对话/聊天线程
Section titled “对话/聊天线程”每次调用 runner.run()(或 run() 工具)都代表应用层对话中的一个轮次。你可以决定向最终用户展示多少 RunResult——有时只展示 finalOutput,有时展示所有生成项。
import { Agent, run } from '@openai/agents';import type { AgentInputItem } from '@openai/agents';
let thread: AgentInputItem[] = [];
const agent = new Agent({ name: 'Assistant',});
async function userSays(text: string) { const result = await run( agent, thread.concat({ role: 'user', content: text }), );
thread = result.history; // Carry over history + newly generated items return result.finalOutput;}
await userSays('What city is the Golden Gate Bridge in?');// -> "San Francisco"
await userSays('What state is it in?');// -> "California"交互版本请参见聊天示例。
服务器托管对话
Section titled “服务器托管对话”你可以让 OpenAI Responses API 为你持久化对话历史,而不是在每一轮都发送完整本地转录记录。这在协调长对话或多个服务时很有用。详见对话状态指南。
OpenAI 提供了两种复用服务器端状态的方式:
1. 对整段对话使用 conversationId
Section titled “1. 对整段对话使用 conversationId”你可以通过 Conversations API 创建一次对话,然后在每一轮复用其 ID。SDK 会自动仅包含新生成的项。
import { Agent, run } from '@openai/agents';import { OpenAI } from 'openai';
const agent = new Agent({ name: 'Assistant', instructions: 'Reply very concisely.',});
async function main() { // Create a server-managed conversation: const client = new OpenAI(); const { id: conversationId } = await client.conversations.create({});
const first = await run(agent, 'What city is the Golden Gate Bridge in?', { conversationId, }); console.log(first.finalOutput); // -> "San Francisco"
const second = await run(agent, 'What state is it in?', { conversationId }); console.log(second.finalOutput); // -> "California"}
main().catch(console.error);2. 使用 previousResponseId 从上一轮继续
Section titled “2. 使用 previousResponseId 从上一轮继续”如果你只想从 Responses API 开始,也可以用上一条响应返回的 ID 来串联每次请求。这样无需创建完整对话资源,也能跨轮次保持上下文。
import { Agent, run } from '@openai/agents';
const agent = new Agent({ name: 'Assistant', instructions: 'Reply very concisely.',});
async function main() { const first = await run(agent, 'What city is the Golden Gate Bridge in?'); console.log(first.finalOutput); // -> "San Francisco"
const previousResponseId = first.lastResponseId; const second = await run(agent, 'What state is it in?', { previousResponseId, }); console.log(second.finalOutput); // -> "California"}
main().catch(console.error);钩子与自定义
Section titled “钩子与自定义”调用模型输入过滤器
Section titled “调用模型输入过滤器”使用 callModelInputFilter 可在调用模型之前编辑模型输入。该钩子接收当前智能体、上下文以及合并后的输入项(包含会话历史,如有)。你可以返回更新后的 input 数组和可选 instructions,用于脱敏、丢弃旧消息或注入额外系统引导。
可在单次运行中设置(runner.run(..., { callModelInputFilter })),或在 Runner 配置中作为默认项设置(RunConfig 里的 callModelInputFilter)。
工具错误格式化器
Section titled “工具错误格式化器”使用 toolErrorFormatter 自定义工具调用被拒绝时回传给模型的审批拒绝消息。这样你可以返回领域特定措辞(例如合规指引),而不是 SDK 默认消息。
该格式化器可按次设置(runner.run(..., { toolErrorFormatter })),也可全局设置在 RunConfig 中(new Runner(...) 里的 toolErrorFormatter)。
格式化器当前在 approval_rejected 事件下运行,并接收:
kind(当前始终是'approval_rejected')toolType('function'、'computer'、'shell'或'apply_patch')toolNamecallIddefaultMessage(SDK 回退消息)runContext
返回字符串可覆盖消息;返回 undefined 则使用 SDK 默认值。若格式化器抛错(或返回非字符串),SDK 会记录警告并回退到默认审批拒绝消息。
Reasoning 项 ID 策略
Section titled “Reasoning 项 ID 策略”使用 reasoningItemIdPolicy 控制当 SDK 将先前生成的运行项转换回后续模型输入 AgentInputItem[] 时,reasoning 项是否保留其 id 字段。
这会影响 SDK 将已生成模型项回放为输入的场景,例如:
- 同一次运行内的后续模型调用(例如工具执行后);
- 复用已生成项作为输入/历史的后续轮次;
- 从已保存
RunState恢复的运行; - 派生结果视图,如
result.history/result.output(其数组形状与模型输入一致)。 'preserve'(默认)保留 reasoning 项 ID。'omit'会在回传为输入前移除 reasoning 项的id字段。- 非 reasoning 项不受影响。
以下内容不会改变:
- 原始模型响应(
result.rawResponses); - 运行项(
result.newItems); - provider 返回的模型当前轮输出。
换言之,该策略作用于 SDK 基于先前生成项构建下一次输入时。
你可以按次设置策略(runner.run(..., { reasoningItemIdPolicy: 'omit' })),或设为 runner 默认值(new Runner({ reasoningItemIdPolicy: 'omit', ... }))。从已保存 RunState 恢复时,除非你覆盖,否则会复用先前解析出的策略。
与 callModelInputFilter 的交互
Section titled “与 callModelInputFilter 的交互”reasoningItemIdPolicy 会在 callModelInputFilter 之前应用。如果你需要自定义行为,callModelInputFilter 仍可检查准备好的输入,并在模型调用前手动重新引入或移除 reasoning ID。
何时使用 'omit'
Section titled “何时使用 'omit'”当你希望回放的 reasoning 项在无 ID 情况下标准化时使用 'omit'(例如让转发/回放的模型输入更简洁,或满足应用流水线中的集成要求)。
如果你的后端/provider 因请求校验错误而拒绝回放 reasoning 项(例如后续输入里与 reasoning item ID 相关的 HTTP 400 错误),它也是一个有用的故障排除选项。在这些情况下,使用 'omit' 移除回放 reasoning ID 可避免发送被后端视为无效的新请求 ID。
若你希望 SDK 在回放输入中携带 reasoning-item ID,且你的集成可接受它们,请保留 'preserve'。
使用 errorHandlers 可将支持的运行时错误转换为最终输出,而不是抛出异常。当前仅支持 maxTurns。
errorHandlers.maxTurns仅处理最大轮次错误。errorHandlers.default用作受支持类型的回退处理器。- 处理器接收
{ error, context, runData },并可返回{ finalOutput, includeInHistory? }。
SDK 会抛出一组可捕获的小规模错误:
MaxTurnsExceededError– 达到maxTurns。ModelBehaviorError– 模型产生无效输出(例如 JSON 格式错误、未知工具)。InputGuardrailTripwireTriggered/OutputGuardrailTripwireTriggered– 护栏违规。ToolInputGuardrailTripwireTriggered/ToolOutputGuardrailTripwireTriggered– 工具护栏违规。GuardrailExecutionError– 护栏执行未完成。ToolTimeoutError– 函数工具超过timeoutMs且使用了timeoutBehavior: 'raise_exception'。ToolCallError– 函数工具执行因非超时错误失败。UserError– 基于配置或用户输入抛出的任意错误。
它们都继承自基础 AgentsError 类,该类可通过 state 属性访问当前运行状态。
下面是处理 GuardrailExecutionError 的代码示例。由于输入护栏只在首次用户输入时运行,示例会使用原始输入和上下文重新启动运行。它还展示了如何复用已保存状态,在不再次调用模型的情况下重试输出护栏:
import { Agent, GuardrailExecutionError, InputGuardrail, InputGuardrailTripwireTriggered, OutputGuardrail, OutputGuardrailTripwireTriggered, run,} from '@openai/agents';import { z } from 'zod';
// Shared guardrail agent to avoid re-creating it on every fallback run.const guardrailAgent = new Agent({ name: 'Guardrail check', instructions: 'Check if the user is asking you to do their math homework.', outputType: z.object({ isMathHomework: z.boolean(), reasoning: z.string(), }),});
async function main() { const input = 'Hello, can you help me solve for x: 2x + 3 = 11?'; const context = { customerId: '12345' };
// Input guardrail example
const unstableInputGuardrail: InputGuardrail = { name: 'Math Homework Guardrail (unstable)', execute: async () => { throw new Error('Something is wrong!'); }, };
const fallbackInputGuardrail: InputGuardrail = { name: 'Math Homework Guardrail (fallback)', execute: async ({ input, context }) => { const result = await run(guardrailAgent, input, { context }); const isMathHomework = result.finalOutput?.isMathHomework ?? /solve for x|math homework/i.test(JSON.stringify(input)); return { outputInfo: result.finalOutput, tripwireTriggered: isMathHomework, }; }, };
const agent = new Agent({ name: 'Customer support agent', instructions: 'You are a customer support agent. You help customers with their questions.', inputGuardrails: [unstableInputGuardrail], });
try { // Input guardrails only run on the first turn of a run, so retries must start a fresh run. await run(agent, input, { context }); } catch (e) { if (e instanceof GuardrailExecutionError) { console.error(`Guardrail execution failed (input): ${e}`); try { agent.inputGuardrails = [fallbackInputGuardrail]; // Retry from scratch with the original input and context. await run(agent, input, { context }); } catch (ee) { if (ee instanceof InputGuardrailTripwireTriggered) { console.log('Math homework input guardrail tripped on retry'); } else { throw ee; } } } else { throw e; } }
// Output guardrail example
const replyOutputSchema = z.object({ reply: z.string() });
const unstableOutputGuardrail: OutputGuardrail<typeof replyOutputSchema> = { name: 'Answer review (unstable)', execute: async () => { throw new Error('Output guardrail crashed.'); }, };
const fallbackOutputGuardrail: OutputGuardrail<typeof replyOutputSchema> = { name: 'Answer review (fallback)', execute: async ({ agentOutput }) => { const outputText = typeof agentOutput === 'string' ? agentOutput : (agentOutput?.reply ?? JSON.stringify(agentOutput)); const flagged = /math homework|solve for x|x =/i.test(outputText); return { outputInfo: { flaggedOutput: outputText }, tripwireTriggered: flagged, }; }, };
const agent2 = new Agent<unknown, typeof replyOutputSchema>({ name: 'Customer support agent (output check)', instructions: 'You are a customer support agent. Answer briefly.', outputType: replyOutputSchema, outputGuardrails: [unstableOutputGuardrail], });
try { await run(agent2, input, { context }); } catch (e) { if (e instanceof GuardrailExecutionError && e.state) { console.error(`Guardrail execution failed (output): ${e}`); try { agent2.outputGuardrails = [fallbackOutputGuardrail]; // Output guardrails can be retried using the saved state without another model call. await run(agent2, e.state); } catch (ee) { if (ee instanceof OutputGuardrailTripwireTriggered) { console.log('Output guardrail tripped after retry with saved state'); } else { throw ee; } } } else { throw e; } }}
main().catch(console.error);输入与输出重试的区别:
- 输入护栏仅在运行的第一条用户输入上执行,因此要重试必须用相同输入/上下文启动一次新的运行——传入已保存
state不会重新触发输入护栏。 - 输出护栏在模型响应后执行,因此你可以复用
GuardrailExecutionError中保存的state,在不再次调用模型的情况下重跑输出护栏。
运行上述示例后,你会看到如下输出:
Guardrail execution failed (input): Error: Input guardrail failed to complete: Error: Something is wrong!Math homework input guardrail tripped on retryGuardrail execution failed (output): Error: Output guardrail failed to complete: Error: Output guardrail crashed.Output guardrail tripped after retry with saved state