跳转至

OpenAI 于 2026 年 4 月发布了 Agents SDK 的重大更新,这是自 Swarm 实验项目以来的最大升级。新功能包括原生沙盒执行、模型原生执行框架、MCP 协议支持等。本文将带你从入门到生产,全面掌握 OpenAI Agents SDK 2026 的核心特性与实战技巧。

什么是 OpenAI Agents SDK?

OpenAI Agents SDK 是 OpenAI 官方推出的 Python 库,用于构建生产级的 AI Agent 应用。它提供了一套简洁而强大的 API,让开发者能够快速创建具备工具调用、多轮对话、安全防护等能力的智能体。

从 Swarm 到 Agents SDK 的演进

2024 年,OpenAI 发布了 Swarm 作为实验性多智能体框架。虽然 Swarm 展示了多 Agent 协作的可能性,但在生产环境中缺乏必要的安全机制和持久化支持。

2026 年 4 月,OpenAI 推出了全新的 Agents SDK,彻底重构了架构:

  • 原生沙盒执行:代码在隔离环境中运行,防止恶意操作
  • 模型原生 Harness:配置化记忆和编排能力
  • MCP 协议支持:与外部工具生态无缝集成
  • 企业级安全:内置 Guardrails 和输入验证

核心设计原则

Agents SDK 遵循以下设计原则:

  1. 简洁优先:用最少的代码实现复杂功能
  2. 安全第一:默认启用安全防护机制
  3. 可扩展:支持自定义工具和中间件
  4. 生产就绪:内置追踪、监控和错误处理

2026 年 4 月重大更新详解

沙盒执行(Sandbox Execution)

沙盒执行是 Agents SDK 2026 最重磅的功能。它允许 Agent 在隔离环境中安全地运行代码,无需担心安全风险。

核心特性

  • 隔离环境:每个 Agent 运行在独立的容器中
  • 文件系统访问:可读写临时文件,支持依赖安装
  • 网络控制:可配置网络访问权限
  • 资源限制:CPU、内存、执行时间均可限制

Agents SDK 目前支持多家沙盒提供商:

模型原生 Harness 架构

Harness 是 Agents SDK 的核心编排层,它负责管理 Agent 的执行流程:

配置化记忆(Configurable Memory)

from agents import Agent, Runner, Memory

# 配置持久化记忆
memory = Memory(
    type="persistent",
    storage="redis",  # 支持 redis、postgres、sqlite
    ttl=3600  # 记忆过期时间
)

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant",
    memory=memory
)

沙盒感知编排

Harness 能够智能判断何时需要启动沙盒环境,自动管理资源生命周期。

MCP 协议原生支持

MCP(Model Context Protocol) 是 Anthropic 推出的开放协议,用于标准化 AI 模型与外部工具的交互。OpenAI Agents SDK 2026 原生支持 MCP,这意味着你可以:

  • 使用任何兼容 MCP 的工具
  • 与 Claude Code 共享工具生态
  • 构建可移植的 Agent 应用
from agents import Agent, MCPTools

# 连接到 MCP 服务器
mcp_tools = MCPTools.from_server("http://localhost:3000/sse")

agent = Agent(
    name="MCP Agent",
    instructions="Use available tools to help the user",
    tools=mcp_tools.get_tools()
)

快速入门:构建你的第一个 Agent

环境安装与配置

首先,确保你有一个 OpenAI API Key。然后安装 Agents SDK:

pip install openai-agents

设置环境变量:

export OPENAI_API_KEY="your-api-key-here"

Hello World 示例

创建一个最简单的 Agent:

from agents import Agent, Runner

# 创建 Agent
agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant"
)

# 运行 Agent
result = Runner.run_sync(agent, "Write a haiku about recursion.")
print(result.final_output)

输出示例:

Code calls itself deep,
Infinite mirrors reflect—
Base case breaks the loop.

添加工具调用

让 Agent 具备实际能力:

from agents import Agent, Runner, function_tool
from pydantic import BaseModel

# 定义工具参数
class WeatherInput(BaseModel):
    city: str
    unit: str = "celsius"

@function_tool
async def get_weather(input: WeatherInput) -> str:
    """Get weather information for a city."""
    # 这里可以调用真实的天气 API
    return f"The weather in {input.city} is 22°{input.unit[0].upper()}"

# 创建带工具的 Agent
agent = Agent(
    name="Weather Assistant",
    instructions="You help users with weather information.",
    tools=[get_weather]
)

# 运行
result = Runner.run_sync(agent, "What's the weather like in Tokyo?")
print(result.final_output)

实战:构建一个带沙盒的代码审查 Agent

场景描述

我们将构建一个能够审查 Python 代码的 Agent,它可以:

  1. 在沙盒环境中运行代码
  2. 检查代码风格和潜在问题
  3. 生成详细的审查报告

