# AI agent sandboxing: isolating code and tools

> AI agent sandboxing confines what an agent's code and tools can touch. The isolation layers, the limits to set and where sandboxes stop protecting you.

Source: https://www.honeyhive.ai/learn/ai-agent-sandboxing (updated 2026-11-18)

AI agent sandboxing means running an agent's code and tool calls inside a boundary that limits what they can read, write, reach over the network and use as credentials. The boundary assumes the agent will sometimes do the wrong thing, whether from a bad instruction, a poisoned input or its own error, and makes sure the damage stays small. It is one layer of [AI agent security](/learn/ai-agent-security), which covers the wider threat list; this page covers the isolation itself.

## Why agents need one

An agent that runs shell commands or generated code can do whatever its process can do. OWASP's [AI Agent Security cheat sheet](https://cheatsheetseries.owasp.org/cheatsheets/AI_Agent_Security_Cheat_Sheet.html) says not to "allow agents to execute arbitrary code without sandboxing," and, for multi-agent setups, to isolate agent execution environments. The Kubernetes Agent Sandbox project [frames](https://agent-sandbox.sigs.k8s.io/) the need the same way: autonomous agents generate and run untrusted code, so they need an isolated execution layer.

## Four things to limit

A sandbox is a set of limits. The sources below cover four of them.

