v148 · web api · experimental
Prompt API
A browser-provided interface to an on-device large language model (Gemini Nano in Chrome). Pages call LanguageModel.create() to get a session, then send text, images, or audio via session.prompt() — no API key, no network round-trip, inference runs locally.
LanguageModel.availability() before calling create(). This page is generated from the webmachinelearning/prompt-api explainer.
at a glance
| Shipped in | Chrome 148 (desktop) |
|---|---|
| Status | Origin trial / Enabled by default (requires Gemini Nano download) |
| Flag | chrome://flags/#optimization-guide-on-device-model (must be set to Enabled) |
| Standards position | Proposed at W3C Web Machine Learning CG |
| Spec / Explainer | webmachinelearning/prompt-api |
| ChromeStatus | 5134603979063296 — Prompt API |
why it exists
Cloud AI APIs require a network connection, a billing account, and introduce latency and privacy concerns for sensitive content. Devices running Chrome already download a small on-device LLM (Gemini Nano) for browser-level features. The Prompt API exposes that same model to web pages through a sandboxed JS interface, letting developers add language model capabilities without external dependencies, API keys, or sending user data off-device.
Source: webmachinelearning/prompt-api README §Background, May 2026.shape of the API
All interactions flow through a session object. The static LanguageModel namespace creates and introspects sessions; each session maintains a rolling conversation context and token budget.
LanguageModel static methods
| Method | Returns | Description |
|---|---|---|
LanguageModel.availability(opts?) |
Promise<"unavailable" | "downloadable" | "downloading" | "available"> |
Check whether the model is ready before attempting creation. "available" = ready to use; "downloadable" = supported but not yet downloaded. |
LanguageModel.create(opts?) |
Promise<LanguageModelSession> |
Create a session. Options: initialPrompts, expectedInputs, expectedOutputs, tools, temperature, topK, signal, monitor. |
LanguageModel.params() |
Promise<{defaultTemperature, maxTemperature, defaultTopK, maxTopK}> |
Retrieve the model's default and maximum sampling parameter values. |
LanguageModelSession instance members
| Member | Type | Description |
|---|---|---|
prompt(input, opts?) | Promise<string> | Send a message, get a complete response. input can be a string or array of multimodal parts. |
promptStreaming(input, opts?) | ReadableStream<string> | Stream the response token by token as an async iterable. |
append(messages, opts?) | Promise<void> | Add messages to the context without generating a reply — useful for seeding few-shot examples. |
measureContextUsage(input, opts?) | Promise<number> | Count the tokens a hypothetical prompt would consume, without actually prompting. |
clone(opts?) | Promise<LanguageModelSession> | Create a copy of the current session (including conversation history). |
destroy() | void | Release the session's resources. |
contextUsage | number | Tokens consumed so far by this session. |
contextWindow | number | Total token budget for the session. |
recipes
Check availability and create a session
const avail = await LanguageModel.availability();
if (avail === 'unavailable') {
console.warn('Prompt API not supported on this device');
} else {
// 'downloadable' → browser will fetch the model on create()
const session = await LanguageModel.create({
initialPrompts: [
{ role: 'system', content: 'You are a concise technical writer.' }
]
});
const answer = await session.prompt('Explain CSS cascade in one sentence.');
console.log(answer);
session.destroy();
}
Streaming response
const session = await LanguageModel.create();
const stream = session.promptStreaming('Write a haiku about the browser cache.');
let text = '';
for await (const chunk of stream) {
text += chunk;
outputEl.textContent = text;
}
session.destroy();
Structured output (JSON schema constraint)
const session = await LanguageModel.create();
const schema = {
type: 'object',
properties: {
sentiment: { type: 'string', enum: ['positive', 'neutral', 'negative'] },
score: { type: 'number', minimum: 0, maximum: 1 }
},
required: ['sentiment', 'score']
};
const raw = await session.prompt(
'Analyse: "The docs were clear and the API is elegant."',
{ responseConstraint: schema }
);
const { sentiment, score } = JSON.parse(raw);
session.destroy();
Multimodal input (image)
const session = await LanguageModel.create({
expectedInputs: [{ type: 'image' }]
});
const blob = await fetch('/screenshot.png').then(r => r.blob());
const result = await session.prompt([
{ type: 'text', value: 'Describe what is wrong in this UI screenshot.' },
{ type: 'image', value: blob }
]);
session.destroy();
Source: webmachinelearning/prompt-api README examples, May 2026.
browser support
| Browser | Support |
|---|---|
| Chrome 148+ (desktop) | Enabled by default where Gemini Nano is installed |
| Edge | Separate Phi-based implementation under consideration |
| Firefox | No position |
| Safari | No position |