跳转至

快速入门

Beta 功能

沙盒智能体目前为 beta 版本。在正式可用前,API 细节、默认值和支持能力都可能发生变化,并且后续会逐步提供更高级的功能。

现代智能体在能够对文件系统中的真实文件进行操作时效果最佳。Agents SDK 中的沙盒智能体为模型提供了一个持久化工作区,它可以在其中检索大型文档集、编辑文件、运行命令、生成产物,并从已保存的沙盒状态继续工作。

SDK 为你提供了这套执行框架,无需你自行拼接文件暂存、文件系统工具、shell 访问、沙盒生命周期、快照以及特定提供方的胶水层。你可以继续使用常规的 AgentRunner 流程,然后为工作区添加 Manifest、为沙盒原生工具添加 capabilities,并通过 SandboxRunConfig 指定工作运行位置。

前提条件

  • Python 3.10 或更高版本
  • 对 OpenAI Agents SDK 有基本了解
  • 一个沙盒客户端。用于本地开发时,可从 UnixLocalSandboxClient 开始。

安装

如果你尚未安装 SDK:

pip install openai-agents

对于基于 Docker 的沙盒:

pip install "openai-agents[docker]"

创建本地沙盒智能体

此示例会在 repo/ 下暂存本地仓库、按需延迟加载本地技能,并让 runner 在运行时创建 Unix 本地沙盒会话。

import asyncio
from pathlib import Path

from agents import Runner
from agents.run import RunConfig
from agents.sandbox import Manifest, SandboxAgent, SandboxRunConfig
from agents.sandbox.capabilities import Capabilities, LocalDirLazySkillSource, Skills
from agents.sandbox.entries import LocalDir
from agents.sandbox.sandboxes.unix_local import UnixLocalSandboxClient

EXAMPLE_DIR = Path(__file__).resolve().parent
HOST_REPO_DIR = EXAMPLE_DIR / "repo"
HOST_SKILLS_DIR = EXAMPLE_DIR / "skills"


def build_agent(model: str) -> SandboxAgent[None]:
    return SandboxAgent(
        name="Sandbox engineer",
        model=model,
        instructions=(
            "Read `repo/task.md` before editing files. Stay grounded in the repository, preserve "
            "existing behavior, and mention the exact verification command you ran. "
            "If you edit files with apply_patch, paths are relative to the sandbox workspace root."
        ),
        default_manifest=Manifest(
            entries={
                "repo": LocalDir(src=HOST_REPO_DIR),
            }
        ),
        capabilities=Capabilities.default() + [
            Skills(
                lazy_from=LocalDirLazySkillSource(
                    source=LocalDir(src=HOST_SKILLS_DIR),
                )
            ),
        ],
    )


async def main() -> None:
    result = await Runner.run(
        build_agent("gpt-5.4"),
        "Open `repo/task.md`, fix the issue, run the targeted test, and summarize the change.",
        run_config=RunConfig(
            sandbox=SandboxRunConfig(client=UnixLocalSandboxClient()),
            workflow_name="Sandbox coding example",
        ),
    )
    print(result.final_output)


if __name__ == "__main__":
    asyncio.run(main())

参见 examples/sandbox/docs/coding_task.py。它使用了一个基于 shell 的微型仓库,因此该示例可以在 Unix 本地运行中进行确定性验证。

关键选择

当基础运行可用后,大多数人接下来会关注这些选项:

  • default_manifest:用于新沙盒会话的文件、仓库、目录和挂载
  • instructions:应在各类提示词中统一适用的简短工作流规则
  • base_instructions:用于替换 SDK 沙盒提示词的高级兜底机制
  • capabilities:沙盒原生工具,例如文件系统编辑/图像检查、shell、skills、memory 和 compaction
  • run_as:面向模型工具使用的沙盒用户身份
  • SandboxRunConfig.client:沙盒后端
  • SandboxRunConfig.sessionsession_statesnapshot:后续运行如何重新连接到先前工作

后续方向

  • 概念:了解 manifest、capabilities、权限、快照、运行配置和组合模式。
  • 沙盒客户端:选择 Unix 本地、Docker、托管提供方和挂载策略。
  • 智能体记忆:保存并复用先前沙盒运行中的经验。

如果 shell 访问只是偶尔使用的一种工具,请先从 工具指南 中的托管 shell 开始。当工作区隔离、沙盒客户端选择或沙盒会话恢复行为是设计的一部分时,再使用沙盒智能体。