Safe Sandbox Limits for Autonomous AI Agents
This guide explains how to set up safe sandbox limits for autonomous AI agents, ensuring they can operate without causing harm to your system, data, or external services. You’ll learn how to define resource constraints, network restrictions, and permission boundaries, and how to implement them effectively. By the end, you’ll have a clear, actionable framework for containing AI agents in a secure environment.
What You’ll Need
- A Linux-based environment (Ubuntu 20.04+ recommended) or macOS with Docker installed.
- Basic familiarity with command-line interfaces and YAML configuration files.
- An AI agent framework or script you intend to sandbox (e.g., a Python-based agent).
- Administrative (sudo) access to your machine to modify system settings.
- Optional: A separate user account for the agent to run under (recommended for isolation).
Step-by-Step
- Define the agent’s scope. Write down exactly what the agent is allowed to do: which files it can read/write, which network endpoints it may contact, and what system resources it can consume. This becomes your policy.
- Create a dedicated user account. Run
sudo useradd -m -s /bin/bash agentuserto create a non-privileged user. This ensures the agent doesn’t run with your personal permissions. - Set up a working directory. Create a directory owned by the agent user, e.g.,
sudo mkdir /home/agentuser/workandsudo chown agentuser:agentuser /home/agentuser/work. This is the only directory the agent can write to. - Use Docker for resource limits. Create a Docker container with explicit limits. Example command:
docker run -d --name agent_sandbox --user 1001:1001 --memory=512m --cpus=1.0 --pids-limit=128 --read-only --tmpfs /tmp:rw,size=100m --network none -v /home/agentuser/work:/work agent_image
This restricts memory, CPU, process count, filesystem writes, and network access. - Configure network restrictions. If the agent needs internet, use Docker’s network capabilities. Instead of
--network none, create a custom network with egress filtering:docker network create --driver bridge --internal agent_netthen attach the container. For allowlisting specific IPs, use iptables rules on the host. - Apply filesystem permissions. Inside the container, ensure the agent only has write access to
/work. Set the rest of the filesystem as read-only (already done with--read-only). For host-level access, use bind mounts with read-only flags:-v /path/to/data:/data:ro. - Limit system calls (seccomp). Generate a seccomp profile that only allows necessary syscalls. Use Docker’s default profile as a baseline:
docker run --security-opt seccomp=default.json .... Customize the JSON to block dangerous calls likerebootormount. - Set CPU and memory limits at the OS level. If not using Docker, use
systemdresource controls. Create a service file for the agent withMemoryMax=512M,CPUQuota=100%, andTasksMax=128. - Implement timeouts. Use a wrapper script that kills the agent after a maximum runtime, e.g.,
timeout 300 ./agent_script.py. This prevents infinite loops. - Log all actions. Enable audit logging on the host:
sudo auditctl -w /home/agentuser/work -p wa -k agent_activity. Also, have the agent itself log its actions to a file outside its writable area. - Test the sandbox. Run the agent with a harmless task that tries to access forbidden resources (e.g., reading /etc/passwd). Verify it fails. Check that it can write to /work but not elsewhere.
- Monitor and update. Set up a cron job or monitoring script to check resource usage daily. Regularly review logs and adjust limits as needed.
Common Mistakes
- Forgetting to restrict network access. Many agents can exfiltrate data if left with default network settings.
- Using the host’s filesystem directly instead of a dedicated directory, which can lead to accidental deletion or modification of system files.
- Setting memory limits too low, causing the agent to crash frequently; or too high, allowing resource exhaustion.
- Ignoring process limits – an agent can fork bomb the system if
pids-limitis not set. - Running the agent as root or with sudo privileges, defeating the purpose of the sandbox.
- Not testing the sandbox thoroughly before deployment.
Tips
- Start with strict limits and gradually loosen them based on observed needs.
- Use Docker’s
--cap-drop ALLto remove all Linux capabilities, then add back only what’s needed (e.g.,--cap-add NET_BIND_SERVICE). - For file access, consider using a FUSE filesystem that restricts paths.
- Use environment variables to pass secrets instead of embedding them in code.
- Regularly update the agent’s image to patch vulnerabilities.
- Document your policies and limits in a README for future reference.
Troubleshooting
- Agent cannot write to /work: Check ownership and permissions:
docker exec agent_sandbox ls -la /work. Ensure the user ID inside the container matches the host user’s ID. - Agent times out unexpectedly: Increase the timeout in the wrapper script, but also check if the agent is stuck in a loop – use
docker statsto see CPU usage. - Network not working when needed: If you used
--network none, you cannot change it without recreating the container. Instead, create a new container with the appropriate network. - Docker container fails to start with seccomp profile: The profile might be too restrictive. Test with the default profile first, then add restrictions incrementally.
- Agent still can access host files: Ensure you didn’t mount the host root. Check
docker inspect agent_sandboxfor mounts. - Resource limits not enforced: Verify you’re using the correct flags – for memory, use
--memorynot--memory-swapalone.
Frequently Asked Questions
Can I use these limits for a non-Docker agent?
Yes, you can use systemd resource controls or traditional Unix limits (ulimit) to restrict memory, CPU, and file size, but network and syscall restrictions are harder without containers.
What is the most important limit to set?
Network restriction is often the most critical, as it prevents data exfiltration and unauthorized access. However, memory and CPU limits are also essential to avoid resource exhaustion.
How do I allow the agent to access an API but not the internet?
Use a proxy or a firewall rule that only allows traffic to the specific API’s IP address and port. In Docker, you can use iptables to filter egress traffic.
Can I change limits after the container is running?
Some limits like memory and CPU can be updated with docker update, but network and seccomp profiles require recreating the container.
Do I need a separate user account if I use Docker?
It’s still recommended for defense-in-depth. If the container is compromised, the user account limits what the attacker can do on the host.
How often should I review the sandbox limits?
Review after any significant change to the agent’s tasks or at least monthly. Also, check logs regularly for any suspicious activity.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.