GeezerVison is showing github.com — fix(usage): scale the streamed Claude utilization from a 0-1 fraction · derektrimm/t3code@b79b0f0
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/AccountLimitsService.test.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,7 +38,7 @@ const claudeUsagePayload = (fiveHour: number, weekly: number) => ({ | 38 | 38 | }, | 39 | 39 | }); | 40 | 40 |
| 41 | | -/** The streamed single-window Claude event. */ | | 41 | +/** The streamed single-window Claude event. `utilization` is a 0-1 fraction. */ | 42 | 42 | const claudeWindowPayload = (utilization: number) => ({ | 43 | 43 | type: "rate_limit_event", | 44 | 44 | rate_limit_info: { | @@ -167,7 +167,7 @@ it.layer(NodeServices.layer)("account limits service", (it) => { | 167 | 167 | }); | 168 | 168 | yield* service.ingest({ | 169 | 169 | provider: "claudeAgent", | 170 | | - payload: claudeWindowPayload(87.5), | | 170 | + payload: claudeWindowPayload(0.875), | 171 | 171 | createdAt: "2026-08-15T12:00:01.000Z", | 172 | 172 | providerInstanceId: asInstanceId("claude_partner"), | 173 | 173 | }); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/accountLimitsNormalize.test.ts +5-3Lines changed: 5 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -98,13 +98,15 @@ describe("claudeUsageSnapshotFromUnknown", () => { | 98 | 98 | }); | 99 | 99 |
| 100 | 100 | 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. | 102 | 104 | const window = claudeWindowFromRateLimitEvent({ | 103 | 105 | type: "rate_limit_event", | 104 | 106 | rate_limit_info: { | 105 | 107 | status: "allowed_warning", | 106 | 108 | rateLimitType: "five_hour", | 107 | | - utilization: 87.5, | | 109 | + utilization: 0.875, | 108 | 110 | resetsAt: 1_786_600_800, | 109 | 111 | }, | 110 | 112 | }); | @@ -120,7 +122,7 @@ describe("claudeWindowFromRateLimitEvent", () => { | 120 | 122 | it("drops hidden window types", () => { | 121 | 123 | expect( | 122 | 124 | claudeWindowFromRateLimitEvent({ | 123 | | - rate_limit_info: { rateLimitType: "seven_day_opus", utilization: 90 }, | | 125 | + rate_limit_info: { rateLimitType: "seven_day_opus", utilization: 0.9 }, | 124 | 126 | }), | 125 | 127 | ).toBeNull(); | 126 | 128 | }); | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/usage/accountLimitsNormalize.ts +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -234,6 +234,12 @@ function claudeWindowFromLimitEntry(entry: unknown): AccountLimitsWindow | null | 234 | 234 | * Parses the streamed `rate_limit_event` SDK message into the one window it | 235 | 235 | * names. Returns null for shapes that are not that message, and for windows | 236 | 236 | * 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. | 237 | 243 | */ | 238 | 244 | export function claudeWindowFromRateLimitEvent(value: unknown): AccountLimitsWindow | null { | 239 | 245 | if (!isRecord(value)) return null; | @@ -248,7 +254,7 @@ export function claudeWindowFromRateLimitEvent(value: unknown): AccountLimitsWin | 248 | 254 | return { | 249 | 255 | id: meta.id, | 250 | 256 | label: meta.label, | 251 | | - usedPercent: utilization === null ? 0 : clampPercent(utilization), | | 257 | + usedPercent: utilization === null ? 0 : clampPercent(utilization * 100), | 252 | 258 | resetsAt: resetsAt === null ? null : isoFromUnixSeconds(resetsAt), | 253 | 259 | windowMinutes: meta.minutes, | 254 | 260 | }; | |
|