Skip to content

📏 fix: Keep the Context Gauge's Used Count Above Its Own Breakdown - #16368

Merged
danny-avila merged 2 commits into
devfrom
danny-avila/context-gauge-floor
Sep 27, 2026
Merged

danny-avila merged 2 commits into
devfrom
danny-avila/context-gauge-floor

Conversation

@danny-avila

@danny-avila danny-avila commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The context gauge can report fewer used tokens than the system prompt it lists, and its breakdown then drops the Messages row entirely.

On the snapshot path, useTokenUsage computes used tokens as contextBudget − remainingContextTokens (plus finalized output and retained tool results). Breakdown.tsx then derives the message share by subtraction:

const messageBudget = Math.max(0, usedTokens - instructionTokens - summaryTokens);

When a snapshot's remainingContextTokens was measured against a smaller instruction total than the effectiveInstructionTokens it publishes, usedTokens falls below instructionTokens. The clamp floors the share at 0, and a legend row renders only for a positive value, so Messages disappears. The percent and the "Free space" figure are understated by the same amount.

#16301 made this visible in e2e: with no saved memories, the memory instruction block now reaches the system prompt on a first turn (~57 tokens), while the fake model's first call reports 2 input tokens. context usage gauge › renders the granular breakdown from the live context snapshot and › preserves the granular breakdown after switching branches fail at the Messages assertion. Upstream CI skips e2e on these PRs, so a downstream fork running e2e on every PR caught it.

It is not only a test artifact. Applying the gauge's arithmetic to the last 400 persisted snapshots on a live deployment, 23 (about 6%) would hide the Messages row, for example effectiveInstructionTokens 12,887 with contextBudget − remainingContextTokens of 10,726. Every underflowing snapshot sampled had breakdown.messageTokens: 0.

The fix keeps the remaining-based count, which covers content the breakdown omits, but never lets it undercut the separately reported instructions, summary, and messages:

const breakdownUsed =
  instructionTokens +
  normalizeTokenCount(breakdown.summaryTokens) +
  normalizeTokenCount(breakdown.messageTokens);
const baseUsed =
  remainingContextTokens != null
    ? Math.max(maxTokens - remainingContextTokens, breakdownUsed)
    : breakdownUsed;

Legacy snapshots without remainingContextTokens use the same three-bucket sum. Breakdown-based branch-history readings include the summary as well, so the restored gauge and runway share one basis. Remaining-based runway growth still uses the backend's raw remaining reading.

The inconsistency itself originates in the @librechat/agents snapshot, where the published remaining and instruction counts can disagree; that is tracked separately in the SDK. This change makes the client robust to it.

Type of change

  • Bug fix

Testing

Automated tests:

  • useTokenUsage.spec.tsx: inconsistent snapshots with and without a summary, legacy live and restored snapshots, and a summarized snapshot whose larger remaining-based total stays unchanged.
  • tokens.spec.ts: legacy branch-history readings include the summary.
  • useTokenUsage.spec.tsx, tokens.spec.ts, and Breakdown.spec.tsx: 115 passing; npx tsc --noEmit in client clean.

Screenshots / recordings

Before, the failing e2e popover: Context window 7 / 21.5K, Tool calls 0, System prompt 57, Free space 21.5K, no Messages row.

Risk / compatibility

Client-only. Remaining-based used tokens rise only when they undercut the reported three-bucket breakdown. Legacy snapshots with a nonzero summary now include that previously omitted bucket. Consistent remaining-based snapshots render exactly as before; stored formats are unchanged.

Checklist

  • I reviewed my own changes
  • Relevant tests have been added or updated
  • Existing relevant tests pass
  • The change does not introduce new warnings or errors
  • User-facing or complex behavior is documented where necessary
  • Required dependency changes have been merged/published
  • Required documentation PR: N/A

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-27T12:46:05.996804Z 44d8777 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0b341e11e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread client/src/hooks/Chat/useTokenUsage.ts Outdated
@danny-avila
danny-avila force-pushed the danny-avila/context-gauge-floor branch from 0b341e1 to 3aa31b6 Compare September 27, 2026 12:05
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review the latest head

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 44d877717b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila merged commit 129a1de into dev Sep 27, 2026
27 checks passed
@danny-avila
danny-avila deleted the danny-avila/context-gauge-floor branch September 27, 2026 13:12
AnJuHyppolite pushed a commit to newjersey/nj-ai-assistant that referenced this pull request Sep 30, 2026
…ibreChat-AI#16368)

* 📏 fix: Keep the Context Gauge's Used Count Above Its Own Breakdown

* 📏 fix: Include summary tokens in context gauge breakdown floor

---------

Co-authored-by: Lia <lia@librechat.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants