What you'll learn: By the end of this lesson you will be able to describe the three routes to client-side inference (a browser-provided model, a library runtime in your page, and the low-level GPU and neural-network APIs), say what each costs in download, storage and hardware, design the first-use flow around availability, user activation and progress, keep inference off the main thread, and build the feature as an enhancement with a server fallback.
Think of a pocket dictionary against phoning a librarian. The dictionary works on a plane and nobody hears your question, but you carry its weight, it covers fewer words, and not everyone owns one. The librarian knows more, but every question crosses a phone line and costs the library time.
A model that runs on the visitor's device is the pocket dictionary. The running example is a review-writing helper on a product page that proofreads or rewrites the shopper's draft before they post it. The illustrations are a minimal example (detect, then use or fall back), a realistic example (the first-use download) and a failure example (the model disappears).
The core idea
- On-device inference moves cost and data; it does not remove them. The server bill and the text leaving the device shrink. Download, storage, battery and hardware variance appear, and the local model may be less capable than a server's.
- There are three routes. The browser can provide and manage the model; your page can bring its own runtime and weights; or you can program the hardware APIs directly.
- Treat it as an enhancement. You cannot assume the device, browser or model, so the feature needs a capability check, a first-use flow and a fallback. Local output is still untrusted text: see Prompt Injection and Output Safety in the UI.
Three routes
| Route | Who supplies the model | What you write | Notes from the docs |
|---|---|---|---|
| Built-in browser AI | The browser downloads and manages it | Calls to task APIs: Prompt, Summarizer, Rewriter, Proofreader and others | Chrome's pages name Gemini Nano and mention only Chrome; the Prompt API for the web is still being developed |
| Library runtime | You: your page fetches model files | A pipeline call | Transformers.js uses ONNX Runtime: CPU via WebAssembly by default, GPU via WebGPU if you set device: 'webgpu' |
| Low-level APIs | You | Your own graph and shaders | WebGPU offers GPU compute; W3C WebNN is a low-level inference API and a Candidate Recommendation Draft |
Chrome's overview says the browser "provides and manages foundation and expert models" and cites low latency, reduced server-side costs and increased user privacy. Transformers.js warns that WebGPU is still experimental in many browsers, and MDN, at the time of writing, labels WebGPU as not Baseline. This lesson gives no support figures because they change: read the current compatibility tables.
Figure 1. A capability check in front of two paths
How to read this: the check decides each time. The server path is what every device without the local model gets.
Illustration 1: detect, then use or fall back
Chrome's Prompt API page says to call availability() first, with the same options you will pass to prompt() (some models do not support every modality or language), and to proceed unless it reports unavailable. A sketch, not run because this course has no browser (helper names are ours):
async function pickEngine(): Promise<"local" | "server"> {
if (!("LanguageModel" in globalThis)) return "server";
const state = await LanguageModel.availability();
return state === "unavailable" ? "server" : "local";
}
For a library runtime the check differs: WebGPU needs a secure context, navigator.gpu may be missing, and requestAdapter() can return nothing, so keep the WebAssembly path for those devices.
Figure 2. The first-use decision
not unavailable?"} A -->|"no"| SV["Server path"] A -->|"yes"| D{"Model on the device?"} D -->|"yes"| L["Run locally"] D -->|"no"| P["Download from this tap,
server meanwhile"] P --> L L -->|"error or model removed"| SV
How to read this: every arrow into the local path has a way out to the server, so a missing model degrades the feature instead of breaking it.
Illustration 2: the first-use download
The browser decides when the model arrives. Chrome's model management page says it is downloaded on demand by the first create() call of an API that needs it, after a hardware check picks a variant or falls back to CPU inference if the device qualifies. The Prompt API page says to check user activation before create() starts the download, to listen for progress when the state is downloading, and that the network requirement applies only to the initial download.
Figure 3. First use, from the tap to the first result
How to read this: the tap supplies the user activation, and the form never waits on the download.
The stated requirements are vendor figures for one browser at one time. At the time of writing the Prompt API page lists at least 22 GB of free space on the volume with the Chrome profile, strictly more than 4 GB of VRAM (or, for the CPU path, 16 GB of RAM and four cores) and an unmetered network, and says Android and iOS are not yet supported. Plan for the fallback to be common.
With a library runtime you own storage. Browsers keep data per origin under quotas, best-effort by default and evictable under pressure, least recently used first, and navigator.storage.estimate() is an estimate (see IndexedDB and Storage Quotas). A cached model can vanish, so loading must be re-runnable.
Illustration 3: the failure, the model disappears
Chrome's pages say the model is removed if free space falls below a threshold, that this can happen mid-session, and that it is not re-downloaded automatically. A feature that checked availability once at page load will throw in the middle of a draft.
Figure 4. A built-in model's life on one device
How to read this: "available" is a state, not a permanent fact. Check at the point of use and treat every local call as able to fail.
By default the Prompt API works in top-level windows and same-origin iframes and is not available in Web Workers. For a library runtime, run inference in a worker so input stays responsive, as in Web Workers, Worklets and Off-Main-Thread Architecture; MDN says WebGPU is available in workers.
Privacy, honestly. Local inference can keep the draft on the device while the model runs, and Chrome lists increased user privacy as a benefit. It is not private end to end: the download is a network request, the page may send the result on, and the server fallback sends the text. Say which path sent what.
Trade-offs
| Choice | Gain | Cost |
|---|---|---|
| Built-in browser model | No model files to host | Only where offered; hardware needs; you do not pick the model |
| Library runtime | You choose model and backend | You host, version and cache weights; quotas apply |
| Local first, server fallback | Works everywhere | Two paths to test; answers may differ |
| Server only | One path, a model you control | Per-request cost; text leaves the device |
Build it
This lesson has no lab. Inference needs a real browser, a GPU or a downloaded model, none of which can run in this course's Node-based lab workspace, so no output is shown.
In practice: the review helper on a product page
This is a realistic scenario, not a case history. It is a composite built from common production patterns. It describes no specific company or product, and every number in it is an illustrative assumption.
The situation
A marketplace wants an "Improve my draft" button on its review form without paying for a server call on every draft. Visitors use many browsers and devices.
Starting architecture
The form calls a server endpoint that proofreads the draft. A prototype adds the built-in browser model, checked once at page load.
Options considered
| Option | What it fixes | What it costs | Risk |
|---|---|---|---|
| Local only | No server calls | Excludes devices without the model | A broken feature for them |
| Local first, server fallback | Cost drops where the model exists | Two paths | Answers differ |
The decision
| Field | Decision record |
|---|---|
| Context | Many device types, a new feature, a cost to control. |
| Decision | Check at the tap, start any download from that tap with progress shown, run locally when ready, fall back to the server on any error, and show which path ran. |
| Consequences, good | Less server use on capable devices; works everywhere. |
| Consequences, bad | Two paths to test; a download to explain. |
| Revisit when | The browser's API status or support changes. |
Rollout and verification
- Ship behind a flag and count local runs, fallbacks and failures separately.
- Test the fallback first, with the local path forced off.
- Test removal: make the local call throw mid-draft and assert the draft survives.
The 2 a.m. failure
Support reports that on some laptops the button spins and does nothing. The model had been removed after free space dropped, and the code checked availability only at load.
First three checks: (1) failure counts by path; (2) whether affected devices are low on disk; (3) reproduce by forcing the local call to fail. Mitigation: turn the local path off with the flag. Follow-up: check at the point of use and catch every local failure into the server path.
Architect's notes
- Start from the fallback, then add the local path as an optimisation.
- Check at the point of use, never once at load.
- Keep inference off the main thread wherever the API allows it.
Common mistakes
- Treating the local model as always present.
- Starting a large download without a user action or progress.
- Claiming "private" without naming what the download and fallback send.
- Running library inference on the main thread.
Key takeaways
- On-device inference trades server cost and data egress for download, storage, hardware and capability limits.
- Three routes: browser-provided, library runtime, low-level APIs.
- Design first use around availability, user activation and progress.
- The model can disappear mid-session: check at use and fall back.
Practice exercises
Exercise 1 — Starter (45 min): Write the state table for the first-use flow (available, downloadable, downloading, unavailable, failed) and what the form shows in each.
Exercise 2 — Intermediate (2 hours): Sketch pickEngine with an injectable capability object and write tests that force each state, without a browser.
Related reading
In this course:
- Web Workers, Worklets and Off-Main-Thread Architecture, where to run inference so input stays responsive.
- IndexedDB and Storage Quotas, limits and eviction for cached model files.
- Prompt Injection and Output Safety in the UI, rendering model output safely wherever it comes from.
Official documentation
- Chrome: Built-in AI, the APIs, the browser-managed model and the stated benefits.
- Chrome: Prompt API, requirements, availability, user activation and limits.
- Chrome: How built-in AI models are managed, download, updates and removal.
- MDN: WebGPU API, GPU compute, secure context, workers and support status.
- Hugging Face: Transformers.js, running models in the browser through ONNX Runtime.
- MDN: Storage quotas and eviction criteria, quotas, eviction and
estimate().