home / skills / cloudflare / skills / agents-sdk

agents-sdk skill

/agents-sdk

This skill helps you build persistent, stateful AI agents on Cloudflare Workers, enabling scheduling, RPC, and streaming chat with Code Mode benefits.

npx playbooks add skill cloudflare/skills --skill agents-sdk

Review the files below or copy the command above to add this skill to your agents.

Files (6)
SKILL.md
5.3 KB
---
name: agents-sdk
description: Build stateful AI agents using the Cloudflare Agents SDK. Load when creating agents with persistent state, scheduling, RPC, MCP servers, email handling, or streaming chat. Covers Agent class, AIChatAgent, state management, and Code Mode for reduced token usage.
---

# Cloudflare Agents SDK

Build persistent, stateful AI agents on Cloudflare Workers using the `agents` npm package.

## FIRST: Verify Installation

```bash
npm install agents
```

Agents require a binding in `wrangler.jsonc`:

```jsonc
{
  "durable_objects": {
    // "class_name" must match your Agent class name exactly
    "bindings": [{ "name": "Counter", "class_name": "Counter" }]
  },
  "migrations": [
    // Required: list all Agent classes for SQLite storage
    { "tag": "v1", "new_sqlite_classes": ["Counter"] }
  ]
}
```

## Choosing an Agent Type

| Use Case | Base Class | Package |
|----------|------------|---------|
| Custom state + RPC, no chat | `Agent` | `agents` |
| Chat with message persistence | `AIChatAgent` | `@cloudflare/ai-chat` |
| Building an MCP server | `McpAgent` | `agents/mcp` |

## Key Concepts

- **Agent** base class provides state, scheduling, RPC, MCP, and email capabilities
- **AIChatAgent** adds streaming chat with automatic message persistence and resumable streams
- **Code Mode** generates executable code instead of tool calls—reduces token usage significantly
- **this.state / this.setState()** - automatic persistence to SQLite, broadcasts to clients
- **this.schedule()** - schedule tasks at Date, delay (seconds), or cron expression
- **@callable** decorator - expose methods to clients via WebSocket RPC

## Quick Reference

| Task | API |
|------|-----|
| Persist state | `this.setState({ count: 1 })` |
| Read state | `this.state.count` |
| Schedule task | `this.schedule(60, "taskMethod", payload)` |
| Schedule cron | `this.schedule("0 * * * *", "hourlyTask")` |
| Cancel schedule | `this.cancelSchedule(id)` |
| Queue task | `this.queue("processItem", payload)` |
| SQL query | `` this.sql`SELECT * FROM users WHERE id = ${id}` `` |
| RPC method | `@callable() async myMethod() { ... }` |
| Streaming RPC | `@callable({ streaming: true }) async stream(res) { ... }` |

## Minimal Agent

```typescript
import { Agent, routeAgentRequest, callable } from "agents";

type State = { count: number };

export class Counter extends Agent<Env, State> {
  initialState = { count: 0 };

  @callable()
  increment() {
    this.setState({ count: this.state.count + 1 });
    return this.state.count;
  }
}

export default {
  fetch: (req, env) => routeAgentRequest(req, env) ?? new Response("Not found", { status: 404 })
};
```

## Streaming Chat Agent

Use `AIChatAgent` for chat with automatic message persistence and resumable streaming.

**Install additional dependencies first:**
```bash
npm install @cloudflare/ai-chat ai @ai-sdk/openai
```

**Add wrangler.jsonc config** (same pattern as base Agent):
```jsonc
{
  "durable_objects": {
    "bindings": [{ "name": "Chat", "class_name": "Chat" }]
  },
  "migrations": [{ "tag": "v1", "new_sqlite_classes": ["Chat"] }]
}
```

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

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

export default {
  fetch: (req, env) => routeAgentRequest(req, env) ?? new Response("Not found", { status: 404 })
};
```

**Client** (React):
```tsx
import { useAgent } from "agents/react";
import { useAgentChat } from "@cloudflare/ai-chat/react";

