---
title: "Cloudflare Agents - Build Stateful AI Agents"
description: "Build proactive AI agents with built-in memory, scheduling, and real-time communication. Every agent gets its own SQLite database and can respond to emails, WebSockets, and scheduled events."
url: "https://www.cloudflare.com/products/agents"
---

# Agents

> The Agents SDK gives you the primitives to build proactive, stateful AI agents on Cloudflare. Built-in memory, scheduling, email handling, and real-time communication — agents that don't just respond, but take initiative.

## Benefits

### Built-in memory

Every agent instance has its own SQLite database. Persist conversation history, user preferences, and state without managing infrastructure.

### Proactive by design

Agents can schedule tasks, respond to emails, handle webhooks, and send messages — not just wait for user input.

### Multiplayer by default

Built-in WebSocket support and state synchronization. Multiple users can interact with the same agent instance in real-time.

## Use Cases

### Proactive assistants

Agents that send scheduled emails, monitor systems, and take action without waiting for user prompts.

### Real-time collaboration

Multiplayer chat, collaborative editing, or shared agent sessions where multiple users interact simultaneously.

### Messaging and notifications

Integrate with Slack, Discord, email, or any messaging platform. Agents can both receive and send messages proactively.

### Workflow automation

Orchestrate multi-step tasks like data processing pipelines and order fulfillment flows. Durable [Workflows](/products/workflows) handle retries and failures automatically.

## Code Examples

### Resumable streaming chat

Build chat agents with automatic stream resumption. If a client disconnects, the agent buffers and resumes seamlessly.

```typescript
import { routeAgentRequest } from "agents";
import { AIChatAgent } from "@cloudflare/ai-chat";
import { streamText, convertToModelMessages } from "ai";
import { openai } from "@ai-sdk/openai";

export class ChatAgent extends AIChatAgent<Env> {
  async onChatMessage(onFinish) {
    const result = streamText({
      model: openai("gpt-5"),
      messages: await convertToModelMessages(this.messages),
      onFinish,
    });

    return result.toUIMessageStreamResponse();
  }
}

export default {
  async fetch(request: Request, env: Env) {
    return (await routeAgentRequest(request, env)) ||
      new Response("Not found", { status: 404 });
  },
};
```

### Let LLMs write code to call tools

Code Mode converts MCP tools into a TypeScript API. LLMs write code instead of making tool calls — better accuracy, fewer tokens.

```typescript
import { Agent } from "agents";
import { experimental_codemode as codemode } from "@cloudflare/codemode/ai";
import { streamText, convertToModelMessages } from "ai";
import { openai } from "@ai-sdk/openai";

export class CodeModeAgent extends Agent<Env> {
  async onChatMessage(onFinish) {
    // Wrap tools with Code Mode - LLMs write code
    // instead of making tool calls directly
    const { prompt, tools } = await codemode({
      prompt: "You are a helpful assistant",
      tools: this.mcp.getAITools(),
      // ... additional config for proxy, loader, globalOutbound
    });

    return streamText({
      model: openai("gpt-5"),
      system: prompt,
      tools,
      messages: await convertToModelMessages(this.messages),
      onFinish,
    });
  }
}
```

### Durable multi-step orchestration

Extend AgentWorkflow for tasks that need durability, retries, and progress reporting back to the Agent.

```typescript
import { AgentWorkflow } from "agents/workflows";
import type { AgentWorkflowEvent, AgentWorkflowStep } from "agents/workflows";
import type { MyAgent } from "./agent";

type TaskParams = { taskId: string; data: string };

export class ProcessingWorkflow extends AgentWorkflow<MyAgent, TaskParams> {
  async run(event: AgentWorkflowEvent<TaskParams>, step: AgentWorkflowStep) {
    const { taskId, data } = event.payload;

    // Durable step with automatic retries
    const result = await step.do("process-data", async () => {
      return processData(data);
    });

    // Report progress to Agent (broadcasts to clients)
    await this.reportProgress({ step: "process", percent: 0.5 });

    // Call Agent method via typed RPC
    await this.agent.saveResult(taskId, result);

    // Report completion
    await step.reportComplete(result);
    return result;
  }
}
```

## Resources

- [Full Documentation](https://developers.cloudflare.com/agents): Complete technical documentation
- [Get Started](https://dash.cloudflare.com/sign-up): Sign up and start building
- [Pricing](/plans.md): See pricing details

## Related Products

- [AI Gateway](/products/ai-gateway.md): AI observability
- [AI Search](/products/ai-search.md): Instant retrieval
- [Vectorize](/products/vectorize.md): Vector database
- [Web3](/products/web3.md): Web3 Infrastructure

---

*This is a markdown version of [https://www.cloudflare.com/products/agents](https://www.cloudflare.com/products/agents) for AI/LLM consumption.*
