---
title: "Sandboxing AI Agents: Where Your Agent's Commands Actually Run"
author: "Ecem Karaman"
source: "https://aiwithecem.com/guides/agent-sandboxing"
published: 2026-11-22
tools: ["Claude","Multi-Tool"]
topics: ["Security","AI Agents"]
---

# Sandboxing AI Agents: Where Your Agent's Commands Actually Run

Permission rules decide **whether** an action runs. A sandbox decides **what it can reach** once it does. That difference matters because, as the [least privilege guide](/guides/agent-least-privilege) showed, rules match the text of a command, and the same program can be invoked a dozen other ways. A sandbox is enforced by the operating system, so it doesn't care how the command was spelled.

> **In this guide**
> 1. [What a sandbox does and doesn't do](#what-a-sandbox-does-and-doesnt-do)
> 2. [Four levels of isolation](#four-levels-of-isolation)
> 3. [Know what's outside the boundary](#know-whats-outside-the-boundary)
> 4. [Close the escape hatches](#close-the-escape-hatches)
> 5. [The network is your exfiltration boundary](#the-network-is-your-exfiltration-boundary)
> 6. [Mounts decide what's reachable](#mounts-decide-whats-reachable)
> 7. [Which one to use](#which-one-to-use)

## What a sandbox does and doesn't do

A sandbox limits three things: **files** (what can be read and written), **network** (where traffic can go) and **processes** (what runs and with which environment). It doesn't change two things people often assume it does:

- **What the model sees.** Files the agent reads are still sent to the model provider, sandbox or not.
- **What it's allowed to touch.** A writable project folder can still be wrecked, and any allowed network route can still carry data out.

So a sandbox shrinks the blast radius. It doesn't make a compromised agent harmless.

## Four levels of isolation

| Level | What's isolated | Examples |
|---|---|---|
| **Per-command sandbox** | Each shell command and its child processes | Claude Code's sandboxed Bash, using Seatbelt on macOS and bubblewrap on Linux |
| **Whole-process sandbox** | The agent itself, including its file tools, hooks and MCP servers | Anthropic's open-source sandbox runtime wrapping Claude Code |
| **Container** | A separate filesystem and process space, sharing the host's kernel | Dev containers, Hermes's Docker backend, OpenClaw's Docker sandbox |
| **Virtual machine or remote box** | A separate operating system | A local VM, Hermes or OpenClaw over SSH, cloud sessions |

Each step up costs more setup and gives a stronger wall. A container shares your kernel, so a kernel bug is a way out. A VM doesn't.

## Know what's outside the boundary

The most common mistake is assuming "sandbox on" covers everything. It usually doesn't.

- **Claude Code's sandbox wraps shell commands only.** The built-in Read, Edit and WebFetch tools follow permission rules instead, and hooks and local MCP servers run directly on your machine. A sandbox `denyRead` doesn't stop the Read tool. To put everything behind one wall, run the whole process in the sandbox runtime, a container or a VM.
- **OpenClaw moves only tool execution.** The Gateway, which holds sessions and channel connections, always stays on the host.

Before trusting a sandbox, list what the agent runs and mark which pieces are inside it.

## Close the escape hatches

Sandboxes come with ways out, usually for good reasons. Know each one:

- **The unsandboxed retry.** When a command fails in Claude Code's sandbox, Claude can retry it outside. Depending on your mode you're asked first, but a matching allow rule approves the retry too: allow `Bash(curl *)` and a blocked `curl` can rerun unsandboxed with no prompt. Turn it off with `"allowUnsandboxedCommands": false`.
- **Excluded commands.** Anything in `excludedCommands` runs with full access and no network proxy. Only list tools you'd trust with your whole machine.
- **Config files as a way out.** A command that can write the agent's own config can grant itself permissions or add a hook that runs outside the sandbox. That's why Claude Code's sandbox blocks writes to `.claude` settings, hooks, `.mcp.json`, shell startup files like `.zshrc`, and `.git/hooks`, even inside folders it can otherwise write. If you build your own container, protect the same classes of file.

## The network is your exfiltration boundary

Anything the agent can read can leave through any connection it can open. Network rules matter more than file rules:

- **Default deny, then allowlist.** Claude Code's sandbox sends traffic through a proxy whose allowed domains start empty. OpenClaw's Docker sandbox defaults to no network at all.
- **Allowing a domain allows everything it accepts.** If a code host, file-sharing site or paste service is on the list, it can receive your data as easily as it serves packages. Allow specific API hosts over broad wildcards.
- **Block the metadata endpoint.** On cloud machines, `169.254.169.254` hands out credentials. Claude Code's proxy refuses a hostname that resolves only to local addresses like that one. Check that your own setup does too.

## Mounts decide what's reachable

In a container, **what you mount is what the agent can reach**:

- **Mount the project, not your home folder.** OpenClaw's default doesn't expose the agent workspace at all; you can choose read-only or read-write.
- **Mount credentials read-only, or not at all.** Hermes mounts credential files read-only into Docker.
- **Never mount the Docker socket.** Whoever controls the Docker daemon can start a privileged container with the host's filesystem mounted, which effectively means control of the host.
- **Keep the default hardening.** OpenClaw's Docker sandbox runs with a read-only root filesystem and every Linux capability dropped; Hermes drops capabilities, blocks privilege escalation and caps the number of processes. Don't loosen those to fix a failing install.

## Which one to use

| Situation | Start with |
|---|---|
| Everyday work on your own machine, fewer prompts | A per-command sandbox with a network allowlist |
| Letting an agent run unattended or skip prompts | A container, VM or the whole-process sandbox runtime |
| An unfamiliar repo or untrusted content | A VM or a throwaway remote machine |
| A chat agent with real tools | The Docker or SSH backend, with no network by default |

**Sandbox checklist:**

- [ ] Sandbox on, and you know which tools and processes sit outside it
- [ ] Unsandboxed retry off, and excluded commands kept to a minimum
- [ ] Network default-deny, with specific hosts allowed
- [ ] Only the project mounted; no home folder, no `~/.ssh`, no Docker socket
- [ ] Unattended runs only inside a container, VM or remote machine

**Details checked against [Claude Code's sandboxing docs](https://code.claude.com/docs/en/sandboxing), [its sandbox environments guide](https://code.claude.com/docs/en/sandbox-environments), [Hermes Agent's security docs](https://hermes-agent.nousresearch.com/docs/user-guide/security) and [OpenClaw's sandboxing docs](https://docs.openclaw.ai/gateway/sandboxing) on November 22, 2026.**

**Go deeper:** [Least privilege for AI agents](/guides/agent-least-privilege) · [Secrets and API keys when agents run code](/guides/agent-secrets-api-keys) · [Claude Code permission modes](/guides/claude-code-permission-modes) · [Prompt injection](/guides/prompt-injection)
