Building AI agents on Cloudflare

Curious about building smarter apps? This article shows you how to create AI agents on Cloudflare using Workers, Workers AI, and the Agents SDK. You’ll see how easy it is to set up a chatbot that runs custom tools, manages state, and interacts in real time—all on Cloudflare’s fast, global network.

IN THIS ARTICLE:

SPREAD THE WORD:

This blog post explores building AI agents on Cloudflare. We will delve into leveraging Cloudflare’s platform, including Workers, Workers AI, and the AI Agent SDK, to easily create AI applications. We will also cover the resources available to get started, such as starter packs, server/tools, and React SDKs. We will build a chatbot agent that can run custom tools for specific prompts.

What is Cloudflare?

Cloudflare is a leading global cloud services provider that delivers a broad range of web infrastructure and website security services. 

Cloudflare offerings

  • AI Gateway: Observe and control your AI applications with caching, rate limiting, request retries, model fallback, and more.
  • Vectorize: Build full-stack AI applications with Vectorize, Cloudflare’s vector database. Adding Vectorize enables you to perform tasks such as semantic search, recommendations, anomaly detection or can be used to provide context and memory to an LLM.
  • Workers: Build serverless applications and deploy instantly across the globe for exceptional performance, reliability, and scale.
  • Workers AI: Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network.
  • Pages: Create full-stack applications that are instantly deployed to the Cloudflare global network.
  • R2: Store large amounts of unstructured data without the costly egress bandwidth fees associated with typical cloud storage services.
  • D1: Create new serverless SQL databases to query from your Workers and Pages projects.
  • Durable Objects: A globally distributed coordination API with strongly consistent storage.
  • KV: Create a global, low-latency, key-value data storage.

What are Cloudflare Workers?

Cloudflare Workers is a serverless computing platform that allows developers to build and deploy applications at the edge of the network, closer to users. This technology represents a shift from traditional server-based architectures to a more distributed computing model.

Cloudflare Workers run on Cloudflare’s global network spanning 275+ cities worldwide. Instead of executing code in a centralized data center, Workers execute at the “edge” – the point closest to your users. This dramatically reduces latency and improves performance.

Comparison to Other Serverless Platforms

Compared to AWS Lambda, Azure Functions, or Google Cloud Functions:

  • Faster cold starts: Milliseconds vs. seconds
  • Global by default: No need to deploy to multiple regions
  • ● Simpler pricing: Based on requests rather than execution time
  • Edge-first: Designed for the edge rather than adapted for it
  • More limited runtime: Less flexibility but better performance

Cloudflare Workers represent a powerful shift in how web applications are built and deployed, bringing computation closer to users and enabling new patterns for performance, security, and scalability.

What are Cloudflare Workers AI?

Workers AI allows you to run AI models in a serverless way, without having to worry about scaling, maintaining, or paying for unused infrastructure. You can invoke models running on GPUs on Cloudflare’s network from your own code — from Workers, Pages, or anywhere via the Cloudflare API.

Workers AI gives you access to:

  • ● 50+ open-source models
  • ● Serverless, pay-for-what-you-use pricing model

What are Agents?

Agents are Autonomous AI systems that dynamically select tools and adapt processes based on real-time context. Characterized by:

  • ● Non-linear decision making
  • ● Non-deterministic outcomes
  • ● Full task automation
  • ● Adaptive tool selection

The difference between agents and co-pilots is that agents do not require human intervention.

Cloudflare agents-sdk

Cloudflare Agents SDK, is a javascript/typescript SDK, which enables you to build and deploy AI-powered agents that can autonomously perform tasks, communicate with clients in real time, call AI models, persist state, schedule tasks, run asynchronous workflows, browse the web, query data from your database, support human-in-the-loop interactions and more.

Benefits of building AI agents on Cloudflare;

  • ● State management included – ability to automatically sync state between an Agent and clients, trigger events on state changes, and read+write to each Agent’s SQL database.
  • ● Communicative – agents can be connected via websockets and they can stream updates back to the client in real time
  • ● Extensible – use any AI model that you want, read data from databases hosted in another cloud, add your own methods to the agent and call them

Cloudflare agents starter template 

For the rest of this blog we will be focusing on the cloudflare agents starter template . This project provides a foundation for creating interactive chat experiences with AI and cloudflare agents sdk, it uses react and the agents-sdk react plugin for the UI and features tool integrations for agent tool usage.

To run it locally the only thing we need is an api key for openai/anthropic or cloudlflare in order to access a LLM.

Lets dive into the code for the starter template. Inside src/server.ts we can see the creation of the agent.

This is extending the AIChatAgent class that is provided by the agents sdk.

We can see that we are providing the tools object to the onChatMessage method. This object represents our user defined tools which are defined in src/tools.ts. We also have a system prompt which is added to all the messages, this prompt instructs the agent to use the appropriate schedule tool.

We are also using AsyncLocalStorage to maintain agent context during execution, think of this as request-scoped agent state. This allows us to maintain agent state across async operation and enables tools to access parent agent’s methods/schedule.

For this project we have 3 tools defined inside src/tools.ts.

1. getWeatherInformation

  • ● Requires human confirmation (its missing the execute method, its execution is specified in a separate object at the end of the file)
  • ● Parameters: { city: string }
  • ● Sample execution: Returns static weather string
  • ● Use case: Demonstrates human-in-the-loop pattern

2. getLocalTime

  • ● Auto-executes
  • ● Parameters: { location: string }
  • ● Implementation: Simple console log + hardcoded response
  • ● Use case: Basic automated tool demonstration

3. scheduleTask

  • ● Complex automated tool
  • ● Parameters: Schedule type (date/delay/cron) + description
  • ● Key feature: Integrates with agent context to persist schedules (reads from the AsyncLocalStorage defined in src/server.ts)
  • ● Use case: Shows stateful operations in Cloudflare Workers

Moving on to the frontend part ( src/app.tsx ), the interesting part is below: 

We are using the useAgent and useAgentChat hooks provided by the agents sdk. Using these hooks allows real time communication with the backend agent without any boilerplate, our messages list gets updated automatically each time the agent responds. 

These messages also indicate to us if a tool invocation is happening and if that tool invocation requires user confirmation, if that is the case we will display the appropriate confirm box as in the below screenshot:

Lets breakdown what is happening in the above screenshots: 

  • ● we asked the agent about the weather
  • ● the agent recognized that he has a tool for this but that tool requires a parameter ( location )
  • ● the agent responded and asked us for the location
  • ● after we provided the agent with the location he asked for confirmation for the tool usage
  • ● after confirming the agent responded with the hardcoded message from the getWeatherInformation tool 

In the above screenshot we can see the usage of the scheduleTask tool, we asked the agent to provide us some information every 2 minutes. We can see inside src/tools.ts, scheduleTask that first we are getting the appropriate agent for which to schedule the task and the based on the input we are deciding which type of scheduling to apply ( scheduled, delayed, cron ) and then we use the agent.schedule method to start the task.

Final words

I’m sure it hasn’t taken you long to realize the endless amount of possibilities provided by Cloudflare’s AI API when it come to building AI agents and how simple it can be, but in case you need some more inspiration I’ll provide some agent examples below 

1. Customer Support Agent

  • ● Auto-tools: Knowledge base lookup, ticket categorization
  • ● Human-approval: Escalate complex cases, process refunds
  • ● Cloudflare edge benefit: Global low-latency responses

2. Personal Productivity Assistant

  • ● Auto-tools: Calendar management, email prioritization
  • ● Human-approval: Schedule meeting reschedules, contact sharing
  • ● Uses scheduleTask: Reminders, deadline tracking

3. DevOps Automation Agent

  • ● Auto-tools: Log analysis, staging deployments
  • ● Human-approval: Production deployments, credential rotations
  • ● Worker advantage: Execute near infrastructure

4. E-commerce Shopping Agent

  • ● Auto-tools: Product recommendations, price tracking
  • ● Human-approval: Finalize purchases, apply discounts
  • ● Context usage: Maintain cart state across sessions

5. Research Analysis Agent

  • ● Auto-tools: Paper summarization, data aggregation
  • ● Human-approval: Share draft reports, external API calls
  • ● Security: Inherits Worker isolation model

Happy building!

Resources:

https://blog.cloudflare.com/build-ai-agents-on-cloudflare
https://developers.cloudflare.com/agents
https://github.com/cloudflare/agents-starter

Be the first one to learn about new Victory openings.

Subscribe to our newsletter and keep up with the new career opportunities in our evergreen engineering square. We promise to send only relevant information, no fluff included.

Interested in more?
Here are some of our moments.

Shopping Basket