Đang tải…
Đang tải…
npx claude-code-templates@latest --sandbox cloudflare/claude-code-sandboxExecute Claude Code in an isolated Cloudflare Workers sandbox environment with AI-powered code execution.
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.
┌─────────────────────────────────────────────────────┐
│ 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) │
└─────────────────────────────────────────────────────┘
# 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 deploy
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 toolnpx claude-code-templates@latest --sandbox cloudflare \
--anthropic-api-key your_anthropic_api_key \
--prompt "Your prompt here"
cd .claude/sandbox/cloudflare
npx wrangler secret put ANTHROPIC_API_KEY
# Paste your API key when prompted
export ANTHROPIC_API_KEY=your_anthropic_api_key_here
# Or create .dev.vars file:
ANTHROPIC_API_KEY=your_anthropic_api_key_here
Note: Wrangler secrets are required for production deployment. CLI parameters work for local execution only.
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?"}'
# 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"}'
// 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)');
// 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();
}
})
);
// 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');
# 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"
| 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 |
# After first deployment, wait 2-3 minutes
npx wrangler containers list
# Check container status
npx wrangler tail
# Verify secret is set
npx wrangler secret list
# Update secret
npx wrangler secret put ANTHROPIC_API_KEY
# Ensure Docker is running
docker ps
# Clear wrangler cache
rm -rf .wrangler
# Reinstall dependencies
rm -rf node_modules package-lock.json
npm install
After installation:
npm install -g wranglernpx wrangler secret put ANTHROPIC_API_KEYnpx wrangler deployUses Cloudflare Sandbox SDK (open source) and requires Cloudflare Workers Paid plan ($5/month) for Durable Objects support.