A lightweight, edge-native AI chat agent built using Cloudflare Workers, Durable Objects, and Workers AI. The application serves a minimal HTML/JS chat interface and uses a stateful Durable Object to maintain conversational memory while running Llama-3.3 inference on Workers AI.
This project is submitted as part of the Cloudflare AI Optional Assignment.
Agent running on Workers: 👉 https://cf_ai_edge_chat_agent.s035187n.workers.dev
GitHub Repository: 👉 [https://github.com/SMCallan/cf_ai_edge_chat_agent]
- ⚡ Edge-native inference — powered by Workers AI (
@cf/meta/llama-3.3-8b-instruct). - 🧠 Stateful memory — conversation history stored in a Durable Object.
- 🧩 Minimal, clear codebase — single Worker + DO + tiny static frontend.
- 🌍 Globally distributed — runs close to users automatically.
- 📦 No external backend required — all logic runs inside Cloudflare's platform.
Handles routing and serves static assets.
GET /→ returnspublic/index.htmlPOST /chat→ receives{ message }, forwards to DO, returns{ reply }
Provides per-session memory and conversation management.
- Stores the last N messages in
state.storage - Builds a prompt from history
- Calls Workers AI
- Saves and returns assistant output
Current model:
@cf/meta/llama-3.3-8b-instruct
Receives:
- A configurable system prompt
- The reconstructed conversation history
- The user’s newest message
A simple HTML/JS chat UI located in:
public/index.html
cf_ai_edge_chat_agent/
│
├── public/
│ ├── index.html # Chat UI
│ ├── README.md # (legacy placeholder)
│ ├── PROMPTS.md # Build prompts (copied to root)
│
├── src/
│ └── agent.ts # Worker + Durable Object logic
│
├── wrangler.toml # Cloudflare configuration
├── README.md # You are here
├── PROMPTS.md # AI prompts used during development
├── LOGOAG.png # Project logo
└── package.json
- Node.js 18+
- Cloudflare account
- Workers AI enabled
- Durable Objects enabled
- Wrangler (via
npxor as a dev dependency)
git clone https://github.com/SMCallan/cf_ai_edge_chat_agent.git
cd cf_ai_edge_chat_agent
npm installnpx wrangler loginnpm run devThen open: 👉 http://localhost:8787
npx wrangler deployYour Worker will be deployed to:
https://<worker-name>.<your-account>.workers.dev
Modify in src/agent.ts:
const response = await env.AI.run("@cf/meta/llama-3.3-8b-instruct", {
messages: [
{
role: "system",
content: "You are a concise, friendly assistant running on Cloudflare Workers at the edge.",
},
{ role: "user", content: prompt },
],
});fetch("/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ message: "Hello!" }),
});Response:
{
"reply": "Hi! How can I help you today?"
}This project includes:
✔ LLM — Workers AI (Llama 3.3 8B Instruct)
✔ Workflow / Coordination — Durable Object controlling prompt + memory
✔ User input via chat — HTML/JS chat UI
✔ Memory / State — DO stores conversation history
✔ Repo prefix — cf_ai_…
✔ README.md — clear documentation + run instructions
✔ PROMPTS.md — transparent prompt history
✔ Live deployment — linked above
→ Fully meets assignment criteria.
These are optional but demonstrate engineering foresight:
- WebSocket streaming responses
- Realtime client sync using
useAgent() - Vectorize-powered long-term memory
- Multiple personas selectable in UI
- Cloudflare Pages frontend
- Integration with Cloudflare Workflows for async tasks
This repository may be used for Cloudflare’s optional assignment or for educational purposes. You are free to fork or reuse the structure.
Callan Smith MacDonald GitHub: https://github.com/SMCallan Cloudflare Workers / AI Engineering Enthusiast
