大网数据广告活动
Web 标准与前端开发

LanguageModel: contextUsage property

· 52CE 我爱测

The **`contextUsage`** read-only property of the LanguageModel interface returns the number of context window tokens currently consumed by the session that calls it, including initial prompts and all subsequent turns.

Prompt API

The contextUsage read-only property of the LanguageModel interface returns the number of context window tokens currently consumed by the session that calls it, including initial prompts and all subsequent turns.

This value increases every time you call LanguageModel.prompt(), LanguageModel.promptStreaming(), or LanguageModel.append().

Compare contextUsage with LanguageModel.contextWindow to determine how many tokens remain. When contextUsage would exceed contextWindow, subsequent method calls throw a QuotaExceededError and the LanguageModel.contextoverflow_event event fires.

To estimate how many tokens a new prompt would use before sending it, call LanguageModel.measureContextUsage().

Value

A number representing the current context window usage in tokens.

Examples

Monitoring context usage during a conversation

This example writes context usage to the console after a session prompt completes.

const session = await LanguageModel.create();

await session.prompt("Tell me about the history of the internet.");

console.log(
  `Context used: ${session.contextUsage} / ${session.contextWindow} tokens`,
);

Warning when the context is nearly full

The following example uses a function to verify that context is available before calling 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) {
  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

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。