License YOLO
Back to Ultralytics Glossary

Agent Sandboxing

Agent sandboxing runs an AI agent's commands and generated code in an isolated environment that limits its files, network, credentials, and compute.

Agent sandboxing is the practice of running an AI agent's actions, such as shell commands, generated code, file edits, and web browsing, inside an isolated environment that limits which files, networks, credentials, and computing resources those actions can reach. An agent built on a large language model (LLM) chooses each command from text it has read, and that text can be wrong or hostile. The sandbox enforces its limits in the operating system or hypervisor, so they hold even when the model follows a prompt injection hidden in a web page or a README, or simply makes a mistake.

Why Agents Need Sandboxes#

Function calling and tool use turn a model's text into real actions, so an agent that can run a shell can also delete files, read SSH keys, or upload source code. The OWASP AI Agent Security Cheat Sheet lists letting agents execute arbitrary code without sandboxing among the practices to avoid, and the OWASP Top 10 for Agentic Applications, published in December 2025, names unexpected code execution as one of the ten main risks of agentic systems. Sandboxes also make agents more usable: asking a person to approve every command leads to approval fatigue, and Anthropic found that sandboxing Claude Code cut permission prompts by 84% in its internal use, because commands that stay inside the boundary no longer need a prompt.

What a Sandbox Restricts#

A sandbox for an agent typically controls five things:

  • Filesystem: The agent sees a scratch workspace or a single repository, often on a read-only base image, and cannot read credentials, browser profiles, or other projects.
  • Network: Outbound traffic is blocked or routed through a proxy that allows only listed domains, such as a package registry, so a hijacked agent cannot send data to an attacker's server.
  • Credentials: Secrets stay outside the sandbox or arrive as short-lived tokens scoped to one task, the least-privilege principle of data security.
  • Compute: Limits on CPU time, memory, process count, and wall-clock time stop runaway loops and fork bombs.
  • System calls: Filters such as seccomp block kernel interfaces that the agent's tools never need.

Isolation strength depends on where the boundary sits. Docker containers share the host kernel, so a kernel exploit can escape them. gVisor adds a user-space kernel that intercepts each container's system calls, and microVMs such as Firecracker, which AWS built for Lambda, give every sandbox its own minimal kernel while booting in under 125 ms with less than 5 MiB of memory overhead. Hosted code interpreters and cloud coding agents typically run each task in a fresh container or microVM that is destroyed afterward. On developer machines, the Claude Code sandbox uses Seatbelt on macOS and bubblewrap on Linux, lets shell commands write only to the working directory and a temp directory, and sends network traffic through a proxy that checks each host against an allowlist.

The sandbox is one layer of the agent harness, the software that executes the model's tool calls. The harness checks each proposed action against policy, runs permitted actions inside the sandbox, and pauses for a person before irreversible actions such as payments, deployments, or messages sent outside the organization:

Sandboxing Generated Code#

Agents that write and run code, from data-analysis assistants to agentic coding tools, need the tightest limits, because generated code can do anything the interpreter can. The innermost layer is ordinary process isolation, which Python's standard library can show: this script runs model-written code in a throwaway directory, with an empty environment and a CPU-time limit set through the resource module (Linux and macOS).

import os
import resource
import subprocess
import sys
import tempfile

os.environ["API_KEY"] = "sk-live-123"  # A secret the agent's own process holds

# Code the model wrote: it reads the environment, then loops forever
generated = "import os\nprint('API_KEY =', os.environ.get('API_KEY'), flush=True)\nwhile True: pass"


def limit_cpu():
    """Stop the child process after 2 seconds of CPU time."""
    resource.setrlimit(resource.RLIMIT_CPU, (2, 2))


# Run it in isolated mode, in a throwaway directory, with an empty environment
cmd = [sys.executable, "-I", "-c", generated]
with tempfile.TemporaryDirectory() as workspace:
    proc = subprocess.run(cmd, cwd=workspace, env={}, capture_output=True, text=True, preexec_fn=limit_cpu)

print(proc.stdout.strip())
print("exit code:", proc.returncode)

