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 環境,可透過環境變數或明確設定自動偵測端點。提供兩種互動模式:獨立請求的無狀態動作呼叫,以及具即時事件訂閱功能的有狀態連線。包含低階 HTTP 與 WebSocket 存取,適用於實作 onRequest 或 onWebSocket 處理器的 Actors。提供複合陣列式...
official
rivetkit-client-react
rivet-dev
用於連接Rivet Actors的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 客戶端,支援無狀態與有狀態連線模式。提供無狀態動作呼叫及持久化 WebSocket 連線,並透過 AsyncStream 實現即時事件串流。支援 0 至 5 個位置引數的型別化動作呼叫,以及針對更多引數的原始 JSON 回退方案。透過狀態變更、錯誤、開啟與關閉串流,實現連線生命週期監控。支援低階 HTTP 與 WebSocket 存取,適用於自訂 onRequest 及...
official