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

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.2.0
File Size Uploaded Provenance Actions
cf_agents-0.2.0.tar.gz 265.8 kB Without provenance Details for cf_agents-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 583.6 kB

Release files / cf_agents-0.2.0.tar.gz

Download URL cf_agents-0.2.0.tar.gz
Size 265.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4d8a228ea730a47d73ef7d0c72471133dcdcc02022962ac71bea004726836f21
BLAKE2b-256 checksum
How to use checksums
55461f1512c1cf5ee1b2b6f9c88fa62b036a6e6066d7ab122784dd4ae83efe79
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":"24.04","id":"noble","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.0-py3-none-any.whl

Download URL cf_agents-0.2.0-py3-none-any.whl
Size 317.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2e1a5f6f052a32a25a62ce9cd67c2d847d415940579c55755dc6115b5ede2571
BLAKE2b-256 checksum
How to use checksums
e51a56a387ffa56787898ab5583b24e8ee7187bba3d80e8e6493dfb9ffe86e49
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":"24.04","id":"noble","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.0

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