Agents
An AI agent is software that can decide and act on its own. The Agents SDK gives each one memory, real-time chat and a built-in scheduler.
What are Agents?
The Agents SDK is a toolkit for building AI agents — programs that can hold a conversation, remember what happened, call AI models and tools, and even wake themselves up later to do work. They run on Cloudflare Workers, backed by Durable Objects for memory.
A plain AI chatbot forgets everything the moment a request ends. An agent is different: each one keeps its own memory and can run tasks over time, like a tireless personal assistant that never loses its notes.
Think of it like a dedicated assistant
Imagine hiring an assistant who has their own desk and filing cabinet (state), takes your calls live (WebSockets), and remembers to follow up next Tuesday (scheduling). Each user can get their very own assistant — that's an agent.
Why use it?
Building agents from scratch means juggling memory, real-time connections and background jobs across servers. The Agents SDK bundles all of that into one class so you can focus on behavior, not plumbing.
Built-in memory
Each agent has its own state and a private SQL database, kept safe by Durable Objects — no separate database to set up.
Real-time by default
WebSockets let users stream a conversation live, with messages flowing both ways instantly.
Wakes itself up
An agent can schedule future work — 'check this again in an hour' — without an external cron server.
Tools & MCP
Agents can call AI models and external tools, including Model Context Protocol (MCP) tools, to actually get things done.
When should you use it?
Stateful chatbots
A support or coding assistant that remembers the whole conversation, not just the last message.
Multi-step workflows
Tasks that take several steps or tool calls, where the agent tracks progress along the way.
Scheduled jobs
Follow-ups, reminders or recurring checks that should run later without you triggering them.
One agent per user
Give every user or document its own isolated agent with its own memory and state.
How do you start?
Scaffold from the starter
One command creates a ready-to-run agent project with chat, tools and scheduling already wired up.
npm create cloudflare@latest agents-starter -- --template cloudflare/agents-starter cd agents-starter npm install npm run devConfigure the agent binding
Agents are Durable Objects. In wrangler.jsonc, register your class and add a migration that uses new_sqlite_classes so each agent gets its own SQL database.
{ "name": "my-agent", "main": "src/index.ts", "compatibility_date": "2025-06-01", "durable_objects": { "bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }] }, "migrations": [ { "tag": "v1", "new_sqlite_classes": ["MyAgent"] } ] }Write the agent class
Extend the Agent class. onRequest handles HTTP, this.setState saves memory, and this.schedule books future work. routeAgentRequest connects incoming requests to the right agent.
import { Agent, routeAgentRequest } from "agents"; export class MyAgent extends Agent { async onRequest(request) { const count = (this.state?.count ?? 0) + 1; this.setState({ count }); // run a task 10 seconds from now this.schedule(10, "remind", { note: "follow up" }); return Response.json({ visits: count }); } async remind(payload) { console.log("Scheduled task ran:", payload.note); } } export default { async fetch(request, env) { return ( (await routeAgentRequest(request, env)) || new Response("Not found", { status: 404 }) ); } };Deploy
Ship your agent to Cloudflare's global network with one command.
wrangler deploy
Key concepts
Agent
A class you write that handles requests, holds memory and runs tasks. Each live copy is an independent worker.
Durable Objects
Cloudflare's tool for giving each agent a single, consistent place to store memory and a built-in SQL database.
State 狀態
The agent's memory. Read this.state and update it with this.setState — it survives between requests.
Scheduling 排程
this.schedule() books a method to run later — in seconds, at a time, or on a repeating cron.
Tips & billing
It's Workers + Durable Objects
There's no separate 'Agents' bill. You pay the normal Workers and Durable Objects usage, so learning Workers first makes Agents click faster.
- Use the agents-starter template — it ships with streaming chat, tools and human-in-the-loop approval.
- Connect Workers AI, OpenAI or Anthropic as the agent's 'brain'.
- Keep one agent per user or per document for clean, isolated memory.
- Put AI Gateway in front to add logging, caching and cost limits.
Related products
menu_bookOfficial docsopen_in_new