fix: context percent now matches /context output exactly#30
fix: context percent now matches /context output exactly#30r-firpo wants to merge 1 commit intojarrodwatts:mainfrom
Conversation
Previously, the HUD showed an inflated context percentage (e.g., 88%) compared to what /context reported (e.g., 65%) after compactions. This was because the display used a hardcoded 45k buffer meant for warning thresholds, not the actual value. Changes: - getContextPercent() returns raw token usage (matches /context) - getBufferedPercent() adds 45k buffer for warning calculations - Display shows raw %, but colors/warnings use buffered % - When approaching threshold (85%+), shows remaining headroom 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
@jarrodwatts Think i found the source of at least some of the context-management discrepancies people have been reporting. The plugin was using a fix buffer for the "auto compaction" threshold (ie. how much context you have left until claude auto-compacts) and adding it to your context heuristically, treating it as a fixed amount of context to always add to usage. But a) if you dont use auto-compact (i dont) this doesn't work; b) It's not technically accurate. I think auto-compaction threshold should probably be a best-effort warning rather than a fixed addition to your used context. The users context is strictly just the amount of token's they've used |
- Change hardcoded 45k buffer to 22.5% of context window
- Scales correctly for enterprise windows (>200k)
- Add `display.autocompactBuffer` config option ('enabled' | 'disabled')
- Default 'enabled' preserves current behavior (buffered %)
- 'disabled' shows raw % for users with autocompact off
Fixes #48, #16
Related: #4, #30, #43, #49
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- Change hardcoded 45k buffer to 22.5% of context window
- Scales correctly for enterprise windows (>200k)
- Add `display.autocompactBuffer` config option ('enabled' | 'disabled')
- Default 'enabled' preserves current behavior (buffered %)
- 'disabled' shows raw % for users with autocompact off
Fixes #48, #16
Related: #4, #30, #43, #49
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix: use percentage-based autocompact buffer with config toggle
- Change hardcoded 45k buffer to 22.5% of context window
- Scales correctly for enterprise windows (>200k)
- Add `display.autocompactBuffer` config option ('enabled' | 'disabled')
- Default 'enabled' preserves current behavior (buffered %)
- 'disabled' shows raw % for users with autocompact off
Fixes #48, #16
Related: #4, #30, #43, #49
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* test: add autocompactBuffer tests for renderSessionLine
Address review feedback:
- Add tests verifying autocompactBuffer: 'enabled' uses buffered %
- Add tests verifying autocompactBuffer: 'disabled' uses raw %
- Add autocompactBuffer check to loadConfig test
- Update baseContext() to include autocompactBuffer
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* docs: add clickable GitHub URLs to CHANGELOG credits
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* feat: add debug log for autocompactBuffer mode
When DEBUG=claude-hud is set and autocompactBuffer='disabled',
logs both raw and buffered percentages to help troubleshoot mismatches.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
|
Thank you for your contribution! Your dual-percentage approach (raw display + buffered warnings) was incorporated into #55, which adds a configurable toggle and percentage-based buffer scaling. Credit: Your ideas from this PR informed the final solution. 🙏 |
TL;DR
The HUD's context percentage was showing significantly inflated values compared to Claude Code's /context command. because it was assuming all users would have the auto-compaction feature enabled, however I don't use CC auto-compaction because i think its too noisy. Because I compact ad-hoc (usually very near the limits of the context winxow) compaction the HUD might show 88% while /context reports 65%. This caused confusion about actual context usage. The fix separates "display percentage" (raw, matches /context) from "warning percentage" (buffered, used for color thresholds and compact warnings).
Problem
When Claude Code compacts a conversation, the actual token count drops significantly—but the HUD was adding a hardcoded 45,000 token "autocompact buffer" to the percentage calculation. This buffer exists because Claude Code triggers autocompact around 45k tokens before hitting the limit, so the buffer helps warn users before that happens. This buffer was being applied to the displayed percentage, not just the warning thresholds.
However, the actual auto-compaction thresholds i don't think are reported (i assume this plugin tried to reverse engineer it), nor are they guaranteed to be deterministic (tracking them with a constant is flaky) and very well could be changed often, and model dependent. The result:
The core issue was conflating two different concepts:
Solution
This PR proposes we calculate and use two separate percentages:
getContextPercent(stdin) - Raw percentage (for display)
// Returns actual token usage / context window size
// This matches what /context reports
const percent = getContextPercent(ctx.stdin); // e.g., 65%
getBufferedPercent(stdin) - Buffered percentage (for warnings)
// Returns (actual tokens + 45k buffer) / context window size
// This represents "compact risk" level
const bufferedPercent = getBufferedPercent(ctx.stdin); // e.g., 88%
How they're used together
Example output at 65% actual / 88% buffered:
[Opus] ██████░░░░ 65% | my-project (in: 130k, cache: 5k) (12% until auto-compact if enabled)
Files Changed
Screenshots
BEFORE
This was the output of /context after a compaction

And this is what claude-hud would say

AFTER

This accurately reflects the actual context 1:1 and shows a friendly hit for those who have auto-compaction turned on