Sandbox AI coding agents: 5 isolation options and a setup that works

Sandbox AI coding agents: 5 isolation options and a setup that works

To sandbox an AI coding agent, you run its commands inside an operating system boundary that limits which files it can write, which hosts it can reach and which secrets it can see. This guide compares the five isolation options (built-in sandboxes, sandbox runtimes, containers, VMs and cloud sessions) and ends with a setup you can copy for unattended runs.

In shortTL;DR
  • A sandbox needs both filesystem and network isolation: without network limits an agent can leak what it reads, and without filesystem limits it can plant code that runs later outside the sandbox.
  • Claude Code, Codex and Gemini CLI all ship OS-level sandboxes (Seatbelt on macOS, bubblewrap or containers on Linux), but Claude Code's is off by default and covers only shell commands.
  • For --dangerously-skip-permissions or other prompt-free runs, put the whole agent process in a container, VM or sandbox runtime, not only its shell commands.
  • Keep secrets out: don't mount ~/.ssh or cloud credential files, use short-lived repository-scoped tokens, and allowlist only the domains the task needs.
  • Isolation limits damage; it does not review code. You still read every diff before it merges.

What does it mean to sandbox an AI coding agent?

Sandboxing an AI coding agent means running it inside a boundary, enforced by the operating system or a virtual machine, that caps which files it can change, which network hosts it can contact and which credentials it can read. The agent still works normally inside the boundary, but it cannot reach past it, even if it tries.

Coding agents such as Claude Code, Codex and Gemini CLI run real shell commands: npm install, test runners, build scripts, git, curl. Each of those commands runs with your user's permissions unless something stops it. A sandbox is that something.

Two ideas are worth keeping apart:

  • Permissions decide whether an action runs (the "Allow this command?" prompt, allow and deny rules).
  • Isolation decides what a command can touch after it runs.

Anthropic's sandboxing post makes the second point plainly: effective sandboxing requires both filesystem and network isolation. Filesystem limits alone still let an agent upload what it reads. Network limits alone still let it edit your shell startup files so something runs unsandboxed next time.

Why isolate coding agents at all?

You isolate coding agents for two reasons: ordinary mistakes and hostile input. A sandbox turns both from "anything my user account can do" into "anything inside this box".

Mistakes. Models misread instructions. A cleanup step deletes the wrong directory, a migration runs against the wrong database URL, a script overwrites a config file outside the project. On your laptop, these hit your real files.

Hostile input. An agent reads a lot of text it didn't write: issues, READMEs, dependency source, web pages, MCP tool output. Any of it can carry instructions aimed at the model. Our guide to prompt injection in coding agents covers how that works. A sandbox does not prevent the trick, but it limits the payoff: an agent that cannot reach attacker.example cannot send your .env there.

There is also a practical reason. When the operating system enforces the boundary, the agent can run routine commands without asking you each time. Anthropic reported in October 2025 that in its internal usage, sandboxing reduced permission prompts by 84%. Fewer prompts is what makes long or unattended runs workable, such as headless Claude Code in CI or cron.

Which built-in sandboxes do Claude Code, Codex and Gemini CLI have?

All three major terminal agents ship an OS-level sandbox, but they differ in defaults and scope. Claude Code's sandbox is off until you enable it and wraps shell commands only; Codex is built around sandbox modes you pick per session; Gemini CLI offers Seatbelt profiles or a container.

AgentHow you turn it onmacOSLinuxWhat it covers
Claude Code/sandbox or "sandbox": {"enabled": true}Seatbeltbubblewrap + socat (also WSL2)Bash, PowerShell and Monitor commands and their child processes
Codex--sandbox <mode> or sandbox_mode in configSeatbeltbubblewrap (also WSL2)Commands the agent runs; native sandbox on Windows too
Gemini CLI-s / --sandbox, GEMINI_SANDBOX, or tools.sandboxSeatbelt (sandbox-exec)Docker, Podman, gVisor (runsc) or LXCThe CLI's tool execution

