Prerequisites
Cloudflare Workers Project
An existing project with
wrangler.jsoncNode.js 18+
Required for the agents SDK
Installation
1
Install the package
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:
namein bindings becomes the property onenv(e.g.,env.Counter)class_namemust match your exported class name exactlynew_sqlite_classesenables SQLite storage for state persistence- The
nodejs_compatflag is required for the agents package
Export the Agent Class
Your agent class must be exported from your main entry point. Update yoursrc/index.ts:
src/index.ts
Wire Up Routing
Choose the approach that matches your project structure:- Plain Workers
- Hono
- With Static Assets
For projects using the standard
fetch handler:src/index.ts
Add TypeScript Types
Update yourEnv type to include the agent namespace. Create or update env.d.ts:
env.d.ts
Connect from the Frontend
- React
- Vanilla JavaScript
src/components/CounterWidget.tsx
Adding Multiple Agents
Add more agents by extending the configuration:src/agents/chat.ts
src/agents/scheduler.ts
wrangler.jsonc:
wrangler.jsonc
src/index.ts
Common Integration Patterns
Agents Behind Authentication
Agents Behind Authentication
Check auth before routing to agents:
Custom Agent Path Prefix
Custom Agent Path Prefix
By default, agents are routed at
/agents/{agent-name}/{instance-name}. You can customize this:Accessing Agents from Server Code
Accessing Agents from Server Code
You can interact with agents directly from your Worker code:
Troubleshooting
Agent not found or 404 errors
Agent not found or 404 errors
- Check the export - Agent class must be exported from your main entry point
- Check the binding -
class_nameinwrangler.jsoncmust match the exported class name exactly - Check the route - Default route is
/agents/{agent-name}/{instance-name}
No such Durable Object class error
No such Durable Object class error
Add the migration to
wrangler.jsonc:WebSocket connection fails
WebSocket connection fails
Ensure your routing passes the response through unchanged:
State not persisting
State not persisting
Check that:
- You’re using
this.setState(), not mutatingthis.statedirectly - The agent class is in
new_sqlite_classesin migrations - 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