Cloudflare Claude Code Sandbox
Execute Claude Code in isolated Cloudflare Workers sandboxes with AI-powered code generation.
Quick Start
1. Install Dependencies
npm install
2. Configure API Key
# For local development, create .dev.vars:
echo "ANTHROPIC_API_KEY=your-api-key-here" > .dev.vars
# For production, use wrangler secrets:
npx wrangler secret put ANTHROPIC_API_KEY
3. Local Development
# Start development server (requires Docker)
npm run dev
# In another terminal, test the endpoint:
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "What is the 10th Fibonacci number?"}'
4. Deploy to Cloudflare
# Deploy worker
npx wrangler deploy
# Wait 2-3 minutes for container provisioning
npx wrangler containers list
# Test production endpoint
curl -X POST https://your-worker.your-subdomain.workers.dev/execute \
-H "Content-Type: application/json" \
-d '{"question": "Calculate factorial of 5"}'
Architecture
This sandbox combines three powerful technologies:
- Claude AI (Anthropic) - Generates executable code from natural language
- Cloudflare Workers - Runs at the edge for global low-latency access
- Sandbox SDK - Provides isolated container execution
User Question → Cloudflare Worker → Claude AI → Generated Code → Sandbox → Results
API Reference
POST /execute
Execute a natural language question as code.
Request:
{
"question": "What is the 100th Fibonacci number?",
"maxTokens": 2048, // Optional: Max tokens for code generation
"timeout": 30000, // Optional: Execution timeout in ms
"language": "python" // Optional: "python" or "javascript"
}
Response:
{
"success": true,
"question": "What is the 100th Fibonacci number?",
"code": "def fibonacci(n):\n if n <= 1:\n return n\n return fibonacci(n-1) + fibonacci(n-2)\n\nprint(fibonacci(100))",
"output": "354224848179261915075\n",
"error": "",
"sandboxId": "user-1234567890-abc123",
"executionTime": 2147
}
GET /health
Check worker health status.
Response:
{
"status": "healthy",
"timestamp": "2025-10-19T12:00:00.000Z",
"worker": "cloudflare-claude-sandbox"
}
Command Line Tools
Launcher
Execute prompts directly from command line:
# Basic usage
node launcher.ts "Calculate factorial of 5"
# With custom worker URL
node launcher.ts "Fibonacci 10" "" your_api_key https://your-worker.workers.dev
# With components
node launcher.ts "Create a React app" "--agent frontend-developer" your_api_key
Monitor
Monitor execution with detailed metrics:
# Monitor execution
node monitor.ts "Calculate factorial of 5" your_api_key
# Monitor production worker
node monitor.ts "Sum array" your_api_key https://your-worker.workers.dev
Examples
Mathematical Calculations
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "What is the factorial of 20?"}'
String Manipulation
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "Reverse the string Hello World"}'
Data Analysis
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "Calculate the mean of [10, 20, 30, 40, 50]"}'
JavaScript Execution
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{
"question": "Sort an array of numbers",
"language": "javascript"
}'
Configuration
Environment Variables
Local Development (.dev.vars):
ANTHROPIC_API_KEY=sk-ant-your-key-here
Production (Wrangler Secrets):
npx wrangler secret put ANTHROPIC_API_KEY
Wrangler Configuration
Edit wrangler.toml to customize:
# Worker name (must be unique)
name = "my-custom-sandbox"
# Resource limits
[limits]
cpu_ms = 100 # CPU time per request
# Environment-specific configuration
[env.production]
vars = { ENVIRONMENT = "production" }
Troubleshooting
Container Not Ready
After first deployment, wait 2-3 minutes:
npx wrangler containers list
Docker Issues (Local Development)
Ensure Docker is running:
docker ps
API Key Not Set
For local development:
echo "ANTHROPIC_API_KEY=your-key" > .dev.vars
For production:
npx wrangler secret put ANTHROPIC_API_KEY
View Logs
# Real-time logs
npx wrangler tail
# Pretty formatted
npx wrangler tail --format=pretty
Performance Tips
- Use specific prompts: More specific questions generate faster code
- Implement caching: Cache generated code for common questions
- Stream output: Use streaming for long-running operations
- Set appropriate timeouts: Balance between UX and resource usage
- Monitor metrics: Use the monitor tool to identify bottlenecks
Security
- Sandboxes are isolated containers with no network access
- Code execution is limited by timeout (default 30s)
- CPU and memory limits are enforced by Cloudflare
- API keys are stored as encrypted Wrangler secrets
- CORS is enabled for browser access (configure as needed)
Cost Estimation
Cloudflare Workers:
- Free tier: 100,000 requests/day (limited Durable Objects)
- Paid plan ($5/month): 10M requests/month + unlimited Durable Objects
Anthropic API:
- Claude Sonnet 4.5: ~$3 per million input tokens
- Average request: ~200 tokens = $0.0006 per request
Example costs for 10,000 requests/month:
- Cloudflare: $5/month (paid plan)
- Anthropic: ~$6/month (avg 200 tokens/request)
- Total: ~$11/month
Development
Project Structure
cloudflare-claude-sandbox/
├── src/
│ └── index.ts # Worker source code
├── launcher.ts # CLI launcher tool
├── monitor.ts # Monitoring tool
├── wrangler.toml # Cloudflare configuration
├── package.json # Dependencies
├── tsconfig.json # TypeScript config
└── README.md # This file
Scripts
npm run dev- Start local development servernpm run deploy- Deploy to Cloudflarenpm run tail- View real-time logsnpm run launch- Run launcher toolnpm run monitor- Run monitoring toolnpm run type-check- Check TypeScript typesnpm test- Run tests
Resources
- Cloudflare Sandbox SDK
- Cloudflare Workers Documentation
- Anthropic API Documentation
- Wrangler CLI Reference
License
MIT License - See LICENSE file for details
Support
For issues and questions:
- Check the debugging guide
- Run the monitor tool for detailed metrics
- Check Cloudflare worker logs:
npx wrangler tail - Open an issue on GitHub
Built with ❤️ using Cloudflare Workers, Claude AI, and the Sandbox SDK