完整代码实现

import asyncio
from agents import Agent, Runner, SandboxAgent, function_tool
from pydantic import BaseModel
from typing import List

class CodeReviewInput(BaseModel):
    code: str
    filename: str = "script.py"

class CodeReviewResult(BaseModel):
    issues: List[str]
    suggestions: List[str]
    execution_output: str
    passed: bool

@function_tool
async def run_code_in_sandbox(input: CodeReviewInput) -> str:
    """Execute Python code in a secure sandbox environment."""
    # 使用 SandboxAgent 执行代码
    sandbox = SandboxAgent(
        provider="e2b",  # 使用 E2B 沙盒
        timeout=30,  # 30 秒超时
        memory_limit="512mb",
        cpu_limit=1.0
    )

    # 写入代码文件
    await sandbox.write_file(f"/home/user/{input.filename}", input.code)

    # 运行代码
    result = await sandbox.execute(
        f"python /home/user/{input.filename}",
        env={"PYTHONUNBUFFERED": "1"}
    )

    return result.stdout + result.stderr

@function_tool
async def analyze_code_style(code: str) -> List[str]:
    """Analyze code style using pylint in sandbox."""
    sandbox = SandboxAgent(provider="e2b")

    # 安装 pylint
    await sandbox.execute("pip install pylint -q")

    # 写入代码
    await sandbox.write_file("/home/user/temp.py", code)

    # 运行 pylint
    result = await sandbox.execute("pylint /home/user/temp.py --output-format=text")

    issues = []
    for line in result.stdout.split("\n"):
        if ":" in line and any(severity in line for severity in ["E", "W", "C", "R"]):
            issues.append(line.strip())

    return issues

# 创建代码审查 Agent
code_reviewer = Agent(
    name="Code Reviewer",
    instructions="""You are an expert Python code reviewer. Your task is to:
1. Run the code in a sandbox to check for runtime errors
2. Analyze code style and best practices
3. Provide actionable suggestions for improvement
4. Generate a comprehensive review report

Be thorough but constructive in your feedback.""",
    tools=[run_code_in_sandbox, analyze_code_style],
    model="gpt-4o"  # 使用更强的模型进行代码分析
)

async def review_code(user_code: str, filename: str = "script.py"):
    """Review code using the Code Reviewer Agent."""
    prompt = f"""Please review the following Python code:

Filename: {filename}

```python
{user_code}

Please: 1. Run the code and report any execution errors 2. Check code style issues 3. Provide specific suggestions for improvement 4. Give an overall assessment (PASS or FAIL)

Format your response as a structured code review report."""

result = await Runner.run(code_reviewer, prompt)
return result.final_output

示例代码进行审查

sample_code = ''' def calculate_sum(numbers): total = 0 for n in numbers: total = total + n return total

result = calculate_sum([1, 2, 3, 4, 5]) print(f"Sum: {result}") '''

运行审查

if name == "main": review = asyncio.run(review_code(sample_code, "calculate_sum.py")) print(review)


### 运行与测试

运行上述代码,Agent 将:

1. 在 E2B 沙盒中执行代码
2. 使用 pylint 检查代码风格
3. 生成包含执行结果、风格问题和改进建议的完整报告

**输出示例**:

Code Review Report for calculate_sum.py

Execution Results ✅

  • Status: Success
  • Output: Sum: 15
  • Runtime: 0.23s

Style Analysis ⚠️

  • Missing module docstring
  • Function lacks type hints
  • Variable 'total' could use augmented assignment (total += n)

Suggestions for Improvement

  1. Add type hints: def calculate_sum(numbers: List[int]) -> int:
  2. Use built-in sum() function for simplicity
  3. Add docstring explaining the function's purpose
  4. Consider handling empty list edge case

Overall Assessment: PASS with recommendations


## 安全防护与最佳实践

### Guardrails 配置

Agents SDK 提供多层安全防护:

