Agent Sandboxing

Production-grade guide to agent sandboxing covering architecture patterns, implementation strategies, testing approaches, and operational best practices for enterprise engineering teams.

Agent Sandboxing

Agent sandboxing confines AI agent execution to an isolated environment so that untrusted code, dynamic prompts, or tool use cannot compromise the host process or access privileged resources. It matters whenever agents receive dynamic inputs, invoke code execution, interact with external systems, or operate in multi-tenant settings. Without sandboxing, a single malicious or buggy agent can read secrets, exfiltrate data, or corrupt shared state.

Launching an Agent in a Sandbox

The most immediate step is to ensure the agent process starts inside a confined namespace rather than directly on the host. The docker run command exposes a set of well‑documented flags that enforce isolation without requiring a custom SDK.

Command Example

docker run \
  --rm \
  --security-opt apparmor=default \
  --cap-drop ALL \
  --read-only \
  --tmpfs /tmp:rw,noexec \
  --network none \
  --memory 512m \
  --cpus 1 \
  my-agent-image:latest

If the image includes an entrypoint script that starts the agent, the same flags apply. The agent process (PID 1 inside the container) inherits the sandbox configuration.

Filesystem Restrictions

Beyond the read‑only root, you can bind‑mount only the paths the agent legitimately needs:

docker run \
  --rm \
  --read-only \
  --tmpfs /tmp:rw,noexec \
  --volume /path/on/host:/agent/data:ro \
  my-agent-image

The :ro suffix on the bind‑mount keeps the agent’s data directory immutable. Writes are rejected unless a second writable mount is provided, and even then, the --tmpfs area remains the safest place for temporary files.

Network and Capability Controls

When network access is required, prefer a user‑defined bridge with egress filtering over the default docker0. Attach the container with --network my-bridge and use iptables or nftables to allow only the outbound ports the agent actually needs. Never run an agent with --network host unless you are certain the agent’s codebase cannot be compromised.

Enforcing Resource Quotas

Resource limits prevent a poorly behaved agent from consuming all host CPU, memory, or I/O throughput.

CPU and Memory Limits

docker run \
  --rm \
  --memory 512m \
  --memory-swap -1 \
  --cpus 1 \
  my-agent-image

If the agent attempts a CPU‑intensive loop or a memory‑leaking pattern, Docker logs an event and the kernel terminates the process. The exit code is 137 (SIGKILL) when the OOM killer acts.

Process and Signal Constraints

docker run \
  --rm \
  --pids-limit 256 \
  --ulimit nofile=1024:2048 \
  my-agent-image

These constraints are enforced by the kernel’s cgroup subsystem and are visible via docker stats or cgroup-stats.

Inspecting and Debugging Sandboxed Runs

When a sandboxed agent misbehaves, you need visibility into its state without breaking isolation.

Viewing Container State

docker ps -a --filter name=my-agent
docker inspect my-agent-container --format '{{.State.Status}} {{.State.ExitCode}}'

The --filter name= selects the container by a predictable naming scheme (e.g., created via --name my-agent). The inspect command returns the full JSON state, including OOM kill reasons, restart counts, and the cgroup configuration that was active at runtime.

Log Access and Tracing

docker logs my-agent-container 2>&1

Stdout and stderr from the agent are captured in the container’s journal. If the agent writes to a file inside /tmp (the only writable area), those writes appear in the log stream as well.

For deeper tracing, attach strace at container creation time:

docker run \
  --rm \
  --security-opt apparmor=default \
  --cap-add SYS_PTRACE \
  -it my-agent-image strace -f -e trace=execve,openat,write java -jar agent.jar

The --cap-add SYS_PTRACE flag grants the tracing capability, but it is generally avoided in high‑security sandboxes because it bypasses some permission checks. Use it only in development or debugging contexts.

Common Failure Modes

Sandbox Escape Attempts

Agents sometimes detect the confined environment and attempt to escape. Typical patterns include:

When an escape is attempted, Docker logs an event such as Error: mount denied by AppArmor profile or Operation not permitted. These messages appear in docker logs and in the daemon’s journal (journalctl -u docker).

Misconfigured Capabilities

Granting --cap-add NET_ADMIN or --cap-add SYS_PTRACE to an agent container opens significant attack surface. If the agent code parses or crafts network packets, it can manipulate host networking state. If the agent uses ptrace on host processes, it can read arbitrary memory.

A frequent confusion is between --cap-drop ALL and --cap-add ALL. The former removes every capability; the latter adds them all. Omitting --cap-drop ALL leaves the container with a default set of capabilities (typically 8–10 on a standard Docker daemon), which may include NET_RAW, SETUID, and FSETID. Always pair --cap-drop ALL with explicit --cap-add entries only for the minimum required operations (e.g., --cap-add NET_BIND_SERVICE if the agent must bind to a port below 1024).

Sharp Edges

What Silently Fails

Confused Options

Production Breakage Lessons

In one observed case, an agent container was deployed with --read-only and --tmpfs /tmp:rw,noexec. The agent’s runtime attempted to extract a compressed archive into /tmp/agent-cache and then chmod +x the extracted binary. The chmod succeeded (the process owns the file), but execution failed with Exec format error because noexec prevented the kernel from loading the binary. The team added --tmpfs /tmp:rw,exec and the issue resolved. The lesson: noexec is a safety net, but if the agent’s workflow requires running binaries, the flag must be balanced against the threat model.

Another production incident involved an agent that hit the --pids-limit 256 ceiling during a recursive tree‑search algorithm. The container was OOM‑killed not because of memory, but because the PID allocator refused new forks, and the kernel’s oom killer targeted the main process. The fix was to increase the limit to 4096 and profile the agent’s fork rate, demonstrating that PID limits, like memory limits, should be set from observed workload patterns rather than arbitrary values.

Key Takeaways

This page was rewritten on 13 October 2026. It replaced a templated version that was not specific to its own title. How these pages are written and checked. If anything here is wrong, tell us at [email protected] and we will correct it.