Skip to content

TUI context usage always shows 0% — model.limit.context is undefined #39595

Description

@wanghaozi

Description

The context usage indicator in the TUI (the progress circle button showing "used" percentage in the right panel) always shows 0%, regardless of how much conversation has occurred.

Root Cause Analysis

The issue is in the build\ function (which computes session context) in the bundled frontend code. The chain is:

  1. build\ computes usage from the model's context limit:

    const limit = model?.limit.context;   // line 130424
    usage: limit ? Math.round(total2 / limit * 100) : null  // line 130435
  2. When model.limit is undefined (server doesn't return it), limit becomes undefined, usage is set to null.

  3. SessionContextUsage component renders:

    percentage: context()?.usage ?? 0   // null → 0
  4. ProgressCircle shows 0%:

    const clampedPercentage = Math.max(0, Math.min(100, 0 || 0));  // = 0

Two Possible Causes

  1. Server API doesn't return limit.context — The model.limit field at line 75945 is passed straight through from the server's model list API response (sdk.model.list()). If the server doesn't include limit: { context: <number> } in the model data, limit is undefined.

  2. Server doesn't send token data in session.step.ended — At line 78140, event.data.tokens populates the assistant message. If this is empty, tokenTotal returns 0, lastAssistantWithTokens skips the message, and build\ returns undefined → 0%.

Additional crash risk

The same model.limit is accessed without null safety in ModelTooltip component (line 136394):

props.model.limit.context.toLocaleString()  // crashes if model.limit is undefined

Suggested Fix

In build\, add a fallback for model.limit:

const limit = model?.limit?.context ?? 0;

And in ModelTooltip:

props.model.limit?.context?.toLocaleString() ?? "unknown"

But the real fix should be on the server side to ensure model.limit.context is always populated with the correct context window size for each model.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions