核心框架9k+ · since 2025 · Python / TypeScript

Agents SDK

by OpenAI

Lightweight handoff + guardrails · Swarm grown up

OpenAI 官方轻量级多 agent 框架,核心是 handoff(职责转交)和 guardrails(安全护栏)。

主要内容

OpenAI 的 Swarm 是实验项目,Agents SDK 是它的生产级继任者。整个 API 只有少数几个概念:Agent、Tool、Handoff、Guardrail、Runner。handoff 本质是个特殊 tool call,模型决定把控制权转给哪个下游 agent。guardrail 是并行跑的检查器,对输入输出做校验。内置 tracing 和 Responses API 集成,一切都给 OpenAI 生态最顺的体验。Python 和 TypeScript 双版本同步发布。

核心关注点

简单优于复杂。它不暴露图,不暴露状态机,只把"谁负责什么"说清楚。

极简 APIhandoff 语义OpenAI 原生体验内置 tracingPython + TypeScript

安装与 hello world

$ pip install openai-agents
hello-world.py
from agents import Agent, Runner, function_tool
 
@function_tool
def lookup_order(order_id: str) -> str:
    return f"订单 {order_id} 正在运输中"
 
billing = Agent(
    name="billing",
    instructions="你处理所有账单和付款相关问题。",
    tools=[lookup_order],
)
 
tech = Agent(
    name="tech_support",
    instructions="你处理所有技术故障和产品使用问题。",
)
 
triage = Agent(
    name="triage",
    instructions="识别用户意图,账单类问题转给 billing,技术类问题转给 tech_support。",
    handoffs=[billing, tech],
)
 
result = Runner.run_sync(triage, "我的订单 #1234 还没到")
print(result.final_output)

关键 API / 概念

Agent

定义 name、instructions、tools、handoffs、output_type。

handoff(target, on_handoff=...)

配置高级 handoff 行为,可以在转交时执行回调、传递 input_filter。

Guardrail

input_guardrails / output_guardrails 列表,并行跑检查器(比如 PII 检测)。

Runner.run / run_sync / run_streamed

三种运行模式。run_streamed 返回异步迭代器流式输出。

适用场景

01

客服 / 工单分派

一个 triage agent 识别意图,handoff 到账单 / 技术 / 投诉 / 退货等领域专家。

02

多语言 / 多领域分发

用户的查询先被路由 agent 分类,再交给对应语言或领域专长的 agent。

03

已深度用 OpenAI 生态的团队

如果你已经在用 Responses API、Computer Use、Structured Outputs,Agents SDK 是最自然的扩展。

什么时候别用

  • 需要确定性的多步工作流:handoff 不是流程引擎,用 LangGraph
  • 对 vendor lock-in 敏感:虽然能换模型,但体验最好的就是 OpenAI

vs 相邻选项

vs LangGraph

Agents SDK 以 handoff 为轴,LangGraph 以图为轴。前者适合转交,后者适合流程。

vs CrewAI

CrewAI 倚重角色建模,Agents SDK 只关心 handoff 拓扑,API 更薄。