Prerequisites
Export your Archil API key, a region where sandboxes are available (aws-us-east-1, aws-us-west-2, or aws-eu-west-1), and an Anthropic API key, then install the SDK:
Run Claude Code in a sandbox
This script creates a disk, starts a sandbox with the disk mounted at/workspace, installs Claude Code, and gives it a task in print mode (claude -p), which works until the task is done, prints the answer, and exits. Then it deletes the sandbox; everything the agent made is on the disk.
npx tsx start.ts or python start.py. The agent’s wording varies from run to run. One run printed:
Pick up where you left off
Claude Code’s project and session history are on the disk, so any sandbox that mounts it can continue the conversation. This script starts a new sandbox on the same disk and runs Claude Code with--continue, which resumes the most recent conversation in /workspace.
Going further
- Stream output. Pass
onOutput: ({ data }) => process.stdout.write(data)toexec(in Python,on_output=lambda out: sys.stdout.buffer.write(out.data)) to watch the agent as it works. Add--output-format stream-json --verboseto get one JSON event per line. - Work interactively. Start
claudewithout-pin a pseudo-terminal withsandbox.run; see Opening interactive shells.
Use Vercel AI Gateway instead of an Anthropic key
Use Vercel AI Gateway instead of an Anthropic key
Claude Code can send its requests through Vercel AI Gateway. Export
AI_GATEWAY_API_KEY instead of ANTHROPIC_API_KEY, and replace the ANTHROPIC_API_KEY line in env with these lines. ANTHROPIC_API_KEY must be an empty string, because Claude Code prefers it over ANTHROPIC_AUTH_TOKEN. On the Developer plan, sandboxes can’t reach the gateway, because it isn’t on the managed allowlist; use your provider key instead.Clean up
Each script deletes its sandbox when it finishes, so only the disk is left. Delete it:Next steps
OpenCode
Run OpenCode headlessly in a sandbox.
OpenAI Codex
Run the Codex CLI headlessly in a sandbox.
Coding agent in a sandbox
Build your own coding agent with the AI SDK instead.
Persistent sandboxes
Lifecycle, services, network access, and forking in detail.