Skip to content

    Frontend System Design

    On-Device AI and Client-Side Inference

    Learn the three ways to run a model in the browser, what the download, storage and hardware really cost, and how to build it as an enhancement with a fallback.

    By Published 9 min read

    • frontend-system-design
    • on-device-ai
    • webgpu
    • progressive-enhancement
    Share

    Focused lessonadvancedFrontend System Design

    Before this: Web Workers, Worklets and Off-Main-Thread Architecture, AI-Powered Frontend Architecture

    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

    1. 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.
    2. 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.
    3. 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

    The page checks what the device offers, uses a local model when it can and a server model when it cannotReview form to Capability check (asks for help); Capability check to Local model (ready); Capability check to Server model (not available)ON THE VISITOR'S DEVICEReview form: draft textReview formdraft textCapability check: at the tapCapabilitycheckat the tapLocal model: built-in or libraryLocal modelbuilt-in orlibraryServer model: fallbackServer modelfallbackasks for helpreadynot available

    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

    flowchart TD accTitle: Choosing local or server inference at first use accDescr: If the built-in API is absent or unavailable, use the server. If the model is already on the device, run locally. If not, start the download from the shopper's tap, show progress and use the server meanwhile. Any later local failure falls back to the server. S["Shopper taps Improve my draft"] --> A{"API present and
    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

    sequenceDiagram accTitle: First use of a built-in model accDescr: The shopper taps the button. The page checks availability, then calls create inside the tap so the download can start. The browser reports progress events. The page shows progress and answers from the server meanwhile. When the model is ready the next request runs locally. participant U as Shopper participant P as Page participant B as Browser U->>P: taps Improve my draft P->>B: availability() B-->>P: downloadable P->>B: create() inside the tap B-->>P: download progress P->>U: shows progress, server answers now B-->>P: model ready P->>B: next request runs locally

    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

    stateDiagram-v2 accTitle: Lifecycle of a built-in model on a device accDescr: The model starts unavailable or downloadable. A tap starts a download. When ready it is available. Low disk space or a policy can remove it, returning it to downloadable or unavailable, even during a session. [*] --> Downloadable [*] --> Unavailable Downloadable --> Downloading: create() from a tap Downloading --> Available: ready Available --> Downloadable: removed, e.g. low disk space Available --> Unavailable: removed by policy

    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

    NOTE

    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

    1. Ship behind a flag and count local runs, fallbacks and failures separately.
    2. Test the fallback first, with the local path forced off.
    3. 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.

    In this course:

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments