Skip to content
This repository was archived by the owner on Sep 30, 2026. It is now read-only.

fix(usage): share account-labeled cache key so /usage stops self-storming 429s - #9

Merged
yjuyjuy merged 2 commits into
masterfrom
usage-cache-key-fix
Aug 13, 2026
Merged

yjuyjuy merged 2 commits into
masterfrom
usage-cache-key-fix

Conversation

@yjuyjuy

@yjuyjuy yjuyjuy commented Aug 11, 2026

Copy link
Copy Markdown
Owner

Problem

/usage shows the Anthropic row as Usage 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) produces label:<label> when given Some(label) and token:<prefix> when given None.

Every writer and the active-account reader key by Some(label):

  • fetch_anthropic_usage_for_token (the /usage command fetch)
  • fetch_usage (the header/info-widget refresh)
  • the pace-aware probe's active-account branch (reuses get_sync())

But the probe's non-active-account branch, fetch_usage_for_account_sync, keyed with None -> token:<prefix>. That key never matched the label: entries the writers populate, so its 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 into 429s. The 429 was then cached under the active account's label: 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_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 rotation tick.

Tests

  • New regression test probe_and_write_path_share_anthropic_usage_cache_key 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.
  • cargo test -p jcode-base --lib usage:: green (65 passed).
  • cargo clippy -p jcode-base clean.

1jehuang and others added 2 commits August 11, 2026 11:43
…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.
@yjuyjuy
yjuyjuy merged commit 7c6fbb0 into master Aug 13, 2026
10 of 11 checks passed
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