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 addEventListener():
addEventListener("contextoverflow", (event) => {})
Alternatively, assign to the event handler property of the interface:
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.
const session = await LanguageModel.create();
session.addEventListener("contextoverflow", () => {
console.warn("Context overflow detected.");
});
Alternatively, use the oncontextoverflow event handler property (but note that this replaces other event listeners added using oncontextoverflow, if any):
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.
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> |