Skip to content

Visualize context window usage in Ask Sourcebot#1370

Merged
jsourcebot merged 12 commits into
mainfrom
jminnetian/visualize-context
Jun 26, 2026
Merged

Visualize context window usage in Ask Sourcebot#1370
jsourcebot merged 12 commits into
mainfrom
jminnetian/visualize-context

Conversation

@jsourcebot

@jsourcebot jsourcebot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor
image

We support showing the context usage in cases where we know the context window the model (from models.dev). For self hosted models or if they are behind an API, this will not currently work.

Summary by CodeRabbit

  • New Features
    • Added a context-window usage gauge to chat details, visualizing the latest turn’s prompt token consumption as a percent of the selected model’s context window (with tooltip and progress arc).
    • Streaming now includes the model’s context-window value in message metadata so the gauge can render when available.
    • Context-window sizes are resolved from the models catalog, including provider/model mapping.
  • Bug Fixes
    • The gauge is not shown when context-window data is missing, invalid, or unsupported.
  • Tests
    • Expanded tests for catalog lookup, mapping, caching/refresh, and failure scenarios.

@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a7bd1907-3351-47cc-8b2e-9949b6810958

📥 Commits

Reviewing files that changed from the base of the PR and between 005f251 and 28f61eb.

📒 Files selected for processing (8)
  • CHANGELOG.md
  • packages/web/src/app/api/(server)/ee/chat/route.ts
  • packages/web/src/ee/features/chat/agent.ts
  • packages/web/src/ee/features/chat/components/chatThread/detailsCard.tsx
  • packages/web/src/ee/features/mcp/askCodebase.ts
  • packages/web/src/features/chat/modelContextWindow.server.ts
  • packages/web/src/features/chat/modelContextWindow.test.ts
  • packages/web/src/features/chat/types.ts
✅ Files skipped from review due to trivial changes (2)
  • packages/web/src/features/chat/types.ts
  • CHANGELOG.md
🚧 Files skipped from review as they are similar to previous changes (6)
  • packages/web/src/app/api/(server)/ee/chat/route.ts
  • packages/web/src/features/chat/modelContextWindow.server.ts
  • packages/web/src/ee/features/chat/components/chatThread/detailsCard.tsx
  • packages/web/src/ee/features/chat/agent.ts
  • packages/web/src/ee/features/mcp/askCodebase.ts
  • packages/web/src/features/chat/modelContextWindow.test.ts

Walkthrough

Adds context-window resolution to chat flows, carries that value through message metadata, and renders a usage gauge in chat details. The changelog also notes the new gauge.

Changes

Context window usage gauge

Layer / File(s) Summary
Catalog lookup and caching
packages/web/src/features/chat/modelContextWindow.server.ts, packages/web/src/features/chat/modelContextWindow.test.ts
lookupContextWindow and resolveContextWindow load the models.dev catalog, map provider IDs, and return context-window values; tests cover lookup cases and cached fetch behavior.
Context window propagation
packages/web/src/app/api/(server)/ee/chat/route.ts, packages/web/src/ee/features/mcp/askCodebase.ts
The chat route and askCodebase resolve the selected model context window and pass it into createMessageStream.
Metadata carries contextWindow
packages/web/src/features/chat/types.ts, packages/web/src/ee/features/chat/agent.ts
SBChatMessageMetadata and createMessageStream now carry contextWindow in message metadata.
Chat details gauge
packages/web/src/ee/features/chat/components/chatThread/detailsCard.tsx, CHANGELOG.md
DetailsCardComponent derives context-window usage from the latest step token counts, renders ContextWindowGauge, and the changelog records the new gauge.

Sequence Diagram(s)

