Sandboxes
A Durable Object has no file system and can’t run a process. Tools that need files or a shell work in a sandbox: a Linux machine on a sandbox platform. Two services split the work. AI.Sandboxes is a Layer you provide once: it opens, suspends and removes a sandbox on your platform. AI.Sandbox is what a tool uses: the files and shell of the conversation its call belongs to.
Take a Coder Durable Object with two conversations, pr-142 and pr-157. The harness gives each its own sandbox, started from one image.
Use the sandbox from a tool
Section titled “Use the sandbox from a tool”AI.Sandbox runs commands and reads and writes files:
const sandbox = yield* AI.Sandbox;
const result = yield* sandbox.exec(["npm", "test"], { cwd: "/home/user/repo", timeout: 120_000 });const text = yield* sandbox.readTextFile("/home/user/repo/package.json");yield* sandbox.writeFile("/home/user/repo/notes.md", "checked\n");exec takes an argument list or a shell line, and returns exitCode, stdout and stderr. Every operation fails with AI.SandboxError.
A tool that uses AI.Sandbox makes its Layer require AI.Sandboxes. See Tools.
Say where sandboxes run
Section titled “Say where sandboxes run”@alchemy.run/ai has an AI.Sandboxes Layer for each platform, as its own import. Give it the image sandboxes start from:
import * as AIBoat from "@alchemy.run/ai/Boat";import * as Boat from "@alchemy.run/boat";
export const Sandbox = AIBoat.Sandboxes(Boat.Snapshot("Base", { run: ["git --version"] }));The snapshot is declared with the Layer, so the deploy builds it, and every sandbox starts from it. The platform’s package is an optional peer of @alchemy.run/ai: install the one you use.
Use another platform
Section titled “Use another platform”Every platform’s Layer has the same shape, so changing platform changes two imports and the image:
import * as AIBoat from "@alchemy.run/ai/Boat";import * as Boat from "@alchemy.run/boat";import * as AIE2B from "@alchemy.run/ai/E2B";import * as E2B from "@alchemy.run/e2b";
export const Sandbox = AIBoat.Sandboxes(Boat.Snapshot("Base", { run: ["git --version"] }));export const Sandbox = AIE2B.Sandboxes(E2B.Template("Base", { image: "node:22-bookworm" }));Nothing in the harness or its tools changes. See Sandbox platforms for E2B, Daytona, Modal, Boat and Archil, and Build your own for a platform that isn’t there or a Layer that works differently.
The harness chooses the key, and the Layer only maps it to a sandbox. Pi uses one key for each conversation, and OpenCode one for each session. A key is lowercase letters, digits and -, at most 48 characters, so it works as a name or a label on every platform.
Because the key is stored on the platform, the same conversation reaches the same sandbox after its Durable Object is evicted.
Open, suspend, remove
Section titled “Open, suspend, remove”All three operations are safe to repeat. open returns the key’s sandbox running, whatever state it was in: it creates it, resumes it, or returns it as it is. suspend stops paying for compute and keeps the files where the platform can. remove deletes the sandbox, and does nothing when it is already gone.
A harness opens a sandbox when a tool first needs it. Suspending and removing are yours to call, when a conversation goes quiet or ends:
yield* harness.suspendSandbox("pr-142");yield* harness.removeSandbox("pr-142");Whether files outlive an idle sandbox is up to the platform. A sandbox the platform deleted is created again, empty, by the next open.