Agent with Tools
Give a Cloudflare agent web search with WebSearchTools.
Code
The OpenAI tools / tool_calls schema is forwarded as-is by the gateway. The upstream Workers AI model must support function calling. @cf/zai-org/glm-4.7-flash does.
import asyncio
from agno.agent import Agent
from agno.models.cloudflare import Cloudflare
from agno.tools.websearch import WebSearchTools
agent = Agent(
model=Cloudflare(id="@cf/zai-org/glm-4.7-flash"),
tools=[WebSearchTools()],
markdown=True,
add_datetime_to_context=True,
)
if __name__ == "__main__":
# --- Sync ---
agent.print_response("Whats happening in France?")
# --- Sync + Streaming ---
agent.print_response("Whats happening in France?", stream=True)
# --- Async + Streaming ---
asyncio.run(agent.aprint_response("Whats happening in France?", stream=True))Usage
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateSet your environment variables
export CLOUDFLARE_API_TOKEN=xxx
export CLOUDFLARE_ACCOUNT_ID=xxx
export CLOUDFLARE_AI_GATEWAY_ID=xxx # optional, defaults to "default"Install dependencies
uv pip install -U openai ddgs agnoRun Agent
Save the code above as tool_use.py, then run:
python tool_use.py