Cloudflare Agents SDK
Simplifying Durable Objects for AI Applications
Cloudflare recently released their Agents SDK that simplifies building AI-powered applications on their Workers platform (particularly Durable Objects).
While this library is still in its early stages, it already brings a lot of value by providing convenient access to the power of Cloudflare's platform. Check out Sunil's post for why Cloudflare Workers are a great choice for AI agents.
Getting Started
First you can read about the framework:
Then you can check out the starter:
npm create cloudflare@latest agents-starter -- --template=cloudflare/agents-starter
What's in the Starter
The Cloudflare Vite Plugin
This is worth highlighting on its own. Cloudflare have recently released a Vite plugin that makes it incredibly easy to deploy your Vite app to Cloudflare Workers using their static assets support. Note, this is effectively infinite free hosting for static sites!
For the Agents SDK, this means your agent can have its own front-end app which is deployed with the Agent. The starter template includes a simple React chat app (index.html
, app.tsx
, client.tsx
) which uses the useChatAgent
react hook to interact with the chat agent.
A Chat Agent Durable Object
The server side of the starter provides a simple chat agent using the ChatAgent
class from the Agents SDK. This agent is responsible for handling a single chat conversation, and uses the AI SDK to generate responses to user messages.
What's in the SDK
The SDK is still young and many features have been added during the drafting of this post, but the core concept is that an Agent
is a Durable Object. The SDK provides some nice abstractions for handling WebSocket connections and state management.
The SDK provides an AgentClient
class for connecting to the agent from the client side using a WebSocket connection, and there's additional support for React with the useAgent
hook.
Additionally there is a ChatAgent
class which is a specialized Agent
for building conversational interfaces. This class handles streaming messages to and from the client, and provides a corresponding useChatAgent
hook for the client side.
My First Agent SDK Application
It started out as a simple chat with files app, but it quickly vibe-coded out of control. The main components of the app are:
A FileAgent
derived from Agent
that manages the processing lifecycle of a file. This agent also uses Cloudflare Queues to offload the processing to Workers (e.g. token counting), R2 for file storage, D1 for shared state and Browser Rendering for processing HTML. Having all these services in one place is part of what makes the Cloudflare stack so appealing.
The FileAgent
uses setState
to update the state of the file processing, and the client uses the useAgent
hook to connect to the agent and display the state of the file processing. This is super convenient and makes it easy to build a reactive UI.
I then modified the ChatAgent
very slightly to read the files into context at the start of each thread, with the client-side logic basically unchanged from the starter.
Conclusion
It's early days for the Agents SDK, but thanks to the power of Durable Objects, and the Cloudflare stack, it's already worth checking out. If you are already using Cloudflare Workers, then it's an improved developer experience with no cost, if you are not, then it's a great way to start.