WebLLM — In-Browser LLM Inference
Overview
WebLLM is a high-performance, in-browser LLM inference engine from the MLC project. It runs open models (Llama 3, Phi 3, Gemma, Mistral, Qwen, and more) entirely on the client using WebGPU for hardware acceleration, with no server-side processing. It exposes an OpenAI-compatible API (streaming, JSON mode), making it useful for privacy-preserving, offline-capable, or zero-backend AI features. Read this when you need client-side inference in a web app.
Official sources
Install / setup
npm install @mlc-ai/web-llm
yarn and pnpm equivalents are documented:
yarn add @mlc-ai/web-llm
pnpm install @mlc-ai/web-llm
Core concepts
MLCEngine is the core interface for model loading, chat completions, and embeddings; create it with the CreateMLCEngine factory or new MLCEngine() then engine.reload(model).
- Models load asynchronously; pass an
initProgressCallback to surface download/compile progress to users (first load can be large).
- WebGPU is the acceleration backend — runs entirely in-browser with no server support required.
- OpenAI-compatible chat completion API, including streaming and structured JSON-mode output.
- Built-in model registry plus support for custom MLC-format models compiled via MLC LLM.
- Cache backends for model weights: Cache API, IndexedDB, OPFS, and Cross-Origin Storage; optional SRI integrity verification.
Best practices
- Offload generation to a Web Worker or Service Worker so heavy compute does not block the main UI thread (documented in Advanced Use Cases).
- Reuse a single loaded engine instance and persist weights in a cache backend to avoid re-downloading on every visit.
- Always wire
initProgressCallback so users see progress during the first (potentially large) model download.
- Pick the smallest model that meets quality needs; in-browser memory and download size are real constraints.
Common pitfalls
- Assuming it works everywhere → WebGPU support is required; gate the feature and provide a fallback when the browser/device lacks it.
- Loading the model on the main thread → use a Web Worker / Service Worker to keep the UI responsive.
- Treating first load as instant → weights must download and compile; cache them and show progress.
Examples
import { CreateMLCEngine } from "@mlc-ai/web-llm";
const engine = await CreateMLCEngine(
"Llama-3.1-8B-Instruct-q4f32_1-MLC",
{ initProgressCallback: (p) => console.log(p) }
);
const reply = await engine.chat.completions.create({
messages: [{ role: "user", content: "Hello!" }],
});
console.log(reply.choices[0].message.content);
Further reading
Related skills
- ../vercel-ai-sdk-elements — UI components for AI-native apps
- ../ai-tool-ui-patterns — rendering tool-call results in chat UIs
1---2name: ai-in-browser-webllm3description: Run LLMs fully in-browser/locally with WebLLM via WebGPU; consult for client-side inference, offline AI, and OpenAI-style chat without a server.4domain: reference5category: reference6tags: [webllm, llm, webgpu, in-browser, mlc, local-inference]7official_sources:8 - https://webllm.mlc.ai/docs/9 - https://github.com/mlc-ai/web-llm10verified: 2026-06-1611metadata:12 source: https://skillmd.com/skills/gabriel7silva/ai-in-browser-webllm13---1415# WebLLM — In-Browser LLM Inference1617## Overview18WebLLM is a high-performance, in-browser LLM inference engine from the MLC project. It runs open models (Llama 3, Phi 3, Gemma, Mistral, Qwen, and more) entirely on the client using WebGPU for hardware acceleration, with no server-side processing. It exposes an OpenAI-compatible API (streaming, JSON mode), making it useful for privacy-preserving, offline-capable, or zero-backend AI features. Read this when you need client-side inference in a web app.1920## Official sources21- Docs: https://webllm.mlc.ai/docs/22- Repo: https://github.com/mlc-ai/web-llm23- Getting started: https://webllm.mlc.ai/docs/user/get_started.html2425## Install / setup26```bash27npm install @mlc-ai/web-llm28```29yarn and pnpm equivalents are documented:30```bash31yarn add @mlc-ai/web-llm32pnpm install @mlc-ai/web-llm33```3435## Core concepts36- `MLCEngine` is the core interface for model loading, chat completions, and embeddings; create it with the `CreateMLCEngine` factory or `new MLCEngine()` then `engine.reload(model)`.37- Models load asynchronously; pass an `initProgressCallback` to surface download/compile progress to users (first load can be large).38- WebGPU is the acceleration backend — runs entirely in-browser with no server support required.39- OpenAI-compatible chat completion API, including streaming and structured JSON-mode output.40- Built-in model registry plus support for custom MLC-format models compiled via MLC LLM.41- Cache backends for model weights: Cache API, IndexedDB, OPFS, and Cross-Origin Storage; optional SRI integrity verification.4243## Best practices44- Offload generation to a Web Worker or Service Worker so heavy compute does not block the main UI thread (documented in Advanced Use Cases).45- Reuse a single loaded engine instance and persist weights in a cache backend to avoid re-downloading on every visit.46- Always wire `initProgressCallback` so users see progress during the first (potentially large) model download.47- Pick the smallest model that meets quality needs; in-browser memory and download size are real constraints.4849## Common pitfalls50- Assuming it works everywhere → WebGPU support is required; gate the feature and provide a fallback when the browser/device lacks it.51- Loading the model on the main thread → use a Web Worker / Service Worker to keep the UI responsive.52- Treating first load as instant → weights must download and compile; cache them and show progress.5354## Examples55```javascript56import { CreateMLCEngine } from "@mlc-ai/web-llm";5758const engine = await CreateMLCEngine(59 "Llama-3.1-8B-Instruct-q4f32_1-MLC",60 { initProgressCallback: (p) => console.log(p) }61);6263const reply = await engine.chat.completions.create({64 messages: [{ role: "user", content: "Hello!" }],65});66console.log(reply.choices[0].message.content);67```6869## Further reading70- Basic Usage: https://webllm.mlc.ai/docs/user/basic_usage.html71- API Reference: https://webllm.mlc.ai/docs/user/api_reference.html72- Advanced Use Cases (Web Worker): https://webllm.mlc.ai/docs/user/advanced_usage.html7374## Related skills75- ../vercel-ai-sdk-elements — UI components for AI-native apps76- ../ai-tool-ui-patterns — rendering tool-call results in chat UIs