WebAI Studio

The Prompt API gives web pages the ability to directly prompt an instruction-tuned language model natively provided by the browser. It supports zero-shot and n-shot prompting, multimodal inputs, streaming responses, JSON schema constraints, and intelligent tool calling.

Sampling Modes: Raw hyperparameters (temperature / topK / LanguageModel.params()) are deprecated for web pages. Use the semantic samplingMode option (e.g. "most-predictable" for strict reproducibility and speculative decoding, or "balanced" by default) when creating a session. Read Explainer

LanguageModel.availability()

A static method that checks if the browser currently supports creating a language model session with the provided configuration. It returns a Promise resolving to an Availability string: 'available' | 'downloadable' | 'downloading' | 'unavailable'.

static availability(options?: LanguageModelCreateCoreOptions): Promise<Availability>;

Parameters (LanguageModelCreateCoreOptions)

NameTypeDescription
expectedInputssequence<LanguageModelExpected>Signals expected inputs to pre-download needed adapters (e.g. { type: "audio" } or { type: "text", languages: ["es"] }).
expectedOutputssequence<LanguageModelExpected>Signals expected output formats/languages. Currently only supports text.
toolssequence<LanguageModelTool>Array of tool definitions with name, description, inputSchema, and execute functions.
samplingModeLanguageModelSamplingModeHigh-level control over output predictability vs. creativity: "most-predictable" | "predictable" | "slightly-predictable" | "balanced" | "slightly-creative" | "creative" | "most-creative". Defaults to "balanced". Replaces the deprecated temperature and topK parameters.
Interactive Playground

LanguageModel.create()

Creates and returns a new language model session. If the model is not yet available but is "downloadable", calling this method initiates the network download. If download fails, it throws a NetworkError.

static create(options?: LanguageModelCreateOptions): Promise<LanguageModel>;

Parameters (LanguageModelCreateOptions)

Inherits all properties from LanguageModelCreateCoreOptions (above), plus:

NameTypeDescription
initialPromptssequence<LanguageModelMessage>Establishes system context or n-shot examples. The "system" role must be at the 0th index.
monitorCreateMonitorCallbackA function (m) => void where m emits downloadprogress events to track model weight download (e.loaded 0 to 1).
signalAbortSignalPass an AbortController.signal to cancel session creation and halt the model download.
Interactive Playground

Sampling Modes (LanguageModelSamplingMode)

Rather than exposing raw model-dependent parameters like topK and temperature, the API provides seven semantic presets and exposes the active mode on session.samplingMode:

ModeUse Case
"most-predictable"Strict consistency, reproducibility, code generation, content extraction, and speculative decoding.
"predictable"Focused outputs with minimal variation.
"slightly-predictable"Focused outputs with a bit more variation than "predictable".
"balanced"Default. Standard preset suitable for most conversational interactions.
"slightly-creative"Creative outputs slightly more focused than "creative".
"creative"Variety and creativity preferred over strict reproducibility.
"most-creative"Maximum diversity of output and creative brainstorming.

session.prompt()

Executes inference and returns a single Promise resolving to the complete generated text. If the input exceeds the available context window, it evicts older conversation history one prompt/response pair at a time (the initialPrompts are never evicted), and throws a QuotaExceededError only when eviction cannot free enough space.

prompt(input: LanguageModelPrompt, options?: LanguageModelPromptOptions): Promise<DOMString>;

Input Parameter (LanguageModelPrompt)

The input can be a simple DOMString (zero-shot shorthand), or an array of LanguageModelMessage objects for complex/multimodal prompts:

[
  {
    role: "user" | "assistant",
    content: "String content" | [
      { type: "text", value: "Hello" },
      { type: "image", value: ImageBitmapSource | BufferSource },
      { type: "audio", value: Blob | AudioBuffer | BufferSource }
    ],
    prefix: boolean // (If true, pre-fills the assistant response. Valid only on final assistant message)
  }
]

Options Parameter

NameTypeDescription
responseConstraintobject | RegExpA JSON Schema object or RegExp to strictly constrain the model's output formatting.
omitResponseConstraintInputbooleanIf true, stops the browser from automatically injecting the schema definition into the prompt context, saving tokens.
signalAbortSignalPass an AbortController.signal to cancel this specific generation request.
Interactive Playground

session.promptStreaming()

Identical parameters to prompt(), but returns a ReadableStream of string chunks as they are generated by the model.

promptStreaming(input: LanguageModelPrompt, options?: LanguageModelPromptOptions): ReadableStream;

Typically consumed with an async iterator (e.g., for await (const chunk of stream)), though getReader() works too. Highly recommended for improving perceived latency (TTFT).

Interactive Playground

session.append()

Ingests and processes tokens into the context window ahead of time without triggering a response from the model. Extremely useful for "pre-warming" the session with large files or multimodal images before the user hits "Generate".

append(input: LanguageModelPrompt, options?: LanguageModelAppendOptions): Promise<undefined>;
Interactive Playground

session.measureContextUsage()

Measures how many tokens a given prompt will consume (implementations must include control tokens in the count). Returns a Promise. Note: This does not add the prompt to the session history.

measureContextUsage(input: LanguageModelPrompt, options?: LanguageModelPromptOptions): Promise<double>;

Related Context Attributes

  • readonly contextUsage: double - The current number of tokens stored in the session history.
  • readonly contextWindow: double - The absolute maximum token limit for this model instance.
  • oncontextoverflow - Event fired when a prompt causes the session history to overflow and evict older messages.
Interactive Playground

session.clone()

Creates a new session starting from this session's initialPrompts and configuration. Whether conversation history appended after creation is carried over is implementation-defined — the explainer's canonical use is running multiple unrelated continuations of the same initial setup without re-processing it.

clone(options?: LanguageModelCloneOptions): Promise<LanguageModel>;

Options takes an optional AbortSignal.

Interactive Playground

session.destroy()

Immediately unloads the session from memory. Any pending prompt() or promptStreaming() calls on this session are rejected with an AbortError. If no other APIs are using the language model, Chrome can free the VRAM/RAM payload.

destroy(): undefined;
Interactive Playground

Deprecations & Aliases

To address inconsistencies across models, several parameters have been heavily restricted. The following features are deprecated in web pages and are now only functional within Chrome Extension contexts.

  • Hyperparameters (topK & temperature)temperature and topK inside create() options, as well as accessing session.temperature and session.topK, are deprecated and ignored in standard web page contexts. Use the samplingMode option instead. If both samplingMode and raw parameters are provided, samplingMode takes precedence and the raw parameters are ignored.
  • LanguageModel.params() The static params() method used to query default topK/temperature is deprecated for web pages.
  • Renamed Aliases
    • inputUsage is now contextUsage
    • inputQuota is now contextWindow
    • measureInputUsage() is now measureContextUsage()
    • onquotaoverflow is now oncontextoverflow