Yuval Avidani
Author
Key Takeaway
Cloudflare Agents is a serverless SDK that gives our AI agents persistent memory and state at the edge using Cloudflare's Durable Objects technology. Created by Cloudflare, it eliminates the 'amnesia problem' that plagues traditional serverless AI agents by treating each agent as a unique actor with its own storage, allowing millions of agents to maintain independent state, hibernate to save costs, and wake instantly on demand.
What is Cloudflare Agents?
Cloudflare Agents is a robust SDK for building stateful AI agents on Cloudflare's edge network. The project cloudflare/agents solves the problem of state management in serverless environments that we all face when building AI agents that need to remember context across multiple interactions.
Traditional serverless functions are stateless by design - they execute, return a result, and forget everything. This works great for simple APIs but becomes a nightmare when we're building AI agents that need to maintain conversation history, user preferences, or multi-step task progress. Our agents essentially get amnesia after every request.
The Problem We All Know
We've all hit this wall: Our AI agents scale beautifully on serverless platforms, but they can't remember anything between requests. Every time a user sends a message, the agent starts fresh, forcing us to either pass entire conversation histories in every request or set up complex external state stores.
The typical solutions we reach for - Redis, DynamoDB, or other external databases - work, but they add latency, introduce synchronization challenges, and create another service we need to manage, monitor, and pay for. We end up with a serverless function that isn't really serverless anymore because it depends on a stateful database running somewhere else.
Even worse, scaling becomes tricky. Do we partition state by user? By session? How do we handle concurrent requests to the same agent? How do we avoid cold start penalties while keeping costs reasonable?
How Cloudflare Agents Works
Cloudflare Agents takes a fundamentally different approach by moving state management to the edge using Durable Objects. Think of it like giving each AI agent its own tiny dedicated server that only runs when needed and remembers everything between requests.
Here's the technical magic: Durable Objects are a Cloudflare primitive that guarantees a single instance of a JavaScript object exists globally. When we create an agent, it gets its own Durable Object with persistent storage. This object can hibernate - meaning it consumes zero compute resources - but maintains its state. When a request comes in via WebSocket or RPC (Remote Procedure Call - meaning our frontend can call server-side methods as if they were local functions), the object wakes up instantly with full context.
The SDK treats agents as classes. We extend the Agent base class, decorate methods with @callable() to expose them via RPC, and access this.state for persistent data that survives across invocations. On the frontend, a React hook like useAgent() connects to a specific agent instance, and state changes sync automatically in real-time.
Quick Start
Here's how we get started with Cloudflare Agents:
# Install the SDK
npm install @cloudflare/agents
# Define an agent class
import { Agent, callable } from '@cloudflare/agents';
export class CounterAgent extends Agent {
@callable()
async increment() {
this.state.count = (this.state.count || 0) + 1;
return this.state.count;
}
@callable()
async getCount() {
return this.state.count || 0;
}
}
A Real Example
Let's say we want to build a conversational AI agent that remembers chat history without passing it in every request:
import { Agent, callable } from '@cloudflare/agents';
export class ChatAgent extends Agent {
// Initialize with empty conversation history
async init() {
if (!this.state.messages) {
this.state.messages = [];
}
}
@callable()
async sendMessage(userMessage: string) {
// Add user message to persistent history
this.state.messages.push({
role: 'user',
content: userMessage,
timestamp: Date.now()
});
// Call LLM with full conversation context
const response = await this.callLLM(this.state.messages);
// Store assistant response
this.state.messages.push({
role: 'assistant',
content: response,
timestamp: Date.now()
});
return response;
}
@callable()
async getHistory() {
return this.state.messages;
}
@callable()
async clearHistory() {
this.state.messages = [];
}
}
On the React frontend, we connect to this agent:
import { useAgent } from '@cloudflare/agents-react';
function ChatInterface({ userId }) {
// Connect to user's specific ChatAgent instance
const agent = useAgent(ChatAgent, userId);
const [input, setInput] = useState('');
const handleSend = async () => {
// This calls the server-side method directly
const response = await agent.sendMessage(input);
setInput('');
// State updates automatically sync to UI
};
return (
{agent.state.messages?.map((msg, i) => (
{msg.role}: {msg.content}
))}
setInput(e.target.value)} />
);
}
Key Features
- Automatic Hibernation - Our agents sleep when idle, consuming zero compute resources. Think of it like putting your laptop to sleep - instant wake, no boot time, but no battery drain while sleeping. This means we can have millions of agent instances without the costs spiraling.
- WebSocket Support - Real-time bidirectional communication between client and agent. Unlike traditional request-response patterns, agents can push updates to clients instantly, perfect for streaming AI responses or collaborative editing.
- RPC with @callable() - We decorate server-side methods and call them from the frontend as if they were local. No manual API endpoint setup, no serialization headaches. It's like having the backend in our frontend code.
- Model Context Protocol (MCP) - Standardized way to connect agents to tools and data sources. MCP - meaning a protocol that defines how AI models access external context like databases, APIs, or file systems. This lets our agents work with any MCP-compatible tool without custom integration code.
- Durable Workflows - Multi-step tasks that survive failures and restarts. We can implement 'human-in-the-loop' patterns where an agent pauses execution, waits for user approval or input, then resumes exactly where it left off days later.
- Edge SQL - Run SQLite queries directly within the Durable Object for complex data operations. This is wild - we get a full relational database per agent instance, no external database setup required.
When to Use Cloudflare Agents vs. Alternatives
If we're already on Cloudflare Workers and need stateful agents, this is a no-brainer. The integration is seamless, and we avoid the complexity of external state stores. It's particularly strong for use cases like customer support bots (remember conversation across days), collaborative tools (multiple users interacting with same agent), or long-running workflows (agent orchestrates multi-day tasks).
For teams on AWS Lambda or Google Cloud Functions, alternatives like LangGraph with Supabase or Temporal.io workflows provide similar state management but require more infrastructure setup. LangGraph gives us graph-based agent orchestration with checkpointing to Postgres or Redis. Temporal excels at durable workflows with strong guarantees but adds operational complexity.
If we're building on other clouds, Modal or Fly.io with SQLite offer stateful compute primitives similar to Durable Objects. Modal gives us persistent volumes per function instance. Fly.io lets us run long-lived processes with attached persistent storage.
The trade-off is vendor lock-in. Cloudflare Agents code won't easily port to other platforms. But if we're committed to Cloudflare's edge network, that trade-off might be worth it for the simplicity and cost savings.
My Take - Will I Use This?
In my view, this is the cleanest solution to serverless state for AI agents I've seen. The hibernation model is brilliant - we get the scale and cost benefits of serverless with the statefulness of traditional servers. No more paying for idle Redis connections or managing database connection pools.
For any Cloudflare-based agent project, I'd reach for this immediately. The developer experience is fantastic - the RPC pattern feels like magic, and automatic state sync removes entire categories of bugs. Use cases where this shines: multi-session chat applications, AI assistants that learn user preferences over time, or collaborative agents where multiple users interact with the same stateful entity.
The limitations are real though. We're locked into Cloudflare's ecosystem. If we need to move to AWS or Azure later, we're rewriting. The Durable Objects model also has quirks - there's a learning curve around how to think about single-instance actors vs. traditional load-balanced services.
Bottom line: For greenfield projects on Cloudflare, this removes an entire layer of complexity. For existing projects, evaluate whether the migration effort justifies the operational simplification. Check out the repo: cloudflare/agents
Frequently Asked Questions
What is Cloudflare Agents?
Cloudflare Agents is a serverless SDK that enables persistent, stateful AI agents on Cloudflare's edge network using Durable Objects technology.
Who created Cloudflare Agents?
Cloudflare Agents was created by Cloudflare as part of their developer platform for building AI applications at the edge.
When should we use Cloudflare Agents?
Use Cloudflare Agents when building AI applications that need persistent state across requests without external databases, especially for real-time chat, collaborative tools, or long-running workflows.
What are the alternatives to Cloudflare Agents?
Alternatives include LangGraph with Supabase for graph-based agent orchestration, Temporal.io for durable workflows, Modal for stateful serverless on other clouds, or traditional approaches with Redis/DynamoDB for state management.
What are the limitations of Cloudflare Agents?
The main limitation is vendor lock-in to Cloudflare's platform - code won't easily port to AWS, Azure, or Google Cloud. There's also a learning curve around the single-instance actor model of Durable Objects.
