LanguageModel: contextoverflow event

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The contextoverflow event fires on a LanguageModel instance when a call to prompt(), promptStreaming(), or append() causes the session's contextUsage to exceed the contextWindow.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js
addEventListener("contextoverflow", (event) => {})

oncontextoverflow = (event) => {}

Event type

A generic Event.

Examples

Reacting to a context overflow

The code below shows two methods of creating an event listener for the contextoverflow event.

js
const session = await LanguageModel.create();

session.addEventListener("contextoverflow", () => {
  console.warn("Context overflow detected.");
});

Alternatively:

js
const session = await LanguageModel.create();

session.oncontextoverflow = () => {
  console.warn(
    "The session's context window is full. " +
      "Consider cloning the session or starting a new one.",
  );
};

Resetting the session on overflow

The following example creates a new session when the contextoverflow event is triggered.

js
let session = await LanguageModel.create({
  initialPrompts: [{ role: "system", content: "You are a helpful assistant." }],
});

session.addEventListener("contextoverflow", async () => {
  console.log("Context full — creating a fresh session.");
  session.destroy();
  session = await LanguageModel.create({
    initialPrompts: [
      { role: "system", content: "You are a helpful assistant." },
    ],
  });
});

async function chat(userMessage) {
  const response = await session.prompt(userMessage);
  return response;
}

Specifications

Specification
Prompt API
# dom-languagemodel-oncontextoverflow

Browser compatibility

See also