VISIT THE LIVE APP HERE! https://agents-starter.nkhatrichatri.workers.dev/
Documenting everything I've done here and how it works.
When I ran the starter template code, 3 of the 4 conditions were pre-filled.
npm create cloudflare@latest agents-starter -- --template=cloudflare/agents-starter
npx wrangler@latest deploy- Workflow / coordination (recommend using Workflows, Workers or Durable Objects)
- User input via chat or voice (recommend using Pages or Realtime)
- Memory or state
The only thing we needed to work on was the LLM integration (LLaMa 3.3 via Workers AI).
So here's what I did:
// Replaced this:
import { openai } from "@ai-sdk/openai";
// With this:
import { createWorkersAI } from "workers-ai-provider";async onChatMessage(onFinish, _options) {
// Create Workers AI provider using the AI binding
const workersai = createWorkersAI({ binding: this.env.AI });
const model = workersai("@cf/meta/llama-3.3-70b-instruct-fp8-fast" as any);
// Rest of the code stays the same - just using a different model!
}Removed old OpenAI API key checks and updated to reflect Workers AI:
if (url.pathname === "/check-open-ai-key") {
return Response.json({
success: true,
provider: "Workers AI",
model: "llama-3.3-70b-instruct-fp8-fast"
});
}Changed error messages from "OpenAI API Key Not Configured" to "AI Service Not Available" to reflect the new Workers AI setup.
const hasAIReadyPromise = fetch("/check-open-ai-key").then((res) =>
res.json<{ success: boolean; provider?: string; model?: string }>()
);The AI binding was already configured in wrangler.jsonc - no changes needed:
Durable Objects configuration for state management:
"durable_objects": {
"bindings": [
{
"name": "Chat",
"class_name": "Chat"
}
]
}User Browser (React UI)
↓
Cloudflare Worker (src/server.ts)
↓
Durable Object (Chat Agent)
↓
Workers AI (Llama 3.3)
The basic Workers AI pattern is:
env.AI.run(modelName, { prompt: "..." });But we wrap it with createWorkersAI() to get the advanced features:
const workersai = createWorkersAI({ binding: env.AI });
const model = workersai(modelName);
// Now works with streamText(), tools, etc.- Clone the repository
- Install dependencies:
npm install - Run development server:
npm run dev - Access the application at
http://localhost:5173 - Deploy to production:
npm run deploy
OR VISIT https://agents-starter.nkhatrichatri.workers.dev/
I DEPLOYED IT :D
- Cloudflare Agents API: https://developers.cloudflare.com/agents/api-reference/agents-api/
- Llama 3.3 Model Docs: https://developers.cloudflare.com/workers-ai/models/llama-3.3-70b-instruct-fp8-fast/
- Agents Starter Template: https://github.com/cloudflare/agents-starter
- Workers AI Provider: https://github.com/cloudflare/workers-ai-provider
- Workers AI Docs: https://developers.cloudflare.com/workers-ai/
Built for Cloudflare internship application - demonstrating Workers AI, Durable Objects, and modern AI application architecture.