This repository was archived by the owner on Sep 30, 2026. It is now read-only.
Repository navigation
fix(usage): share account-labeled cache key so /usage stops self-storming 429s - #9
Merged
Merged
Conversation
…ming 429s The pace-aware multi-account probe (anthropic_account_usage_probe_sync) fetches each non-active account's usage through fetch_usage_for_account_sync, which keyed the shared Anthropic usage cache with anthropic_usage_cache_key( access_token, None) -> "token:<prefix>". Every writer and the active-account reader key the same cache with Some(label) -> "label:<label>" (fetch_anthropic_usage_for_token for /usage, fetch_usage for the header widget, and the probe's own active-account branch via get_sync). The keyspaces never overlapped, so the probe's cache lookup always missed and it live-hit https://api.anthropic.com/api/oauth/usage for the non-active account on every rotation evaluation (MultiProvider construction, /model provider switch, and maybe_pace_balance). With a two-account rotation that over-called the usage endpoint and Anthropic returned 429 Too Many Requests. The 429 was then cached as a UsageData error under the active account's "label:" key and surfaced by /usage as "Usage API error (429 Too Many Requests): Rate limited", sticky for the 15-minute rate-limit backoff even after the endpoint recovered. Thread the account label into fetch_usage_for_account_sync and its inner fetch_usage_for_account, and key the cache with Some(label) so the probe reuses the entry the writers populate (2-5 min CACHE_DURATION) instead of live-fetching every tick. This only affected multi-account users; single-account setups never run the non-active probe. Add a regression test asserting the probe path and the /usage write path produce an identical cache key for the same account, and that it differs from the pre-fix None key.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
/usageshows the Anthropic row asUsage API error (429 Too Many Requests): Rate limited. Please try again later.(occasionally a transient 401), and it sticks for the 15-minute rate-limit backoff even after the endpoint recovers. The endpoint itself is healthy - a manual GET returns HTTP 200 with valid data. jcode is rate-limiting itself.Root cause
An Anthropic usage cache-key inconsistency.
anthropic_usage_cache_key(access_token, account_label)produceslabel:<label>when givenSome(label)andtoken:<prefix>when givenNone.Every writer and the active-account reader key by
Some(label):fetch_anthropic_usage_for_token(the/usagecommand fetch)fetch_usage(the header/info-widget refresh)get_sync())But the probe's non-active-account branch,
fetch_usage_for_account_sync, keyed withNone->token:<prefix>. That key never matched thelabel:entries the writers populate, so its cache lookup always missed and it live-hithttps://api.anthropic.com/api/oauth/usagefor the non-active account on every rotation evaluation (MultiProvider construction,/modelprovider switch, andmaybe_pace_balance). With a two-account rotation that over-called the usage endpoint into 429s. The 429 was then cached under the active account'slabel:key and surfaced by/usage.Live evidence: 79
Usage API error (429 Too Many Requests)lines over an 11-hour span in the running daemon logs. Only multi-account users are affected; single-account setups never run the non-active probe.Fix
Thread the account label into
fetch_usage_for_account_syncand its innerfetch_usage_for_account, and key the cache withSome(label)so the probe reuses the entry the writers populate (2-5 minCACHE_DURATION) instead of live-fetching every rotation tick.Tests
probe_and_write_path_share_anthropic_usage_cache_keyasserting the probe path and the/usagewrite path produce an identical cache key for the same account, and that it differs from the pre-fixNonekey.cargo test -p jcode-base --lib usage::green (65 passed).cargo clippy -p jcode-baseclean.