Sandbox Environments
Relevant source files
The following files were used as context for generating this wiki page:
- cli-tool/components/sandbox/cloudflare/README.md
- cli-tool/components/sandbox/cloudflare/SANDBOX_DEBUGGING.md
- cli-tool/components/sandbox/cloudflare/QUICKSTART.md
- cli-tool/components/sandbox/docker/README.md
- cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/gotchas.md
- cli-tool/components/sandbox/cloudflare/IMPLEMENTATION_SUMMARY.md
- cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/README.md
The Sandbox Environments within this project provide secure, isolated execution spaces for Claude Code to run AI-generated code, perform data analysis, and manage files. These environments are designed to handle untrusted code safely by enforcing strict resource limits and network isolation while supporting multiple runtime languages such as Python and JavaScript.
The system supports various execution backends, primarily Docker for local development and Cloudflare for edge-based serverless execution. These environments allow for persistent sessions across requests and integration with AI agents for multi-tenant execution.
Sources: cli-tool/components/sandbox/docker/README.md, cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/README.md
Core Architecture and Provisioning
The sandbox architecture is built around the concept of "isolated containers" that remain persistent across requests when the same ID is provided. Each sandbox instance typically consists of a container runtime paired with a management layer (such as a Cloudflare Durable Object) to handle state and lifecycle.
Sandbox Lifecycle Flow
The following diagram illustrates the high-level process from a user prompt to the execution of code within a sandbox.
The flow starts with a user prompt which is handled by a specific launcher (Docker or Cloudflare) that ensures the environment is ready before handing execution to the Claude Agent SDK.
Sources: cli-tool/components/sandbox/docker/README.md:27-40, cli-tool/components/sandbox/cloudflare/IMPLEMENTATION_SUMMARY.md:46-68
Components and Infrastructure
| Component | Responsibility |
|---|---|
| Launcher | Orchestrates container lifecycle, builds images, and prepares environment variables. |
| Container | Isolated Linux environment (e.g., Alpine) containing runtimes like Node.js or Python. |
| Durable Object | (Cloudflare only) Manages persistence and state for specific sandbox IDs. |
| Claude Agent SDK | Provides programmatic access for Claude to interact with the sandbox tools. |
| Volume Mounts | (Docker only) Links host directories to /output for persistent file retrieval. |
Sources: cli-tool/components/sandbox/docker/README.md:42-53, cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/README.md:10-14
Execution Providers
The project implements different providers to balance speed, cost, and environment complexity.
Cloudflare Edge Sandbox
Leverages Cloudflare's global edge network for ultra-fast, serverless execution. It uses Durable Objects to ensure that a specific Sandbox ID consistently maps to the same isolated container.
- Provisioning: Cold starts take approximately 2-3 seconds.
- Isolation: No network access is allowed from within the sandbox.
- Persistent Path: Use
/workspacefor files that must persist across container sleep/wake cycles.
Cloudflare execution relies on Durable Objects to manage the lifecycle of isolated containers at the edge.
Sources: cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/README.md, cli-tool/components/sandbox/cloudflare/IMPLEMENTATION_SUMMARY.md:144-150, cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/gotchas.md:29-32
Docker Local Sandbox
Designed for local development, privacy, and offline support. It uses a non-root user (sandboxuser, UID 10001) for enhanced security.
- Base Image: Node.js 22 Alpine Linux.
- Tooling: Includes Git, Bash, Python3, Pip, and Curl.
- Output: Generated files are automatically copied to a host-mounted
output/directory.
Sources: cli-tool/components/sandbox/docker/README.md:108-115, cli-tool/components/sandbox/docker/README.md:195-204
Operational Limits and Configuration
Sandbox behavior is configurable through environment variables and provider-specific settings.
Resource Limits
| Resource | Lite | Standard | Heavy |
|---|---|---|---|
| RAM | 256MB | 512MB | 1GB |
| vCPU | 0.5 | 1 | 2 |
Sources: cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/gotchas.md:108-112
Default Timeouts
| Operation | Default Timeout | Override Option |
|---|---|---|
| Container Provisioning | 30s | SANDBOX_INSTANCE_TIMEOUT_MS |
| Port Readiness | 90s | SANDBOX_PORT_TIMEOUT_MS |
Command exec() | 120s | timeout parameter |
sleepAfter | 10m | sleepAfter parameter |
Sources: cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/gotchas.md:114-120
Security and Isolation Standards
The sandbox environments enforce several "Guardrails" to protect the host system and the project:
- Network Isolation: Sandboxes generally have no internet access after the initial build phase.
- Read-only Mounts: In Docker, the host filesystem is typically mounted as read-only, except for designated output paths.
- Non-privileged Execution: Run as non-root users to prevent privilege escalation.
- Tokenized Preview URLs: For services exposed via Cloudflare, preview URLs include auto-generated tokens that change on each operation.
Sources: cli-tool/components/sandbox/docker/README.md:188-197, cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/gotchas.md:101-106
Input Validation Pattern
To prevent command injection, the system follows a "Write then Execute" pattern rather than passing user strings directly to shells.
// SAFE: Write to file, then execute the file
await sandbox.writeFile('/workspace/user_code.py', userCode);
const result = await sandbox.exec('python3 /workspace/user_code.py');
Sources: cli-tool/components/skills/development/cloudflare-deploy/references/sandbox/gotchas.md:88-92
Debugging and Monitoring
The project includes specialized tools like monitor.ts and launcher.ts to track performance and health.
- Worker Health: Accessible via
/healthendpoint. - Real-time Metrics:
monitor.tstracks code generation time (Claude API) vs. sandbox execution time (Cloudflare/Docker). - Log Streaming: Cloudflare logs can be monitored using
npx wrangler tail.
Sources: cli-tool/components/sandbox/cloudflare/SANDBOX_DEBUGGING.md:5-18, cli-tool/components/sandbox/cloudflare/QUICKSTART.md:181-185
Sandbox Environments serve as the critical infrastructure for safe AI code execution, providing a bridge between Claude's generated logic and a controlled runtime. By offering both local Docker and edge-based Cloudflare implementations, the system ensures flexibility for diverse development and production requirements.