Claude Code

The Claude Code sandbox is off by default. Run /sandbox to pick a mode, or set it in ~/.claude/settings.json. By default, sandboxed commands can write to the working directory and a per-user temp directory, and they have no direct network route: traffic goes through a local proxy that checks each hostname against sandbox.network.allowedDomains, which starts empty. With autoAllowBashIfSandboxed (on by default), sandboxed commands run without a prompt.

Reads are a different story. Out of the box, sandboxed commands can still read most of the machine, including ~/.ssh and ~/.aws/credentials. Close that with denyRead:

~/.claude/settings.json
{  "sandbox": {    "enabled": true,    "failIfUnavailable": true,    "allowUnsandboxedCommands": false,    "filesystem": {      "denyRead": ["~/.ssh", "~/.aws", "~/.config/gcloud"]    },    "network": {      "allowedDomains": ["registry.npmjs.org", "github.com"]    }  }}

failIfUnavailable makes Claude Code exit if the sandbox can't start, instead of silently running commands unsandboxed. allowUnsandboxedCommands: false disables the escape hatch where Claude retries a failing command with dangerouslyDisableSandbox.

Codex

Codex has three sandbox modes: read-only, workspace-write (the low-friction mode for local work: edits and routine commands inside the workspace) and danger-full-access (no filesystem or network boundary). The approval policy is separate: on-request asks when the agent wants to step outside the sandbox, never doesn't ask. The documented low-risk preset is:

terminal
codex --sandbox workspace-write --ask-for-approval on-request

Extra writable directories go in sandbox_workspace_write.writable_roots. For a side-by-side of the two agents beyond sandboxing, see Codex vs Claude Code.

Gemini CLI

Gemini CLI enables its sandbox with -s or --sandbox, the GEMINI_SANDBOX environment variable (true, docker, podman, sandbox-exec, runsc or lxc) or "sandbox": true in the tools section of settings.json. On macOS, SEATBELT_PROFILE picks a profile such as permissive-open (the default), restrictive-proxied or strict-proxied.

What does a per-command sandbox not cover?

A per-command sandbox only wraps the shell commands the agent spawns, so everything else in the session runs on your host with your permissions. For Claude Code, the docs list what sits outside the Bash sandbox.

  • The built-in Read, Edit and Write tools and WebFetch (permission rules gate these instead).
  • Hooks, local MCP servers, plugin monitors, LSP servers and status line commands.
  • Tools that ignore proxy settings, such as plain ssh and most database drivers, which can't reach even allowed hosts.

This matters most when you remove prompts. The sandbox environments guide says to always run --dangerously-skip-permissions sessions inside a container, a VM or the sandbox runtime, so file tools, MCP servers and hooks sit inside the boundary too. Hooks deserve a second look here: a cloned repository can ship Claude Code hooks in .claude/settings.json, and those run as regular processes.

The sandbox runtime closes the gap without Docker. Anthropic's open source @anthropic-ai/sandbox-runtime package (a beta research preview) wraps the whole Claude Code process in the same Seatbelt or bubblewrap isolation:

terminal
npm install -g @anthropic-ai/sandbox-runtime# configure ~/.srt-settings.json first: writable paths and allowed domainssrt claude

Without a settings file, the runtime blocks all network access, so Claude Code can't reach its API. Allow at least your project directory, ~/.claude, ~/.claude.json, api.anthropic.com and, for subscription sign-in, claude.ai and platform.claude.com.

Containers, dev containers, microVMs or cloud sessions: which should you use?

Pick the lightest boundary that matches your risk. A built-in sandbox suits everyday work on your own laptop, a container suits unattended runs on trusted repositories, and a VM or microVM suits untrusted code.

