Skip to content
mentoor

Topic 2 of 5 · 5 min read

Running models in the browser with WebGPU

Goal: Write browser code that checks for a built-in AI API and falls back when it is missing.

A web page can run an AI model inside the visitor's browser, but it must first check that the browser and the device support it, and it must have a fallback when they do not.

Check yourself · 1 of 3

Why should page code check that a built-in AI API exists before calling it?

Choose one answer

Why it matters

Running a model in the browser means no server cost per request and the text stays on the visitor's machine. It also means you do not control the hardware. One visitor has a new laptop with a strong GPU. Another has an old phone. Your page has to work for both.

The three ways to do it

  1. Built-in browser APIs. Chrome offers built-in AI APIs, such as ones for summarising, translating and prompting, backed by a model the browser manages. The page calls the API. It does not ship the model. Availability varies by browser, version and device, so check the Chrome documentation for current status.
  2. WebLLM. A library that downloads an open-weight chat model and runs it in the page using WebGPU, the browser's modern GPU API.
  3. Transformers.js. A library from Hugging Face that runs many smaller models, such as classifiers, translators and embeddings, in the browser.

Download size and first run

With WebLLM or Transformers.js, the model file is downloaded to the browser the first time. It can be hundreds of megabytes or more for a chat model. Tell the user, show progress, and ask before starting a large download on a metered connection. Browsers cache the files, so later visits are faster.

Check, then fall back

Never assume the API exists. Test for it, test its availability, and keep a second path.

async function summarise(text) {
  if ("Summarizer" in self) {
    const state = await Summarizer.availability();
    if (state !== "unavailable") {
      const s = await Summarizer.create();
      return s.summarize(text);
    }
  }
  const res = await fetch("/api/summarise", { method: "POST", body: text });
  return (await res.json()).summary;
}

The fallback sends text to your server, so tell users when that happens (see the privacy topic).

Before and after

Before: The page calls the built-in API directly. On a browser without it, the button throws an error and does nothing.

After: The page checks for the API and its availability, uses it when ready, and otherwise calls the server or hides the feature.

What goes wrong

  • Testing only on your own fast machine.
  • Starting a big model download with no warning or progress.
  • Forgetting that WebGPU support varies, so a library that needs it fails on some browsers.

Examples

Availability states

A built-in API usually reports more than yes or no. It may say the model is available, needs a download first, or is unavailable. Handle each: use it, show a download progress bar, or take the fallback.

Choosing a route

Summarise or translate a short text: try the built-in browser API first.

Chat with a specific open model: WebLLM, with a download prompt.

Classify or embed text: Transformers.js with a small model.

Key points

  • Browsers can run models locally through built-in APIs or libraries such as WebLLM and Transformers.js.
  • Built-in APIs differ by browser, version and device, so always feature-detect before calling.
  • Library-based models download on first use, which can be a large file, so show progress and ask first.
  • WebGPU is the browser's GPU API that in-browser chat models rely on, and not every browser or device has it.
  • Every in-browser feature needs a fallback path: a server call or a hidden feature.

Common mistakes

  • Calling a built-in API without checking it exists.
  • Starting a large model download silently.
  • Assuming WebGPU works on every visitor's browser.
  • Having no fallback, so the feature breaks for part of the audience.

Check yourself · 2 of 3

A page loads a chat model with WebLLM. What should it do on the first visit?

Choose one answer

Check yourself · 3 of 3

Which library runs smaller models such as classifiers and embeddings in the browser?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic