Skip to content

feat(studio): show estimated context usage before model load - #9475

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
InfoSage05:feat/studio-estimated-context-before-load
Oct 5, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
InfoSage05:feat/studio-estimated-context-before-load

Conversation

@InfoSage05

@InfoSage05 InfoSage05 commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Shows an estimated context usage in the chat header for a stored chat that no model has counted yet, for example when browsing chats before loading a model, so you can see roughly how much context to load it with. The estimate is the selected branch's text length divided by 4 and is shown with a leading ~ (~900 tokens, ~4.1k tokens, or ~900 / 32.8k when a window is known).

Fixes #9330

Changes

  • utils/estimate-chat-tokens.ts: estimateContextUsage(records) walks the selected branch with the existing orderBySelectedBranch from message-order.ts and returns a usage snapshot flagged estimated: true.
  • runtime-provider.tsx history loader and the compare-exit restore in chat-page.tsx: when no saved usage matches the active model, the estimate fills the bar instead of leaving it empty.
  • The recount gates (ThreadContextUsageRecount, the status hydrate in use-chat-model-runtime.ts) treat an estimated bar as blank, so a loaded model still replaces it with the exact count.
  • context-usage-bar-state.ts / context-usage-bar.tsx: estimated faces get the ~ prefix, no per-turn rows and no limit advice, and the tooltip notes that the number is an estimate without attachments.

Before / after

Same stored chat, no model loaded, merge base vs this branch:

header

Tooltip on hover:

tooltip

Testing

  • node --experimental-strip-types --test tests/estimate-chat-tokens.test.ts tests/context-window-visible.test.ts: 21/21 pass. Two of the new bar-state tests fail with the previous context-usage-bar-state.ts.
  • npm run typecheck clean. Full npm test: 10477/10480 pass. The 3 failures are in unrelated files (reasoning-source-render, tauri-backend-transport-failure), hit timeouts under load, and pass when re-run alone.

Show estimated token usage (~char length / 4) when browsing chat histories prior to model load or when exact tokenizer counts are not yet produced. Formats with a leading ~ (e.g. ~900 tokens) to give immediate feedback on context size before loading a model.

Fixes unslothai#9330
@danielhanchen

This comment has been minimized.

@InfoSage05

This comment has been minimized.

@danielhanchen

This comment has been minimized.

…Adapter imports

The merge of main into this branch resolved a conflict in
runtime-provider.tsx's import block by keeping only the incoming
imports from main and this branch's new estimateMessagesTokenCount
import, silently dropping two imports still used later in the file
(VideoAttachmentAdapter for the video attachment adapter,
sanitizeThreadScopedSettings for per-thread settings restore).

Reviewed per PR unslothai#9475 feedback.
@InfoSage05

This comment has been minimized.

@danielhanchen danielhanchen self-assigned this Oct 5, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 5, 2026
danielhanchen pushed a commit to danielhanchen/unsloth-staging-2 that referenced this pull request Oct 5, 2026
@danielhanchen

Copy link
Copy Markdown
Member

Review record for #9475, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#446.

Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found.

Round 1: reviewed mirror 3532e1673 (no equivalent commit found on this PR)

chatgpt-codex-connector[bot] (on the mirror)

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 3532e16736

No triage reaction was recorded on the mirror

Verdict

The round converged at mirror 3532e1673 (no equivalent commit found on this PR).

This is the review as it stood at that commit. Anything pushed to this PR afterwards was not part of it.

@danielhanchen
danielhanchen merged commit e347174 into unslothai:main Oct 5, 2026
danielhanchen added a commit that referenced this pull request Oct 5, 2026
…ates from (#12747)

* Recount harness: bind the loader's msgs that the history restore now estimates from

#9475 made the history restore fall back to estimateContextUsage(msgs) when no saved
usage is usable. The harness replays that block inside hydrateThreadUsage, which never
declared msgs, so every history-hydration case threw 'msgs is not defined' on main.

Bind msgs from props, pin the estimate-then-recount behaviour, and guard that every
loader local the sliced block reads is declared by the harness.

* Recount harness guard: read destructured loader locals too

The binding guard only collected 'const name' declarations, so a destructured loader
local such as remoteId was never checked. Collect object and array destructuring
names as well, and require the guard to see remoteId.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Show estimated context usage before model load

2 participants