The Edge-Native Architect: Deploying Stateful AI Agents on Cloudflare


The Edge-Native Architect: Deploying Stateful AI Agents on Cloudflare

cloudflare/agents

2026-02-23

Traditionally, we’ve built APIs that wait for a request and send a response. With Cloudflare Agents (often paired with Workflows), we're moving toward autonomous, stateful entities that can think, plan, and execute tasks over long periods.

Here’s a breakdown of why this matters and how you can get started.

From a dev perspective, the "magic" here isn't just the AI—it's the infrastructure.

State Management
Agents can maintain state across multiple turns of a conversation or steps in a process without you having to manually manage a database for every single interaction.

Long-Running Tasks
By using Workflows, your agent isn't capped by the typical 30-second timeout of a standard Worker. It can "sleep" while waiting for an API response or a human approval.

Edge Compute
Everything runs on Cloudflare's edge, meaning your AI logic is physically close to your users, reducing latency significantly.

Tool Use (Function Calling)
You can give your agents "tools" (like fetching weather data, querying a DB, or sending an email) which they can decide to use autonomously.

To use Cloudflare Agents, you'll typically work within the Wrangler CLI environment and the cloudflare/agents library.

You’ll need a Cloudflare account and the latest version of Wrangler

npm install -g wrangler

An agent usually consists of

The Brain
A Large Language Model (like Llama 3 running on Workers AI).

The Memory
A way to store past interactions.

The Tools
Specialized functions the agent can call.

Here’s a conceptual example of how you might define an agent that uses a tool to "look up" information.

import { Agent } from "@cloudflare/agents";

export default {
  async fetch(request, env) {
    // 1. Initialize the Agent
    const agent = new Agent({
      model: "@cf/meta/llama-3.1-8b-instruct",
      instructions: "You are a helpful research assistant. Use tools when you don't know something.",
    });

    // 2. Define a Tool
    agent.addTool("getWeather", {
      description: "Get the current weather for a location",
      parameters: { city: "string" },
      execute: async ({ city }) => {
        // Imagine a fetch to a weather API here
        return `The weather in ${city} is 22°C and sunny.`;
      }
    });

    // 3. Run the Agent with a user prompt
    const response = await agent.say("What is the weather in Tokyo like today?");
    
    return new Response(response.text);
  },
};

This is where it gets powerful. If your agent needs to perform a task that takes 10 minutes (like generating a complex report), you wrap it in a Cloudflare Workflow.

Trigger
A user submits a request.

Workflow Step
The Agent plans the report.

Workflow Step
The Agent calls external APIs (retrying automatically if they fail).

Completion
The Agent emails the user the result.

Observability
Use wrangler tail to watch your agent's thought process in real-time.

Cost Control
Since agents can "loop" (calling the LLM multiple times), always implement a maximum iteration cap to avoid unexpected compute bills.

Security
Never give an agent a tool that has "delete" permissions on your primary database unless you have a human-in-the-loop verification step!


cloudflare/agents




The Lightweight Framework for Collaborative AI Agents

This framework is a lightweight, powerful Python SDK (Software Development Kit) from the developers of GPT models, designed specifically for creating multi-agent workflows


Beyond Bug-Fixing: Unleashing Open-SWE for Software Development

Hello! As a software engineer, you're always looking for tools that can automate and streamline your workflow. The langchain-ai/open-swe project


Developing Collaborative Edge Applications: A Deep Dive into the cloudflare/vibesdk

The cloudflare/vibesdk is essentially a set of open-source tools and platform components that allow you to build your own "vibe-coding" application


From Noise to Knowledge: An Engineer's Guide to the 'summarize' CLI

Here is a breakdown of why this tool is a gem for engineers and how you can get it running.In our world, we are constantly bombarded with documentation


Shipping Agent-Ready Backends: Why InsForge is the Missing Piece of Your Stack

Since you're looking for a breakdown from a developer's perspective, think of this as a specialized "Backend-as-a-Service" (BaaS) specifically tuned for AI agents


AI Application Development with Genkit

Genkit isn't just another library; it's a complete framework designed to streamline the entire AI application development lifecycle


Generative AI for Engineers: How awesome-generative-ai Supercharges Your Projects

Hey there! As a fellow software engineer, I'm stoked to tell you how steven2358/awesome-generative-ai can be a real game-changer for your work


Next-Gen Data Viz: Implementing AI-Driven Infographics in Your Web Apps

Infographics have always been a bit of a "pain point" in development. Usually, you either have to hard-code complex SVG layouts (which is tedious) or rely on designers to export static images (which isn't dynamic). This framework changes the game by using AI to bridge the gap between raw data and visual storytelling


Beyond Single Models: Unleashing AI Collaboration with CrewAI

CrewAI is a powerful framework designed to orchestrate autonomous AI agents that work together to solve complex problems