Skip to main content
ai-query provides first-class support for Cloudflare Durable Objects, allowing you to deploy stateful, long-running Agents to the edge using Python in Workers.

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:
  1. AgentDO: A base class for your Durable Object that wraps your Agent.
  2. CloudflareRegistry: A helper to route requests to the correct DO binding.

1. Define your Agent

Write your Agent as usual. Use self.state for persistence.

2. Create the Worker Script

Wrap your Agent in AgentDO and set up the fetch handler.

3. Configure wrangler.toml

You must define the Durable Object binding and class name.

Deployment

Use wrangler to deploy:

Client Usage

You can connect to your Agent via HTTP or WebSocket. HTTP Action:
WebSocket:

Resources