Skip to main content
Sandbox provides secure, isolated environments for executing code generated by AI agents, protecting your system from potentially harmful operations.
Need model-generated code to call your registered tools? See Code Execution with Tools. The subprocess sandbox backend cannot see the agent’s tool registry, so the tool bridge runs in-process rather than in a subprocess sandbox.
The user asks the agent to execute generated code; work stays inside an isolated sandbox instead of the host shell.

Quick Start

1

Simple Usage

Enable sandbox with a single line - the easiest way to get started.
2

With Configuration

Use factory methods for specific sandbox types.
3

Full Configuration

Complete control over sandbox settings.

Agent Sandbox API

When sandbox is configured, agents gain powerful execution capabilities:

SandboxMixin API


Auto-Generated Agent Tools

When sandbox is set on an Agent, two tools are automatically available:
Available auto-generated tools:
  • execute_python_code(code: str) -> str — runs Python in the sandbox, returns stdout or error
  • execute_shell_command(command: str) -> str — runs shell command in the sandbox, returns output or error

Sandbox Backends

Choose the right backend for your security and performance needs:
Fastest, minimal isolation - development only
  • POSIX (Linux/macOS): Subprocess now blocks host env leakage, enforces memory_mb/max_processes/max_open_files via setrlimit, truncates output at max_output_size, and terminates the whole process group on timeout.
  • Windows: Still weaker — setrlimit and process-group kill are unavailable; the sandbox warns at runtime.
  • Untrusted code: Remains a Docker/E2B job, but the gap is now smaller.
Zero import cost when unused. The sandbox subsystem — including SandboxConfig, SandboxManager, security checks, and all backends — is lazy-loaded. from praisonaiagents import Agent does not load any sandbox module. Sandbox modules are only imported the first time you set sandbox=True (or pass a SandboxConfig) on an Agent, or call a sandbox method. This keeps the default import Agent path light for agents that never execute code.

What SecurityPolicy actually controls in subprocess


Resource limits in practice

POSIX setrlimit mapping enforces hard limits:
When limits are exceeded:
  • Memory: Process is killed by the kernel
  • Processes: fork() fails with EAGAIN
  • Files: open() fails with EMFILE
  • Timeout: Whole process group killed with SIGKILL

Timeout handling

On POSIX the whole process group is killed (killpg(SIGKILL)); on Windows only the leader is killed.

Subprocess execution flow


Security Pre-checks

Static code analysis warns about potentially dangerous patterns before execution:

Security API

SecurityWarning fields:
Security pre-checks do not block execution - they provide warnings only. The sandbox provides the real isolation.

Agent Integration

Agents automatically run security checks unless disabled:

Path Traversal Protection

DockerSandbox and SubprocessSandbox validate every path passed to write_file, read_file, and list_files. Paths that resolve outside the sandbox root — via ../, absolute paths, or symlink escapes — are rejected:
Windows note: Symlink escape rejection only triggers when the OS actually creates symlinks. Default non-admin Windows users cannot create symlinks (Python raises OSError: [WinError 1314] A required privilege is not held by the client) unless Windows Developer Mode is enabled (Settings → System → For developers) or the process holds SeCreateSymbolicLinkPrivilege. On such Windows setups, path-traversal protection via ../ and absolute paths still applies; the symlink branch is effectively unreachable. See PR #3224 / issue #3214.
Blocked attempts are logged as Path traversal attempt blocked: <path>. This is in addition to the container/OS isolation the sandbox already provides — defense in depth.

SandboxManager

Factory and async context manager for sandbox backends:

SandboxManager API


Configuration Options

Progressive disclosure from simple to advanced:

Factory Shortcuts

SandboxConfig Options

ResourceLimits Presets

SecurityPolicy Presets


Common Patterns


Timeout Behavior & Resource Cleanup

Understanding how different backends handle timeouts and resource cleanup ensures your resource limits are properly enforced.

Docker Timeout Handling

Docker containers get deterministic names and are properly killed on timeout: Why containers are no longer orphaned: Every docker run is launched with --name praisonai-<execution_id>. On timeout, the sandbox issues docker kill <name> to stop the actual container — not just detach the client. Your memory_mb and cpu_percent limits are now enforced through the entire execution lifecycle.

SSH Timeout Handling

SSH backend prevents both remote process leaks and temp file accumulation: Remote process cleanup: Commands are wrapped with timeout N sh -c ... to ensure remote processes terminate even if the SSH connection drops. Temp file cleanup: File cleanup (rm -f) is now in a finally block. Even if execution raises (timeout, network blip), the remote temp file is removed. Cleanup errors are swallowed so they never mask the real execution result.

Best Practices

Always use Docker sandbox when executing code from untrusted sources. Subprocess isolation is not sufficient for security-critical applications.
Keep check_security=True (default) when calling execute_code(). Review warnings in result.metadata["security_warnings"] for insights.
Configure memory and timeout limits based on expected workload. Start with minimal limits and increase as needed.
Keep allow_network=False unless code specifically needs network access. This prevents data exfiltration.
For multiple operations on the same sandbox, use async with SandboxManager(config) as sandbox: to reuse the environment efficiently.
Filesystem boundaries are enforced — write_file/read_file/list_files reject paths that escape the sandbox root, even before the backend’s isolation kicks in.

Sandboxed Agent

Complete agent with built-in sandbox

Sandbox Backends

Shell control and backend selection