Stacked shipping containers seen from above, each a separate sealed box
Each agent session gets its own box, with only the project inside
OptionWhat is isolatedKernel shared with hostGood for
Built-in sandboxShell commands onlyYesFewer prompts during interactive work
Sandbox runtime (srt)Whole agent processYesIsolating tools, hooks and MCP without Docker
Dev containerFull dev environmentYesTeam-standard setup, unattended runs on trusted repos
Docker Sandboxes (microVM)Full environment with its own Docker daemonNoAgents that need Docker, untrusted dependencies
VM or cloud sessionFull operating systemNoUntrusted repositories, strict policy

Dev containers. Claude Code installs into any dev container with the ghcr.io/anthropics/devcontainer-features/claude-code feature. Anthropic's reference container adds an init-firewall.sh script with a default-deny egress firewall, which needs the NET_ADMIN and NET_RAW capabilities. Inside it you can pass --dangerously-skip-permissions, which the CLI refuses when launched as root. The docs are clear about the limit: with that flag, a malicious project can still exfiltrate anything inside the container, including the Claude credentials in ~/.claude. Use it with trusted repositories.

Docker Sandboxes. Docker's sbx CLI runs agents in microVMs, each with its own Docker daemon, filesystem and network. The CLI and local sandboxes are free at the time of writing (November 2026):

terminal
cd ~/my-projectsbx policy init balancedsbx policy allow network registry.npmjs.orgsbx run --name my-sandbox claude

By default the current directory is mounted read-write; clone mode gives the agent a private copy instead. When you sign in with /login inside the sandbox, Docker says the session token stays on the host.

VMs and cloud sessions. A separate VM gives the agent its own kernel, which is what you want for code you don't trust. Claude Code's cloud sessions run in Anthropic-managed VMs where a proxy holds your GitHub token outside the sandbox and hands out scoped credentials. If you want agents on a server that keeps running after you close the laptop, Orkio rents an always-on cloud box in Europe, isolated from other customers, where Claude Code and Codex work on your repositories and open pull requests. Our comparison of remote dev servers for AI agents covers the self-managed route.

How do you limit network access and secrets?

Allowlist the few domains the task needs and keep long-lived credentials out of the boundary entirely. Whatever the agent can read and whatever host it can reach together define what can leak.

Network. Start from deny-all and add hosts as tasks fail, not the other way round. A typical web project needs the package registry, your git host and the model API. Watch for broad entries: allowing a domain where anyone can host content (a large file-sharing or paste site) reopens the exfiltration route. Claude Code's proxy checks hostnames but, by default, doesn't inspect TLS traffic, so treat each allowed domain as fully trusted.

Secrets. Use this order of preference:

  1. No secret at all. Run tests against local fixtures or a throwaway database.
  2. Injected outside the sandbox. Claude Code's sandbox.credentials can deny or mask files and environment variables; Docker Sandboxes stores secrets with sbx secret set; cloud sessions keep the GitHub token in a proxy.
  3. Short-lived and scoped. A fine-grained GitHub token limited to one repository, with an expiry.
  4. Never mount ~/.ssh, ~/.aws, ~/.config/gcloud or a .env with production keys into the agent's environment.
Warning

A sandbox protects the machine, not the code. An agent can still write a backdoor into the project it is allowed to edit, and that code reaches production through your merge button. Review every diff.

A practical sandbox setup for unattended runs

This setup gives an agent a container with the project, a firewall, no host secrets and no prompts. It assumes Docker and VS Code with the Dev Containers extension, and a trusted repository.

  1. Copy the .devcontainer/ folder from the anthropics/claude-code repository into your project.
  2. Edit init-firewall.sh and add only the domains your build needs (package registry, git host).
  3. Check devcontainer.json: remoteUser is a non-root user, and no mount points at ~/.ssh, ~/.aws or other credential folders.
  4. Run Dev Containers: Rebuild Container, then sign in with claude inside the container.
  5. Create a fine-grained GitHub token for this one repository with a short expiry, and pass it in as an environment variable.
  6. Inside the container, enable the Bash sandbox too. Unprivileged containers need enableWeakerNestedSandbox for bubblewrap to start.
  7. Run the task with claude --dangerously-skip-permissions (or auto mode, which keeps a classifier in the loop) and have the agent work on a branch.
  8. Review the diff and the list of files changed outside src/ before you merge.

