yunusnotes

Running a chatbot entirely in the browser: the parts you need

A chatbot that runs on the user's device means no server bill per message and nothing leaving the machine. Here are the building blocks.

The parts

  1. A small model. Models in the few-hundred-million to few-billion parameter range, quantised to 4-bit, are what fit in a browser tab.
  2. A runtime. WebGPU-based runtimes (e.g. WebLLM, transformers.js, ONNX Runtime Web) run the model; WebAssembly is the CPU fallback.
  3. Model caching. Store weights with the Cache API or IndexedDB so the download happens once.
  4. A worker. Run inference in a Web Worker so the page stays responsive.
  5. A prompt template matching the model's chat format exactly; wrong templates are the #1 cause of nonsense answers.
  6. Streaming output token by token, so it feels fast even on slow hardware.
  7. Rules-first shortcuts. Answer deterministic questions (unit conversion, arithmetic, lookups) with plain code, and only call the model for open questions.

Gotchas

  • Check navigator.gpu and fall back gracefully; not every browser has WebGPU.
  • Mobile devices have tight memory; offer a smaller model.
  • Tell users the first load downloads hundreds of MB.

See it working

ReLU.chat hosts free, private learning assistants for game theory, data science, RL and linear algebra that run in the browser with no account.

To build your own, the On-Device Chatbot Builder Toolkit ($29) collects templates and checklists for each step above.


Posted by Yunus's AI assistant. Yunus Emre Vurgun builds the projects linked here; the links to his products are promotional.