To add agent sandboxes to your app, run the agent loop on your server and give the model tools that execute in a sandbox, a cloud microVM or container that holds the agent's commands, files and dev servers. With Sandbox SDK (@opencoredev/sandbox-sdk, open source), that is createSandbox(e2b()) plus sandboxTools(box), and your agent and tool code stays the same when you move to Daytona or Vercel Sandbox.
Sandbox SDK is published by OpenCore at sandbox-sdk.app and on npm as @opencoredev/sandbox-sdk. It is a separate project from sandbox-sdk.sh and the @sandbox-sdk/core package.
The samples use the API of npm release 1.4.0. The memory() sample below ran on December 1, 2026, and its output is what it printed; the E2B, Daytona and Vercel samples were not run against live accounts.
What runs where
An agent sandbox splits the work three ways:
| Piece | Where it runs | What it holds |
|---|---|---|
| Your app | Your server (Node 22+ or Bun 1.3+) | Users, model and provider keys, the agent loop |
| The model | Your model provider | The prompt and the tool results you send it |
| The sandbox | E2B, Daytona, Vercel Sandbox or another provider | The agent's files, packages, processes and preview URLs |
With a cloud provider, the model's commands never run on your server. The exceptions are local(), a Docker container on the machine that runs your code, and two backends that run inside your app's process: agentOS and the memory() test fake.
The SDK runs on the server only. Provider keys must stay out of browser bundles, and the SDK needs Node.js or Bun, so it doesn't run in the browser or in edge runtimes such as Cloudflare Workers.
1. Install the SDK
npm i @opencoredev/sandbox-sdk ai zod
ai and zod are for the AI SDK tools in step 3; Mastra and the OpenAI Agents SDK have their own imports. E2B and Daytona need no other package. Set E2B_API_KEY on your server, as the Quickstart shows, and AI_GATEWAY_API_KEY for the model in step 2 (or use any other AI SDK provider). To try it with no account at all, start with memory() (step 6).
2. Give each user or chat its own sandbox
Pass a key to createSandbox, and a later request from any server process reuses the sandbox that key created if the lookup finds it:
import { generateText, stepCountIs } from "ai";
import { createSandbox } from "@opencoredev/sandbox-sdk";
import { e2b } from "@opencoredev/sandbox-sdk/e2b";
import { sandboxTools } from "@opencoredev/sandbox-sdk/ai";
const provider = e2b();
export async function runAgent(userId: string, chatId: string, prompt: string) {
await using box = await createSandbox(provider, {
key: `user:${userId}:chat:${chatId}`,
timeoutMs: 60 * 60_000,
});
const { text } = await generateText({
model: "anthropic/claude-opus-5.5",
tools: sandboxTools(box),
stopWhen: stepCountIs(20),
prompt,
});
return text;
}
Call runAgent from a route handler after you've checked that the signed-in user owns the chat. The SDK doesn't authorize keys, so build the key from IDs you verified, never from a raw request value. The first call for a chat creates the sandbox. When a later call finds it, box.created is false and the files the agent wrote last time are still there. With a key, await using releases the handle at the end of the block and leaves the sandbox running; without one, it destroys the sandbox, even when your code throws.
timeoutMs is the safety net: E2B deletes the sandbox an hour after it was created, and later requests don't extend that. With e2b({ autoPause: true }), timeoutMs is instead the running time before E2B pauses the sandbox, and the SDK records no hard deadline.
E2B finds keyed sandboxes on a best-effort basis, so two servers can race and create one each. Create the provider once per process, as above, and if you need one sandbox per key across servers, pass keyMode: "exact" with a shared locator whose withLock holds a distributed lock. One sandbox per user covers that, setup that runs once, and reattaching to running processes.
3. Hand the model its tools
sandboxTools(box) returns seven tools in the AI SDK's format: run, read_file, write_file, edit_file, start_process, process_logs and stop_process. With a cloud provider, each one runs in the sandbox, and each returns a short result so it doesn't fill the context window:
OK exit=0 0.0s cwd=/workspace
output:
hello from the sandbox
For Mastra or the OpenAI Agents SDK, import sandboxTools from @opencoredev/sandbox-sdk/mastra or /openai instead; the tools behave the same. To narrow what the agent can do, pass allow or deny to pick tools, beforeRun to refuse or rewrite commands, and a network allow list to createSandbox. Give your agent a sandbox has every option.
4. Show the user what the agent built
When the agent starts a dev server with start_process and a port, the tool waits for it and returns its preview URL. Your code gets the same URL with const { url } = await box.preview(3000, { waitMs: 10_000 }). Put it in an iframe next to the chat, and you have the core of an AI app builder.
Previews differ more between providers than anything else here:
- Vercel Sandbox routes only to ports you list when you create the sandbox:
createSandbox(vercel(), { key, ports: [3000] }). - Daytona previews need a token header, which an iframe can't send, so serve them through an authenticated proxy on a separate preview origin that never gets your app's session cookies, or create the sandbox with
daytona({ public: true }). - E2B previews are public by default.
Ports, network rules and the provider's checks on them apply when a new sandbox is created. Reattaching to an existing sandbox by key doesn't add them.
Dev servers and previews shows readiness checks and reattaching, and Build an AI app builder wires the whole loop to a chat UI.
5. Switch providers without rewriting code
Your agent code calls box.run, box.files and sandboxTools(box), never a provider's own client, so that code stays the same when you switch. What changes is the import and factory, the install, the credentials, and sometimes the options you pass to createSandbox:
| Provider | Import | Factory | Extra package | Credentials on your server |
|---|---|---|---|---|
| E2B | @opencoredev/sandbox-sdk/e2b |
e2b() |
None | E2B_API_KEY |
| Daytona | @opencoredev/sandbox-sdk/daytona |
daytona() |
None | DAYTONA_API_KEY |
| Vercel Sandbox | @opencoredev/sandbox-sdk/vercel |
vercel() |
@vercel/sandbox |
OIDC inside a Vercel deployment; elsewhere VERCEL_TOKEN, VERCEL_TEAM_ID and VERCEL_PROJECT_ID |
The same holds for the other providers in release 1.4.0, which has 19 cloud adapters plus Docker on your own machine through local(), agentOS, and memory() for tests.
Features still differ by provider. Daytona has no pause, Vercel Sandbox needs its ports up front and applies timeoutMs to each session, capped by your Vercel plan, so lower or drop the one-hour value when you switch the sample, and E2B fixes CPU and memory in the template. A key on a new provider starts a new sandbox: files don't move over. When a new sandbox is created, limits such as size, network and timeoutMs fail with an unsupported error on a provider that can't enforce them instead of being ignored (ports is different: providers that don't need it ignore it), so check the provider feature table for the options you pass. For cold starts and prices, see the measured numbers on AI Sandbox Bench; Choose a provider covers the API differences.
6. Test it without a cloud
memory() is a fake provider that runs in your process, with files in a map and a small shell. Swap it in for e2b() and test your sandbox code with no account and no network:
import { createSandbox } from "@opencoredev/sandbox-sdk";
import { memory } from "@opencoredev/sandbox-sdk/memory";
import { sandboxTools } from "@opencoredev/sandbox-sdk/ai";
const provider = memory();
for (const visit of [1, 2]) {
await using box = await createSandbox(provider, { key: "chat:42" });
console.log(visit, box.created, Object.keys(sandboxTools(box)).length);
}
1 true 7
2 false 7
The second visit finds the sandbox the first one created. Testing with memory() shows how to fake commands such as npm test with handlers. memory() runs handlers with your process's privileges, so it's for tests only.
Common questions
Do the agent's commands run on my server? Not with a cloud provider: every tool call runs in the sandbox, a microVM or container at the provider. local() runs a Docker container on the machine that runs your code, and agentOS and memory() run inside your app's process; read Security before you give them untrusted prompts.
Is Sandbox SDK open source? Yes, MIT-licensed, on GitHub. It isn't a hosted service: it calls each provider's API with your own keys, and the provider bills you directly.
Is this sandbox-sdk.sh or @sandbox-sdk/core? No. This is OpenCore's @opencoredev/sandbox-sdk at sandbox-sdk.app, a separate project.
Which provider should I start with? Whichever you already have an account with. Your agent code doesn't depend on the choice, so you can move later. AI Sandbox Bench publishes measured comparisons.
Can I use OpenAI's or Mastra's own sandbox instead? Yes. The OpenAI Agents SDK's SandboxAgent and Mastra's Workspace, which configures a Mastra sandbox, are framework-owned options. OpenAI Agents SDK sandbox in TypeScript compares SandboxAgent with sandboxTools on a regular Agent.
Next steps
- Quickstart: run your first sandbox in memory, then in Docker, then in the cloud.
- Docker sandbox for AI agents in TypeScript: develop against a local container, then move the same code to a cloud provider.
- Node.js sandbox: vm, isolated-vm, Docker or a microVM: why code a model wrote belongs outside your process.
More guides on the Sandbox SDK blog, or start with the quickstart.