📌 项目:openai/openai-agents-python | 星数:29.7k(2026-09-24) | 协议:MIT | 语言:Python | GitHub
一句话定位:"OpenAI官方出品的轻量多Agent框架——provider-agnostic,支持100+LLM,从Agent到生产只需几行代码。" OpenAI Agents SDK提供Agent、Tool、Handoff、Guardrail等核心原语,支持OpenAI Responses和Chat Completions API以及100+其他LLM,是构建多Agent工作流的官方标准方案。
OpenAI Agents SDK围绕9个核心概念构建:
和 Agent技能工程化 的技能视角不同,OpenAI Agents SDK从Agent编排视角出发,强调Handoff和Guardrail。
pip install openai-agents
from agents import Agent, Runner
# 定义Agent
agent = Agent(
name="助手",
instructions="你是一个有帮助的AI助手",
tools=[my_function_tool]
)
# 运行
result = Runner.run_sync(agent, "帮我查一下天气")
print(result.final_output)多Agent Handoff示例:
from agents import Agent, Runner
triage = Agent(name="分诊", instructions="根据问题类型交接给对应Agent")
router = Agent(name="路由", handoffs=[triage])
result = Runner.run_sync(router, "我要写代码")
# router会handoff给triage处理
Guardrail是OpenAI Agents SDK的特色——在Agent执行前后做安全校验:
from agents import Agent, GuardrailFunctionOutput, input_guardrail
@input_guardrail
async def check_toxicity(ctx, agent, input):
is_toxic = await analyze(input)
return GuardrailFunctionOutput(
output_info={"toxic": is_toxic},
tripwire_triggered=is_toxic
)
agent = Agent(
name="安全助手",
instructions="...",
input_guardrails=[check_toxicity]
)当tripwire_triggered=True时,Agent执行立即中止。
| ✅ 优点 | ⚠️ 不足 |
|---|---|
| OpenAI官方出品,文档完善 | OpenAI模型体验最佳,其他LLM可能有差异 |
| 9个核心概念覆盖完整Agent开发 | 概念较多,入门需理解Agent/Tool/Handoff/Guardrail |
| provider-agnostic支持100+LLM | 高级特性(Realtime/Voice)依赖OpenAI模型 |
| Guardrail内置安全校验 | |
| MIT协议,PyPI安装 |
支持。SDK是provider-agnostic的,通过配置不同model provider可接入100+LLM,包括Anthropic、Google、本地模型等。
Agents as Tools:Agent A调用Agent B作为工具,B执行完返回结果给A。Handoffs:Agent A将控制权完全交给Agent B,B继续与用户交互。
不是必需的,但推荐用于生产环境。Guardrail分input和output两种,可在Agent执行前后做安全校验。