Running Claude Code Safely: Sandboxing & Isolation Guide
As AI coding agents like Claude Code gain autonomous capabilities—executing terminal commands, running tests, refactoring modules, and integrating MCP servers—ensuring system safety becomes paramount. Running an autonomous agent directly on your primary host OS without boundaries carries operational risk: an unintended command or malformed shell script could modify system configuration files or overwrite critical databases. For broader agent management patterns, see Autonomous Execution with Auto Mode and Claude Code Security Auditing.
A sandbox acts as a restricted enclosure for an AI coding agent. It strictly limits what the agent can access, modify, or transmit over the network, ensuring that unexpected behavior or unsafe execution cannot affect your host operating system. In this guide, we break down how sandboxing works, compare the 5 primary isolation tiers, clarify the critical difference between permissions and sandboxing, and outline practical safety recommendations.
Understanding Sandboxing vs Permissions
A common point of confusion when configuring Claude Code is the distinction between permissions and sandboxing controls. While both protect your system, they operate at completely different layers of the security stack:
- Permissions Layer: Decides whether Claude Code must ask for human approval before taking an action (such as reading a file, executing a shell command, or spawning a subagent).
- Sandboxing Layer: Decides what system resources Claude Code can physically reach, read, or modify after an action is authorized.
1# Bypassing permission prompts means ALL security relies on your Sandboxing layer:2claude -p "Refactor authentication flow" --dangerously-skip-permissionsWhen you use flags like --dangerously-skip-permissions, Claude Code executes actions automatically without waiting for interactive confirmation. In this unattended mode, relying solely on basic terminal permission prompts is insufficient—you must wrap the agent in a container, virtual machine, or sandbox runtime.
Quick reference
- Permissions act as an interactive prompt gate requiring user confirmation.
- Sandboxing may add an OS, container, or hypervisor boundary, but the actual protection depends on mounts, privileges, and network policy.
- Skipping permission prompts transfers all security enforcement to container or VM sandboxing.
Remember this
Permissions control authorization prompts; sandboxing enforces resource containment.
Five Security Isolation Options Compared
Depending on your threat model, repository trust level, and execution environment, Claude Code can be run across five distinct isolation tiers. Choosing the appropriate tier depends on whether you are working on trusted personal projects or untrusted open-source codebases.
The matrix below outlines the spectrum from lightweight terminal filtering to full cloud-managed VM instances, helping team leaders select the right isolation boundary for their developer workflow.
| Isolation Option | What It Protects | Best For |
|---|---|---|
| Sandboxed Bash | Only restricts terminal commands | Everyday local development on trusted projects |
| Sandbox Runtime | Claude Code, tools, MCP servers, & hooks | Safer unattended local tasks without Docker overhead |
| Dev Container / Docker | Entire coding environment & filesystem | Team-standard reproducible setups & CI pipelines |
| Virtual Machine (VM) | Full separate guest OS & kernel | Untrusted repositories & strongest local isolation |
| Claude Code Web | Anthropic-managed cloud VM instance | Working remotely without local environment setup |
Quick reference
- Sandboxed Bash filters terminal execution but shares the host filesystem.
- Dev Containers isolate processes and dependencies, but bind mounts and Docker privileges can still expose host data.
- Virtual machines usually provide a stronger boundary than containers, but they still require patching, secret isolation, and network controls.
Remember this
Choose the isolation tier that matches your repository trust level and autonomy requirements.
Key Security Warnings & Blast Radius Management
While sandboxing dramatically reduces blast radius, it is vital to recognize that sandboxing reduces security risk, but does not eliminate it completely.
Consider these key security implications when configuring sandboxed agent environments:
1. Readable Project Data: If the sandbox mounts your project folder as readable, the agent (or a compromised tool) can still inspect source code, configuration files, and embedded API keys. 2. Outbound Internet Egress: If the sandbox retains active internet access (e.g. to download npm packages or fetch documentation), readable data could potentially be transmitted externally if untrusted instructions are ingested. 3. Writable Mounts: Mounting your repository directory as writable inside a container allows the agent to modify code files. Always keep git commit checkpoints before running broad agent tasks.
Quick reference
- Read-access inside a sandbox allows exposure of environment secrets stored in project files.
- Outbound network egress must be restricted in strict isolation scenarios.
- Always isolate
.envsecret files or use secret placeholders when running untrusted repos.
Remember this
Sandboxing limits host OS compromise but requires careful management of mounted project data and network egress.
Recommended Sandboxing Workflows & Best Practices
To balance developer productivity with security, follow these simple operational rules based on your task context:
- Normal Personal Coding: Use the built-in
/sandboxcommand or local terminal sandboxing for interactive feature work on your own repositories. - Unattended or Auto Agent Work: When launching background tasks with
--dangerously-skip-permissionsor using Auto Mode, run inside a Sandbox Runtime or Docker Dev Container. - Unknown or Untrusted Repositories: When evaluating third-party code, open-source pull requests, or unverified repos, always use a dedicated Virtual Machine or Claude Code Web.
Quick reference
- Interactive personal coding can use the product's documented permission controls on trusted repositories.
- Automated background agents must always run inside containers or dedicated runtimes.
- Untrusted codebases need a boundary selected for the threat model; never mount long-lived credentials by default.
Remember this
Match your sandboxing tier to the autonomy level of the agent and the trust level of the repository.
Hands-on Practice: Run Claude Code inside a Dev Container
Test a container boundary with a disposable project and no host credential mount. This example is a teaching baseline, not a complete security boundary:
1mkdir -p agent-sandbox-demo && cd agent-sandbox-demo2printf '%s\n' '{"name":"sandbox-demo"}' > package.json3docker run -it --rm \4 --network none \5 --cap-drop=ALL \6 --read-only \7 -v "$PWD:/workspace:rw" \8 -w /workspace \9 node:20-slim bashDo not pass a long-lived API key into an untrusted container. If the agent needs network access, add only the required egress and use short-lived credentials or a host-side proxy. The bind mount remains writable, so commit checkpoints and inspect the diff after the run.
Quick reference
- Mounting $(pwd) to /workspace gives the container access only to the current project directory.
- Passing ANTHROPIC_API_KEY as an environment variable avoids writing secrets to disk inside the container.
- The --rm flag automatically cleans up container artifacts upon session termination.
Remember this
Docker can reduce blast radius when privileges, mounts, network, and secrets are explicitly constrained; it does not guarantee complete host protection.
Key takeaway
Practice task: remove --network none or add a test that tries to read a mounted secret. The expected result is either a blocked network request or a failed secret read. Restore the restriction, inspect the container's exit status and repository diff, and pass only when the task succeeds without network access or secret exposure.
Polo Khan
Lead Author & Systems ArchitectSoftware engineer and distributed systems architect specializing in backend scalability, cloud-native infrastructure, databases, and AI engineering workflows. Author and maintainer of Core Concept Learning.
Related Articles
Explore this topic