| Limit | What it means | Example from the docs |
|---|---|---|
| Filesystem | Where the agent can write, and what it can read | Claude Code's sandbox [allows writes](https://code.claude.com/docs/en/sandboxing) to the working directory and temp directory, and keeps settings, shell startup files and git hooks write-protected |
| Network | Which hosts the agent's code can reach | In Claude Code on Linux, commands run in a [separate network namespace](https://code.claude.com/docs/en/sandboxing) and reach the outside only through a proxy that checks each hostname |
| Credentials | Which secrets are visible inside | Claude Code can [deny reads](https://code.claude.com/docs/en/sandboxing) of credential files and remove environment variables, or substitute a placeholder that the proxy swaps for the real value |
| Resources | How much work a runaway agent can do | OWASP says to [enforce](https://cheatsheetseries.owasp.org/cheatsheets/AI_Agent_Security_Cheat_Sheet.html) token, cost, retry and tool-chain limits |

The protected paths are worth noticing. Claude Code's docs [explain](https://code.claude.com/docs/en/sandboxing) that a command able to edit its own settings, `.mcp.json` or git hooks could grant itself permissions or add a hook that runs outside the sandbox. A sandbox that lets the agent rewrite its own rules is not a boundary.

Reads need the same care. By default that sandbox [allows reading](https://code.claude.com/docs/en/sandboxing) the whole computer except denied directories, including credential files, so you have to add the denials yourself.

## Isolation layers, from light to heavy

The sources describe four ways to draw the boundary. They differ in what sits between the agent's code and the host.

| Layer | How it isolates | Documented in |
|---|---|---|
| OS process sandbox | The operating system restricts one process and its children: Seatbelt on macOS, bubblewrap on Linux | [Claude Code](https://code.claude.com/docs/en/sandboxing), [Codex](https://learn.chatgpt.com/docs/sandboxing) |
| Container | A dedicated container with minimal privileges | Anthropic's [computer use docs](https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool) |
| User-space kernel (gVisor) | An application kernel that [intercepts system calls](https://gvisor.dev/docs/) and does not pass them to the host kernel; file access outside goes through a mediator | [gVisor](https://gvisor.dev/docs/), as a backend in [Agent Sandbox](https://agent-sandbox.sigs.k8s.io/) |
| microVM or VM | A lightweight virtual machine, run on KVM, with a [minimal device model](https://github.com/firecracker-microvm/firecracker) | [Firecracker](https://github.com/firecracker-microvm/firecracker); Kata Containers gives [a dedicated kernel per sandbox](https://agent-sandbox.sigs.k8s.io/) |

Heavier layers separate the agent from the host more completely. The costs the docs name are specific. gVisor [trades](https://gvisor.dev/docs/) some compatibility and per-call overhead for its isolation. Firecracker says its security [depends on](https://github.com/firecracker-microvm/firecracker) a well configured Linux host. The OS process sandboxes are the lightest, and Claude Code's own docs [say](https://code.claude.com/docs/en/sandboxing) the sandbox reduces risk but is not a complete isolation boundary, and suggest running the whole Claude Code process in a container, VM or their sandbox runtime to put everything behind one boundary.

None of these pages ranks the layers for a given workload, so choose by what the agent runs. A coding agent working in your own repository with a human watching is a different case from a service executing code written by an untrusted user's prompt.

## Where a sandbox stops protecting you

Read the limits section of any sandbox's docs. Claude Code's lists several that apply broadly:

- **Scope.** It [covers shell commands only](https://code.claude.com/docs/en/sandboxing). Built-in file tools, local MCP servers and hooks run outside it. A tool the agent calls through [MCP](/learn/mcp-security) is only as contained as the server that runs it.
- **Hostname filtering.** The default proxy [filters](https://code.claude.com/docs/en/sandboxing) by requested hostname and does not inspect TLS traffic, so an allowed host is trusted with whatever the agent sends it.
- **Escape hatches.** Excluded commands and retries with the sandbox off run unsandboxed, and enabling nested user namespaces [turns off](https://code.claude.com/docs/en/sandboxing) a security boundary.
- **Off by default.** Claude Code's sandbox [is off](https://code.claude.com/docs/en/sandboxing) until you enable it, and native Windows runs commands unsandboxed.

Codex has a matching escape: its `danger-full-access` mode [runs](https://learn.chatgpt.com/docs/sandboxing) without sandbox restrictions.

## Sandbox plus permissions

A sandbox answers what the agent can do. Permissions and approvals answer when it may. Codex's docs [separate the two](https://learn.chatgpt.com/docs/sandboxing): the sandbox defines technical boundaries, and an approval policy such as `on-request` lets the agent work inside the sandbox and ask when it needs to go beyond it. Automatic review of approval requests does not change the sandbox boundary.

That split is useful in practice. Routine actions inside the boundary need no prompt, so people are not trained to click through approvals, and the actions that cross it get a human decision. Anthropic's computer use docs pair both in one list: a dedicated VM or container, no sensitive data in reach, an allowlist for the network, and [human confirmation](https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool) for decisions with real-world consequences. For approvals see [human-in-the-loop AI](/learn/human-in-the-loop-ai); for tool-level rules, [AI agent guardrails](/learn/ai-agent-guardrails); for who the agent acts as, [AI agent identity](/learn/ai-agent-identity).

## Setting one up

A checklist drawn from the sources above:

1. Run code and commands in the lightest layer that fits the threat, and put a container or VM around the whole agent process when it handles untrusted input.
2. Start the network allowlist empty and add hosts as needed.
3. Keep credentials out of the sandbox, or hand the agent placeholders.
4. Write-protect the agent's own configuration, hooks and tool definitions.
5. Cap tokens, cost, retries and tool-chain depth.
6. Keep the sandbox on by default, and treat any option that turns it off as a decision to review.
7. Trace what ran inside it. [Agent tracing](/learn/agent-tracing) records the tool calls and their outputs, so you can see what a sandbox blocked and what the agent tried.

For agents that drive a screen, the same limits apply to the VM or browser they control; see [computer use agents](/learn/computer-use-agents). For the loop that executes tool calls, see [agent harness](/learn/agent-harness).

Isolation does not make a hijacked agent behave. OWASP's cheat sheet and Anthropic both treat the sandbox as one control among several, and Anthropic says its computer use precautions [remain important](https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool) even with prompt injection classifiers running. Pair it with defences against [prompt injection](/learn/prompt-injection) and with the approvals above.

## FAQ

### What is an AI agent sandbox?

An isolated environment where an agent's code and commands run with limited access to files, network and credentials, so a mistake or a hijacked instruction harms only the sandbox. OWASP's agent cheat sheet lists allowing arbitrary code execution [without sandboxing](https://cheatsheetseries.owasp.org/cheatsheets/AI_Agent_Security_Cheat_Sheet.html) as something to avoid.

### Is a container enough to sandbox an agent?

It depends on the threat. Anthropic's computer use docs [suggest](https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool) a dedicated virtual machine or container with minimal privileges. Kubernetes Agent Sandbox [offers](https://agent-sandbox.sigs.k8s.io/) gVisor or Kata Containers underneath for stronger separation from the host.

### What is the difference between a sandbox and permissions?

A sandbox is a technical boundary the operating system or hypervisor enforces. Permissions and approvals decide when the agent may act or must ask. OpenAI's Codex docs [put it](https://learn.chatgpt.com/docs/sandboxing) as: the sandbox defines technical boundaries, and the approval policy decides when the agent pauses.

### Should a sandboxed agent have internet access?

Only to an allowlist. Anthropic's docs [recommend](https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool) limiting internet access to allowed domains, and Claude Code's sandbox [starts with an empty domain allowlist](https://code.claude.com/docs/en/sandboxing).

### Does a sandbox stop prompt injection?

No. It limits the damage after an injection succeeds. Anthropic says its computer use precautions [remain important](https://platform.claude.com/docs/en/agents-and-tools/tool-use/computer-use-tool) even with prompt injection classifiers on. See [prompt injection](/learn/prompt-injection).
