Skip to content

Commit d52ee1b

Browse files
authored
fix(usage): preserve Claude monthly spend through CLIProxy (#23435)
1 parent ec6020e commit d52ee1b

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

‎apps/server/src/usage/cliproxyApi.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,70 @@ describe("CLIProxyAPI built-in management API", () => {
345345
}),
346346
);
347347

348+
it.effect.each([
349+
{
350+
name: "spend budget",
351+
budget: {
352+
spend: {
353+
enabled: true,
354+
used: { amount_minor: 4_631, currency: "USD", exponent: 2 },
355+
limit: { amount_minor: 50_000, currency: "USD", exponent: 2 },
356+
},
357+
},
358+
},
359+
{
360+
name: "extra usage",
361+
budget: {
362+
extra_usage: {
363+
is_enabled: true,
364+
used_credits: 4_631,
365+
monthly_limit: 50_000,
366+
utilization: 9,
367+
},
368+
},
369+
},
370+
])("keeps Claude monthly spend from $name beside rolling windows", ({ budget }) =>
371+
Effect.gen(function* () {
372+
const test = fixture({
373+
accounts: [{ ...accounts[0]!, provider: "claude" }],
374+
upstream: () => ({
375+
status: 200,
376+
body: { five_hour: { utilization: 10, resets_at: null }, ...budget },
377+
}),
378+
});
379+
const result = yield* (yield* test.api).readAccounts(config);
380+
expect(
381+
result[0]?.usageLimits.windows.map((window) => [window.id, window.usedPercent]),
382+
).toEqual([
383+
["five_hour", 10],
384+
["monthly_spend", expect.closeTo(9.262, 10)],
385+
]);
386+
}),
387+
);
388+
389+
it.effect("omits disabled Claude budgets without losing rolling windows", () =>
390+
Effect.gen(function* () {
391+
const test = fixture({
392+
accounts: [{ ...accounts[0]!, provider: "claude" }],
393+
upstream: () => ({
394+
status: 200,
395+
body: {
396+
five_hour: { utilization: 10, resets_at: null },
397+
spend: { enabled: false, used: null, limit: null },
398+
extra_usage: {
399+
is_enabled: false,
400+
used_credits: null,
401+
monthly_limit: null,
402+
utilization: null,
403+
},
404+
},
405+
}),
406+
});
407+
const result = yield* (yield* test.api).readAccounts(config);
408+
expect(result[0]?.usageLimits.windows.map((window) => window.id)).toEqual(["five_hour"]);
409+
}),
410+
);
411+
348412
it.effect("pins redemption to the displayed credit and clears only that account's cooldown", () =>
349413
Effect.gen(function* () {
350414
const test = fixture();

‎apps/server/src/usage/cliproxyApi.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ const ClaudeWindow = Schema.Struct({
5555
const ClaudeUsage = Schema.Struct({
5656
five_hour: Schema.optional(Schema.NullOr(ClaudeWindow)),
5757
seven_day: Schema.optional(Schema.NullOr(ClaudeWindow)),
58+
spend: Schema.optional(Schema.Unknown),
59+
extra_usage: Schema.optional(
60+
Schema.NullOr(
61+
Schema.Struct({
62+
is_enabled: Schema.Boolean,
63+
monthly_limit: Schema.NullOr(Schema.Number),
64+
used_credits: Schema.NullOr(Schema.Number),
65+
utilization: Schema.NullOr(Schema.Number),
66+
}),
67+
),
68+
),
5869
limits: Schema.optional(
5970
Schema.Array(
6071
Schema.Struct({
@@ -279,18 +290,21 @@ export const makeCliproxyApi = Effect.gen(function* () {
279290
]
280291
: [],
281292
);
293+
const rateLimits = {
294+
five_hour: usage.five_hour ?? null,
295+
seven_day: usage.seven_day ?? null,
296+
model_scoped,
297+
spend: usage.spend,
298+
extra_usage: usage.extra_usage ?? null,
299+
};
282300
return {
283301
...base,
284302
plan: "Claude Subscription",
285303
usageLimits: claudeUsageResponseToLimits({
286304
checkedAt,
287305
response: {
288306
rate_limits_available: true,
289-
rate_limits: {
290-
five_hour: usage.five_hour ?? null,
291-
seven_day: usage.seven_day ?? null,
292-
model_scoped,
293-
},
307+
rate_limits: rateLimits,
294308
},
295309
}).limits,
296310
};

0 commit comments

Comments
 (0)