Skip to main content
This guide shows how to add agents to an existing Cloudflare Workers project. If you’re starting fresh, see Quick Start instead.

Prerequisites

Cloudflare Workers Project

An existing project with wrangler.jsonc

Node.js 18+

Required for the agents SDK

Installation

1

Install the package

For React applications, no additional packages are needed—React bindings are included.
2

For Hono applications

If you’re using Hono, install the Hono integration:

Create an Agent

Create a new file for your agent (e.g., src/agents/counter.ts):
src/agents/counter.ts

Update wrangler.jsonc

Add the Durable Object binding and migration:
wrangler.jsonc
Key points:
  • name in bindings becomes the property on env (e.g., env.Counter)
  • class_name must match your exported class name exactly
  • new_sqlite_classes enables SQLite storage for state persistence
  • The nodejs_compat flag is required for the agents package

Export the Agent Class

Your agent class must be exported from your main entry point. Update your src/index.ts:
src/index.ts

Wire Up Routing

Choose the approach that matches your project structure:
For projects using the standard fetch handler:
src/index.ts

Add TypeScript Types

Update your Env type to include the agent namespace. Create or update env.d.ts:
env.d.ts

Connect from the Frontend

src/components/CounterWidget.tsx

Adding Multiple Agents

Add more agents by extending the configuration:
src/agents/chat.ts
src/agents/scheduler.ts
Update wrangler.jsonc:
wrangler.jsonc
Export all agents from your entry point:
src/index.ts

Common Integration Patterns

Check auth before routing to agents:
By default, agents are routed at /agents/{agent-name}/{instance-name}. You can customize this:
You can interact with agents directly from your Worker code:

Troubleshooting

  1. Check the export - Agent class must be exported from your main entry point
  2. Check the binding - class_name in wrangler.jsonc must match the exported class name exactly
  3. Check the route - Default route is /agents/{agent-name}/{instance-name}
Add the migration to wrangler.jsonc:
Ensure your routing passes the response through unchanged:
Check that:
  1. You’re using this.setState(), not mutating this.state directly
  2. The agent class is in new_sqlite_classes in migrations
  3. You’re connecting to the same agent instance name

Next Steps

State Management

Deep dive into agent state

Scheduling

Background tasks and cron jobs

Agent Class

Full lifecycle and methods

Client SDK

Complete client API reference