来源:市场资讯

(来源:磐创AI)

轻量级但强大,支持 100+ LLM,内置追踪、护栏、人在回路,Python 开发者必备

如果你在用 OpenAI API 开发多 Agent 应用,一定遇到过这些麻烦:

要自己管理 Agent 之间的通信和 handedoff; 要自己实现会话历史和上下文管理; 要自己搭建追踪系统来调试 Agent 行为……

多 Agent 开发,重复造轮子太多。

现在,OpenAI 官方把这个轮子做好了。

openai/openai-agents-python——OpenAI Agents SDK 的 Python 版本,已经拿到22k Star,成为多 Agent 开发的首选框架。

01 Agents SDK 是什么:官方多 Agent 框架

用最简单的话说:

这是 OpenAI 官方出的多 Agent 工作流开发框架。

它帮你解决了: - Agent 之间的协作和任务分配 - 会话历史和上下文管理 - 工具调用和权限控制 - 运行追踪和调试 - 安全护栏和人在回路

核心价值:让你专注于业务逻辑,不用重复造轮子。

核心特性

特性

轻量级

代码简洁,易上手

供应商中立

支持 OpenAI API 和 100+ 其他 LLM

内置追踪

自动记录 Agent 运行,方便调试

人在回路

支持人工审批和干预

Sandbox Agents

在隔离环境中执行真实任务

实时语音 Agent

支持 gpt-realtime-1.5

02 核心概念:9 个关键组件

1. Agents(Agent)

定义:配置了指令、工具、护栏和交接的 LLM。

from agents import Agent  agent = Agent(     name="研究助手",     instructions="你是一个研究助手,帮助用户查找和分析信息。",     tools=[search_tool, calculator_tool], ) 

2. Sandbox Agents(沙盒 Agent)

定义:预配置了容器环境的 Agent,可以执行真实任务。

用途: - 检查文件 - 运行命令 - 应用补丁 - 跨长时间任务保持状态

from agents.sandbox import SandboxAgent, Manifest, GitRepo  agent = SandboxAgent(     name="工作区助手",     instructions="在回答前先检查沙盒工作区。",     default_manifest=Manifest(         entries={             "repo": GitRepo(repo="openai/openai-agents-python", ref="main"),         }     ), ) 

3. Handoffs(交接)

定义:Agent 之间委托任务的机制。

场景: - 研究 Agent 发现需要写代码 → 交给编码 Agent - 客服 Agent 遇到复杂问题 → 交给人工客服

from agents import Agent, handoff  research_agent = Agent(name="Research") coding_agent = Agent(name="Coding")  # 研究 Agent 可以把任务交给编码 Agent research_agent.handoffs = [handoff(coding_agent)] 

4. Tools(工具)

定义:让 Agent 采取行动的各种能力。

支持类型: - Python 函数 - MCP 工具 - Hosted 工具

from agents import Agent, function_tool  @function_tool def get_weather(city: str) -> str:     return f"Weather in {city}: Sunny, 25°C"  agent = Agent(tools=[get_weather]) 

5. Guardrails(护栏)

定义:输入输出的安全检查和验证。

用途: - 过滤敏感信息 - 验证输出格式 - 防止越权操作

from agents import Agent, input_guardrail, output_guardrail  @input_guardrail def check_sensitive_data(input):     # 检查是否包含敏感数据     pass  agent = Agent(input_guardrails=[check_sensitive_data]) 

6. Human in the Loop(人在回路)

定义:在 Agent 运行过程中引入人工审批。

场景: - 高风险操作需要人工批准 - Agent 不确定时请求人工帮助 - 关键决策需要人工确认

from agents import Runner, human_in_the_loop  # 在关键步骤暂停,等待人工审批 result = Runner.run(     agent,     "执行敏感操作",     hooks=human_in_the_loop.approval_required() ) 

7. Sessions(会话)

定义:自动管理跨 Agent 运行的对话历史。

价值: - 不用手动维护上下文 - 支持长期记忆 - 可选 Redis 持久化

from agents import Session  session = Session(agent_id="my-agent") session.add_message("user", "你好") session.add_message("assistant", "你好!有什么可以帮助你的?")  # 下次运行时自动加载历史 

8. Tracing(追踪)

定义:自动记录和追踪 Agent 运行。

功能: - 查看完整的 Agent 执行轨迹 - 调试问题 - 优化性能

