Persistent sandboxes
A persistent sandbox is one sandbox whose files outlive the container that runs it. It runs in sessions: each one up to an hour, paused after five idle minutes, and resumed with the files the last one left.
Sessions come and go, the files stay
Section titled “Sessions come and go, the files stay”Declare a box
Section titled “Declare a box”import * as Modal from "@alchemy.run/modal";import { Coder, Warm } from "./Coder.ts";
export const DevBox = Modal.PersistentSandbox("DevBox", { app: Coder, image: Warm, workdir: "/work" });Resume it from a Worker
Section titled “Resume it from a Worker”// construction phaseconst box = yield* Modal.ConnectSandbox(DevBox);// in fetch:const session = yield* box.session();const status = yield* session.exec("git status");session() returns the running session, or resumes the box from its last session’s files.
Create one per user while your app runs
Section titled “Create one per user while your app runs”const box = yield* sandboxes.createPersistent({ image: "node:22-bookworm" });// store box.persistentSandboxId with the userPersistent sandboxes are experimental on Modal: no names, GPUs, env or tags yet.