LanguageModel: contextWindow property
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
{{APIRef("Prompt API")}}{{SecureContext_Header}}
The **contextWindow** read-only property of the {{domxref("LanguageModel")}} interface returns the total number of context window tokens available for this session. It is set when the session is created and does not change during the session's lifetime.
Compare contextWindow against {{domxref("LanguageModel.contextUsage", "contextUsage")}} to determine how many tokens remain. Use {{domxref("LanguageModel.measureContextUsage()", "measureContextUsage()")}} to estimate how many tokens a new prompt would consume before sending it.
The value is implementation-specific and varies depending on the model, device capabilities, and the session's configuration. A value of Infinity indicates that the user agent does not impose a hard limit.
Value
A number representing the session's context window capacity in tokens. This value may be Infinity if the user agent does not impose a specific limit beyond available memory or JavaScript string constraints.
Examples
Warning when the context is nearly full
The following example uses a function to verify that context is available before calling {{domxref("LanguageModel.prompt()")}}. It first calculates the remaining context and passes that value to measureContextUsage(). If needed is less than or equal to remaining, it returns true and the session continues.
const promptText = "Let me ask you an interesting question...";
const session = await LanguageModel.create();
async function contextAvailable(promptText) {
if (session.contextWindow === Infinity) {
return true;
}
const remaining = session.contextWindow - session.contextUsage;
const needed = await session.measureContextUsage(promptText);
return needed <= remaining;
}
if (await contextAvailable(promptText)) {
const response = await session.prompt(promptText);
console.log(response);
} else {
console.warn("Prompt skipped: Not enough context window remaining.");
}Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- {{domxref("LanguageModel.contextUsage")}}
- {{domxref("LanguageModel.measureContextUsage()")}}
- Prompt API
- Using the Prompt API