Skip to main content
The AI SDK is Vercel’s TypeScript toolkit for building agents. AI SDK 7 introduces a sandbox interface — a standard surface (run, spawn, file I/O) that tools and harness agents execute code against. @e2b/ai-sdk-sandbox implements that interface on E2B: your agent’s code runs in an isolated Firecracker microVM instead of your server, with a real Debian toolchain, pause/resume persistence, and outbound network controls. The package is the E2B counterpart to @ai-sdk/sandbox-vercel. It is experimental and targets AI SDK 7.

Install

Set E2B_API_KEY (get one from the dashboard), or pass apiKey in the settings.

Quickstart

createE2BSandbox() configures the provider without reaching E2B; the sandbox is created when you call createSession().
The session also supports readTextFile, and spawn for long-running processes with streamed stdout/stderr and kill().

Hand tools a sandbox

session.restricted() returns the same sandbox narrowed to the tool-safe surface — file I/O, run, spawn — with the lifecycle and network controls (stop, destroy, ports, getPortUrl, setNetworkPolicy) removed. That is the security boundary: pass the restricted view to an AI SDK tool’s execute() via experimental_sandbox, and keep the full session with your application. Code the model drives can execute commands, but it cannot kill the box or loosen its network policy.
See the harness docs for the restricted() contract and the experimental_sandbox wiring in harness-managed tools.

Settings

Everything goes in the object passed to createE2BSandbox(...). Any of E2B’s SandboxOpts (template, envs, metadata, network, …) are forwarded straight through, and the provider adds two options of its own:
Already have a sandbox? Pass it as sandbox to reuse it. The provider then never touches its lifecycle — stop() and destroy() become no-ops and cleanup stays yours.

Run a coding agent in the sandbox

The provider plugs into AI SDK harness agents: the harness adapter (Claude Code, Codex) runs inside the E2B sandbox, and the host talks to it over a WebSocket bridge on the first advertised port.
The claude-code adapter installs its own pinned CLI and bridge inside the sandbox with pnpm, so pass a template with pnpm preinstalled.

Pause and resume

stop() pauses the sandbox — filesystem and memory preserved — rather than killing it; destroy() removes it for good. Resume works off a session-id tag in the sandbox metadata, so pass a sessionId to createSession if you plan to resume, then reconnect with resumeSession — even from a different process.

Network policy

Outbound access can be tightened or loosened on a running sandbox — from the full session only, never from the restricted view:
Hostname rules apply to HTTP(S) traffic only; use CIDR rules for other protocols. See internet access.

Good to know

  • run and spawn switch off E2B’s 60-second per-command timeout, so long builds and background servers don’t get cut off. The overall sandbox timeoutMs still applies.
  • Relative paths in file operations and commands resolve against the session’s working directory (/home/user).
  • The default sandbox timeout is 30 minutes; pass timeoutMs to change it.

Templates

Build custom sandbox templates with pre-installed dependencies

Sandbox persistence

Pause, resume, and manage sandbox lifecycle

Internet access

Restrict outbound traffic with network rules