On this page · 8 min
When a Claude Code sandbox is worth the setup
A Claude Code sandbox decides what an agent can touch once nobody is approving its commands. Claude Code asks before it runs a command, and that is the right default for a person at the keyboard. An agent working on a task for an hour, while nobody watches, stops at every prompt. The prompts also train you to approve things without reading them.
The alternative is --dangerously-skip-permissions, which runs commands without asking. Once you use it, nothing stops a bad command except the boundary around the session. Anthropic's sandbox environment guide says to run that flag inside a container, a virtual machine or the sandbox runtime, a separate tool that wraps the whole Claude Code process. It also says to run it as a normal user, because Claude Code refuses the flag as root.
A boundary answers two questions. The first is whose code runs: yours, the model's, or a user's. The second is whose machine it runs on: your laptop, or shared infrastructure. Answer both before you choose, because a boundary that protects your laptop may leave a shared server open.
Step 1: Turn on the built-in sandbox for interactive work
Claude Code has a sandbox for the shell commands it runs, called the sandboxed Bash tool. Inside a session, type:
/sandboxThe sandbox panel opens. Choose auto-allow, which runs sandboxed commands without a prompt. Commands that need more access are blocked, and you can allow specific hosts in the settings:
{
"sandbox": {
"enabled": true,
"network": {
"allowedDomains": ["registry.npmjs.org"]
}
}
}What you get: shell commands that stay inside the working directory and reach only the hosts you listed. What you don't get: file tools such as Read and Edit, MCP servers (add-on tools that Claude Code connects to) and hooks run outside it. A blocked command can also be retried without the sandbox, which prompts you again. One engineer who tried this writes that approving the prompt runs the whole command unsandboxed, not just the blocked part. The sandboxing guide lists the full set of limits.
Use this for your own work on your own laptop. If the agent runs unattended, or on a machine other people depend on, it is not enough on its own, because it covers only shell commands. Go to step 2, where boxd gives each task its own machine.
Step 2: Give each unattended task its own machine
For a background agent, use a separate machine for each task. Each machine is a KVM microVM, a lightweight virtual machine. KVM is Linux's built-in virtualization. Unlike containers, which share the host's kernel, each guest runs its own kernel. The agent sandbox guide explains the model. The --isolated flag takes the machine off the default network and removes its access to boxd's in-machine tools and integration tokens. The --delete-after flag removes it on its own when the time is up:
boxd machine new task-fix-123 --isolated --delete-after=2hThe machine appears in boxd machine list. Isolation is set when the machine is created and cannot be turned on later, so decide at this step. The command reference lists every flag.
Copy the repository in with boxd machine cp:
boxd machine cp -r ./my-repo task-fix-123:~This puts the folder at /home/boxd/my-repo on the machine. Nothing on your laptop is shared with the machine except what you copy.
Step 3: Limit where the machine can reach
An egress allowlist is the list of hosts a machine may connect to. boxd enforces it outside the machine, so the agent cannot change it from inside. Set one with boxd machine egress:
boxd machine egress task-fix-123 registry.npmjs.orgRunning it with no hosts shows the current list. Use none to cut the machine off from the internet, name lookups included. Changes take effect within a second, with no restart. The egress guide covers wildcard hosts and IP ranges.
An allowlist limits where data can go, but it does not stop data going to a host you allowed that accepts writes. A package registry, or a code host where the agent can push, is still a way out. Allow only what the task needs to run, and no more.
Step 4: Give the agent a key it can use but not read
The agent needs an API key to call the model. Store it as a host-bound secret, so the machine only ever sees a placeholder:
boxd env set ANTHROPIC_API_KEY "$ANTHROPIC_API_KEY" --secret --domains api.anthropic.com --scope privateThe --secret flag hides the value after you set it. The --domains flag says which host may receive the real key. Inside the machine, ANTHROPIC_API_KEY holds a bxds_ placeholder. The real key never sits on the machine. boxd swaps it in only on HTTPS requests to api.anthropic.com, and that host is always reachable, so it does not need to be on the egress allowlist. Code on the machine can still use the key on that host, which is why you keep the list short.
Step 5: Run the task without prompts
Claude Code comes preinstalled on boxd machines, so run it there with boxd machine exec. The command forwards the command's output and exit code, and --timeout stops the run after the number of seconds you give it. The < /dev/null redirect stops Claude Code waiting for input:
boxd machine exec task-fix-123 -- 'cd /home/boxd/my-repo && claude -p "Fix the failing test in tests/test_parse.py and commit the change" --dangerously-skip-permissions < /dev/null'When the agent finishes, its final answer prints to your terminal, and boxd machine exec exits with the agent's exit code. Add --json to get the output and the exit code as one object you can parse. Set --timeout to the longest run you accept, so a stuck agent cannot hold the machine open.
The machine has no git credentials, so the agent can commit locally but cannot push. This helps, because nothing leaves the machine until you choose to pull it out. If the commit fails with an identity error, set one inside the repository with git config user.name and git config user.email.
Step 6: Take the result out, then remove the machine
Write the commit as a patch file on your laptop, then delete the machine:
boxd machine exec task-fix-123 -- 'cd /home/boxd/my-repo && git format-patch -1 --stdout' > fix-123.patch && test -s fix-123.patch && boxd machine remove task-fix-123 -yThe agent makes one commit, and -1 exports it. The test -s check stops the removal if the export failed or wrote an empty file, so the machine and its disk survive a failed step. Check the patch with git apply --check fix-123.patch in your own repository before you apply it.
Step 7: Start the next task from a snapshot
A snapshot is a named, saved state of a machine's memory and disk. Make one from a dedicated machine, agent-base, that has your dependencies installed and has never run a task. Then start each new task from the snapshot:
boxd snapshots save ready-agent --vm=agent-base
boxd machine new task-fix-124 --from-snapshot=ready-agentIf you run many tasks on boxd, this is what gives each new machine the same start. Keep agent-base clean, so every task starts from the same state. The new machine keeps the snapshot's isolation setting. It does not keep the egress allowlist: a machine restored from a snapshot starts with no limit on where it can connect. Before the agent runs, check the list with boxd machine egress task-fix-124, and set one with step 3.
What to do next
Pick the boundary by what is running and where.
For interactive work on your laptop, use the built-in sandbox in auto-allow mode. It removes most prompts, and it covers only shell commands.
For unattended work with many tasks and no servers to run, use a machine per task on boxd, as above. The other option is an Anthropic cloud session. Anthropic runs the VM, and a proxy holds your GitHub token outside it. Sessions need a Claude subscription, and a connected GitHub account to work on a GitHub repository.
For unattended work on infrastructure you run, put Claude Code inside a container or VM, and run the sandbox runtime inside it for command-level limits too. Docker Sandboxes runs Claude Code in a microVM with its own Docker daemon, on the host you already work on. It keeps the work local, but you still set its network rules and mounts yourself.
Rule of thumb: if the only thing between an unattended agent and your files is the Bash sandbox, the agent is not safe to leave alone. Add a VM-level boundary first.
Start with one machine and one task. Once the patch comes out clean, script the rest of the steps. To install the CLI, run:
curl -fsSL https://boxd.sh/downloads/install.sh | shThen sign in with boxd auth login.
Frequently asked
- What is a Claude Code sandbox?
- A boundary that limits what Claude Code can read, write and reach while it runs commands. Claude Code's built-in sandbox covers the shell commands it runs. A container, a virtual machine or a hosted machine can cover the whole session.
- How do I turn on the Claude Code sandbox?
- Run /sandbox inside a Claude Code session and choose auto-allow to run sandboxed commands without a prompt. The sandbox is off by default and covers only shell commands.
- Does the Claude Code sandbox work on Windows?
- Not on native Windows. Claude Code's sandbox runs on macOS and on Linux and WSL2, so on Windows you run it inside a WSL2 distribution, or use a container or virtual machine.
- Should I run Claude Code with --dangerously-skip-permissions?
- Only inside a boundary you trust, such as a container, a virtual machine or the sandbox runtime, and as a normal user rather than root. Anthropic's documentation says so, because the flag runs commands without asking.



