Cloudflare Agents SDK
Status: Production Ready โ
Last Updated: 2026-01-09
Dependencies: cloudflare-worker-base (recommended)
Latest Versions: [email protected], @modelcontextprotocol/sdk@latest
Production Tested: Cloudflare's own MCP servers (https://github.com/cloudflare/mcp-server-cloudflare)
Recent Updates (2025-2026):
- Jan 2026: Agents SDK v0.3.6 with callable methods fix, protocol version support updates
- Nov 2025: Agents SDK v0.2.24+ with resumable streaming (streams persist across disconnects, page refreshes, and sync across tabs/devices), MCP client improvements, schedule fixes
- Sept 2025: AI SDK v5 compatibility, automatic message migration
- Aug 2025: MCP Elicitation support, http-streamable transport, task queues, email integration
- April 2025: MCP support (MCPAgent class),
import { context } from agents
- March 2025: Package rename (agents-sdk โ agents)
AIChatAgent now supports resumable streaming, enabling clients to reconnect and continue receiving streamed responses without data loss. This solves critical real-world scenarios:
- Long-running AI responses that exceed connection timeout
- Users on unreliable networks (mobile, airplane WiFi)
- Users switching between devices mid-conversation
- Background tasks where users navigate away and return
- Real-time collaboration where multiple clients need to stay in sync
Key capability: Streams persist across page refreshes, broken connections, and sync across open tabs and devices.
Implementation (automatic in AIChatAgent):
export class ChatAgent extends AIChatAgent<Env> {
async onChatMessage(onFinish) {
return streamText({
model: openai('gpt-4o-mini'),
messages: this.messages,
onFinish
}).toTextStreamResponse();
}
}
No code changes needed - just use AIChatAgent with [email protected] or later.
Source: Agents SDK v0.2.24 Changelog
What is Cloudflare Agents?
The Cloudflare Agents SDK enables building AI-powered autonomous agents that run on Cloudflare Workers + Durable Objects. Agents can:
- Communicate in real-time via WebSockets and Server-Sent Events
- Persist state with built-in SQLite database (up to 1GB per agent)
- Schedule tasks using delays, specific dates, or cron expressions
- Run workflows by triggering asynchronous Cloudflare Workflows
- Browse the web using Browser Rendering API + Puppeteer
- Implement RAG with Vectorize vector database + Workers AI embeddings
- Build MCP servers implementing the Model Context Protocol
- Support human-in-the-loop patterns for review and approval
- Scale to millions of independent agent instances globally
Each agent instance is a globally unique, stateful micro-server that can run for seconds, minutes, or hours.
Do You Need Agents SDK?
STOP: Before using Agents SDK, ask yourself if you actually need it.
Use JUST Vercel AI SDK (Simpler) When:
- โ
Building a basic chat interface
- โ
Server-Sent Events (SSE) streaming is sufficient (one-way: server โ client)
- โ
No persistent agent state needed (or you manage it separately with D1/KV)
- โ
Single-user, single-conversation scenarios
- โ
Just need AI responses, no complex workflows or scheduling
This covers 80% of chat applications. For these cases, use Vercel AI SDK directly on Workers - it's simpler, requires less infrastructure, and handles streaming automatically.
Example (no Agents SDK needed):
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
export default {
async fetch(request: Request, env: Env) {
const { messages } = await request.json();
const result = streamText({
model: openai('gpt-4o-mini'),
messages
});
return result.toTextStreamResponse();
}
}
import { useChat } from 'ai/react';
function ChatPage() {
const { messages, input, handleSubmit } = useChat({ api: '/api/chat' });
}
Result: 100 lines of code instead of 500. No Durable Objects setup, no WebSocket complexity, no migrations.
Use Agents SDK When You Need:
- โ
WebSocket connections (true bidirectional real-time communication)
- โ
Durable Objects (globally unique, stateful agent instances)
- โ
Built-in state persistence (SQLite storage up to 1GB per agent)
- โ
Multi-agent coordination (agents calling and communicating with each other)
- โ
Scheduled tasks (delays, cron expressions, recurring jobs)
- โ
Human-in-the-loop workflows (approval gates, review processes)
- โ
Long-running agents (background processing, autonomous workflows)
- โ
MCP servers with stateful tool execution
This is ~20% of applications - when you need the infrastructure that Agents SDK provides.
Key Understanding: What Agents SDK IS vs IS NOT
Agents SDK IS:
- ๐๏ธ Infrastructure layer for WebSocket connections, Durable Objects, and state management
- ๐ง Framework for building stateful, autonomous agents
- ๐ฆ Wrapper around Durable Objects with lifecycle methods
Agents SDK IS NOT:
- โ AI inference provider (you bring your own: AI SDK, Workers AI, OpenAI, etc.)
- โ Streaming response handler (use AI SDK for automatic parsing)
- โ LLM integration (that's a separate concern)
Think of it this way:
- Agents SDK = The building (WebSockets, state, rooms)
- AI SDK / Workers AI = The AI brain (inference, reasoning, responses)
You can use them together (recommended for most cases), or use Workers AI directly (if you're willing to handle manual SSE parsing).
Decision Flowchart
Building an AI application?
โ
โโ Need WebSocket bidirectional communication? โโโโโโโโ
โ (Client sends while server streams, agent-initiated messages)
โ
โโ Need Durable Objects stateful instances? โโโโโโโโโโโค
โ (Globally unique agents with persistent memory)
โ
โโ Need multi-agent coordination? โโโโโโโโโโโโโโโโโโโโโค
โ (Agents calling/messaging other agents)
โ
โโ Need scheduled tasks or cron jobs? โโโโโโโโโโโโโโโโโค
โ (Delayed execution, recurring tasks)
โ
โโ Need human-in-the-loop workflows? โโโโโโโโโโโโโโโโโโค
โ (Approval gates, review processes)
โ
โโ If ALL above are NO โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Use AI SDK directly
(Much simpler approach)
If ANY above are YES โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ Use Agents SDK + AI SDK
(More infrastructure, more power)
Architecture Comparison
| Feature |
AI SDK Only |
Agents SDK + AI SDK |
| Setup Complexity |
๐ข Low (npm install, done) |
๐ด Higher (Durable Objects, migrations, bindings) |
| Code Volume |
๐ข ~100 lines |
๐ก ~500+ lines |
| Streaming |
โ
Automatic (SSE) |
โ
Automatic (AI SDK) or manual (Workers AI) |
| State Management |
โ ๏ธ Manual (D1/KV) |
โ
Built-in (SQLite) |
| WebSockets |
โ Manual setup |
โ
Built-in |
| React Hooks |
โ
useChat, useCompletion |
โ ๏ธ Custom hooks needed |
| Multi-agent |
โ Not supported |
โ
Built-in (routeAgentRequest) |
| Scheduling |
โ External (Queue/Workflow) |
โ
Built-in (this.schedule) |
| Use Case |
Simple chat, completions |
Complex stateful workflows |
Still Not Sure?
Start with AI SDK. You can always migrate to Agents SDK later if you discover you need WebSockets or Durable Objects. It's easier to add infrastructure later than to remove it.
For most developers: If you're building a chat interface and don't have specific requirements for WebSockets, multi-agent coordination, or scheduled tasks, use AI SDK directly. You'll ship faster and with less complexity.
Proceed with Agents SDK only if you've identified a specific need for its infrastructure capabilities.
Quick Start (10 Minutes)
1. Scaffold Project with Template
npm create cloudflare@latest my-agent -- \
--template=cloudflare/agents-starter \
--ts \
--git \
--deploy false
What this creates:
- Complete Agent project structure
- TypeScript configuration
- wrangler.jsonc with Durable Objects bindings
- Example chat agent implementation
- React client with useAgent hook
2. Or Add to Existing Worker
cd my-existing-worker
npm install agents
Then create an Agent class:
import { Agent, AgentNamespace } from "agents";
export class MyAgent extends Agent {
async onRequest(request: Request): Promise<Response> {
return new Response("Hello from Agent!");
}
}
export default MyAgent;
3. Configure Durable Objects Binding
Create or update wrangler.jsonc:
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "my-agent",
"main": "src/index.ts",
"compatibility_date": "2025-10-21",
"compatibility_flags": ["nodejs_compat"],
"durable_objects": {
"bindings": [
{
"name": "MyAgent", // MUST match class name
"class_name": "MyAgent" // MUST match exported class
}
]
},
"migrations": [
{
"tag": "v1",
"new_sqlite_classes": ["MyAgent"] // CRITICAL: Enables SQLite storage
}
]
}
CRITICAL Configuration Rules:
- โ
name and class_name MUST be identical
- โ
new_sqlite_classes MUST be in first migration (cannot add later)
- โ
Agent class MUST be exported (or binding will fail)
- โ
Migration tags CANNOT be reused (each migration needs unique tag)
4. Deploy
npx wrangler@latest deploy
Your agent is now running at: https://my-agent.<subdomain>.workers.dev
Architecture Overview: How the Pieces Fit Together
Understanding what each tool does prevents confusion and helps you choose the right combination.
The Stack
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Your Application โ
โ โ
โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ Agents SDK โ โ AI Inference โ โ
โ โ (Infra Layer) โ + โ (Brain Layer) โ โ
โ โ โ โ โ โ
โ โ โข WebSockets โ โ Choose ONE: โ โ
โ โ โข Durable Objs โ โ โข Vercel AI SDK โ
โ โ
โ โ โข State (SQL) โ โ โข Workers AI โ ๏ธ โ โ
โ โ โข Scheduling โ โ โข OpenAI Direct โ โ
โ โ โข Multi-agent โ โ โข Anthropic Direct โ โ
โ โโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