Repository navigation
feat(server): Claude usage and resets through a matching usage source - #42
Merged
Merged
Conversation
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>
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>
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 join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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)provider: "claude", the same email (case-insensitive) and the sameorganization_uuid, not disabled. The first source with exactly one such account wins.oauth/usage), the reset-credit read and the reset claim (organizations/<org>/reset_rate_limits) go through that source'sapi-call; a successful claim then callsreset-quotafor that account. A source error shows as unavailable usage or a failed claim, with no fallback to a direct request.T3CODE_CLAUDE_USAGE_SOURCE_ONLY=1on 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
oauth/usage?cedar_ember=1withoutskip_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.api-call, then clears its cooldown, as Codex source accounts already do.Content-Type: application/json, as the direct path and Codex source accounts do.What a source must report in
auth-filesfor a login to match:provider,email,organization_uuid,auth_index, anddisabledwhen it is. Withoutorganization_uuidno login matches, so T3 uses the direct path (or shows unavailable usage withT3CODE_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) andclaudeLoginSource.ts(routing a directly signed-in login, andT3CODE_CLAUDE_USAGE_SOURCE_ONLY). Upstream files keep only the hooks that call it:cliproxyApi.ts(+10/−8),ClaudeDriver.ts(+7/−4),claudeResetCredits.ts(sixexports),ClaudeProvider.ts(+2/−1) and two lines incliproxyApi.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.tschecks 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 andT3CODE_CLAUDE_USAGE_SOURCE_ONLY=1.cliproxyApi.test.tsandProviderInstanceRegistry.test.tsalso pass unchanged against this layout (18 and 24 tests).vp run check:changedpasses (format, lint, knip, typecheck, tests).Supersedes #39.
🤖 Generated with Claude Code