Build persistent, stateful AI agents on Cloudflare Workers using the agents npm package.
| Custom state + RPC, no chat | Agent | agents | | Chat with message persistence | AIChatAgent | @cloudflare/ai-chat | | Building an MCP server | McpAgent | agents/mcp |
| 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.sqlSELECT FROM users WHERE id = ${id} ` |
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. Source: null-shot/cloudflare-skills.