Skip to content

fix(anthropic): keep weekly usage readings across 5h rollovers - #8723

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
cyhhao:fix/keep-weekly-usage-on-5h-rollover
Nov 16, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
cyhhao:fix/keep-weekly-usage-on-5h-rollover

Conversation

@cyhhao

@cyhhao cyhhao commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Problem

When the 5h window rolls over, UpdateSessionWindow clears every sampled usage reading. That includes the 5h utilization and also the 7d and 7d_oi utilization and reset:

"session_window_utilization":      nil,
"passive_usage_7d_utilization":    nil,
"passive_usage_7d_reset":          nil,
"passive_usage_7d_oi_utilization": nil,
"passive_usage_7d_oi_reset":       nil,
"passive_usage_sampled_at":        nil,

The weekly windows have their own reset times, and a reading only comes back when a later response carries that window's headers. For 7d_oi, those responses stop:

  1. Only responses to Fable requests carry anthropic-ratelimit-unified-7d_oi-*.
  2. When 7d_oi runs out, persistAnthropicFableWindowLimit sets the claude-fable-5 model limit (anthropic_7d_oi_window_exhausted), so the account receives no further Fable requests.
  3. At the next 5h rollover, the 7d F reading is erased. Nothing refills it until the weekly reset, so the account shows no 7d F usage exactly when that window is full.

We saw this on setup-token accounts. Every account that had lost its 7d F reading had served no Fable requests in its current 5h window, and most of them were under the Fable window limit. Every account that had served at least one Fable request in the window still had its reading.

The weekly scheduling thresholds in account_scheduling_threshold_eval.go read the same keys. They also lose their input at each rollover, until the next weekly sample arrives.

Fix

rolloverPassiveUsageClears builds the rollover update:

  • Always clear session_window_utilization, which belongs to the previous 5h window. This is unchanged.
  • Keep a weekly reading (passive_usage_7d_* or passive_usage_7d_oi_*) while its stored reset is still in the future.
  • Clear a weekly reading that has no reset or whose reset has passed. Previous-window data is still never kept.
  • Clear passive_usage_sampled_at only when no weekly reading is kept, because the sample time still describes any reading that remains.

The next sample with weekly headers replaces a kept reading. After its reset passes, the next rollover clears it.

Tests

  • TestUpdateSessionWindow_RolloverKeepsCurrentWeeklyReadings: a Fable-exhausted account gets a non-Fable response (5h and 7d headers, no 7d_oi) when its 5h window has expired. The 7d F reading must survive, and the 5h and 7d readings must take the new values. This test fails on main.
  • TestRolloverPassiveUsageClears: current, expired and missing resets, with reset values stored as int64, float64, json.Number and string.
  • The existing session-window tests are unchanged and pass, including TestUpdateSessionWindow_ClearsFable7dOiOnWindowReset (no stored reading, so everything is still cleared).
  • go test ./internal/service/... ./internal/repository/... passes.

A 5h window rollover cleared the sampled 7d and 7d_oi readings along with
the 5h utilization. Only Fable responses carry 7d_oi, and a Fable-exhausted
account stops receiving Fable requests, so its 7d F reading stayed empty
until the weekly reset. Weekly thresholds also lost their input after every
rollover.

Clear the 5h utilization as before, but keep a weekly reading while its own
reset is still ahead. Readings without a reset or past their reset are
still cleared.
@Wei-Shaw
Wei-Shaw merged commit 90eb174 into Wei-Shaw:main Nov 16, 2026
8 of 9 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants