Skip to content

Build agents on Cloudflare

Build AI-powered agents that can autonomously perform tasks, persist state, browse the web, and communicate back to users in real-time over any channel.

  • Non I/O bound pricing: don't pay for long-running processes when your code is not executing. Cloudflare Workers is designed to scale down and only charge you for CPU time, as opposed to wall-clock time.
  • Designed for durable execution: Durable Objects and Workflows are built for a programming model that enables guaranteed execution for async tasks like long-running deep thinking LLM calls, human-in-the-loop, or unreliable API calls.
  • Scalable, and reliable, without compromising on performance: by running on Cloudflare's network, agents can execute tasks close to the user without introducing latency for real-time experiences.

Start building

Build agents that can execute complex tasks, progressively save state, and call out to any third party API they need to using Workflows. Send emails or text messages, browse the web, process and summarize documents, and/or query your database.

Terminal window
npm create cloudflare@latest workflows-starter -- --template "cloudflare/workflows-starter"
cd workflows-starter
npm i resend
import { WorkflowEntrypoint, WorkflowStep, WorkflowEvent } from 'cloudflare:workers';
import { Resend } from 'resend';
type Env = {
MY_WORKFLOW: Workflow;
RESEND_API_KEY: string;
};
type Params = {
email: string;
metadata: Record<string, string>;
};
export class MyWorkflow extends WorkflowEntrypoint<Env, Params> {
async run(event: WorkflowEvent<Params>, step: WorkflowStep) {
const files = await step.do('my first step', async () => {
// Fetch a list of files from $SOME_SERVICE
return {
files: [
'doc_7392_rev3.pdf',
'report_x29_final.pdf',
'memo_2024_05_12.pdf',
'file_089_update.pdf',
'proj_alpha_v2.pdf',
'data_analysis_q2.pdf',
'notes_meeting_52.pdf',
'summary_fy24_draft.pdf',
],
79 collapsed lines
};
});
const summaries = await step.do('summarize text', async () => {
const results = {};
for (const filename of files.files) {
const fileContent = await this.env.MY_BUCKET.get(filename);
if (!fileContent) continue;
const text = await fileContent.text();
const summary = await this.env.WORKERS_AI.run('@cf/meta/llama-3.2-3b-instruct', {
messages: [{
role: 'user',
content: `Please summarize the following text concisely: ${text}`
}]
});
results[filename] = summary.response;
}
return results;
});
await step.sleep('wait on something', '1 minute');
let summaryKey = await step.do(
'store summaries in R2',
async () => {
const summaryKey = `summaries-${Date.now()}.json`;
await this.env.MY_BUCKET.put(summaryKey, JSON.stringify(summaries));
return summaryKey;
},
);
await step.do(
'email summaries',
{
retries: {
limit: 3,
delay: '5 second',
backoff: 'exponential',
}
},
async () => {
const summaryText = Object.entries(summaries)
.map(([filename, summary]) => `${filename}:\n${summary}\n\n`)
.join('');
const resend = new Resend(this.env.RESEND_API_KEY);
await resend.emails.send({
from: 'notifications@yourdomain.com',
to: event.payload.email,
subject: 'Your Document Summaries',
text: summaryText,
});
}
);
return summaryKey;
}
}
export default {
async fetch(req: Request, env: Env): Promise<Response> {
let id = new URL(req.url).searchParams.get('instanceId');
if (id) {
let instance = await env.MY_WORKFLOW.get(id);
return Response.json({
status: await instance.status(),
});
}
let instance = await env.MY_WORKFLOW.create();
return Response.json({
id: instance.id,
details: await instance.status(),
});
},
};

Use your favorite AI framework

Build agents using your favorite AI frameworks, and deploy it directly to Cloudflare Workers.

Use LangChain to build Retrieval-Augmented Generation (RAG) applications using Workers AI and Vectorize.

Give your agents more context and the ability to search across content, reply to user queries, and expand their domain knowledge.

Terminal window
npm i @langchain/cloudflare hono
import {
CloudflareVectorizeStore,
CloudflareWorkersAIEmbeddings
} from "@langchain/cloudflare";
import { VectorizeIndex } from "@cloudflare/workers-types";
import { Ai } from "@cloudflare/ai";
import { Hono } from "hono";
export interface Env {
VECTORIZE_INDEX: VectorizeIndex;
AI: Ai;
}
const app = new Hono<{ Bindings: Env }>();
app.get("/", async (c) => {
const embeddings = new CloudflareWorkersAIEmbeddings({
binding: c.env.AI,
model: "@cf/baai/bge-small-en-v1.5",
});
const store = new CloudflareVectorizeStore(embeddings, {
index: c.env.VECTORIZE_INDEX,
});
const results = await store.similaritySearch("hello", 5);
return c.json(results);
});
38 collapsed lines
app.post("/load", async (c) => {
const embeddings = new CloudflareWorkersAIEmbeddings({
binding: c.env.AI,
model: "@cf/baai/bge-small-en-v1.5",
});
const store = new CloudflareVectorizeStore(embeddings, {
index: c.env.VECTORIZE_INDEX,
});
const documents = [
{ pageContent: "hello", metadata: {} },
{ pageContent: "world", metadata: {} },
{ pageContent: "hi", metadata: {} }
];
await store.addDocuments(documents, {
ids: ["id1", "id2", "id3"]
});
return c.json({ success: true });
});
app.delete("/clear", async (c) => {
const embeddings = new CloudflareWorkersAIEmbeddings({
binding: c.env.AI,
model: "@cf/baai/bge-small-en-v1.5",
});
const store = new CloudflareVectorizeStore(embeddings, {
index: c.env.VECTORIZE_INDEX,
});
await store.delete({ ids: ["id1", "id2", "id3"] });
return c.json({ success: true });
});
export default app;

All the products you need in one platform

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 AI

Run machine learning models, powered by serverless GPUs, on Cloudflare's global network.

Calls

Build real-time serverless video, audio and data applications with WebRTC running on Cloudflare's network.

Workflows

Build stateful agents that guarantee executions, including automatic retries, persistent state that runs for minutes, hours, days, or weeks.