For overnight or background work, combine this with a command log and a tests-must-pass hook; our guide to background coding agents covers the rest of the workflow.

Common sandbox problems and fixes

Most sandbox failures are tools that need something the boundary blocks. Fix them by widening one specific thing, never by turning the sandbox off.

SymptomCauseFix
npm install hangs or failsRegistry not on the allowlistAdd registry.npmjs.org to allowed domains
git push fails on macOS with Claude Code's sandboxSSH remotes don't work through the proxySwitch the remote to HTTPS or exclude git push
docker commands fail inside Claude Code's sandboxDocker is incompatible with the sandboxAdd docker compose * to excludedCommands, or use Docker Sandboxes
bubblewrap fails on Ubuntu 24.04AppArmor restricts user namespacesAdd the AppArmor profile from the Claude Code docs
Database client can't connectDriver ignores proxy variablesUse a local database in the sandbox
Sandbox silently offMissing dependenciesSet failIfUnavailable: true

excludedCommands runs matching commands fully outside the sandbox, so keep the patterns narrow.

Your sandbox checklist

Use this list before you let an agent run without watching it.

  • The whole agent process, not only its shell, runs inside a container, VM or sandbox runtime.
  • The agent runs as a non-root user.
  • Only the project directory is writable; shell startup files, .git/hooks and agent config are not.
  • ~/.ssh, cloud credential folders and production .env files are not mounted or are denied.
  • Outbound network is deny-by-default with a short allowlist.
  • Tokens are scoped to one repository and expire.
  • The sandbox fails closed (failIfUnavailable) instead of falling back to no sandbox.
  • Work lands on a branch, and you review the diff before merging.

Frequently asked questions

Is Claude Code sandboxed by default?

No. The Claude Code Bash sandbox is off by default. You turn it on with the /sandbox command or by setting sandbox.enabled to true in a settings file, and on Linux or WSL2 you first install bubblewrap and socat. Native Windows runs commands unsandboxed, so use WSL2, a container or a VM there.

Is Docker enough to sandbox an AI coding agent?

A plain Docker container is a reasonable boundary for trusted repositories if it runs as a non-root user, mounts only the project, holds no long-lived secrets and restricts outbound network traffic. Containers share the host kernel, so for untrusted code or strict policies a VM or microVM gives stronger separation.

Does a sandbox protect against prompt injection?

Partly. A sandbox cannot stop the model from being tricked, but it limits what a tricked agent can do: it can't write outside the allowed paths or send data to hosts that aren't on the allowlist. Anything the agent can read and any host it can reach is still at risk, so keep both lists short.

What is the difference between permission prompts and a sandbox?

Permission prompts and rules decide whether an action runs at all. A sandbox limits what a command can touch once it runs. You want both: rules for intent, and an OS boundary that still holds when a rule is too broad or a prompt was approved by mistake.

Can I run Codex without a sandbox?

Yes. Codex has a danger-full-access sandbox mode that removes its filesystem and network boundaries, usually combined with an approval policy of never. Use it only inside an environment that is already isolated, such as a container or VM you can throw away.

SOURCES

  1. Anthropic Engineering: Making Claude Code more secure and autonomous with sandboxing
  2. Claude Code docs: Sandboxing
  3. Claude Code docs: Choose a sandbox environment
  4. Claude Code docs: Development containers
  5. OpenAI Codex docs: Sandboxing
  6. Docker docs: Docker Sandboxes
  7. Gemini CLI docs: Sandboxing
  • #ai coding
  • #security
  • #sandboxing
  • #claude code
  • #docker
Orkio team

The team building Orkio. We write about putting AI to work: coding agents that run overnight, always-on boxes and pull requests ready by morning.

Keep reading

All posts

07:06 · YOU MERGED #128

Go to sleep. Wake up to pull requests.

Start for $19 a month
© 2026 Orkio · Hosted in Europe TürkçeStatus