Skip to content

feat(server): Claude usage and resets through a matching usage source - #42

Merged
orbitingflea merged 10 commits into
mainfrom
feat/claude-usage-via-usage-source
Nov 1, 2026
Merged

orbitingflea merged 10 commits into
mainfrom
feat/claude-usage-via-usage-source

Conversation

@orbitingflea

@orbitingflea orbitingflea commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

T3 reads Claude plan usage and reset credits, and redeems resets, by calling Anthropic directly for a login signed in on this machine. When the same login is also held by a usage source, those requests should go through the source instead. Separately, Claude accounts listed by a usage source showed usage but never their reset credits and could not redeem one.

Directly signed-in Claude logins (ClaudeDriver)

  • T3 reads the login's email and organization from the Claude config, then looks through the enabled usage sources for an account with provider: "claude", the same email (case-insensitive) and the same organization_uuid, not disabled. The first source with exactly one such account wins.
  • On a match, usage (oauth/usage), the reset-credit read and the reset claim (organizations/<org>/reset_rate_limits) go through that source's api-call; a successful claim then calls reset-quota for that account. A source error shows as unavailable usage or a failed claim, with no fallback to a direct request.
  • If an enabled source cannot list its accounts, the lookup stops there: usage shows as unavailable and a claim fails, with no direct request and no try of later sources. T3 cannot tell whether that source holds the login, so it does not guess.
  • When every enabled source answers and none holds the login, behaviour is unchanged by default: T3 connects directly. With T3CODE_CLAUDE_USAGE_SOURCE_ONLY=1 on the server, an unmatched login is never read or redeemed directly; its usage shows as unavailable with "No usage source holds this Claude login." The whiteboard launch script (fork/whiteboard/run-t3.sh) turns it on.

Usage source Claude accounts

  • The usage read now includes reset credits. It asks for oauth/usage?cedar_ember=1 without skip_spend, so monthly spend still arrives for upstream's spend row; a login held by a source passes spend and extra usage on too, as a direct login does.
  • A claim goes to the account's own organization through api-call, then clears its cooldown, as Codex source accounts already do.
  • Each claim gets its own request id, since a Claude grant can hold several resets.
  • Requests through a source send the same URL, body and auth headers as the direct path, with no hard-coded Claude Code user agent; the source sets its own client headers. A claim sends Content-Type: application/json, as the direct path and Codex source accounts do.
  • A redeem through a source waits 30 seconds for its answer; reads keep 15 seconds.
  • The source path reuses the native claim constants, response schema and error type, and maps answers the same way (429, 401/403, cooldown, unavailable).

What a source must report in auth-files for a login to match: provider, email, organization_uuid, auth_index, and disabled when it is. Without organization_uuid no login matches, so T3 uses the direct path (or shows unavailable usage with T3CODE_CLAUDE_USAGE_SOURCE_ONLY=1).

Layout
The logic lives in fork-only files under apps/server/src/fork/claudeUsageSource/: claudeHub.ts (Claude through a hub's management API) and claudeLoginSource.ts (routing a directly signed-in login, and T3CODE_CLAUDE_USAGE_SOURCE_ONLY). Upstream files keep only the hooks that call it: cliproxyApi.ts (+10/−8), ClaudeDriver.ts (+7/−4), claudeResetCredits.ts (six exports), ClaudeProvider.ts (+2/−1) and two lines in cliproxyApi.test.ts.

Tests

  • fork/claudeUsageSource/claudeHub.test.ts: login matching, usage and credits through the source, claims and their outcomes with a JSON content type, a fresh request id per claim, a redeem that answers after 29 s, unreadable sources. cliproxyApi.test.ts checks no user agent is sent.
  • fork/claudeUsageSource/claudeLoginSource.test.ts: the Claude driver against a fake source (matched usage with monthly spend, redeem, a source that cannot list accounts shows unavailable without a direct request), the direct path when nothing matches, and no direct request when nothing matches and T3CODE_CLAUDE_USAGE_SOURCE_ONLY=1.
  • The previous revision's cliproxyApi.test.ts and ProviderInstanceRegistry.test.ts also pass unchanged against this layout (18 and 24 tests).
  • vp run check:changed passes (format, lint, knip, typecheck, tests).

Supersedes #39.

🤖 Generated with Claude Code

orbitingflea and others added 3 commits October 31, 2026 22:57
Claude accounts listed by a usage source showed usage but never their
reset credits, and could not redeem one, while native Claude logins and
Codex source accounts could.

The source's Claude usage read now asks for the cedar_ember block and
maps it with claudeResetCreditsToContract. Redemption posts the claim
through the source's api-call to the account's own organization
(organization_uuid from auth-files), with a request id derived from
account and grant so retries deduplicate, then clears the source
cooldown as for Codex. The native and source paths share claim
validation and answer mapping, so 429, 401/403, cooldown and
unavailable read the same on both. The source's Claude user agent is
now the CLI's, which Anthropic requires for reset eligibility.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… usage source

A Claude login signed in on this machine may also be held by an enabled
usage source. When exactly one account in a source has the same email
and organization, plan usage, reset credits and reset claims now go
through that source's api-call, and a successful claim clears the
account's cooldown there. Source failures show as unavailable usage or a
failed claim instead of falling back to a direct request. Logins no
source holds keep reading and redeeming directly, as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A Claude grant can hold several resets. Claims for a usage source account
derived the request id from the account and grant, so a second claim on the
same grant repeated the first and never spent the next reset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 31, 2026 23:23

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

orbitingflea and others added 7 commits October 31, 2026 23:27
Claude requests through a usage source no longer carry a hard-coded
Claude Code user agent; the source sets its own client headers. A
redeem through a source now waits 30 seconds for the source's answer,
since it includes the source's own upstream call; reads keep 15 seconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With T3CODE_CLAUDE_USAGE_SOURCE_ONLY=1, a Claude login that no usage source
holds is never read or redeemed directly: its usage shows as unavailable with
"No usage source holds this Claude login." Off by default, so an unmatched
login keeps reading Claude directly. The whiteboard launch script turns it on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude reads and resets through a usage source now live in
apps/server/src/fork/claudeUsageSource/, with their tests beside them.
Upstream files keep only the hooks that call into it, so upstream merges
touch as little as possible. Behaviour is unchanged: the PR's original
hub and registry tests pass against the new layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… type

The direct claim and the Codex source path both send
Content-Type: application/json; the Claude source path did not, so a hub
that forwards request headers could send the claim body untyped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Upstream now maps Claude monthly spend from usage reads. The source read
asked the usage endpoint to skip spend and dropped spend and extra usage
from the answer, so hub accounts and logins held by a source lost the
Monthly spend row that direct logins show.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@orbitingflea
orbitingflea merged commit 6ea9b1d into main Nov 1, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants