Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/agents-sdk/README.md
1# Cloudflare Agents SDK23Cloudflare Agents SDK enables building AI-powered agents on Durable Objects with state, WebSockets, SQL, scheduling, and AI integration.45## Core Value6Build stateful, globally distributed AI agents with persistent memory, real-time connections, scheduled tasks, and async workflows.78## When to Use9- Persistent state + memory required10- Real-time WebSocket connections11- Long-running workflows (minutes/hours)12- Chat interfaces with AI models13- Scheduled/recurring tasks with state14- DB queries with agent state1516## What Type of Agent?1718| Use Case | Class | Key Features |19|----------|-------|--------------|20| AI chat interface | `AIChatAgent` | Auto-streaming, tools, message history, resumable |21| MCP tool provider | `Agent` + MCP | Expose tools to AI systems |22| Custom logic/routing | `Agent` | Full control, WebSockets, email, SQL |23| Real-time collaboration | `Agent` | WebSocket state, broadcasts |24| Email processing | `Agent` | `onEmail()` handler |2526## Quick Start2728**AI Chat Agent:**29```typescript30import { AIChatAgent } from "@cloudflare/ai-chat";31import { openai } from "@ai-sdk/openai";3233export class ChatAgent extends AIChatAgent<Env> {34async onChatMessage(onFinish) {35return this.streamText({36model: openai("gpt-4"),37messages: this.messages,38onFinish,39});40}41}42```4344**Base Agent:**45```typescript46import { Agent } from "agents";4748export class MyAgent extends Agent<Env> {49onStart() {50this.sql`CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY)`;51}5253async onRequest(request: Request) {54return Response.json({ state: this.state });55}56}57```5859## Reading Order6061| Task | Files to Read |62|------|---------------|63| Quick start | README only |64| Build chat agent | README → api.md (AIChatAgent) → patterns.md |65| Setup project | README → configuration.md |66| Add React frontend | README → api.md (Client Hooks) → patterns.md |67| Build MCP server | api.md (MCP) → patterns.md |68| Background tasks | api.md (Scheduling, Task Queue) → patterns.md |69| Debug issues | gotchas.md |7071## Package Entry Points7273| Import | Purpose |74|--------|---------|75| `agents` | Server-side Agent classes, lifecycle |76| `agents/react` | `useAgent()` hook for WebSocket connections |77| `agents/ai-react` | `useAgentChat()` hook for AI chat UIs |7879## In This Reference80- [configuration.md](./configuration.md) - SDK setup, wrangler config, routing81- [api.md](./api.md) - Agent classes, lifecycle, client hooks82- [patterns.md](./patterns.md) - Common workflows, best practices83- [gotchas.md](./gotchas.md) - Common issues, limits8485## See Also86- durable-objects - Agent infrastructure87- d1 - External database integration88- workers-ai - AI model integration89- vectorize - Vector search for RAG patterns