sandbox-agent

作者: rivet-dev

在沙盒环境中编排多个AI编码代理的通用API。支持四种编码代理(Claude Code、Codex、OpenCode、Amp),提供统一的响应、事件和会话管理架构。可部署到本地或云端沙盒,包括E2B、Daytona、Docker、Vercel和Cloudflare,并支持环境特定配置。流式传输代理事件,管理跨多个后端(内存、SQLite、Postgres、IndexedDB)的会话持久化,并处理人工介入...

npx skills add https://github.com/rivet-dev/skills --skill sandbox-agent

Sandbox Agent

Sandbox Agent provides a universal API for orchestrating AI coding agents in sandboxed environments.

What To Do If You Get Stuck

If something is not working as intended or you are stuck, prompt the user to join the Rivet Discord or file an issue on GitHub to report an issue and get help.

Provide the user with a pre-generated report with:

  • Symptoms
    • If this is happening in local dev, deployed, or both
    • The error you're seeing
    • Relevant source code related to this
  • What you've tried to solve it
  • Environment
    • Sandbox Agent version
    • SDK in use (TypeScript, Python, or HTTP)
    • Agent in use (Claude Code, Codex, OpenCode, Amp, Mock)
    • Sandbox provider (E2B, Daytona, Docker, local)

Quickstart

Install skill (optional)

npx

npx skills add rivet-dev/skills -s sandbox-agent

bunx

bunx skills add rivet-dev/skills -s sandbox-agent

Set environment variables

Each coding agent requires API keys to connect to their respective LLM providers.

Local shell

export ANTHROPIC_API_KEY="sk-ant-..."
export OPENAI_API_KEY="sk-..."

E2B

import { Sandbox } from "@e2b/code-interpreter";

const envs: Record<string, string> = {};
if (process.env.ANTHROPIC_API_KEY) envs.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
if (process.env.OPENAI_API_KEY) envs.OPENAI_API_KEY = process.env.OPENAI_API_KEY;

const sandbox = await Sandbox.create({ envs });

Daytona

import { Daytona } from "@daytonaio/sdk";

const envVars: Record<string, string> = {};
if (process.env.ANTHROPIC_API_KEY) envVars.ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY;
if (process.env.OPENAI_API_KEY) envVars.OPENAI_API_KEY = process.env.OPENAI_API_KEY;

const daytona = new Daytona();
const sandbox = await daytona.create({
  snapshot: "sandbox-agent-ready",
  envVars,
});

Docker

docker run -p 2468:2468 \
  -e ANTHROPIC_API_KEY="sk-ant-..." \
  -e OPENAI_API_KEY="sk-..." \
  rivetdev/sandbox-agent:0.4.2-full \
  server --no-token --host 0.0.0.0 --port 2468

Extracting API keys from current machine

Use sandbox-agent credentials extract-env --export to extract your existing API keys (Anthropic, OpenAI, etc.) from local Claude Code or Codex config files.

Testing without API keys

Use the mock agent for SDK and integration testing without provider credentials.

Multi-tenant and per-user billing

For per-tenant token tracking, budget enforcement, or usage-based billing, see LLM Credentials for gateway options like OpenRouter, LiteLLM, and Portkey.

Run the server

curl

Install and run the binary directly.

curl -fsSL https://releases.rivet.dev/sandbox-agent/0.4.x/install.sh | sh
sandbox-agent server --no-token --host 0.0.0.0 --port 2468

npx

Run without installing globally.

npx @sandbox-agent/[email protected] server --no-token --host 0.0.0.0 --port 2468

bunx

Run without installing globally.

bunx @sandbox-agent/[email protected] server --no-token --host 0.0.0.0 --port 2468

npm i -g

Install globally, then run.

npm install -g @sandbox-agent/[email protected]
sandbox-agent server --no-token --host 0.0.0.0 --port 2468

bun add -g

Install globally, then run.

bun add -g @sandbox-agent/[email protected]
# Allow Bun to run postinstall scripts for native binaries (required for SandboxAgent.start()).
bun pm -g trust @sandbox-agent/cli-linux-x64 @sandbox-agent/cli-linux-arm64 @sandbox-agent/cli-darwin-arm64 @sandbox-agent/cli-darwin-x64 @sandbox-agent/cli-win32-x64
sandbox-agent server --no-token --host 0.0.0.0 --port 2468

Node.js (local)

For local development, use SandboxAgent.start() to spawn and manage the server as a subprocess.

npm install [email protected]
import { SandboxAgent } from "sandbox-agent";

const sdk = await SandboxAgent.start();

Bun (local)

For local development, use SandboxAgent.start() to spawn and manage the server as a subprocess.

bun add [email protected]
# Allow Bun to run postinstall scripts for native binaries (required for SandboxAgent.start()).
bun pm trust @sandbox-agent/cli-linux-x64 @sandbox-agent/cli-linux-arm64 @sandbox-agent/cli-darwin-arm64 @sandbox-agent/cli-darwin-x64 @sandbox-agent/cli-win32-x64
import { SandboxAgent } from "sandbox-agent";

const sdk = await SandboxAgent.start();

Build from source

If you're running from source instead of the installed CLI.

cargo run -p sandbox-agent -- server --no-token --host 0.0.0.0 --port 2468

Binding to 0.0.0.0 allows the server to accept connections from any network interface, which is required when running inside a sandbox where clients connect remotely.

Configuring token

Tokens are usually not required. Most sandbox providers (E2B, Daytona, etc.) already secure networking at the infrastructure layer.