The child prints API_KEY = None, because it starts with an empty environment and cannot see the parent's secret, and exits with code -24, meaning the kernel killed it with SIGXCPU when its infinite loop reached the 2-second CPU limit. A container or microVM adds the filesystem and network boundaries around this process.

  • AI guardrails: Classifiers and rules that screen inputs, outputs, or proposed actions before they run. The sandbox contains whatever an action does after a guardrail lets it through.
  • Permissions: Access control decides which tools and resources an agent may use at all. Sandboxing enforces those decisions at the operating-system level, beneath any check the model could talk its way past.
  • Containerization: Packaging software with its dependencies for reproducible deployment. A container serves as a sandbox once it runs with dropped privileges, restricted mounts, and egress rules.
  • Test sandboxes: Nonproduction copies of a system where software is tried out safely. Agent sandboxes wrap every action an agent takes, in production as well as in development.

Where Agent Sandboxes Are Used#

  • Coding agents: Each task gets a fresh container or virtual machine holding one repository, where the agent installs dependencies, runs tests, and returns a diff for human review.
  • Computer-use agents: Agents that operate a desktop or browser through screenshots and clicks run in a virtual machine with its own accounts, since any page they view can carry instructions aimed at them.
  • Code interpreters: Assistants that analyze uploaded spreadsheets or images run their Python in a per-session sandbox, often with no internet access.
  • MCP servers and agent skills: Third-party tools and skill scripts run with the user's credentials, so careful hosts start local servers and scripts inside the same boundary as the agent's shell.

Hosted agent platforms apply the same limits to their own workflows. Ultralytics Platform Agents, which chains YOLO26 detection, vision-language models, and alert blocks, caps each shared trial run at 60 seconds, 20 input images, and 20 model or provider calls, and its Webhook block posts only to public HTTPS URLs, follows no redirects, and times out after 5 seconds.

Explore solutions

Computer vision in sports

Computer vision in sports

Discover how Ultralytics YOLO powers sports vision with player and ball tracking, performance analytics, coaching insights, and athlete rehabilitation.
Learn more
Computer vision for aerial imagery

Computer vision for aerial imagery

Turn drone and aerial imagery into real-time insights for agriculture, aquaculture, conservation, and geospatial analysis with Ultralytics YOLO.
Learn more
Computer vision in aerospace

Computer vision in aerospace

Apply computer vision in aerospace with Ultralytics YOLO for aircraft inspection, runway FOD detection, airport ground operations, and composite defect detection.
Learn more
Computer vision in security

Computer vision in security

Protect every site with Ultralytics YOLO models. Vision AI powers perimeter monitoring, threat detection, license-plate recognition, and workplace safety.
Learn more
Computer vision in robotics

Computer vision in robotics

Power smarter machines with Ultralytics YOLO models. Vision AI in robotics drives autonomous navigation, perception, object tracking, and real-time control.
Learn more
Computer vision in logistics

Computer vision in logistics

Streamline logistics with Ultralytics YOLO models. Vision AI enables package inspection, sorting, vehicle tracking, and real-time warehouse safety monitoring.
Learn more
Computer vision in retail

Computer vision in retail

Reimagine retail with Ultralytics YOLO models. Vision AI powers inventory tracking, shelf monitoring, queue management, and smarter customer insights.
Learn more
Computer vision in healthcare

Computer vision in healthcare

Build healthcare solutions with Ultralytics YOLO models. Vision AI in healthcare powers faster medical imaging, smarter diagnostics, and patient monitoring.
Learn more
Computer vision in manufacturing

Computer vision in manufacturing

Optimize manufacturing with Ultralytics YOLO models. Vision AI drives quality control, defect detection, PPE compliance, and assembly line automation.
Learn more
Computer vision in automotive

Computer vision in automotive

Apply computer vision in automotive with Ultralytics YOLO models. Vision AI elevates road safety, driver assistance, and vehicle automation for smarter roads.
Learn more
Computer vision in agriculture

Computer vision in agriculture

Bring vision AI to smart agriculture with Ultralytics YOLO models. Power crop monitoring, livestock tracking, and precision farming for higher, smarter yields.
Learn more

Let's build the future of AI together!

Begin your journey with the future of machine learning