```python
from agents import Agent, Guardrails, InputGuardrail, OutputGuardrail

# 输入验证
def validate_input(context) -> bool:
    """Check if input is safe to process."""
    forbidden_patterns = ["rm -rf", "exec(", "eval("]
    return not any(pattern in context.user_input for pattern in forbidden_patterns)

# 输出过滤
def filter_output(response) -> str:
    """Filter sensitive information from output."""
    # 移除可能的 API keys、密码等
    import re
    response = re.sub(r'sk-[a-zA-Z0-9]{48}', '[API_KEY_REDACTED]', response)
    return response

agent = Agent(
    name="Safe Agent",
    instructions="You are a helpful assistant",
    guardrails=Guardrails(
        input_guardrails=[InputGuardrail(check=validate_input)],
        output_guardrails=[OutputGuardrail(filter=filter_output)]
    )
)

输入验证

使用 Pydantic 进行严格的输入验证:

from pydantic import BaseModel, Field, validator

class SafeCodeInput(BaseModel):
    code: str = Field(..., max_length=5000)
    language: str = Field(default="python", regex="^(python|javascript|bash)$")

    @validator('code')
    def check_forbidden_patterns(cls, v):
        forbidden = ['import os', 'import subprocess', '__import__']
        for pattern in forbidden:
            if pattern in v.lower():
                raise ValueError(f"Forbidden pattern detected: {pattern}")
        return v

错误处理

生产环境中必须做好错误处理:

from agents import Agent, Runner
from agents.exceptions import AgentError, ToolError, SandboxError

async def safe_run(agent: Agent, prompt: str):
    try:
        result = await Runner.run(agent, prompt)
        return result.final_output
    except SandboxError as e:
        # 沙盒执行失败
        return f"Sandbox execution failed: {e.message}"
    except ToolError as e:
        # 工具调用失败
        return f"Tool execution error: {e.message}"
    except AgentError as e:
        # Agent 内部错误
        return f"Agent error: {e.message}"
    except Exception as e:
        # 未知错误
        return f"Unexpected error: {str(e)}"

Agents SDK vs 其他框架

vs LangGraph

特性 Agents SDK LangGraph
学习曲线
多 Agent 编排 内置 需配置
沙盒支持 原生 需集成
MCP 支持 原生 需适配
可视化 内置追踪 LangSmith
适用场景 快速开发 复杂工作流

选择建议:快速原型开发选 Agents SDK,复杂企业工作流选 LangGraph。

vs CrewAI

CrewAI 专注于多 Agent 协作场景:

  • Agents SDK:单 Agent 能力强,沙盒执行是优势
  • CrewAI:多 Agent 角色扮演和任务委托更成熟

选择建议:需要角色扮演和 Agent 协作选 CrewAI,需要安全代码执行选 Agents SDK。

vs Claude Code SDK

Anthropic 的 Claude Code 也提供 Agent 能力:

  • Agents SDK:与 OpenAI 模型深度集成,工具生态丰富
  • Claude Code:与 Claude 3.5/3.7 Sonnet 配合最佳,代码理解能力强

选择建议:使用 OpenAI 模型选 Agents SDK,使用 Claude 模型选 Claude Code。

生产环境部署建议

持久化与状态管理

生产环境中需要持久化 Agent 状态:

from agents import Agent, Memory, RedisStorage

# 使用 Redis 作为状态存储
storage = RedisStorage(
    host="localhost",
    port=6379,
    db=0,
    password="your-password"
)

memory = Memory(
    type="persistent",
    storage=storage,
    ttl=86400  # 24小时过期
)

agent = Agent(
    name="Stateful Agent",
    instructions="You remember previous conversations",
    memory=memory
)

监控与追踪

Agents SDK 内置追踪功能:

from agents import Agent, Runner, Tracing

# 启用详细追踪
Tracing.configure(
    enabled=True,
    endpoint="https://api.openai.com/v1/traces",
    sample_rate=1.0  # 100% 采样
)

# 运行时会自动记录所有步骤
result = Runner.run_sync(agent, "Hello")

# 查看追踪信息
print(result.trace_id)  # 可用于在 OpenAI 平台查看详细追踪

成本控制

Agent 应用容易产生高额 API 费用,建议:

  1. 使用合适的模型:简单任务用 GPT-4o-mini,复杂任务用 GPT-4o
  2. 设置 token 限制
agent = Agent(
    name="Cost-conscious Agent",
    instructions="Be concise in your responses",
    model_settings={
        "max_tokens": 500,
        "temperature": 0.3  # 降低随机性,减少 token 消耗
    }
)
  1. 缓存常见响应
from functools import lru_cache

@lru_cache(maxsize=1000)
def cached_agent_run(prompt: str) -> str:
    result = Runner.run_sync(agent, prompt)
    return result.final_output

总结与展望

核心要点回顾

OpenAI Agents SDK 2026 带来了革命性的更新:

  1. 沙盒执行:安全运行代码,消除安全风险
  2. MCP 支持:与外部工具生态无缝集成
  3. Harness 架构:配置化记忆和编排能力
  4. 生产就绪:内置安全防护和监控追踪

即将推出的功能

根据 OpenAI 官方公告,以下功能即将发布:

  • Subagents:子智能体支持,实现更复杂的 Agent 层级
  • Code Mode:专为代码生成和编辑优化的模式
  • TypeScript 支持:目前仅 Python,TS 版本即将发布

学习资源推荐

相关文章


OpenAI Agents SDK 2026 是构建生产级 AI Agent 的最佳选择之一。无论你是想快速原型开发,还是构建企业级应用,它都能提供所需的功能和安全性。现在就开始你的 Agents SDK 之旅吧!