Repository navigation
feat(statusline): show tokens saved in the 5h window on a subscription - #511
Conversation
On a Claude subscription, Claude Code sends rate_limits in the status-line input. The first line now shows the real 5-hour use and the tokens compaction kept out of that window, split into cache reads (replay_tokens) and cache writes (saved_unique). Counts are plain, not weighted. The spend and ka segments are hidden. API-key accounts keep the money view. Window totals come from the existing /api/stats?lean=1&since=<ms>. An opt-in `fit` element shows a list-price weighted gain. Closes #506 Assisted-By: Claude <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
Verdict: changes needed (posted as COMMENT because the same account owns the PR)
The Go test file does not compile. CI for ./context-guru-plugin/ cannot pass. The feature itself works once that is fixed.
Must fix
statusline_golden_test.godoes not build.- The row struct has 6 fields (
name, stdin, layout, width, want, extra). Six rows give only 5 values (balanced,minimal,detailed-hides-money,narrow,no-reset-time,null-limits-is-api-key). Go rejects an unkeyed literal with too few values. - Evidence:
go test ./context-guru-plugin/ -run Statuslineon eval box 1 ata62804edgivestoo few values in struct literal(6 errors) and[build failed]. - Fix: add
""as the last value in those rows, or use keyed fields. - After I added
""locally (not pushed), all 7TestStatuslineGoldenSubscriptioncases pass, and so do all otherStatusline*tests. The goldens are not vacuous: the window stub only answerssince=1800000123000, which isresets_atminus 5h in ms. A wrong window start returns the default stats and the test fails.
- The row struct has 6 fields (
Should fix
-
A past
resets_atshows a dead window._subscriptionaccepts anyresets_at > 0._window_start_msthen returnsreset - 5h, even when that is hours ago.- Scenario: Claude Code keeps the last
rate_limitsafter the window ended and before the next response. I renderedresets_at = now - 2h. Output:5h 34% · saved 1.2M rd / 85.0k wr, with counts since 7 hours ago. - Proposal: if
resets_at <= now, treat it as no reset time. Show5h NN%only, or the money view. Add a golden for it. - Units are fine: seconds and milliseconds both give the right start (checked with
now*1000).
-
On a subscription the status line still fetches data it never shows.
_gatherstill starts thelifejob (since=0) whensaveis on. Thesavesegment is replaced, so nothing reads it.- The code comment says this read "takes seconds on a large history". I saw
since=0and today requests on every render in the stub log. - Proposal: skip
life(anddayunlesssparkis on) whensubis set.
-
Narrow widths drop the one real number.
- At 29 columns, level 2 shows
saved 1.2M rd / 85.0k wrand removes5h 34%. The5hshare is Claude Code's own figure. The saved counts are the proxy's estimates. - Proposal: keep
5h NN%last inDEGRADE. Drop the saved counts first.
- At 29 columns, level 2 shows
-
fitis out of scope and has no tests of its value.- The owner asked for plain counts with no list-price weighting. This PR still adds
fit,_limit_weight(an identity function),_fit_gain,_fit_segment, aDEGRADEentry, aROWentry,WINDOW_TTL_SECONDSsharing, and docs. - It is dead by default and adds a weighted number the owner said not to show. It is also the only user of
cost_usdfrom the window read. - Proposal: remove it. Add it later if data supports a weight.
- The owner asked for plain counts with no list-price weighting. This PR still adds
Answers to the review questions
- Are
rdandwrhonest? Mostly.wr=saved_unique: removals counted once.rd=replay_tokens=saved_gross - saved_unique(dash/overview.go:680): the same removals absent on later turns.- Those are the proxy's token estimates. A replayed token is a cache read only when the cache hit. After a cache miss it is billed as a write. The docs say "would have been". The status line says only
rdandwr. - Cachesplit, keep-alive and declaration-filter savings have no token count and are missing. The docs disclose this (
docs/plugin/statusline.md). The line does not. Accept it, but the labelsavedcan read as the full saving.
- Window scoping: correct for seconds and ms. Past or missing
resets_at: see item 2 andno-reset-timegolden (shows5h 34%only, good). - Detection: a numeric
five_hour.used_percentageis a sound test. I rendered these and got the old money view each time: string"34",five_hour: null, onlyseven_day,rate_limitsas a list,spend_limitonly. No crash. - Dollars on a subscription: none found in
balanced,detailed,minimal,watch.spendandkaare hidden. - Other layouts:
fillandsparkhave nosaveelement, so they show no window data. That is consistent with before.
Tests I ran
python3 -I -m py_compile statusline.py: pass.- Go on box 1, as-is: build failed (item 1).
- Go on box 1 with the 6 rows patched locally:
TestStatuslineGoldenSubscription7/7 pass. All-run Statuslinetests:ok. - Manual renders against a stub of
/api/stats: 13 payloads and widths (200, 50, 40, 33, 29, 20). Results above.
The table gained an `extra` column; the other rows did not supply it, so the package did not compile. Assisted-By: Claude <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
- Treat a past resets_at as an ended window: show no 5h share, no counts. - Skip the day and lifetime reads on a subscription; nothing shows them. - Drop the counts first at narrow widths; the 5h share stays longest. - Remove the weighted fit element, _limit_weight and _fit_gain. - The detailed layout writes "compaction saved" to name what the counts cover. Assisted-By: Claude <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
|
All 5 findings fixed in 22db925. Replies per finding:
Also: Tests on the eval box: |
amiddavid
left a comment
There was a problem hiding this comment.
Verdict: all 5 findings fixed. No new blocking issues.
- Goldens do not compile: fixed.
-run Statuslineon box 1 at22db925: 107 pass, 0 fail.TestStatuslineGoldenSubscriptionpasses 7/7. - Past
resets_at: fixed. I renderedresets_at = now-60and= now. Both show no5hshare, no counts and no money. Theexpired-windowgolden passes. A live window and a missingresets_atstill render as before (5h 34% · saved 1.2M rd / 85.0k wrand5h 34%). - Unused reads: fixed. My request log over 9 renders shows only
cachestate,stats?session=andstats?lean=1&since=<window start>. Nosince=0and no day read. - Narrow widths: fixed. At 29 and 20 columns the first line is
5h 34%. The counts drop first. fitremoved: fixed. Nofit,_limit_weightor_fit_gainremain in the diff.--show=fitis now ignored without error.
Also checked, no regression:
detailednow reads5h 34% · 7d 12% · compaction saved 1.2M rd / 85.0k wr. This names what the counts cover. Good.- The API-key and
rate_limits: nullgolden still shows the money view. - Not rerun by me: the full package (498 pass per the author) and
test_tv.py(36 per the author).
|
Moved to the next release by the release owner. v0.5.3 is out without it. It needs a rebase on |
Keep main's status line (one timer, next-request cost, saved $x of $y, per-agent money, weekly tip). Re-apply the subscription view on top: 5h share plus saved rd/wr tokens, no dollars on any element, and no spark or spend-explanation reads on a subscription. Assisted-By: Claude <[email protected]> Signed-off-by: DAVID AMID <[email protected]>
amiddavid
left a comment
There was a problem hiding this comment.
Looks good
The merge (ff0f5a3) keeps the subscription view and the API-key view intact. No findings.
- Conflict resolution. No conflict markers remain in
statusline.py,statusline.md,SKILL.mdor the golden test. The diff againstorigin/maininstatusline.pyholds only the window code and the money switches. Main'smoney,tipandnextcode is untouched. - Dollars on a subscription: none reachable. I rendered a subscription payload with every money source in the stub (session and sub-agent saving,
warm/cold/rewriteprices, manual-ping saving, keep-alive cost, Claude Code cost). I covered 8 layouts, each with no--show, with all 17 elements together, and with each element alone: 144 renders. None printed$. Kept as designed:ka 3 pings, a count with no cost. - API key unchanged from main. I rendered PR and
origin/mainside by side: 8 layouts, no--show, all elements, each element alone, for a payload with norate_limitsand one withrate_limits: null. That is 48 pairs, 0 differences after I removed theTip:line. My copy of main did not include the tips data file, which produced the only differences. - The new test catches regressions. I changed one line at a time in the local worktree, ran
TestStatuslineSubscriptionShowsNoMoney, then restored the file. All three mutations failed the test:- re-enable
spend:detailedfails; - re-enable
next:balancedanddetailedfail; - pass the keep-alive cost through again:
spark,strip,heat,fillandwatchfail.
- re-enable
- Tests, eval box 1,
-race, new empty dir.go test -race ./context-guru-plugin -run Statusline -count=1: 162 pass, 0 fail. I did not rerun the full package. The author reports 685 pass.
Not a finding: the doc example in docs/plugin/statusline.md still shows agents 2 · soonest. Main now writes agent caches (N). It is only an illustration.
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 2 (bob, user of @amiddavid) | IBM Bob Shell 1.0.3 | offload-review skill, @amiddavid's session | fresh | 2 of 2 | 1 of 2 | ff0f5a3a409a |
approve |
Verdict: approve
Findings
None. The implementation is correct.
Non-blocking
- The subscription detection logic is robust. It handles missing fields, expired windows, and malformed data gracefully.
- The token counting correctly maps
replay_tokensto "rd" andsaved_uniqueto "wr" as documented. - The fail-open behavior is preserved: subscription detection returns
Nonewhenrate_limitsis absent or malformed, keeping the money view. - The test coverage is comprehensive, including edge cases like expired windows, missing reset times, and null rate_limits.
- The money-hiding logic correctly removes all dollar signs on subscriptions while keeping counts (e.g., keep-alive pings).
What I checked
- Read the full diff (statusline.py, statusline_golden_test.go, SKILL.md, statusline.md)
- Verified the subscription detection logic in
_subscription()handles all edge cases - Traced the token counting through
_saved_tokens()→_saved_tokens_text()→_window_segment() - Confirmed the return value order matches the docstring:
(read, write)=(replay_tokens, saved_unique) - Verified the test stub data matches the expected output format
- Checked that all money elements are correctly hidden when
subis not None - Confirmed the window start calculation uses the correct formula:
reset - WINDOW_SECONDS - Verified the expired window logic prevents showing stale data
- Checked that the API key path (no rate_limits) preserves the original money view
- Confirmed the documentation accurately describes the feature behavior
amiddavid
left a comment
There was a problem hiding this comment.
| Reviewer | Kind | Started by | Context | Review | Round | Head | Verdict |
|---|---|---|---|---|---|---|---|
| Reviewer 1 (bob, user of @amiddavid) | IBM Bob Shell 1.0.3 | offload-review skill, @amiddavid's session | fresh | 1 of 2 | 1 of 2 | ff0f5a3a409a |
approve |
Verdict: approve
Findings
None. The subscription view implementation is correct.
Non-blocking
-
statusline.py:1412 — The window start calculation
int((reset - WINDOW_SECONDS) * 1000)could produce a negative timestamp ifreset < 18000(5 hours after epoch, January 1970). This is a theoretical edge case: Claude Code sends current timestamps (year 2026+), not epoch-relative ones. The expired-window guard at line 1406 (reset <= _now_s()) would catch any timestamp before now, making the window show nothing. Still, an explicit guardreset >= WINDOW_SECONDSwould make the constraint clear. -
statusline.py:1669 — The cache key
f"win-{win_start // 1000}"divides milliseconds by 1000 to get seconds. This is correct, but the division could be documented:f"win-{win_start // 1000}" # milliseconds → seconds for stable key.
What I checked
- Read CLAUDE.md and docs/design.md for repo rules
- Read the full diff (statusline.py, statusline_golden_test.go, docs/plugin/statusline.md, SKILL.md)
- Verified the subscription detection logic (
_subscription()) correctly parsesrate_limits.five_hourand handles missing/null/expired cases - Verified the window start calculation (
_window_start_ms()) correctly computes the 5-hour window boundary - Verified the token counting logic (
_saved_tokens(),_saved_tokens_text()) correctly extractssaved_unique(writes) andreplay_tokens(reads) from/api/stats - Verified the window segment display (
_window_segment()) correctly formats the output and respects thedetailedlayout - Verified all money-related segments are correctly hidden on subscriptions:
save,next,agents(level override),spend,ka(cost only),spark(removed fromon),strip/heat/fill(money parameter) - Verified the spend fetch is correctly skipped on subscriptions (line 1657) and the
moneyparameter guards prevent unsafe calls - Verified test coverage:
TestStatuslineGoldenSubscriptioncovers balanced/minimal/detailed/narrow layouts, expired windows, missing reset times, and nullrate_limits(API key fallback) - Verified test coverage:
TestStatuslineSubscriptionShowsNoMoneyconfirms no dollar signs appear in any layout on a subscription, and keep-alive count stays - Verified the stub at line 29-30 in
statusline_golden_test.gocorrectly returns window stats forsince=1800000123000 - Verified the documentation in docs/plugin/statusline.md correctly describes the subscription view, token counts, and money hiding
- Verified the SKILL.md update correctly documents the subscription behavior
The implementation is correct. The subscription view replaces money with token counts, hides all dollar figures, and handles edge cases (expired windows, missing reset times, API key fallback). The test coverage is comprehensive. The documentation is clear and accurate.
Closes #506
Summary
spentandka $…segments are hidden.What changed
Before and after, balanced layout, same traffic (current main):
--layout=detailedadds7d 12%and writescompaction saved …. At narrow widths the counts drop first and5h 34%stays (30 columns:5h 34%). An ended window (resets_atin the past) shows nothing.What each number means:
5h 34%rate_limits.five_hour.used_percentagerd(saved read)/api/statsreplay_tokens=saved_gross − saved_unique. Removed tokens that later turns would have re-sent. They would have been cache reads.wr(saved write)/api/statssaved_unique. Each distinct removal counted once. It would have been a cache write.rate_limits.five_hour.used_percentage. Window start =resets_atminus 5 hours.GET /api/stats?lean=1&since=<ms>. No Go code changed. They are plain, unweighted token counts.sparkrow and the spend-explanation read, and makes no lifetime read.Money elements on a subscription
saved $x of $y session,… subagents5h NN% · saved … rd / … wr↻ $zmanual-ping savingnext $a warm · $b coldsaved $x of $y(--show=agents)ka 3 pings $0.04ka 3 pingsspent(detailed)sparkrow (saved today)lost today(strip,heat) anda miss now costs(fill)▲ updateNew test
TestStatuslineSubscriptionShowsNoMoneyfeeds every money source into a subscription payload and fails on any$in 8 layouts. The same inputs on an API key still show them.API-key accounts: main's behaviour, unchanged.
How it was tested
Merge of
origin/maininto this branch (no rebase). On the eval box,CGO_ENABLED=1:-race -run Statusline: 162 pass, 0 fail../context-guru-plugin/in a clean directory: 685 pass, 0 fail.test_tv.pyandtest_costly.py: pass.Open questions
rd/wr.BilledTokenFactor, about 1.4x to 1.7x). The status line shows the unscaled estimate./context-guru:statusare not changed. Recording theanthropic-ratelimit-unified-*headers in the proxy is left out.rate_limitscan be absent before the first response. The line then shows the money view until it arrives.Technical notes
statusline.py:_subscription,_saved_tokens,_window_segment. The window read uses_bg_jsonwith a cache key per window start, TTL 30 s. It adds no new network target.rdismax(replay_tokens, 0).wrismax(saved_unique, 0).