Cloudflare Agents for Python Workers
cf-agents is a Python SDK for building AI agents on Cloudflare. Write stateful, durable agents in Python and deploy them on Cloudflare Workers. Orchestrate WebSockets, run scheduled and background work and stream LLM model responses.
Note
This is a port of the official Agents SDK. Crosswind ships a Pydantic AI harness and still aims to ship one similar to Project Think. If you'd like to help with this, please do contribute!
Installation
Note
Python 3.12+ is required.
Run the following in your terminal:
uv add cf-agents
If you'd like to host MCP servers (Python 3.14+) run the following:
uv add cf-agents[mcp-server]
Getting Started
Spinning up your first agent is super easy. The example below explores some basics. You might be thinking, "where are my LLM calls?"
They'll come soon, it's important to understand agent state and a request's lifecycle first.
from agents import Agent, callable, route_agent_request
from workers import WorkerEntrypoint, Request, Response
class Counter(Agent):
initial_state = {"count": 0}
@callable
async def increment(self, by: int = 1) -> int:
self.set_state({"count": self.state["count"] + by})
return self.state["count"]
class Default(WorkerEntrypoint):
async def fetch(self, request: Request) -> Response:
return await route_agent_request(request, self.env)
A browser connected with the useAgent React hook can call agent.stub.increment(1). Every connected client then receives the new state.
import { useAgent } from "agents/react";
export function Counter() {
const agent = useAgent<{ count: number }>({ agent: "Counter" });
return (
<button onClick={() => agent.stub.increment(1)}>
Count: {agent.state?.count ?? 0}
</button>
);
}
Open it in two tabs and click the button. Both tabs show the new count, without you writing any code to sync them. See Connect clients for more.
How it works
Each agent instance is a Durable Object: a class instance with its own SQLite database and a unique name. Every request for one name reaches the same instance, so it can hold state between requests and connections without a separate database.
A request reaches an agent in three steps:
- A browser opens a WebSocket with
useAgent, orcurlorfetchsends an HTTP request, to your Worker. - The Worker calls
route_agent_request, which forwards the request to the Durable Object with that name, such ascounter. - The Durable Object runs your
Agentsubclass. It holds the state it syncs to clients, its SQLite database (self.sql(...)), its schedules and queue, and its durable tasks.
route_agent_request forwards /agents/{agent}/{name} to the instance called
{name}. {agent} is the binding name in kebab case, so the binding Counter
answers at /agents/counter/{name}.
When no request, message or alarm is running, the runtime can evict the agent
from memory, even while clients are connected. Their WebSockets stay open
while the agent hibernates. The next message, request or alarm wakes it and
calls on_start() again, so keep anything you need between events in state
or SQL, not in instance attributes.
What the SDK covers
| Area | Python API |
|---|---|
| State | initial_state, self.state, set_state() |
| Client RPC | @callable methods, including streaming ones |
| WebSocket connections | on_connect, on_message, broadcast(), connection tags |
| HTTP | on_request, route_agent_request, get_agent_by_name |
| Storage | self.sql(...) over the agent's SQLite database |
| Timed work | schedule(), schedule_every(), cron expressions |
| Background work | queue(), @task with durable steps, fibers |
| Chat | AIChatAgent with on_chat_message |
| Sub-agents | self.dynamic_agents, child agents with their own SQLite |
| Durable agent loops | PydanticAIHarness, Harness, @tool |
| MCP | add_mcp_server, create_mcp_handler |
| Durable output and history | Streams, Sessions |
| Your own features | LifecycleCapability |
AIChatAgent doesn't call a model for you. You call any model client you
like in on_chat_message and yield text or typed chunks.
Not ported yet
- Chat client tools, tool approvals and automatic recovery of interrupted chat turns.
- Voice, email, workflows, channels and browser automation.
- The experimental Cap'n Web transport.
- Tracing. Structured events are ported.
Status
These pages cover cf-agents 0.2.1, which is alpha. Its API may change
between minor releases. It is an independent community project, not an
official Cloudflare SDK. It ports agents v0.25.0 and the AIChatAgent from
@cloudflare/ai-chat, with MCP from agents v0.28.1.
Next steps
- Getting started builds a counter agent and calls it over HTTP.
- Agent class lists every hook and method.
- Run a Pydantic AI agent adds a model with tools that survives a crash.