sequenceDiagram
  participant ChatRoute
  participant AskCodebase
  participant ResolveContextWindow
  participant CreateMessageStream
  participant DetailsCardComponent
  ChatRoute->>ResolveContextWindow: resolveContextWindow(languageModelConfig)
  AskCodebase->>ResolveContextWindow: resolveContextWindow(languageModelConfig)
  ResolveContextWindow-->>ChatRoute: contextWindow
  ResolveContextWindow-->>AskCodebase: contextWindow
  ChatRoute->>CreateMessageStream: createMessageStream(..., contextWindow)
  AskCodebase->>CreateMessageStream: createMessageStream(..., contextWindow)
  CreateMessageStream-->>DetailsCardComponent: message-metadata.contextWindow
  DetailsCardComponent->>DetailsCardComponent: derive usage and render gauge
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • brendan-kellam
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding a context window usage visualization for Ask Sourcebot.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch jminnetian/visualize-context

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@CHANGELOG.md`:
- Line 15: Update the changelog entry for the context-window usage gauge to
follow the enterprise-only convention by adding the [EE] prefix, and replace the
placeholder PR reference with the real GitHub PR number/link. Keep the wording
otherwise the same, and make sure the entry still clearly describes the Ask
Sourcebot chat details gauge and its models.dev-based window sizing.

In `@packages/web/src/features/chat/modelContextWindow.server.ts`:
- Around line 59-73: The loadCatalog caching flow currently drops the previously
cached ModelsDevCatalog on a failed refresh and immediately retries every
request, which can block chat sends on the request path. Update loadCatalog so a
TTL-triggered fetch failure preserves and returns the last-known-good catalog
instead of replacing it with null, and add a short negative-cache/backoff window
for repeated fetchCatalog failures. Keep the retry behavior bounded, but avoid
clearing a usable catalog or reattempting on every resolveContextWindow call.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 808a7227-a756-49ec-9fad-14d26485aa46

📥 Commits

Reviewing files that changed from the base of the PR and between 5e1b8ee and b02535e.

📒 Files selected for processing (8)
  • CHANGELOG.md
  • packages/web/src/app/api/(server)/ee/chat/route.ts
  • packages/web/src/ee/features/chat/agent.ts
  • packages/web/src/ee/features/chat/components/chatThread/detailsCard.tsx
  • packages/web/src/ee/features/mcp/askCodebase.ts
  • packages/web/src/features/chat/modelContextWindow.server.ts
  • packages/web/src/features/chat/modelContextWindow.test.ts
  • packages/web/src/features/chat/types.ts

Comment thread CHANGELOG.md Outdated
Comment thread packages/web/src/features/chat/modelContextWindow.server.ts
if (percent >= CONTEXT_USAGE_YELLOW_PERCENT) {
return "text-yellow-500";
}
return "text-[#6cb38f]";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

to confirm, does this look good in light mode?

Resolve each model's context window from the models.dev catalog (already
fetched by the setup wizard) and bake it into chat message metadata. The
Details card now renders a usage gauge from the latest step's input tokens.
Models with no catalog entry (openai-compatible/self-hosted) fall back to
the existing raw token count.
createMessageStream's MCP/programmatic caller omitted contextWindow, so
chats created via ask_codebase rendered the Details card without a usage
gauge even for catalogued models — unlike the same chat created from the
web API. Resolve it from the already-available languageModelConfig so the
gauge is deterministic per model, not per entry point.
Replace the horizontal bar with a circular ring showing the percentage
inside and the used / total token counts beside it. The arc and percentage
are colored by usage — green below 70%, yellow from 70%, red from 90%.
Shrink the ring and move the percentage beside it, reading
"<percent>% of <total>" instead of a number-in-ring. The arc and
percentage stay colored by usage (green/yellow/red).
Reduce the ring to 14px, switch the track to a solid palette gray (the
theme tokens lack an alpha channel, so /opacity on them was ignored and
the track rendered at full brightness), and use a desaturated sage green
for the in-range percentage.
Also drop the gauge's yellow/red thresholds from 70/90 to 50/80 so the
ring shifts color earlier as the context fills.
Real PR number to be filled in once the PR is opened.
Previously a failed fetch reset the cache to null, so every chat send
re-attempted the fetch and blocked up to the 8s timeout during a models.dev
outage; a failed TTL refresh also discarded the previously-cached catalog.

Switch loadCatalog to stale-while-revalidate: after the first successful
load the request path never blocks (it serves the last-known-good catalog
and refreshes in the background), failed refreshes keep the cached value,
and a 60s negative-cache window bounds retries during an outage.
@jsourcebot jsourcebot force-pushed the jminnetian/visualize-context branch from 005f251 to 28f61eb Compare June 26, 2026 20:32
@jsourcebot jsourcebot merged commit 0fdf0b3 into main Jun 26, 2026
9 checks passed
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