跳到主要内容

编程智能体

Claude Code

在独立沙箱中运行 Claude Code,配备真实的文件系统、终端和 git。预构建的 claude 模板已安装好 Claude Code,它可以无人值守地工作,您只需取回 diff。

维护方
E2B
集成方式
预构建的 claude 沙箱模板
软件包
e2b
语言
TypeScript, Python

配置

快速开始

将提示词粘贴到您的编程智能体中,或按以下步骤自行配置。

粘贴到您的编程智能体中

在此项目中,于 E2B 沙箱内运行 Claude Code。

  1. 1.使用项目的包管理器安装 e2b SDK。
  2. 2.SDK 需要 E2B_API_KEY 环境变量,Claude Code 需要 ANTHROPIC_API_KEY。如果尚未设置 E2B_API_KEY,请向我索要 API 密钥(在 https://console.e2b.dev/?tab=keys 获取)。切勿将任何一个密钥硬编码。
  3. 3.按照 https://docs.e2b.dev/agents/claude-code 上的 Claude Code 指南操作:从 claude 模板创建沙箱,在 envs 中传入 ANTHROPIC_API_KEY,并以 --dangerously-skip-permissions 和为 0 的命令超时运行 claude -p。在 finally 块中终止沙箱。
  4. 4.运行一个任务,并展示来自沙箱的输出。

或自行配置

  1. 1. 安装 E2B SDK

    npm init -y && npm pkg set type=module
    npm install e2b
    npm install -D tsx
  2. 2. 设置 API 密钥

    export E2B_API_KEY="e2b_..."
    export ANTHROPIC_API_KEY="..."
  3. 3. 以 headless 模式运行 Claude Code

    import { Sandbox } from 'e2b'
    
    const apiKey = process.env.ANTHROPIC_API_KEY
    if (!apiKey) throw new Error('Set ANTHROPIC_API_KEY')
    
    const sandbox = await Sandbox.create('claude', {
      envs: { ANTHROPIC_API_KEY: apiKey },
    })
    
    try {
      const result = await sandbox.commands.run(
        `claude --dangerously-skip-permissions -p "Create a hello world HTTP server in Go"`,
        { cwd: '/home/user', timeoutMs: 0 }
      )
      console.log(result.stdout)
    } finally {
      await sandbox.kill()
    }
  4. 4. 处理代码仓库

    await sandbox.commands.run(
      'git clone --depth 1 https://github.com/e2b-dev/claude-code-fastapi.git /home/user/repo'
    )
    
    await sandbox.commands.run(
      `claude --dangerously-skip-permissions -p "Add error handling to all API endpoints"`,
      { cwd: '/home/user/repo', timeoutMs: 0 }
    )
    
    const diff = await sandbox.commands.run('git diff', { cwd: '/home/user/repo' })
    console.log(diff.stdout)

安全

沙箱中运行的内容

Claude Code 及其运行的每条命令都在沙箱中,与您的机器和文件隔离。

  • 智能体claude CLI,会自动批准自己的工具调用。这些调用被限制在沙箱内,无法触及您的主机。
  • 文件您克隆的代码仓库,以及 Claude Code 写入的所有内容。可用 files.read() 或 git diff 读回结果。
  • 会话对话保存在沙箱中。传入 -⁠-⁠resume 和会话 ID,即可发送后续任务。
  • 网络默认可以访问互联网。处理不受信任的输入或密钥时,请用网络规则限制出站流量。

已启动 30亿+ 个沙箱

为每个智能体 配一台机器