Last updated on

RAG with no server and no API key: running retrieval and generation in the browser


While building a tool that collects economic news, we wanted to be able to ask the news questions.

Retrieval-augmented generation usually means an embedding API, an LLM API, a server to hold the keys and a monthly bill. For a small tool, that’s a lot of infrastructure for one feature.

So we tried the opposite approach: run all of it inside the browser.

Embeddings first

Retrieval comes first. We run multilingual-e5-small in the browser with Transformers.js. The news is in Korean and English, so we needed a multilingual model, and we picked the smallest good one.

Titles and summaries are embedded into an in-memory vector index, and questions are matched by cosine similarity. There’s no server, so the index is a JavaScript Map.

The model is about 112 MB on the first visit and comes from the browser cache after that.

Generation, where model choice was really a compatibility choice

Retrieval alone only returns a list of articles, so we also needed generation. We run Qwen2.5-1.5B-Instruct in the browser with WebLLM.

The lesson here was about quantisation. The same model ships in several builds, and we chose q4f32_1. Speed wasn’t the reason; compatibility was.

The f16 builds require the GPU to support shader-f16, and requiring it cuts the number of devices that can run the model considerably. We took a slightly heavier build so it runs on more GPUs.

Three tiers, one output format

A browser LLM won’t work everywhere. Some devices have no WebGPU, and some users won’t be happy with a 1.5B model’s answers. So the feature steps down through three tiers:

  1. Local WebGPU model. The default. No key, no server.
  2. OpenAI. Used if the user adds their own key in settings.
  3. Retrieval only. If neither is available, no generation, just the relevant articles.

All three share one prompt and one answer format with numbered citations ([1], [2]). Whichever tier a device ends up on, the result looks the same to the user.

Things that bit us

  • Bundling. Importing @xenova/transformers directly doesn’t bundle under Create React App 5, and there’s no fix short of ejecting. We bypassed the bundler with import(/* webpackIgnore: true */ cdnUrl) and load the library from a CDN at runtime. With in-browser ML libraries, check bundler compatibility before anything else.
  • First load. A first-time visitor downloads over 100 MB of model weights. Without a progress indicator (progress_callback), that just looks like a frozen page.

What we took away

RAG without an API key turned out to be practical.

The main difference from the server version is how models get chosen. Compatibility matters more than raw quality, because the model has to run on whatever the user’s device has. And because it runs in the browser, it will fail on some devices, so the fallback path has to be designed from the start.