Skip to main content

  cf-agents

Cloudflare Agents for Python Workers

PyPI - Version pypi

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.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cf-agents 0.1.2
File Size Uploaded Provenance Actions
cf_agents-0.1.2.tar.gz 208.2 kB Without provenance Details for cf_agents-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cf-agents 0.1.2
File Interpreter ABI Platform Actions
cf_agents-0.1.2-py3-none-any.whl Python 3 none any Details for cf_agents-0.1.2-py3-none-any.whl

Total release size: 469.1 kB

Release files / cf_agents-0.1.2.tar.gz

Download URL cf_agents-0.1.2.tar.gz
Size 208.2 kB
Tags Source
SHA-256 checksum
How to use checksums
828bda0727952313133f64ea1412d226cdf1ad2f079925b67398297f49eef2f7
BLAKE2b-256 checksum
How to use checksums
f1cd947378c0a815419f6bc0f0e25877ff29872438dba77c160c72465f991ec6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / cf_agents-0.1.2-py3-none-any.whl

Download URL cf_agents-0.1.2-py3-none-any.whl
Size 260.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7784acd16a230564304a1799186ef9f198415c7ff5ceb026b8f7446157d902d3
BLAKE2b-256 checksum
How to use checksums
135644dc6a2554ea9621ef85d3ecd9cc6e3ad23ab892ecdef376bce7d9377829
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.19 {"installer":{"name":"uv","version":"0.12.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for cf-agents 0.1.2

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page