Skip to content
TypeScript

fix(usage): preserve Claude monthly spend through CLIProxy (#23435)

Aditya Garudd52ee1b2376e
diff --git a/apps/server/src/usage/cliproxyApi.test.ts b/apps/server/src/usage/cliproxyApi.test.ts
index 0119adfd940..4a76d32c15d 100644
--- a/apps/server/src/usage/cliproxyApi.test.ts
+++ b/apps/server/src/usage/cliproxyApi.test.ts
@@ -345,6 +345,70 @@ describe("CLIProxyAPI built-in management API", () => {
}),
);
+ it.effect.each([
+ {
+ name: "spend budget",
+ budget: {
+ spend: {
+ enabled: true,
+ used: { amount_minor: 4_631, currency: "USD", exponent: 2 },
+ limit: { amount_minor: 50_000, currency: "USD", exponent: 2 },
+ },
+ },
+ },
+ {
+ name: "extra usage",
+ budget: {
+ extra_usage: {
+ is_enabled: true,
+ used_credits: 4_631,
+ monthly_limit: 50_000,
+ utilization: 9,
+ },
+ },
+ },
+ ])("keeps Claude monthly spend from $name beside rolling windows", ({ budget }) =>
+ Effect.gen(function* () {
+ const test = fixture({
+ accounts: [{ ...accounts[0]!, provider: "claude" }],
+ upstream: () => ({
+ status: 200,
+ body: { five_hour: { utilization: 10, resets_at: null }, ...budget },
+ }),
+ });
+ const result = yield* (yield* test.api).readAccounts(config);
+ expect(
+ result[0]?.usageLimits.windows.map((window) => [window.id, window.usedPercent]),
+ ).toEqual([
+ ["five_hour", 10],
+ ["monthly_spend", expect.closeTo(9.262, 10)],
+ ]);
+ }),
+ );
+
+ it.effect("omits disabled Claude budgets without losing rolling windows", () =>
+ Effect.gen(function* () {
+ const test = fixture({
+ accounts: [{ ...accounts[0]!, provider: "claude" }],
+ upstream: () => ({
+ status: 200,
+ body: {
+ five_hour: { utilization: 10, resets_at: null },
+ spend: { enabled: false, used: null, limit: null },
+ extra_usage: {
+ is_enabled: false,
+ used_credits: null,
+ monthly_limit: null,
+ utilization: null,
+ },
+ },
+ }),
+ });
+ const result = yield* (yield* test.api).readAccounts(config);
+ expect(result[0]?.usageLimits.windows.map((window) => window.id)).toEqual(["five_hour"]);
+ }),
+ );
+
it.effect("pins redemption to the displayed credit and clears only that account's cooldown", () =>
Effect.gen(function* () {
const test = fixture();
diff --git a/apps/server/src/usage/cliproxyApi.ts b/apps/server/src/usage/cliproxyApi.ts
index 91504d80022..eb956a67a31 100644
--- a/apps/server/src/usage/cliproxyApi.ts
+++ b/apps/server/src/usage/cliproxyApi.ts
@@ -55,6 +55,17 @@ const ClaudeWindow = Schema.Struct({
const ClaudeUsage = Schema.Struct({
five_hour: Schema.optional(Schema.NullOr(ClaudeWindow)),
seven_day: Schema.optional(Schema.NullOr(ClaudeWindow)),
+ spend: Schema.optional(Schema.Unknown),
+ extra_usage: Schema.optional(
+ Schema.NullOr(
+ Schema.Struct({
+ is_enabled: Schema.Boolean,
+ monthly_limit: Schema.NullOr(Schema.Number),
+ used_credits: Schema.NullOr(Schema.Number),
+ utilization: Schema.NullOr(Schema.Number),
+ }),
+ ),
+ ),
limits: Schema.optional(
Schema.Array(
Schema.Struct({
@@ -279,6 +290,13 @@ export const makeCliproxyApi = Effect.gen(function* () {
]
: [],
);
+ const rateLimits = {
+ five_hour: usage.five_hour ?? null,
+ seven_day: usage.seven_day ?? null,
+ model_scoped,
+ spend: usage.spend,
+ extra_usage: usage.extra_usage ?? null,
+ };
return {
...base,
plan: "Claude Subscription",
@@ -286,11 +304,7 @@ export const makeCliproxyApi = Effect.gen(function* () {
checkedAt,
response: {
rate_limits_available: true,
- rate_limits: {
- five_hour: usage.five_hour ?? null,
- seven_day: usage.seven_day ?? null,
- model_scoped,
- },
+ rate_limits: rateLimits,
},
}).limits,
};