Build an AI code executor
Build an AI-powered code execution system using Sandbox SDK and Claude. Turn natural language questions into Python code, execute it securely, and return results.
Time to complete: 20 minutes
An API that accepts questions like "What's the 100th Fibonacci number?", uses Claude to generate Python code, executes it in an isolated sandbox, and returns the results.
- Sign up for a Cloudflare account ↗.
- Install
Node.js
↗.
Node.js version manager
Use a Node version manager like Volta ↗ or nvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0
or later.
You'll also need:
- An Anthropic API key ↗ for Claude
- Docker ↗ running locally
Create a new Sandbox SDK project:
npm create cloudflare@latest -- ai-code-executor --template=cloudflare/sandbox-sdk/examples/minimal
yarn create cloudflare ai-code-executor --template=cloudflare/sandbox-sdk/examples/minimal
pnpm create cloudflare@latest ai-code-executor --template=cloudflare/sandbox-sdk/examples/minimal
cd ai-code-executor
Install the Anthropic SDK:
npm i @anthropic-ai/sdk
yarn add @anthropic-ai/sdk
pnpm add @anthropic-ai/sdk
Replace the contents of src/index.ts
:
import { getSandbox, type Sandbox } from '@cloudflare/sandbox';import Anthropic from '@anthropic-ai/sdk';
export { Sandbox } from '@cloudflare/sandbox';
interface Env { Sandbox: DurableObjectNamespace<Sandbox>; ANTHROPIC_API_KEY: string;}
export default { async fetch(request: Request, env: Env): Promise<Response> { if (request.method !== 'POST' || new URL(request.url).pathname !== '/execute') { return new Response('POST /execute with { "question": "your question" }'); }
try { const { question } = await request.json();
if (!question) { return Response.json({ error: 'Question is required' }, { status: 400 }); }
// Use Claude to generate Python code const anthropic = new Anthropic({ apiKey: env.ANTHROPIC_API_KEY }); const codeGeneration = await anthropic.messages.create({ model: 'claude-sonnet-4-5', max_tokens: 1024, messages: [{ role: 'user', content: `Generate Python code to answer: "${question}"
Requirements:- Use only Python standard library- Print the result using print()- Keep code simple and safe
Return ONLY the code, no explanations.` }], });
const generatedCode = codeGeneration.content[0]?.type === 'text' ? codeGeneration.content[0].text : '';
if (!generatedCode) { return Response.json({ error: 'Failed to generate code' }, { status: 500 }); }
// Execute the code in a sandbox const sandbox = getSandbox(env.Sandbox, 'demo-user'); await sandbox.writeFile('/tmp/code.py', generatedCode); const result = await sandbox.exec('python /tmp/code.py');
return Response.json({ success: result.success, question, code: generatedCode, output: result.stdout, error: result.stderr });
} catch (error: any) { return Response.json( { error: 'Internal server error', message: error.message }, { status: 500 } ); } },};
How it works:
- Receives a question via POST to
/execute
- Uses Claude to generate Python code
- Writes code to
/tmp/code.py
in the sandbox - Executes with
sandbox.exec('python /tmp/code.py')
- Returns both the code and execution results
Store your Anthropic API key as a secret:
npx wrangler secret put ANTHROPIC_API_KEY
Paste your API key from the Anthropic Console ↗ when prompted.
Start the development server:
npm run dev
Test with curl:
curl -X POST http://localhost:8787/execute \ -H "Content-Type: application/json" \ -d '{"question": "What is the 10th Fibonacci number?"}'
Response:
{ "success": true, "question": "What is the 10th Fibonacci number?", "code": "def fibonacci(n):\n if n <= 1:\n return n\n return fibonacci(n-1) + fibonacci(n-2)\n\nprint(fibonacci(10))", "output": "55\n", "error": ""}
Deploy your Worker:
npx wrangler deploy
Try different questions:
# Factorialcurl -X POST https://ai-code-executor.YOUR_SUBDOMAIN.workers.dev/execute \ -H "Content-Type: application/json" \ -d '{"question": "Calculate the factorial of 5"}'
# Statisticscurl -X POST https://ai-code-executor.YOUR_SUBDOMAIN.workers.dev/execute \ -H "Content-Type: application/json" \ -d '{"question": "What is the mean of [10, 20, 30, 40, 50]?"}'
# String manipulationcurl -X POST https://ai-code-executor.YOUR_SUBDOMAIN.workers.dev/execute \ -H "Content-Type: application/json" \ -d '{"question": "Reverse the string \"Hello World\""}'
You created an AI code execution system that:
- Accepts natural language questions
- Generates Python code with Claude
- Executes code securely in isolated sandboxes
- Returns results with error handling
- Analyze data with AI - Add pandas and matplotlib for data analysis
- Code Interpreter API - Use the built-in code interpreter instead of exec
- Streaming output - Show real-time execution progress
- API reference - Explore all available methods
- Anthropic Claude documentation ↗
- Workers AI - Use Cloudflare's built-in models
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-