const agent = useAgent({ agent: "Chat", name: "my-chat" });
const { messages, input, handleSubmit } = useAgentChat({ agent });
```

## Detailed References

- **[references/state-scheduling.md](references/state-scheduling.md)** - State persistence, scheduling, queues
- **[references/streaming-chat.md](references/streaming-chat.md)** - AIChatAgent, resumable streams, UI patterns
- **[references/codemode.md](references/codemode.md)** - Generate code instead of tool calls (token savings)
- **[references/mcp.md](references/mcp.md)** - MCP server integration
- **[references/email.md](references/email.md)** - Email routing and handling

## When to Use Code Mode

Code Mode generates executable JavaScript instead of making individual tool calls. Use it when:

- Chaining multiple tool calls in sequence
- Complex conditional logic across tools
- MCP server orchestration (multiple servers)
- Token budget is constrained

See [references/codemode.md](references/codemode.md) for setup and examples.

## Best Practices

1. **Prefer streaming**: Use `streamText` and `toUIMessageStreamResponse()` for chat
2. **Use AIChatAgent for chat**: Handles message persistence and resumable streams automatically
3. **Type your state**: `Agent<Env, State>` ensures type safety for `this.state`
4. **Use @callable for RPC**: Cleaner than manual WebSocket message handling
5. **Code Mode for complex workflows**: Reduces round-trips and token usage
6. **Schedule vs Queue**: Use `schedule()` for time-based, `queue()` for sequential processing

Overview

This skill helps you build stateful AI agents on Cloudflare Workers using the Cloudflare Agents SDK. It covers Agent and AIChatAgent classes, state persistence, scheduling, RPC, MCP servers, email handling, streaming chat, and Code Mode for lower token usage. Use it to create durable, schedulable agents with streaming chat or RPC endpoints.

How this skill works

The SDK provides base classes (Agent, AIChatAgent, McpAgent) that run as Durable Objects and persist state to SQLite. Agents expose @callable RPC methods, can schedule or queue tasks, and broadcast state changes. AIChatAgent adds automatic message persistence and resumable streaming; Code Mode emits executable code instead of tool calls to save tokens.

When to use it

  • Build a persistent counter, workflow, or bot with durable state and RPC
  • Create streaming chat UIs with message persistence and resumable streams
  • Run scheduled jobs or cron-based tasks from within an agent
  • Implement an MCP server or email routing inside Cloudflare Workers
  • Optimize token usage across multi-step tool orchestration using Code Mode

Best practices

  • Install and configure Durable Object bindings and migrations in wrangler.jsonc before deploying
  • Type your agent state (Agent<Env, State>) to make state updates safer and clearer
  • Prefer streaming responses for chat (streamText + toUIMessageStreamResponse) to improve UX and reduce latency
  • Expose client methods with @callable for clean RPC over WebSocket
  • Use schedule() for time-based tasks and queue() for sequential processing; cancel schedules with cancelSchedule(id)
  • Switch to Code Mode when chaining multiple tool calls or when token budget is tight

Example use cases

  • A chat agent that persists conversation history and streams model responses to a web UI
  • A scheduled billing or maintenance task run from a durable agent using cron schedules
  • An MCP orchestration agent that coordinates multiple servers with reduced token overhead via Code Mode
  • An RPC-enabled agent that exposes control methods to a client dashboard via @callable
  • An email routing agent that receives, processes, and queues messages inside Durable Objects

FAQ

Do agents automatically persist state?

Yes. Use this.setState() and this.state; state is persisted to SQLite and can be broadcast to clients.

When should I use AIChatAgent vs Agent?

Use AIChatAgent for chat scenarios with message persistence and resumable streaming. Use Agent for non-chat stateful RPC, scheduling, MCP, or email workflows.