From 808a2ea7c9ba7af2030d94d10982b5c33ab81d24 Mon Sep 17 00:00:00 2001 From: WhiteEagle-12 Date: Mon, 16 Nov 2026 14:43:57 -0700 Subject: [PATCH 1/4] fix(usage): pooled Claude limits weight Pro, Max and Ultra by plan size Claude capacity keys were "Pro"/"Max"/"Ultra", but the provider reports "Claude Pro Subscription" and the CLIProxyAPI hub reported a bare "Claude Subscription", so Claude pools always counted accounts equally. Key capacities by the reported labels, read the hub account's tier from the OAuth profile, and use the current pricing multipliers: Max 5x Pro per 5 hours and week, Ultra 10x Pro per week. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/cliproxyApi.ts | 44 +++++++++++++++++++++++-- packages/shared/src/usageLimits.test.ts | 39 ++++++++++++++++++++++ packages/shared/src/usageLimits.ts | 13 +++++--- 3 files changed, 89 insertions(+), 7 deletions(-) diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 537a2b436f4a4..842dd0ea1fa1a 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -106,6 +106,16 @@ const ClaudeUsage = Schema.Struct({ ), ), }); +const ClaudeProfile = Schema.Struct({ + organization: Schema.optional( + Schema.NullOr( + Schema.Struct({ + organization_type: Schema.optional(Schema.NullOr(Schema.String)), + rate_limit_tier: Schema.optional(Schema.NullOr(Schema.String)), + }), + ), + ), +}); const CreditList = Schema.Struct({ credits: Schema.Array( Schema.Struct({ @@ -123,6 +133,25 @@ const decodeApiResponse = Schema.decodeUnknownEffect(ApiResponse); const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(CreditList)); const decodeClaudeUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeUsage)); const decodeCodexUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(CodexUsage)); +const decodeClaudeProfile = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeProfile)); + +/** Names the tier the way the native Claude provider does, so pools weight both alike. */ +function claudeHubPlanLabel(profile: typeof ClaudeProfile.Type | undefined): string { + const organization = profile?.organization; + if (organization?.rate_limit_tier?.includes("max_20x")) return "Claude Ultra Subscription"; + switch (organization?.organization_type) { + case "claude_max": + return "Claude Max Subscription"; + case "claude_pro": + return "Claude Pro Subscription"; + case "claude_team": + return "Claude Team Subscription"; + case "claude_enterprise": + return "Claude Enterprise Subscription"; + default: + return "Claude Subscription"; + } +} const decodeGrokUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(GrokUsageResponse)); const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); const isUsageLimitSourceError = Schema.is(UsageLimitSourceError); @@ -309,9 +338,18 @@ const HUB_PROVIDERS = new Map([ "User-Agent": "claude-code/2.1.273", }), read: Effect.fnUntraced(function* ({ account, call, checkedAt }) { - const usage = yield* decodeClaudeUsage( - yield* call("https://api.anthropic.com/api/oauth/usage"), + const [body, profile] = yield* Effect.all( + [ + call("https://api.anthropic.com/api/oauth/usage"), + // The tier only weights pools, so a failed read keeps the usage. + call("https://api.anthropic.com/api/oauth/profile").pipe( + Effect.flatMap(decodeClaudeProfile), + Effect.orElseSucceed(() => undefined), + ), + ], + { concurrency: 2 }, ); + const usage = yield* decodeClaudeUsage(body); const model_scoped = (usage.limits ?? []).flatMap((limit) => limit.kind === "weekly_scoped" && limit.scope?.model && typeof limit.percent === "number" ? [ @@ -332,7 +370,7 @@ const HUB_PROVIDERS = new Map([ }; return { ...listedIdentity(account), - plan: "Claude Subscription", + plan: claudeHubPlanLabel(profile), usageLimits: claudeUsageResponseToLimits({ checkedAt, response: { rate_limits_available: true, rate_limits: rateLimits }, diff --git a/packages/shared/src/usageLimits.test.ts b/packages/shared/src/usageLimits.test.ts index b66001ca19244..05d3c224e1c5e 100644 --- a/packages/shared/src/usageLimits.test.ts +++ b/packages/shared/src/usageLimits.test.ts @@ -1018,6 +1018,45 @@ describe("pools", () => { ).toContainEqual(["pro@example.com", 95]); }); + it("weights pooled Claude weekly quota by the plan labels the provider reports", () => { + const [pool] = collectLimitPools( + collectLimitAccounts( + new Map([ + [ + EnvironmentId.make("env-a"), + { + ...laptop, + serverConfig: { + providers: [], + usageLimitSources: [ + { + ...source, + accounts: [ + ["ultra", "Claude Ultra Subscription", 100], + ["pro", "Claude Pro Subscription", 0], + ].map(([id, plan, usedPercent]) => ({ + id: String(id), + email: `${id}@example.com`, + driver: claude, + plan: String(plan), + usageLimits: { + checkedAt, + windows: [{ ...weekly, usedPercent: Number(usedPercent) }], + }, + })), + }, + ], + }, + }, + ], + ]), + ), + now, + ); + // Ultra holds 10 of the pool's 11 capacity parts. + expect(pool?.windows[0]?.remainingPercent).toBe(9); + }); + it("pools windows by id across accounts and orders resets by when they land", () => { const input = new Map([ [ diff --git a/packages/shared/src/usageLimits.ts b/packages/shared/src/usageLimits.ts index 6fe6c70a89a4f..c911002a117d1 100644 --- a/packages/shared/src/usageLimits.ts +++ b/packages/shared/src/usageLimits.ts @@ -568,16 +568,21 @@ const CODEX_PLAN_CAPACITY: Record = { "ChatGPT Pro 100 Subscription": 5, "ChatGPT Pro 200 Subscription": 20, }; -// Claude's published plan multipliers describe the session allowance only. -const CLAUDE_SESSION_CAPACITY: Record = { Pro: 1, Max: 5, Ultra: 20 }; +// Claude's pricing page: Max is 5x Pro every 5 hours and week; Ultra is 10x Pro weekly with no 5-hour limit. +// Keyed by the auth label the Claude provider and hubs report. +const CLAUDE_PLAN_CAPACITY: Record = { + "Claude Pro Subscription": 1, + "Claude Max Subscription": 5, + "Claude Ultra Subscription": 10, +}; function planCapacity(account: LimitAccount, kind: ServerProviderUsageWindow["kind"]) { if (!account.plan) return undefined; if (account.driver === "codex" && (kind === "session" || kind === "weekly")) { return CODEX_PLAN_CAPACITY[account.plan]; } - if (account.driver === "claudeAgent" && kind === "session") { - return CLAUDE_SESSION_CAPACITY[account.plan]; + if (account.driver === "claudeAgent" && (kind === "session" || kind === "weekly")) { + return CLAUDE_PLAN_CAPACITY[account.plan]; } return undefined; } From 37b049897e4f3fecfd2a6a5fe8e6a5b52e4cb170 Mon Sep 17 00:00:00 2001 From: WhiteEagle-12 Date: Mon, 16 Nov 2026 14:57:07 -0700 Subject: [PATCH 2/4] fix(usage): bound the optional Claude profile read so usage refreshes in time Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/cliproxyApi.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index 842dd0ea1fa1a..f5e168bad68d4 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -344,6 +344,7 @@ const HUB_PROVIDERS = new Map([ // The tier only weights pools, so a failed read keeps the usage. call("https://api.anthropic.com/api/oauth/profile").pipe( Effect.flatMap(decodeClaudeProfile), + Effect.timeout("3 seconds"), Effect.orElseSucceed(() => undefined), ), ], From b7daf4539856b2e595ddfcbb009e8d1d547c935e Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 19:02:48 +0000 Subject: [PATCH 3/4] test(usage): cover hub Claude plan labels read from the profile Keep the decoder constants together and pin the profile mapping (Ultra by rate-limit tier, Pro by organization type, and the fallback when the profile read fails) next to the existing hub Claude cases. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/cliproxyApi.test.ts | 33 +++++++++++++++++++++++ apps/server/src/usage/cliproxyApi.ts | 6 ++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 5002d2223d1ba..344d45a144f21 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -373,6 +373,39 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect.each([ + { + profile: { + status: 200, + body: { + organization: { + organization_type: "claude_max", + rate_limit_tier: "default_claude_max_20x", + }, + }, + }, + plan: "Claude Ultra Subscription", + }, + { + profile: { status: 200, body: { organization: { organization_type: "claude_pro" } } }, + plan: "Claude Pro Subscription", + }, + { profile: { status: 500, body: {} }, plan: "Claude Subscription" }, + ])("labels a hub Claude account $plan from its profile", ({ profile, plan }) => + Effect.gen(function* () { + const test = fixture({ + accounts: [{ ...accounts[0]!, provider: "claude" }], + upstream: (request) => + request.url?.endsWith("/api/oauth/profile") + ? profile + : { status: 200, body: { seven_day: { utilization: 50, resets_at: null } } }, + }); + const result = yield* (yield* test.api).readAccounts(config); + expect(result[0]?.plan).toBe(plan); + expect(result[0]?.usageLimits.windows.map((window) => window.id)).toEqual(["seven_day"]); + }), + ); + it.effect.each([ { name: "spend budget", diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index f5e168bad68d4..d9c436161f21d 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -134,6 +134,9 @@ const decodeCreditList = Schema.decodeUnknownEffect(Schema.fromJsonString(Credit const decodeClaudeUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeUsage)); const decodeCodexUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(CodexUsage)); const decodeClaudeProfile = Schema.decodeUnknownEffect(Schema.fromJsonString(ClaudeProfile)); +const decodeGrokUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(GrokUsageResponse)); +const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); +const isUsageLimitSourceError = Schema.is(UsageLimitSourceError); /** Names the tier the way the native Claude provider does, so pools weight both alike. */ function claudeHubPlanLabel(profile: typeof ClaudeProfile.Type | undefined): string { @@ -152,9 +155,6 @@ function claudeHubPlanLabel(profile: typeof ClaudeProfile.Type | undefined): str return "Claude Subscription"; } } -const decodeGrokUsage = Schema.decodeUnknownEffect(Schema.fromJsonString(GrokUsageResponse)); -const decodeJson = Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Unknown)); -const isUsageLimitSourceError = Schema.is(UsageLimitSourceError); // Extra CAs can fix untrusted chains; expiry/hostname failures report codes only. const UNTRUSTED_CERTIFICATE_CODES = new Set([ From 78430a3cfa6c85a9f8b0ae93d92241d10219c0a5 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 19:45:17 +0000 Subject: [PATCH 4/4] fix(usage): share one profile budget across a hub refresh Each Claude profile read had its own 3s timeout, but accounts are read four at a time, so five stalled profiles took two timeout batches and outlasted the usage-limit reset fallback's 5s deadline. Optional reads now share one deadline per refresh, and are skipped once it has passed. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/usage/cliproxyApi.test.ts | 55 ++++++++++++++++++----- apps/server/src/usage/cliproxyApi.ts | 31 ++++++++++--- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts index 344d45a144f21..0f1570f182bc6 100644 --- a/apps/server/src/usage/cliproxyApi.test.ts +++ b/apps/server/src/usage/cliproxyApi.test.ts @@ -58,13 +58,13 @@ function fixture( project_id?: string; id_token?: { chatgpt_account_id: string; plan_type?: string; chatgpt_plan_type?: string }; }>; - upstream?: (request: RequestBody) => { status: number; body: unknown }; + upstream?: (request: RequestBody) => { status: number; body: unknown } | "stall"; cooldownStatus?: number; } = {}, ) { const requests: Array<{ path: string; body?: RequestBody }> = []; const http = HttpClient.make((request) => - Effect.sync(() => { + Effect.suspend(() => { expect(request.headers.authorization).toBe("Bearer management-secret"); const path = new URL(request.url).pathname; const body = @@ -73,14 +73,18 @@ function fixture( : undefined; requests.push({ path, ...(body ? { body } : {}) }); if (path.endsWith("/auth-files")) - return HttpClientResponse.fromWeb( - request, - Response.json({ files: options.accounts ?? accounts }), + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({ files: options.accounts ?? accounts }), + ), ); if (path.endsWith("/reset-quota")) - return HttpClientResponse.fromWeb( - request, - Response.json({}, { status: options.cooldownStatus ?? 200 }), + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({}, { status: options.cooldownStatus ?? 200 }), + ), ); expect(path).toMatch(/\/v0\/management\/api-call$/); expect(body?.header?.Authorization).toBe("Bearer $TOKEN$"); @@ -108,9 +112,12 @@ function fixture( }, }, }; - return HttpClientResponse.fromWeb( - request, - Response.json({ status_code: upstream.status, body: encodeJson(upstream.body) }), + if (upstream === "stall") return Effect.never; + return Effect.succeed( + HttpClientResponse.fromWeb( + request, + Response.json({ status_code: upstream.status, body: encodeJson(upstream.body) }), + ), ); }), ); @@ -406,6 +413,32 @@ describe("CLIProxyAPI built-in management API", () => { }), ); + it.effect("bounds stalled Claude profile reads across the whole refresh", () => + Effect.gen(function* () { + const claude = Array.from({ length: 5 }, (_, index) => ({ + ...accounts[0]!, + id: `claude-${index}.json`, + auth_index: `c${index}`, + provider: "claude", + })); + const test = fixture({ + accounts: claude, + upstream: (request) => + request.url?.endsWith("/api/oauth/profile") + ? "stall" + : { status: 200, body: { seven_day: { utilization: 100, resets_at: null } } }, + }); + const api = yield* test.api; + const reading = yield* Effect.forkChild(api.readAccounts(config)); + yield* TestClock.adjust("3 seconds"); + const exit = reading.pollUnsafe(); + expect(exit?._tag).toBe("Success"); + expect( + exit?._tag === "Success" ? exit.value.map((account) => account.plan) : undefined, + ).toEqual(Array.from({ length: 5 }, () => "Claude Subscription")); + }), + ); + it.effect.each([ { name: "spend budget", diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts index d9c436161f21d..f827d4261e82c 100644 --- a/apps/server/src/usage/cliproxyApi.ts +++ b/apps/server/src/usage/cliproxyApi.ts @@ -228,6 +228,8 @@ type HubCall = ( url: string, data?: unknown, ) => Effect.Effect; +/** Runs an optional request, or yields undefined once the refresh's shared budget is spent. */ +type HubOptional = (effect: Effect.Effect) => Effect.Effect; type HubReading = Omit; interface HubProvider { readonly driver: ProviderDriverKind; @@ -239,6 +241,7 @@ interface HubProvider { readonly read: (input: { readonly account: AuthFile; readonly call: HubCall; + readonly optional: HubOptional; readonly checkedAt: string; }) => Effect.Effect; } @@ -337,15 +340,15 @@ const HUB_PROVIDERS = new Map([ // The OAuth usage endpoint can return 429 without a Claude Code user agent. "User-Agent": "claude-code/2.1.273", }), - read: Effect.fnUntraced(function* ({ account, call, checkedAt }) { + read: Effect.fnUntraced(function* ({ account, call, optional, checkedAt }) { const [body, profile] = yield* Effect.all( [ call("https://api.anthropic.com/api/oauth/usage"), // The tier only weights pools, so a failed read keeps the usage. - call("https://api.anthropic.com/api/oauth/profile").pipe( - Effect.flatMap(decodeClaudeProfile), - Effect.timeout("3 seconds"), - Effect.orElseSucceed(() => undefined), + optional( + call("https://api.anthropic.com/api/oauth/profile").pipe( + Effect.flatMap(decodeClaudeProfile), + ), ), ], { concurrency: 2 }, @@ -505,11 +508,22 @@ export const makeCliproxyApi = Effect.gen(function* () { hub: CliproxyHub, account: AuthFile, provider: HubProvider, + optionalUntil: number, ) { const checkedAt = DateTime.formatIso(yield* DateTime.now); const call: HubCall = (url, data) => apiCall(hub, account, provider.header(account, platform, architecture), url, data); - const reading = yield* provider.read({ account, call, checkedAt }).pipe( + const optional: HubOptional = Effect.fn("CliproxyApi.optional")(function* ( + effect: Effect.Effect, + ) { + const left = optionalUntil - DateTime.toEpochMillis(yield* DateTime.now); + if (left <= 0) return undefined; + return yield* effect.pipe( + Effect.timeout(left), + Effect.orElseSucceed(() => undefined), + ); + }); + const reading = yield* provider.read({ account, call, optional, checkedAt }).pipe( Effect.provideService(Digest.Digest, digest), Effect.orElseSucceed(() => ({ ...listedIdentity(account), @@ -533,12 +547,15 @@ export const makeCliproxyApi = Effect.gen(function* () { : new UsageLimitSourceError({ detail: "The hub could not list accounts." }), ), ); + // One budget for the whole refresh: per-account timeouts would stack across batches + // and outlast the usage-limit reset fallback's deadline. + const optionalUntil = DateTime.toEpochMillis(yield* DateTime.now) + 3_000; return yield* Effect.forEach( accounts.flatMap((account) => { const provider = HUB_PROVIDERS.get(account.provider); return provider && !account.disabled ? [{ account, provider }] : []; }), - ({ account, provider }) => readAccount(hub, account, provider), + ({ account, provider }) => readAccount(hub, account, provider, optionalUntil), { concurrency: 4 }, ); });