Enable sandbox with a single line - the easiest way to get started.
from praisonaiagents import Agentagent = Agent( name="Coder", instructions="Write and execute Python code safely.", sandbox=True # one-line enable)agent.start("Calculate fibonacci(10) and print the result")
2
With Configuration
Use factory methods for specific sandbox types.
from praisonaiagents import Agent, SandboxConfigagent = Agent( name="DataAnalyst", instructions="Analyze data with Python.", sandbox=SandboxConfig.docker("python:3.11-slim"))agent.start("Read CSV data and create a summary")
3
Full Configuration
Complete control over sandbox settings.
from praisonaiagents import Agent, SandboxConfig, ResourceLimits, SecurityPolicyagent = Agent( name="SecureAgent", instructions="Execute code with strict security.", sandbox=SandboxConfig( sandbox_type="e2b", resource_limits=ResourceLimits(memory_mb=512, timeout_seconds=60), security_policy=SecurityPolicy.standard(), ))agent.start("Process sensitive data securely")
When sandbox is set on an Agent, two tools are automatically available:
from praisonaiagents import Agentagent = Agent( name="ShellAgent", instructions="Use execute_python_code or execute_shell_command to answer questions.", sandbox=True,)agent.start("What's the current working directory and the first 5 files in it?")# The agent will autonomously call execute_shell_command("pwd && ls | head -n 5")
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
Static code analysis warns about potentially dangerous patterns before execution:
from praisonaiagents.sandbox import check_code_safety, format_warningscode = """import osos.system("rm -rf /tmp/test")"""warnings = check_code_safety(code, language="python")print(format_warnings(warnings))# Security analysis found 1 potential issue(s):# HIGH RISK:# - Direct system command execution (line 3)# Context: os.system("rm -rf /tmp/test")# Note: These are warnings only. The sandbox provides real isolation.
Agents automatically run security checks unless disabled:
result = await agent.execute_code( code="import os; os.system('ls')", check_security=True, # default)# Warnings are stored in result.metadata["security_warnings"]
from praisonaiagents import Agent, SandboxConfig, ResourceLimitsagent = Agent( name="DataScientist", instructions="Analyze data and create visualizations.", sandbox=SandboxConfig( sandbox_type="docker", image="python:3.11-slim", resource_limits=ResourceLimits.generous(), # Need memory for data mount_paths=["./data:/workspace/data:ro"], # Mount data read-only ))agent.start("Load the CSV file and show descriptive statistics")
from praisonaiagents import Agent, SandboxConfig, SecurityPolicyagent = Agent( name="SecurityReviewer", instructions="Review code for security issues.", sandbox=SandboxConfig( sandbox_type="e2b", security_policy=SecurityPolicy.strict(), # Maximum security ))agent.start("Analyze this code for potential vulnerabilities")
from praisonaiagents.sandbox import SandboxManager, SandboxConfigconfig = SandboxConfig( sandbox_type="docker", persist_files=True, # Keep files between runs auto_cleanup=False # Manual cleanup)# Process multiple files using same sandboxasync with SandboxManager(config) as sandbox: for file in files: result = await sandbox.execute(f"process_file('{file}')") print(result.stdout)
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 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.