Skip to content

Commit 7ca3883

Browse files
committed
fix: invert utilization values — API returns remaining, not used
The OAuth usage endpoint returns utilization as percentage remaining, not percentage used. Claude.ai showing 10% used corresponded to utilization=90 from the API.
1 parent 7e2d356 commit 7ca3883

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

open-sse/services/usage.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,36 +445,45 @@ async function getClaudeUsage(accessToken) {
445445
const quotas: Record<string, any> = {};
446446

447447
// Parse five_hour window (session limit)
448+
// utilization = percentage remaining, not used
448449
if (data.five_hour !== undefined) {
450+
const remaining = data.five_hour.utilization ?? 0;
449451
quotas["session (5h)"] = {
450-
used: data.five_hour.utilization ?? 0,
452+
used: 100 - remaining,
451453
total: 100,
452454
resetAt: data.five_hour.resets_at || null,
453-
remainingPercentage: 100 - (data.five_hour.utilization ?? 0),
455+
remainingPercentage: remaining,
454456
unlimited: false,
455457
};
456458
}
457459

458460
// Parse seven_day window (weekly limit)
459461
if (data.seven_day !== undefined) {
462+
const remaining = data.seven_day.utilization ?? 0;
460463
quotas["weekly (7d)"] = {
461-
used: data.seven_day.utilization ?? 0,
464+
used: 100 - remaining,
462465
total: 100,
463466
resetAt: data.seven_day.resets_at || null,
464-
remainingPercentage: 100 - (data.seven_day.utilization ?? 0),
467+
remainingPercentage: remaining,
465468
unlimited: false,
466469
};
467470
}
468471

469472
// Parse model-specific weekly windows (e.g., seven_day_sonnet, seven_day_opus)
470473
for (const [key, value] of Object.entries(data)) {
471-
if (key.startsWith("seven_day_") && key !== "seven_day" && value && typeof value === "object") {
474+
if (
475+
key.startsWith("seven_day_") &&
476+
key !== "seven_day" &&
477+
value &&
478+
typeof value === "object"
479+
) {
472480
const modelName = key.replace("seven_day_", "");
481+
const remaining = (value as any).utilization ?? 0;
473482
quotas[`weekly ${modelName} (7d)`] = {
474-
used: (value as any).utilization ?? 0,
483+
used: 100 - remaining,
475484
total: 100,
476485
resetAt: (value as any).resets_at || null,
477-
remainingPercentage: 100 - ((value as any).utilization ?? 0),
486+
remainingPercentage: remaining,
478487
unlimited: false,
479488
};
480489
}

0 commit comments

Comments
 (0)