sellAI
support_agent

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.

330+Cities it runs in
1Command to scaffold
SQLDatabase per agent
lightbulb

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.

badge

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.

help

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.

save

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.

bolt

Real-time by default

WebSockets let users stream a conversation live, with messages flowing both ways instantly.

schedule

Wakes itself up

An agent can schedule future work — 'check this again in an hour' — without an external cron server.

extension

Tools & MCP

Agents can call AI models and external tools, including Model Context Protocol (MCP) tools, to actually get things done.

target

When should you use it?

forum

Stateful chatbots

A support or coding assistant that remembers the whole conversation, not just the last message.

checklist

Multi-step workflows

Tasks that take several steps or tool calls, where the agent tracks progress along the way.

alarm

Scheduled jobs

Follow-ups, reminders or recurring checks that should run later without you triggering them.

groups

One agent per user

Give every user or document its own isolated agent with its own memory and state.

rocket_launch

How do you start?

  1. Scaffold from the starter

    One command creates a ready-to-run agent project with chat, tools and scheduling already wired up.

    bash
    npm create cloudflare@latest agents-starter -- --template cloudflare/agents-starter
    cd agents-starter
    npm install
    npm run dev
  2. Configure 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.

    jsonc
    {
      "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"] }
      ]
    }
  3. 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.

    ts
    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 })
        );
      }
    };
  4. Deploy

    Ship your agent to Cloudflare's global network with one command.

    bash
    wrangler deploy
school

Key concepts

smart_toy

Agent

A class you write that handles requests, holds memory and runs tasks. Each live copy is an independent worker.

database

Durable Objects

Cloudflare's tool for giving each agent a single, consistent place to store memory and a built-in SQL database.

memory

State 狀態

The agent's memory. Read this.state and update it with this.setState — it survives between requests.

event_repeat

Scheduling 排程

this.schedule() books a method to run later — in seconds, at a time, or on a repeating cron.

tips_and_updates

Tips & billing

info

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.