Skip to content

Commit b79b0f0

Browse files
author
Derek Trimm
committed
fix(usage): scale the streamed Claude utilization from a 0-1 fraction
The streamed rate_limit_event mirrors the anthropic-ratelimit-unified-* -utilization response headers, which are a 0-1 fraction, while the usage control response reports 0-100. Both converters clamped without scaling, so the binding window - the only one the stream names - rendered at 1/100th (0.87 shown as 1% on a week the account flags as warning). Measured on one account at one moment: header 0.11, usage endpoint 12.0. Reported by Moinax on #5739.
1 parent 03650c2 commit b79b0f0

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const claudeUsagePayload = (fiveHour: number, weekly: number) => ({
3838
},
3939
});
4040

41-
/** The streamed single-window Claude event. */
41+
/** The streamed single-window Claude event. `utilization` is a 0-1 fraction. */
4242
const claudeWindowPayload = (utilization: number) => ({
4343
type: "rate_limit_event",
4444
rate_limit_info: {
@@ -167,7 +167,7 @@ it.layer(NodeServices.layer)("account limits service", (it) => {
167167
});
168168
yield* service.ingest({
169169
provider: "claudeAgent",
170-
payload: claudeWindowPayload(87.5),
170+
payload: claudeWindowPayload(0.875),
171171
createdAt: "2026-08-15T12:00:01.000Z",
172172
providerInstanceId: asInstanceId("claude_partner"),
173173
});

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ describe("claudeUsageSnapshotFromUnknown", () => {
9898
});
9999

100100
describe("claudeWindowFromRateLimitEvent", () => {
101-
it("maps the binding window with a unix-seconds reset", () => {
101+
it("maps the binding window with a unix-seconds reset, scaling the 0-1 utilization", () => {
102+
// The streamed field carries the response header's fraction, not the
103+
// usage endpoint's percent: 0.875 here is the 87.5 the endpoint reports.
102104
const window = claudeWindowFromRateLimitEvent({
103105
type: "rate_limit_event",
104106
rate_limit_info: {
105107
status: "allowed_warning",
106108
rateLimitType: "five_hour",
107-
utilization: 87.5,
109+
utilization: 0.875,
108110
resetsAt: 1_786_600_800,
109111
},
110112
});
@@ -120,7 +122,7 @@ describe("claudeWindowFromRateLimitEvent", () => {
120122
it("drops hidden window types", () => {
121123
expect(
122124
claudeWindowFromRateLimitEvent({
123-
rate_limit_info: { rateLimitType: "seven_day_opus", utilization: 90 },
125+
rate_limit_info: { rateLimitType: "seven_day_opus", utilization: 0.9 },
124126
}),
125127
).toBeNull();
126128
});

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ function claudeWindowFromLimitEntry(entry: unknown): AccountLimitsWindow | null
234234
* Parses the streamed `rate_limit_event` SDK message into the one window it
235235
* names. Returns null for shapes that are not that message, and for windows
236236
* we hide.
237+
*
238+
* Units differ from the usage response: `rate_limit_info.utilization` mirrors
239+
* the `anthropic-ratelimit-unified-*-utilization` response headers, which are
240+
* a 0-1 fraction (`0.11` while the usage endpoint reports `12.0` for the same
241+
* window at the same moment), so it is scaled here. Passing it through would
242+
* show the binding window - the only one this message names - at 1/100th.
237243
*/
238244
export function claudeWindowFromRateLimitEvent(value: unknown): AccountLimitsWindow | null {
239245
if (!isRecord(value)) return null;
@@ -248,7 +254,7 @@ export function claudeWindowFromRateLimitEvent(value: unknown): AccountLimitsWin
248254
return {
249255
id: meta.id,
250256
label: meta.label,
251-
usedPercent: utilization === null ? 0 : clampPercent(utilization),
257+
usedPercent: utilization === null ? 0 : clampPercent(utilization * 100),
252258
resetsAt: resetsAt === null ? null : isoFromUnixSeconds(resetsAt),
253259
windowMinutes: meta.minutes,
254260
};

0 commit comments

Comments
 (0)