The Problem

Adding an AI chat feature to a product usually means re-solving the same foundational problems every time: streaming tokens to the browser without janky reloads, keeping conversation state client-side, and getting basic observability on model calls before shipping to real users.

The Approach

This template runs entirely on Cloudflare Workers using the Workers AI platform, exposing a single /api/chat endpoint that accepts a POST request and streams the model's response back over Server-Sent Events. The frontend is plain HTML/CSS/JavaScript that renders tokens as they arrive and keeps history client-side, with the whole stack written in TypeScript.

Where AI Fits In

The underlying model is swappable, and Cloudflare's AI Gateway can be layered in for request monitoring and rate limiting without changing application code — useful for keeping AI-feature cost and abuse under control from day one instead of retrofitting it after a spike.

The Outcome

A deployable-in-minutes reference (Node.js 18+ and Wrangler CLI) for teams that need a real-time AI chat surface without starting the streaming and infrastructure work from a blank file. It's open source and directly reusable for client projects that need an AI chat feature added to an existing product.