from agents import trace  with trace("研究工作流"):     result = Runner.run(agent, "研究量子计算")     # 可以在 UI 中查看完整的执行轨迹 

9. Realtime Agents(实时 Agent)

定义:基于 gpt-realtime-1.5 的语音 Agent。

用途: - 语音对话 - 实时响应 - 低延迟交互

from agents.realtime import RealtimeAgent  agent = RealtimeAgent(     name="语音助手",     instructions="你是一个友好的语音助手。", ) 

03 安装使用:5 分钟上手

前置要求

  • Python

    >= 3.10

  • OpenAI API Key

安装

# 使用 venv python -m venv .venv source .venv/bin/activate  # Windows: .venv\Scripts\activate pip install openai-agents  # 或使用 uv(更快) uv init uv add openai-agents  # 可选:语音支持 pip install 'openai-agents[voice]'  # 可选:Redis 会话支持 pip install 'openai-agents[redis]' 

第一个 Agent

from agents import Agent, Runner  agent = Agent(     name="助手",     instructions="你是一个乐于助人的助手。", )  result = Runner.run_sync(agent, "你好,请介绍一下自己。") print(result.final_output) 

多 Agent 协作

from agents import Agent, Runner, handoff  # 研究 Agent research_agent = Agent(     name="Research Agent",     instructions="研究用户的问题,如果需要写代码,交给编码 Agent。",     handoffs=[handoff(coding_agent)], )  # 编码 Agent coding_agent = Agent(     name="Coding Agent",     instructions="根据需求编写代码。", )  # 运行 result = Runner.run_sync(research_agent, "研究 Python 并发编程") 

04 高级功能:深入使用

Sandbox Agent 详解

什么是 Sandbox Agent?

Sandbox Agent 是一个可以在隔离环境中执行真实任务的 Agent。

适用场景: - 需要检查和修改代码 - 需要运行命令 - 需要跨长时间任务保持状态

完整示例:

from agents import Runner, RunConfig from agents.sandbox import SandboxAgent, Manifest, GitRepo from agents.sandbox.sandboxes import UnixLocalSandboxClient  # 创建 Sandbox Agent agent = SandboxAgent(     name="工作区助手",     instructions="检查沙盒工作区后再回答。",     default_manifest=Manifest(         entries={             "repo": GitRepo(repo="openai/openai-agents-python", ref="main"),         }     ), )  # 运行 result = Runner.run_sync(     agent,     "检查 repo 的 README,总结这个项目做什么的。",     run_config=RunConfig(sandbox=SandboxRunConfig(client=UnixLocalSandboxClient())), )  print(result.final_output) 

追踪系统

内置追踪 UI:

打开网易新闻 查看精彩图片

手动追踪:

from agents import trace, Runner  with trace("复杂工作流"):     result = Runner.run_sync(agent, "执行复杂任务")  # 在 UI 中查看完整轨迹 

追踪内容: - Agent 调用 - 工具调用 - Handoff 事件 - Guardrail 触发

护栏配置

输入护栏:

from agents import Agent, input_guardrail  @input_guardrail def check_prompt_injection(input):     # 检测提示词注入攻击     if "ignore previous instructions" in input.lower():         raise ValueError("检测到提示词注入")  agent = Agent(input_guardrails=[check_prompt_injection]) 

输出护栏:

from agents import Agent, output_guardrail  @output_guardrail def check_sensitive_output(output):     # 检查是否泄露敏感信息     if "API_KEY" in output:         raise ValueError("检测到敏感信息泄露")  agent = Agent(output_guardrails=[check_sensitive_output]) 

人在回路

简单审批:

from agents import Runner, human_in_the_loop  result = Runner.run(     agent,     "删除数据库",     hooks=human_in_the_loop.approval_required(), ) 

自定义审批逻辑:

from agents import human_in_the_loop  @human_in_the_loop.custom_approval def custom_approval(action):     # 自定义审批逻辑     if action == "delete":         return input("确定要删除吗?(y/n)") == "y"     return True 

05 典型用例:真实场景

用例一:客服机器人

需求:自动回答常见问题,复杂问题转人工。

方案:

from agents import Agent, Runner, handoff  # 客服 Agent support_agent = Agent(     name="客服助手",     instructions="回答用户问题,无法回答时转人工。",     handoffs=[handoff(human_agent)],     tools=[faq_search, order_lookup], )  # 人工客服 human_agent = Agent(     name="人工客服",     instructions="处理复杂问题。", ) 

