Skip to content

fix(server): Claude usage served from a saved reading keeps its real age - #38778

Open
iamshadmantaqi wants to merge 2 commits into
pingdotgg:mainfrom
iamshadmantaqi:fix/claude-usage-rate-limited-freshness
Open

iamshadmantaqi wants to merge 2 commits into
pingdotgg:mainfrom
iamshadmantaqi:fix/claude-usage-rate-limited-freshness

Conversation

@iamshadmantaqi

Copy link
Copy Markdown
Contributor

Problem

While /api/oauth/usage is rate-limited, Claude Code answers the SDK's get_usage from the reading it saved in the login's .claude.json (cachedUsageUtilization, up to an hour old), and the response does not say so. probeClaudeCapabilities stamps every successful usage read with the probe time (usageCheckedAt), and that becomes usageLimits.checkedAt. So each probe moves checkedAt forward while the numbers stay frozen, and an hour-old reading shows as just checked.

Change

After a successful probe, the Claude driver already reads the login's .claude.json for the organisation id. It now also reads cachedUsageUtilization. When every usage window in the get_usage response matches that saved reading, usageCheckedAt becomes the saved fetchedAtMs, never later than the probe. When the numbers differ, the response came from a fresh read and keeps the probe time.

This is safe in the fresh case too: Claude Code rewrites the saved reading on every successful read unless it is under a minute old, so a match means the numbers are at most a minute older than fetchedAtMs.

Scope

Refs #38070

This covers the first problem in the issue: cached data shown as freshly checked. It does not change how often readClaudeResetCredits calls /api/oauth/usage or add a back-off on 429. #33979 is open for the reset-credit side, so that part stays out of this PR.

Verification

Added one case to apps/server/src/provider/ProviderInstanceRegistry.test.ts. It runs the Claude driver with a mocked SDK whose get_usage returns the same numbers as a saved reading fetched 40 minutes before the probe. It then changes the numbers and checks that a fresh read keeps the probe time.

Without the fix (test commit only):

 FAIL  src/provider/ProviderInstanceRegistry.test.ts > (multi-instance slice) > dates Claude usage served from the CLI's saved reading by when it was fetched
AssertionError: expected '1970-01-01T01:00:00.000Z' to be '1970-01-01T00:20:00.000Z' // Object.is equality

Expected: "1970-01-01T00:20:00.000Z"
Received: "1970-01-01T01:00:00.000Z"

With the fix:

$ vp test run src/provider/ProviderInstanceRegistry.test.ts
 Test Files  1 passed (1)
      Tests  28 passed (28)

vp run check:changed: format, lint, knip, graphql, typecheck (3 packages) and tests (3 files) all pass.

I checked the Claude Code 2.1.326 binary for this behaviour. Its usage read serves the saved snapshot with source: "persisted" on a failed fetch, answers from a snapshot under 60 s old without asking the endpoint ("Usage read answered from a snapshot Ns old; endpoint not asked"), and skips rewriting the snapshot while it is under 60 s old.

Not verified: I did not reproduce a live 429 against a real login, so the end-to-end path through a real rate-limited CLI is untested.

Model: Claude Opus 5.5. Harness: Claude Code in T3 Code.

…its fetch time

Fails on main: the probe stamps the saved reading with the probe time.
While /api/oauth/usage is rate-limited, Claude Code answers get_usage from
the reading it saved in .claude.json and does not say so. The probe stamped
that reading with the probe time, so an hour-old reading showed as just
checked. Usage that matches the saved reading now takes its fetch time,
never later than the probe.
@github-actions github-actions Bot added vouch:unvouched Author is not yet trusted in the VOUCHED list. size:M 30-99 changed lines (additions + deletions). labels Nov 18, 2026
@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Claude capability probes now read saved usage data. When all probed utilization values match the saved values, the reported usage timestamp uses the saved record’s fetch time. The provider registry test checks this behavior and a later changed usage reading.

Changes: Claude usage timestamp handling

Layer / File(s) Summary
Read saved Claude usage
apps/server/src/provider/claudeResetCredits.ts
A new exported function reads optional cached usage data from the Claude account config. Read or decode failures return undefined.
Adjust matching probe timestamps
apps/server/src/provider/Drivers/ClaudeDriver.ts, apps/server/src/provider/ProviderInstanceRegistry.test.ts
Fresh capability probes read saved usage and use its fetch time when every probed utilization window matches. The test checks the saved timestamp for a matching reading and the current timestamp after the mocked usage changes.

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Bug fix

Possibly related issues

Suggested labels: bugfix

Suggested reviewers: justyannicc, juliusmarminge, macroscopeapp

Merge Risk: Minimal · up to 17e17

Cached Claude usage now keeps its real age instead of looking freshly checked. The change is small, falls back safely, and has test coverage. No merge-blocking risk is identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: Claude usage served from a saved reading now keeps its real fetch age instead of the probe time. The changeset matches this. The driver reads cachedUsageUtilization, a…
Description check Passed The description follows the template's sections: Problem, Change, Scope, and Verification. The Problem section explains the expected and actual behaviour, and the Change section explains the fix and w…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Claude usage served from a saved reading now keeps its real fetch age instead of the probe time. The changeset matches this. The driver reads cachedUsageUtilization, and when every window matches, usageCheckedAt is set to the saved fetchedAtMs capped at the probe time. A regression test covers this case. The title follows the conventional commit form and is specific enough for a teammate scanning history.

Full details: Description check

Explanation

The description follows the template's sections: Problem, Change, Scope, and Verification. The Problem section explains the expected and actual behaviour, and the Change section explains the fix and why the match check keeps it safe. The Scope section gives the issue reference (#38070) and states what is excluded. The Verification section gives a failing run before the fix, a passing run after it, and a note on what was not tested live. Minor gap: the template asks for the issue that shows the bug, and the PR links #38070 with a 'Refs' prefix. The description does not say whether that issue is the one that shows the bug, but it does reference it, so the gap is small.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 1cefbc4 and 17e172e.

Files selected for processing: 3

  • apps/server/src/provider/Drivers/ClaudeDriver.ts
  • apps/server/src/provider/ProviderInstanceRegistry.test.ts
  • apps/server/src/provider/claudeResetCredits.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M 30-99 changed lines (additions + deletions). vouch:unvouched Author is not yet trusted in the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant