$npx -y skills add jackspace/ClaudeSkillz --skill cloudflare-agentsComprehensive guide for the Cloudflare Agents SDK - build AI-powered autonomous agents on Workers + Durable Objects. Use when: building AI agents, creating stateful agents with WebSockets, implementing chat agents with streaming, scheduling tasks with cron/delays, running asynchr
| 1 | # Cloudflare Agents SDK |
| 2 | |
| 3 | **Status**: Production Ready ✅ |
| 4 | **Last Updated**: 2025-10-21 |
| 5 | **Dependencies**: cloudflare-worker-base (recommended) |
| 6 | **Latest Versions**: agents@latest, @modelcontextprotocol/sdk@latest |
| 7 | **Production Tested**: Cloudflare's own MCP servers (https://github.com/cloudflare/mcp-server-cloudflare) |
| 8 | |
| 9 | --- |
| 10 | |
| 11 | ## What is Cloudflare Agents? |
| 12 | |
| 13 | The Cloudflare Agents SDK enables building AI-powered autonomous agents that run on Cloudflare Workers + Durable Objects. Agents can: |
| 14 | |
| 15 | - **Communicate in real-time** via WebSockets and Server-Sent Events |
| 16 | - **Persist state** with built-in SQLite database (up to 1GB per agent) |
| 17 | - **Schedule tasks** using delays, specific dates, or cron expressions |
| 18 | - **Run workflows** by triggering asynchronous Cloudflare Workflows |
| 19 | - **Browse the web** using Browser Rendering API + Puppeteer |
| 20 | - **Implement RAG** with Vectorize vector database + Workers AI embeddings |
| 21 | - **Build MCP servers** implementing the Model Context Protocol |
| 22 | - **Support human-in-the-loop** patterns for review and approval |
| 23 | - **Scale to millions** of independent agent instances globally |
| 24 | |
| 25 | Each agent instance is a **globally unique, stateful micro-server** that can run for seconds, minutes, or hours. |
| 26 | |
| 27 | --- |
| 28 | |
| 29 | ## Quick Start (10 Minutes) |
| 30 | |
| 31 | ### 1. Scaffold Project with Template |
| 32 | |
| 33 | ```bash |
| 34 | npm create cloudflare@latest my-agent -- \ |
| 35 | --template=cloudflare/agents-starter \ |
| 36 | --ts \ |
| 37 | --git \ |
| 38 | --deploy false |
| 39 | ``` |
| 40 | |
| 41 | **What this creates:** |
| 42 | - Complete Agent project structure |
| 43 | - TypeScript configuration |
| 44 | - wrangler.jsonc with Durable Objects bindings |
| 45 | - Example chat agent implementation |
| 46 | - React client with useAgent hook |
| 47 | |
| 48 | ### 2. Or Add to Existing Worker |
| 49 | |
| 50 | ```bash |
| 51 | cd my-existing-worker |
| 52 | npm install agents |
| 53 | ``` |
| 54 | |
| 55 | **Then create an Agent class:** |
| 56 | |
| 57 | ```typescript |
| 58 | // src/index.ts |
| 59 | import { Agent, AgentNamespace } from "agents"; |
| 60 | |
| 61 | export class MyAgent extends Agent { |
| 62 | async onRequest(request: Request): Promise<Response> { |
| 63 | return new Response("Hello from Agent!"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | export default MyAgent; |
| 68 | ``` |
| 69 | |
| 70 | ### 3. Configure Durable Objects Binding |
| 71 | |
| 72 | Create or update `wrangler.jsonc`: |
| 73 | |
| 74 | ```jsonc |
| 75 | { |
| 76 | "$schema": "node_modules/wrangler/config-schema.json", |
| 77 | "name": "my-agent", |
| 78 | "main": "src/index.ts", |
| 79 | "compatibility_date": "2025-10-21", |
| 80 | "compatibility_flags": ["nodejs_compat"], |
| 81 | "durable_objects": { |
| 82 | "bindings": [ |
| 83 | { |
| 84 | "name": "MyAgent", // MUST match class name |
| 85 | "class_name": "MyAgent" // MUST match exported class |
| 86 | } |
| 87 | ] |
| 88 | }, |
| 89 | "migrations": [ |
| 90 | { |
| 91 | "tag": "v1", |
| 92 | "new_sqlite_classes": ["MyAgent"] // CRITICAL: Enables SQLite storage |
| 93 | } |
| 94 | ] |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | **CRITICAL Configuration Rules:** |
| 99 | - ✅ `name` and `class_name` **MUST be identical** |
| 100 | - ✅ `new_sqlite_classes` **MUST be in first migration** (cannot add later) |
| 101 | - ✅ Agent class **MUST be exported** (or binding will fail) |
| 102 | - ✅ Migration tags **CANNOT be reused** (each migration needs unique tag) |
| 103 | |
| 104 | ### 4. Deploy |
| 105 | |
| 106 | ```bash |
| 107 | npx wrangler@latest deploy |
| 108 | ``` |
| 109 | |
| 110 | Your agent is now running at: `https://my-agent.<subdomain>.workers.dev` |
| 111 | |
| 112 | --- |
| 113 | |
| 114 | ## Conf |