用例二:代码审查助手

需求:自动审查代码,发现问题后建议修改。

方案:

from agents import Agent, Runner from agents.sandbox import SandboxAgent, GitRepo  # 代码审查 Agent reviewer = SandboxAgent(     name="代码审查员",     instructions="检查代码质量问题,给出修改建议。",     tools=[lint_tool, security_scan], )  result = Runner.run_sync(     reviewer,     "审查这个 PR 的代码。", ) 

用例三:数据分析工作流

需求:多 Agent 协作完成数据分析。

方案:

from agents import Agent, Runner, handoff  # 数据获取 Agent fetch_agent = Agent(     name="数据获取",     instructions="从数据源获取数据。",     tools=[database_query, api_fetch],     handoffs=[handoff(analysis_agent)], )  # 分析 Agent analysis_agent = Agent(     name="数据分析",     instructions="分析数据,生成报告。",     tools=[statistical_analysis, visualization], )  # 运行 result = Runner.run_sync(fetch_agent, "分析上个月的销售数据") 

06 与竞品对比

vs LangChain

维度

LangChain

OpenAI Agents SDK

复杂度

学习曲线

陡峭

平缓

官方支持

社区维护

OpenAI 官方

追踪系统

需额外配置

内置

多 Agent

支持

原生支持

结论:LangChain 功能更全面,Agents SDK 更轻量、易上手。

vs AutoGen

维度

AutoGen

OpenAI Agents SDK

定位

研究导向

生产导向

复杂度

文档

一般

完善

生产就绪

中等

结论:AutoGen 适合研究,Agents SDK 适合生产。

vs CrewAI

维度

CrewAI

OpenAI Agents SDK

多 Agent

核心功能

供应商中立

支持多模型

支持 100+ LLM

追踪系统

基础

完善

官方支持

社区

OpenAI 官方

结论:功能相近,Agents SDK 有官方背书。

07 依赖生态:灵活的选择

核心依赖

用途

Pydantic

数据验证

Requests

HTTP 请求

MCP Python SDK

工具协议

Griffe

文档生成

可选依赖

用途

websockets

实时通信

SQLAlchemy

数据库

any-LLM / LiteLLM

多模型支持

开发工具

工具

用途

uv

包管理

ruff

代码格式化

mypy / Pyright

类型检查

pytest

测试框架

08 我的看法:这是官方在统一多 Agent 标准

Agents SDK 的发布,我看作是一个信号。

信号一:多 Agent 是未来

OpenAI 之前 focus 在单模型能力,现在推出多 Agent 框架,说明:

多 Agent 工作流是 AI 应用的下一个战场。

单个 Agent 能力再强,也有局限。

多个 Agent 协作,可以: - 分工合作 - 互相校验 - 处理复杂任务

信号二:标准化是趋势

之前的多 Agent 框架,各有各的标准。

Agents SDK 的发布,意味着: - OpenAI 在推动标准化 - Handoff、Guardrail、Tracing 都有标准定义 - 开发者可以复用最佳实践

信号三:生产就绪是重点

与研究导向的框架不同,Agents SDK 明显更关注生产:

  • 内置追踪(方便调试)

  • 人在回路(方便审批)

  • Sandbox(方便执行真实任务)

  • 完善文档(方便上手)

这些都是生产环境需要的。

局限性

当然,Agents SDK 也有局限:

  • Python only

    :其他语言需要等 JS/TS 版本

  • 较新

    :社区生态还在建设中

  • 最佳实践

    :还在探索中

但这些会随时间完善。

2024 年,我们用单 Agent 完成简单任务。

2025 年,我们用多 Agent 协作处理复杂工作。

2026 年,多 Agent 开发变得像写普通代码一样简单。

这个变化看似微小,实则深远:

多 Agent 开发正在从"实验"变成"工程"。

Agents SDK 可能不是最终形态,但它代表了一个方向:

多 Agent 应用的开发门槛会越来越低,每个开发者都能构建复杂的多 Agent 系统。

如果你也在开发 AI 应用,不妨试试这个 SDK。

说不定,下一个爆款应用就从这里开始。

参考资料: - openai/openai-agents-python GitHub 仓库 - 官方文档 - JavaScript/TypeScript 版本 - 示例代码