OpenAI Agents Python 21 files ยท 0 subfolders
Copy to Workspace 09_SANDBOX_SYSTEM Shared from "OpenAI Agents Python" on Inkdown
Sandbox System - Comprehensive Deep Dive
Overview
The Sandbox system provides isolated execution environments for agents to perform real work with filesystems, run commands, and maintain state across longer time horizons. Think of a sandbox as a "secure workspace" or "container" where an agent can safely interact with files, execute code, and perform tasks without affecting your actual system.
Core Concepts
What is a Sandbox?
A sandbox is an isolated execution environment that:
Provides a filesystem for the agent to work with
Allows command execution in a controlled manner
Maintains state across multiple agent runs
Isolates the agent's actions from the host system
Supports snapshots for state preservation
Why Sandboxes Matter
Safety - Agents can't accidentally damage your system
01_AGENT_SYSTEM.md
Statefulness - Work persists across runs
Real Work - Agents can actually perform tasks (edit files, run code, etc.)
Isolation - Multiple agents can work independently
Reproducibility - Snapshots enable reproducible environments
Long-Running Tasks - Support for extended work sessions
Sandbox Types The SDK supports several sandbox implementations:
UnixLocalSandboxClient - Local Unix-based sandbox
DockerSandboxClient - Docker container sandbox
E2BSandboxClient - E2B cloud sandbox
ModalSandboxClient - Modal cloud sandbox
RunloopSandboxClient - Runloop cloud sandbox
DaytonaSandboxClient - Daytona cloud sandbox
VercelSandboxClient - Vercel cloud sandbox
Custom Clients - Implement your own sandbox client
SandboxAgent
SandboxAgent Class SandboxAgent is a specialized agent designed for sandbox environments:
Key Differences from Regular Agent:
Manifest - Defines what files/resources are available
Sandbox-aware - Knows it's running in a sandbox
Workspace focus - Designed for file/workspace operations
State persistence - Maintains sandbox state across runs
Manifest The manifest defines the sandbox workspace:
GitRepo - Clone a Git repository
LocalFile - Copy a local file
LocalDir - Copy a local directory
RemoteFile - Download a remote file
RemoteDir - Download a remote directory
InlineFile - Create a file from inline content
SandboxRunConfig Configuration for sandbox execution:
Unix Local Sandbox
UnixLocalSandboxClient Local Unix-based sandbox for development:
Creates a temporary directory on your local filesystem
Provides shell access via subprocess
Isolates via chroot (if supported)
Good for development and testing
Not truly isolated (on same machine)
Requires Unix-like system
May require sudo for full isolation
Unix Local Sandbox Options
Docker Sandbox
DockerSandboxClient Docker container-based sandbox:
True isolation via containers
Reproducible environments
Can use any Docker image
Network isolation
Docker installed and running
Sufficient disk space
Docker Sandbox Options
Cloud Sandboxes
E2B Sandbox
Cloud-hosted (no local resources)
Scalable
Pre-configured environments
Good for production
Modal Sandbox Modal cloud-based sandbox:
Runloop Sandbox Runloop cloud-based sandbox:
Daytona Sandbox Daytona cloud-based sandbox:
Vercel Sandbox Vercel cloud-based sandbox:
Sandbox Entries
GitRepo Entry
LocalFile Entry
LocalDir Entry
InlineFile Entry Create a file from inline content:
RemoteFile Entry
Sandbox Capabilities
Capability System Sandbox capabilities define what operations are allowed:
Built-in Capabilities The SDK includes several built-in capabilities:
ExecCapability - Execute commands
ReadCapability - Read files
WriteCapability - Write files
NetworkCapability - Network access
BrowserCapability - Browser automation
Capability Configuration
Sandbox Session Management
Sandbox Session Sandbox sessions maintain state across runs:
Session State Sandbox sessions can be resumed:
Sandbox Snapshots
Creating Snapshots Snapshots capture sandbox state:
Using Snapshots Restore sandbox from a snapshot:
Snapshot Benefits
Reproducibility - Exact environment reproduction
State Sharing - Share sandbox state across runs
Rollback - Return to previous state
Testing - Test against known states
Sandbox Memory
Memory Rollouts Sandbox memory can be rolled out:
Memory Types Sandbox supports different memory types:
File-based memory - Store in files
Database memory - Store in database
Custom memory - Implement your own
Sandbox Execution
Executing Commands Agents can execute commands in the sandbox:
Reading Files Agents can read files in the sandbox:
Writing Files Agents can write files in the sandbox:
Sandbox Errors
Error Types Sandbox operations can raise specific errors:
Error Handling Handle sandbox errors gracefully:
Sandbox Best Practices
1. Use Appropriate Sandboxes Choose the right sandbox for your use case:
2. Manage Manifest Size Keep manifests efficient:
3. Set Timeouts Prevent hanging operations:
4. Use Snapshots for Reproducibility Use snapshots for testing:
5. Clean Up Sessions Always clean up sessions:
Common Sandbox Patterns
1. Code Repository Analysis
2. Data Processing Pipeline
3. Testing Environment Run tests in isolated environment:
4. Build Process Build projects in sandbox:
5. Documentation Generation
Sandbox and Tracing
Sandbox Tracing Sandbox operations are traced:
Sandbox Span Data Sandbox operations create spans:
Summary The Sandbox system provides isolated execution environments. Key takeaways:
SandboxAgent is designed for sandbox environments
Manifest defines the sandbox workspace
SandboxRunConfig configures sandbox execution
UnixLocalSandboxClient provides local Unix sandbox
DockerSandboxClient provides container isolation
Cloud sandboxes (E2B, Modal, etc.) provide cloud hosting
Entries define what's in the sandbox (GitRepo, LocalFile, etc.)
Capabilities control what operations are allowed
Sessions maintain state across runs
Snapshots enable reproducible environments
Memory rollouts provide memory capabilities
Command execution via sandbox tools
File operations (read/write) in sandbox
Error handling for sandbox failures
Timeouts prevent hanging operations
Cleanup prevents resource leaks
Tracing includes sandbox operations
Isolation protects the host system
Statefulness enables long-running tasks
Reproducibility via snapshots
Sandboxes are essential for agents that need to perform real work with files and commands in a safe, isolated environment.