Cloudflare containers are such good fit for Claude Agent SDK because they work differently than other container solutions. Instead of just a container, you get three components: a Worker (serverless compute), a Durable Object (storage), and a Container (isolated Agent runtime).
You use the Worker to set up context (sql queries etc) allowing you to triage requests to make sure they actually need an agent to solve them before even starting the container. So fast, so economical, so good!
npm install -g wrangler # Cloudflare CLIGet your Anthropic API key from https://console.anthropic.com/settings/keys
# 1. Install dependencies
npm install && cd container && npm install && cd ..
# 2. Create config with your Anthropic API key
cat > .dev.vars << EOF
ANTHROPIC_API_KEY=sk-ant-your-api-key-here
MODEL=claude-sonnet-4-5
API_KEY=your-secret-key-here
EOF
# 3. Start dev server (first run builds container image, takes ~30s)
npm run devWhen you see: Ready on http://localhost:XXXX
# 4. Test it (use the port from above)
./test.sh 8787
# Or manually (replace YOUR_API_KEY with value from .dev.vars):
curl -X POST http://localhost:8787/query \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_API_KEY" \
-d '{"query": "What is 2+2?"}'Expected response:
{"success": true, "response": "4"}"Unauthorized"
- Check
Authorization: Bearer <API_KEY>header is included - Verify API_KEY in
.dev.varsmatches the header value
"ANTHROPIC_API_KEY not set"
- Check
.dev.varsexists and contains your API key - Get your API key from https://console.anthropic.com/settings/keys
- Key must start with
sk-ant-
"Container failed to start"
- First run builds Docker image (~30 seconds)
- Check
docker psto see if container is running - Try
wrangler dev --localfor faster local testing
Rate limits or quota errors
- Check your Anthropic API usage at https://console.anthropic.com/settings/limits
- Upgrade your plan if needed
Request → Worker → DO.idFromName(accountId) → Container → Claude SDK
- Same
accountId= same Durable Object = serialized requests - Different
accountId= different Durable Objects = parallel execution - Containers stay warm 5 minutes (
sleepAfterin server.ts)
Learn more: Claude Agent SDK Documentation
This repo demonstrates how to set up Agent Skills in containers (current best practice, though this may change in future SDK versions). Skills are modular capabilities that transform Claude from a general-purpose assistant into a domain specialist, packaging instructions and resources that Claude uses automatically when relevant. By enabling progressive disclosure of specialized knowledge, skills eliminate repetition and context overhead—making them the recommended approach for production Agent SDK deployments.
Included: The skill-test skill in .claude/skills/skill-test/ verifies the skills system is working.
Critical requirement: The container cannot run as root when using bypassPermissions mode. The Claude CLI explicitly rejects the --dangerously-skip-permissions flag (which bypass mode maps to) when running as root user. This is a security measure to prevent accidental privilege escalation. The Dockerfile switches to the node user before running the agent (USER node).
Key setup requirements:
.claude/skills/directory copied into container (seeDockerfile:21)settingSources: ['local', 'project']in query options (seecontainer/server.ts:36)permissionMode: 'bypassPermissions'for autonomous operation (seecontainer/server.ts:37)- Container runs as non-root user:
USER node(seeDockerfile:25)
Test it:
./test-skill.sh 8787npm run deploy
wrangler secret put ANTHROPIC_API_KEY # Prompts for Anthropic API key
wrangler secret put API_KEY # Prompts for your API auth key
wrangler secret put MODEL # Optional: defaults to claude-sonnet-4-5Environment variables (.dev.vars for local, wrangler secret for production):
ANTHROPIC_API_KEY=sk-ant-... # Get from https://console.anthropic.com/settings/keys
API_KEY=your-secret-key-here # Your own API auth key for protecting the endpoint
MODEL=claude-sonnet-4-5 # Optional, defaults to claude-sonnet-4-5If you have permission from Anthropic to use Claude Code OAuth tokens (see authentication docs):
Prerequisites:
npm install -g @anthropic-ai/claude-codeSetup:
claude setup-token # Opens browser, copy token from terminal
cat > .dev.vars << EOF
CLAUDE_CODE_OAUTH_TOKEN=sk-ant-your-oauth-token-here
MODEL=claude-sonnet-4-5
API_KEY=your-secret-key-here
EOFDeploy:
wrangler secret put CLAUDE_CODE_OAUTH_TOKENNote: OAuth tokens require prior approval from Anthropic. For most users, use ANTHROPIC_API_KEY instead.
server.ts:
sleepAfter = "5m"; // How long containers stay warm
const accountId = body.accountId || "default"; // Isolation keywrangler.toml:
instance_type = "standard-2" # basic | standard-1/2/3/4
max_instances = 60MIT