If you expose the server publicly, use --token "$SANDBOX_TOKEN" to require authentication:

sandbox-agent server --token "$SANDBOX_TOKEN" --host 0.0.0.0 --port 2468

Then pass the token when connecting:

TypeScript

import { SandboxAgent } from "sandbox-agent";

const sdk = await SandboxAgent.connect({
  baseUrl: "http://your-server:2468",
  token: process.env.SANDBOX_TOKEN,
});

curl

curl "http://your-server:2468/v1/health" \
  -H "Authorization: Bearer $SANDBOX_TOKEN"

CLI

sandbox-agent --token "$SANDBOX_TOKEN" api agents list \
  --endpoint http://your-server:2468

CORS

If you're calling the server from a browser, see the CORS configuration guide.

Install agents (optional)

To preinstall agents:

sandbox-agent install-agent --all

If agents are not installed up front, they are lazily installed when creating a session.

Install desktop dependencies (optional, Linux only)

If you want to use /v1/desktop/*, install the desktop runtime packages first:

sandbox-agent install desktop --yes

Then use GET /v1/desktop/status or sdk.getDesktopStatus() to verify the runtime is ready before calling desktop screenshot or input APIs.

Create a session

import { SandboxAgent } from "sandbox-agent";

const sdk = await SandboxAgent.connect({
  baseUrl: "http://127.0.0.1:2468",
});

const session = await sdk.createSession({
  agent: "claude",
  sessionInit: {
    cwd: "/",
    mcpServers: [],
  },
});

console.log(session.id);

Send a message

const result = await session.prompt([
  { type: "text", text: "Summarize the repository and suggest next steps." },
]);

console.log(result.stopReason);

Read events

const off = session.onEvent((event) => {
  console.log(event.sender, event.payload);
});

const page = await sdk.getEvents({
  sessionId: session.id,
  limit: 50,
});

console.log(page.items.length);
off();

Test with Inspector

Open the Inspector UI at /ui/ on your server (for example, http://localhost:2468/ui/) to inspect sessions and events in a GUI.

Sandbox Agent Inspector

Next steps

  • Session Persistence — Configure in-memory, Rivet Actor state, IndexedDB, SQLite, and Postgres persistence.

  • Deploy to a Sandbox — Deploy your agent to E2B, Daytona, Docker, Vercel, or Cloudflare.

  • SDK Overview — Use the latest TypeScript SDK API.

Reference Map

Agents

AI

Deploy

General

来自 rivet-dev 的更多技能

rivetkit-actors
rivet-dev
使用 Rivet Actors 构建有状态后端:高性能、长生命周期、内存驻留、持久化进程。当 HTTP、数据库或队列无法满足需求时使用。…
official
agent-browser
rivet-dev
自动化浏览器交互,用于网页测试、表单填写、截图和数据提取。当用户需要浏览网站、与网页交互时使用…
official
multiplayer-game
rivet-dev
构建多人游戏的实用模式:匹配机制、滴答循环、实时状态、兴趣管理及验证。包含10种游戏类型(大逃杀、竞技场、IO风格、开放世界、派对、2D/3D物理、排位、回合制、放置类)的入门代码与架构模式,涵盖角色拓扑、生命周期图及网络代码策略。涉及服务器模拟基础:固定实时循环与动作驱动更新对比、物理引擎选择(Rapier 2D/3D)及空间...
official
rivetkit
rivet-dev
为AI代理、多人应用和工作流自动化提供长期运行、有状态的计算能力。Actor是持久化的内存进程,无需数据库往返即可在请求间保持状态,并支持从零到数百万并发实例的自动扩展。内置通过WebSocket和事件的实时通信、用于有序消息处理的持久队列,以及用于结构化数据查询的SQLite。通过Rivet Cloud或自托管方式,状态可在重启和崩溃后持续保留...
official
rivetkit-actors
rivet-dev
使用 Rivet Actors 构建有状态后端:高性能、长生命周期、内存驻留、持久化进程。当 HTTP、数据库或队列无法满足需求时使用。…
official
rivetkit-client-javascript
rivet-dev
用于连接Rivet Actors的JavaScript客户端,支持无状态和有状态连接。兼容浏览器、Node.js和Bun环境,可通过环境变量自动检测端点或进行显式配置。提供两种交互模式:用于独立请求的无状态操作调用,以及支持实时事件订阅的有状态连接。包含针对实现onRequest或onWebSocket处理器的Actor的低级HTTP和WebSocket访问。提供复合数组式...
official
rivetkit-client-react
rivet-dev
用于连接Rivet Actor的React客户端,提供钩子和实时状态管理。通过createRivetKit()创建类型化钩子,使用useActor()通过键和可选参数连接actor实例。通过useEvent()订阅actor事件,并通过connStatus和error状态监控连接生命周期。使用createClient()进行无状态一次性调用、actor发现方法(get、getOrCreate、create、getForId)以及底层HTTP/WebSocket访问。支持复合数组键...
official
rivetkit-client-swift
rivet-dev
用于连接Rivet Actors的Swift客户端,支持无状态和有状态连接模式。支持无状态动作调用和通过AsyncStream实现实时事件流的持久WebSocket连接。提供0-5个位置参数的类型化动作调用,以及用于更多参数的原始JSON回退。通过状态变化、错误、打开和关闭流监控连接生命周期。支持底层HTTP和WebSocket访问,用于自定义onRequest和...
official