Why Durable Objects?
Durable Objects (DO) provide a unique capability in the serverless landscape: State. Unlike standard serverless functions (lambdas) that are stateless and ephemeral, a Durable Object is a single instance of a class that persists data and stays alive while connections are active. This maps perfectly to the Agent model:- State: The Agent’s memory (chat history, custom state) is persisted automatically.
- Identity: You can address an Agent by ID (e.g.,
user-123,game-lobby-a). - Real-time: DOs support WebSockets natively, enabling instant streaming responses and events.
Installation
You need to install the package with standard dependencies. Since Cloudflare Workers use Pyodide (WASM), no special system dependencies are needed, but you must bundle your code correctly.Quick Start
The integration is handled by two main components:AgentDO: A base class for your Durable Object that wraps your Agent.CloudflareRegistry: A helper to route requests to the correct DO binding.
1. Define your Agent
Write your Agent as usual. Useself.state for persistence.
2. Create the Worker Script
Wrap your Agent inAgentDO and set up the fetch handler.
3. Configure wrangler.toml
You must define the Durable Object binding and class name.
Deployment
Usewrangler to deploy: