cf-agents
Cloudflare Agents for Python Workers
Features
- Typed,
async-first Python 3.12+ - FFI Handling under the hood. Just write Python, we handle talking to the runtime.
- Behavioural and API parity with the TypeScript Agents SDK.
Installing
It's recommended you use uv - you'll need to use pywrangler to deploy to Cloudflare.
$ uvx --from workers-py pywrangler init
$ uv add cf-agents
Quickstart
The Agent class gives you the core tools to build stateful, durable agents.
from typing import TypedDict
from agents import Agent, Schedule, callable, route_agent_request
from workers import WorkerEntrypoint, Request, Response
from utils import search_menus_by_agent, choose_winners
MODEL = "@cf/zai-org/glm-4.7-flash"
SYSTEM_PROMPT = """
You help deliver results to a bunch of co-workers who are choosing lunch together.
The user is going to provide you with the options.
Your task is to make the choice sound exciting so people who voted for something
else feel validated.
"""
class Restaurant(TypedDict):
cuisine: str
name: str
address: str
class Vote(TypedDict):
username: str
restaurant_name: str
class LunchState(TypedDict):
office_address: str
todays_votes: list[Vote]
todays_ruling: str | None
restaurants: list[Restaurant]
class LunchAgent(Agent[LunchState]):
initial_state = LunchState(
office_address="County Hall, London",
todays_votes=[],
todays_ruling=None,
restaurants=[],
)
async def on_start(self) -> None:
# Cron expressions, evaluated in UTC
await self.schedule("30 23 * * 1-5", self.choose_lunch)
await self.schedule("0 17 * * *", self.reset_lunch)
@callable
async def nominate_restaurant(self, restaurant_name: str) -> None:
# A Workflow researches the restaurant, stores its menu in
# Vectorize, then updates this agent's list of restaurants
await self.env.RESTAURANT_RESEARCHER_WORKFLOW.create(
{
"params": {
"restaurantName": restaurant_name,
"agent": self.name,
"near": self.state["office_address"],
}
}
)
@callable
async def search_restaurants(self, query: str) -> list[str]:
# Vector search, filtered by metadata to this agent's menus
results = await search_menus_by_agent(query, self.name)
return [result["metadata"]["restaurantName"] for result in results]
@callable
async def vote(self, username: str, restaurant_name: str) -> None:
state = self.state
vote = Vote(username=username, restaurant_name=restaurant_name)
votes = [*state["todays_votes"], vote]
# Broadcasts the new state to every connected eater
self.set_state({**state, "todays_votes": votes})
async def reset_lunch(self, payload: None, schedule: Schedule) -> None:
self.set_state({**self.state, "todays_votes": [], "todays_ruling": None})
async def choose_lunch(self, payload: None, schedule: Schedule) -> None:
winners = choose_winners(self.state["todays_votes"])
result = await self.env.AI.run(
MODEL,
{
"messages": [
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": ", ".join(winners)},
]
},
)
self.set_state({**self.state, "todays_ruling": result.response})
class Default(WorkerEntrypoint):
async def fetch(self, request: Request) -> Response:
response = await route_agent_request(request, self.env)
return response or Response("Not Found", status=404)
It can get tedious implementing similar patterns for chat agents over and over again. See AIChatAgent.
Goals
cf-agents is a solo project right now. The scope is restricted, but feature-ful. Planned:
- MCP
- Channels
- Integration with Agent Traces
- A harness/opinionated framework, similar to Think
- Integrations with popular Agent libraries in the Python eco-system (such as LangChain or CrewAI)
Some of these require the Cloudflare Python Workers runtime to improve. It's very easy right now to end up on the paid plan using Python Workers.
Links
Metadata
Release files for cf-agents 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| cf_agents-0.2.1.tar.gz | 266.2 kB | Without provenance | Details for cf_agents-0.2.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| cf_agents-0.2.1-py3-none-any.whl | Python 3 | none | any | Details for cf_agents-0.2.1-py3-none-any.whl |
Total release size: 584.4 kB
Release files / cf_agents-0.2.1.tar.gz
| Download URL | cf_agents-0.2.1.tar.gz |
|---|---|
| Size | 266.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
df91b3d02c6d074a79c0fc5c648b304df82b01a26470260b73f56929394bdbdc
|
|
BLAKE2b-256 checksum How to use checksums |
6ed37f0a4797446efb2fa565c34270bddb90ef3a243dacfd84cd8e5fbe215312
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / cf_agents-0.2.1-py3-none-any.whl
| Download URL | cf_agents-0.2.1-py3-none-any.whl |
|---|---|
| Size | 318.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e9593daada1de505f0e0d87e78d92675323926b62c661c29ef2c0cac58705682
|
|
BLAKE2b-256 checksum How to use checksums |
2b4a6ccede9779fe47620c413c62048ce0b4e0cd7fc1e97732b7caafafa68945
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for cf-agents 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.