Cloudflare Claude Code Sandbox (cloudflare) (Claude Code templates (davila7)) (sandbox) (4732)
Execute Claude Code in an isolated Cloudflare Workers sandbox environment with AI-powered code execution.
Cloudflare Claude Code Sandbox
Execute Claude Code in an isolated Cloudflare Workers sandbox environment with AI-powered code execution.
Description
This component sets up Cloudflare Sandbox SDK integration to run Claude Code in a secure, isolated cloud environment. Built on Cloudflare's container-based sandboxes with Durable Objects for persistent execution.
Features
- Isolated Execution: Run Claude Code in secure Cloudflare Workers sandboxes
- AI Code Executor: Turn natural language into executable Python/Node.js code
- Real-time Streaming: Stream execution output as it happens
- Persistent Storage: Use Durable Objects for stateful sandbox sessions
- Global Distribution: Leverage Cloudflare's edge network for low latency
- Component Installation: Automatically install agents and commands in sandbox
Requirements
- Cloudflare Account (Workers Paid plan for Durable Objects)
- Anthropic API Key
- Node.js 16.17.0+
- Docker (for local development)
- Wrangler CLI
Architecture
┌─────────────────────────────────────────────────────┐
│ User Request (Natural Language) │
└──────────────────┬──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ Cloudflare Worker (API Endpoint) │
│ • POST /execute │
│ • Receives question/prompt │
└──────────────────┬──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ Claude AI (via Anthropic SDK) │
│ • Generates Python/TypeScript code │
│ • Returns executable implementation │
└──────────────────┬──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ Cloudflare Sandbox (Durable Object) │
│ • Isolated container execution │
│ • Python/Node.js runtime │
│ • File system access │
│ • Real-time streaming output │
└──────────────────┬──────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ Results │
│ • Generated code │
│ • Execution output │
│ • Error messages (if any) │
└─────────────────────────────────────────────────────┘Usage
# Execute a prompt in Cloudflare sandbox
npx claude-code-templates@latest --sandbox cloudflare --prompt "Calculate the 10th Fibonacci number"
# Pass API keys directly
npx claude-code-templates@latest --sandbox cloudflare \
--anthropic-api-key your_anthropic_key \
--prompt "Create a web scraper"
# Install components and execute
npx claude-code-templates@latest --sandbox cloudflare \
--agent frontend-developer \
--command setup-react \
--anthropic-api-key your_anthropic_key \
--prompt "Create a modern todo app"
# Deploy your own Cloudflare Worker sandbox
cd .claude/sandbox/cloudflare
npm install
npx wrangler secret put ANTHROPIC_API_KEY
npx wrangler deployEnvironment Setup
The component creates:
.claude/sandbox/cloudflare/src/index.ts- Worker with sandbox logic.claude/sandbox/cloudflare/wrangler.toml- Cloudflare configuration.claude/sandbox/cloudflare/package.json- Node.js dependencies.claude/sandbox/cloudflare/launcher.ts- TypeScript launcher script.claude/sandbox/cloudflare/monitor.ts- Real-time monitoring tool
API Key Configuration
Option 1: CLI Parameters (Recommended)
npx claude-code-templates@latest --sandbox cloudflare \
--anthropic-api-key your_anthropic_api_key \
--prompt "Your prompt here"Option 2: Wrangler Secrets
cd .claude/sandbox/cloudflare
npx wrangler secret put ANTHROPIC_API_KEY
# Paste your API key when promptedOption 3: Environment Variables
export ANTHROPIC_API_KEY=your_anthropic_api_key_here
# Or create .dev.vars file:
ANTHROPIC_API_KEY=your_anthropic_api_key_hereNote: Wrangler secrets are required for production deployment. CLI parameters work for local execution only.
How it Works
- User sends natural language request (e.g., "What's the factorial of 5?")
- Cloudflare Worker receives request via POST /execute
- Claude generates executable Python/TypeScript code via Anthropic API
- Code is written to sandbox file system
- Sandbox executes code in isolated container
- Results stream back in real-time
- Worker returns both code and execution output
Deployment
Local Development
cd .claude/sandbox/cloudflare
npm install
npm run dev
# Test locally
curl -X POST http://localhost:8787/execute \
-H "Content-Type: application/json" \
-d '{"question": "What is 2^10?"}'Production Deployment
# Set API key secret
npx wrangler secret put ANTHROPIC_API_KEY
# Deploy to Cloudflare Workers
npx wrangler deploy
# Wait 2-3 minutes for container provisioning
npx wrangler containers list
# Test deployment
curl -X POST https://your-worker.your-subdomain.workers.dev/execute \
-H "Content-Type: application/json" \
-d '{"question": "Calculate factorial of 5"}'Security Benefits
- Container Isolation: Each execution runs in isolated Cloudflare container
- No Local Access: Sandboxes have no access to your local system
- Resource Limits: Automatic CPU time and memory constraints
- Temporary Execution: Containers destroyed after execution
- Edge Security: Cloudflare's security infrastructure built-in
Advanced Features
Code Interpreter API
// Use built-in code interpreter instead of exec
import { getCodeInterpreter } from '@cloudflare/sandbox';
const interpreter = getCodeInterpreter(env.Sandbox, 'user-id');
const result = await interpreter.notebook.execCell('print(2**10)');Streaming Output
// Stream execution results in real-time
return new Response(
new ReadableStream({
async start(controller) {
const result = await sandbox.exec('python script.py', {
onStdout: (data) => controller.enqueue(data),
onStderr: (data) => controller.enqueue(data)
});
controller.close();
}
})
);Persistent Sessions
// Maintain sandbox state across requests
const sandbox = getSandbox(env.Sandbox, userId);
await sandbox.writeFile('/data/state.json', JSON.stringify(state));
// Later...
const state = await sandbox.readFile('/data/state.json');Examples
# Mathematical computation
npx claude-code-templates@latest --sandbox cloudflare \
--prompt "Calculate the 100th Fibonacci number"
# Data analysis
npx claude-code-templates@latest --sandbox cloudflare \
--prompt "What is the mean of [10, 20, 30, 40, 50]?"
# String manipulation
npx claude-code-templates@latest --sandbox cloudflare \
--prompt "Reverse the string 'Hello World'"
# Web development
npx claude-code-templates@latest --sandbox cloudflare \
--agent frontend-developer \
--prompt "Create a responsive navigation bar"Comparison with E2B
| Feature | Cloudflare Sandbox | E2B Sandbox |
|---|---|---|
| Provider | Cloudflare Workers | E2B.dev |
| Infrastructure | Cloudflare Edge Network | Cloud VMs |
| Pricing | $5/month (Workers Paid) | Usage-based |
| Cold Start | ~100ms | ~2-3 seconds |
| Max Duration | 30 seconds (Workers) | Up to hours |
| Languages | Python, Node.js | Full Linux environment |
| Global | Yes (edge network) | Single region |
| Best For | Fast, lightweight tasks | Long-running operations |
Troubleshooting
Container Not Ready
# After first deployment, wait 2-3 minutes
npx wrangler containers list
# Check container status
npx wrangler tailAPI Key Issues
# Verify secret is set
npx wrangler secret list
# Update secret
npx wrangler secret put ANTHROPIC_API_KEYLocal Development Issues
# Ensure Docker is running
docker ps
# Clear wrangler cache
rm -rf .wrangler
# Reinstall dependencies
rm -rf node_modules package-lock.json
npm installPerformance Tips
- Use Code Interpreter API for better Python performance
- Implement caching for frequently used code patterns
- Stream output for long-running operations
- Use Durable Objects for session persistence
- Deploy to multiple regions (automatic with Workers)
Template Information
- Provider: Cloudflare Workers + Sandbox SDK
- Runtime: V8 isolates with container sandboxes
- Languages: Python 3.x, Node.js
- Timeout: 30 seconds (Workers), configurable for Durable Objects
- Memory: 128MB default
- Storage: Ephemeral (use Durable Objects for persistence)
Resources
- Cloudflare Sandbox SDK Docs
- Workers Documentation
- Durable Objects Guide
- Wrangler CLI Reference
- Anthropic API Documentation
Next Steps
After installation:
- Set up Cloudflare account and get API credentials
- Install Wrangler CLI:
npm install -g wrangler - Configure secrets:
npx wrangler secret put ANTHROPIC_API_KEY - Deploy your worker:
npx wrangler deploy - Test with example requests
- Customize sandbox configuration for your use case
License
Uses Cloudflare Sandbox SDK (open source) and requires Cloudflare Workers Paid plan ($5/month) for Durable Objects support.
Author: davila7, Claude Code templates (davila7). License: MIT.
Shown here with formatting for the web. Keep the author and license notice when you reuse it.
Related entries
There are more like this in the members library
The Claude Code library holds 13,843 entries. Members can search all of them by meaning and read every one in full.