Prerequisites
- Node.js 22.19 or later, and
git - An E2B API key
- A key for your agent, for example
ANTHROPIC_API_KEYfor Claude Code
Install
The E2B provider uses thee2b package as an optional peer dependency, so install both:
apiKey option:
Run Claude Code
E2B’s default template has no agent CLI. Use the prebuiltclaude template, which ships Claude Code:
run.mts
npx tsx run.mts. The agent’s commits land on your current branch; pass branchStrategy: { type: "branch", branch: "agent/fix" } to put them on a separate branch instead.
Only the environment variables you give Sandcastle reach the sandbox: keys declared in .sandcastle/.env, and the env of the agent and sandbox providers. That’s why the example passes ANTHROPIC_API_KEY explicitly.
Run Codex
Use the prebuiltcodex template and pass your OpenAI key as CODEX_API_KEY, which codex exec reads. OPENAI_API_KEY alone isn’t enough.
onSandboxReady hook.
Run agents in parallel
Eachrun() creates its own sandbox, so you can start several agents at once with Promise.allSettled, each on its own branch. The cookbook example runs two Claude Code agents on two tasks in parallel, then merges both branches and runs the tests locally.
Configure the sandbox
e2b() accepts the options of the SDK’s Sandbox.create() and passes them through:
envs and lifecycle itself, so you can’t override them. env is merged with the rest of Sandcastle’s environment, and maxOutputTailChars limits how much streamed output Sandcastle keeps for the result (64 KiB by default).
Lifecycle
run()creates a new sandbox for each call and kills it when the run finishes.createSandbox()keeps one sandbox for several runs until you close it.- The repository is copied to
$HOME/workspacein the sandbox. - The sandbox is always killed, never paused, when
timeoutMsruns out. Make the timeout long enough for setup, the agent run, and syncing the commits back. E2B’s Hobby plan caps a sandbox at 1 hour. - If you stop Sandcastle with Ctrl+C, it doesn’t kill the sandbox; E2B kills it when
timeoutMsruns out.
Limitations
interactive()isn’t supported with the E2B provider.sandcastle initdoesn’t scaffold E2B; configuree2b()in your script as shown above.
Learn more
Related guides
Claude Code
The prebuilt
claude templateCodex
The prebuilt
codex templateTemplates
Build a template with your own agent CLI