The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 23:15 UTC
fix(claude): map monthly account spending limits (#19433)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
James C committed
Oct 20, 2026 at 05:28 UTC
fd8dd51654f855dbf4e23c457701f5088c59b689
2 files changed
+173
apps/server/src/provider/claudeUsageLimits.test.ts
+115
@@ -126,6 +126,121 @@ describe("claudeUsageResponseToLimits", () => {
126
},
127
]);
128
});
129
+
130
+ const enterpriseResponse = {
131
+ rate_limits_available: true,
132
+ rate_limits: {
133
+ five_hour: null,
134
+ seven_day: null,
135
+ spend: {
136
+ enabled: true,
137
+ used: { amount_minor: 4_631, currency: "USD", exponent: 2 },
138
+ limit: { amount_minor: 50_000, currency: "USD", exponent: 2 },
139
+ percent: 9,
140
+ },
141
+ extra_usage: {
142
+ is_enabled: true,
143
+ used_credits: 4_631,
144
+ monthly_limit: 50_000,
145
+ utilization: 9.261999999999999,
146
+ currency: "USD",
147
+ decimal_places: 2,
148
+ },
149
+ },
150
+ };
151
+ const monthlyWindow = {
152
+ id: "monthly_spend",
153
+ kind: "monthly",
154
+ label: "Monthly spend",
155
+ usedPercent: expect.closeTo(9.262, 10),
156
+ };
157
+
158
+ it("maps the captured Enterprise budget precisely, preferring spend beside rolling windows", () => {
159
+ expect(claudeUsageResponseToLimits({ checkedAt, response: enterpriseResponse }).limits).toEqual(
160
+ {
161
+ checkedAt,
162
+ windows: [monthlyWindow],
163
+ },
164
+ );
165
+ const response = {
166
+ ...enterpriseResponse,
167
+ rate_limits: {
168
+ ...enterpriseResponse.rate_limits,
169
+ five_hour: { utilization: 12, resets_at: null },
170
+ extra_usage: {
171
+ ...enterpriseResponse.rate_limits.extra_usage,
172
+ used_credits: 30_000,
173
+ utilization: 60,
174
+ },
175
+ },
176
+ };
177
+ expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([
178
+ {
179
+ id: "five_hour",
180
+ kind: "session",
181
+ label: "Session",
182
+ usedPercent: 12,
183
+ windowDurationMins: 300,
184
+ },
185
+ monthlyWindow,
186
+ ]);
187
+ });
188
+
189
+ it("falls back to enabled extra usage amounts or utilization when spend is unusable", () => {
190
+ const response = {
191
+ ...enterpriseResponse,
192
+ rate_limits: {
193
+ ...enterpriseResponse.rate_limits,
194
+ spend: {
195
+ ...enterpriseResponse.rate_limits.spend,
196
+ enabled: false,
197
+ used: { amount_minor: 30_000, currency: "USD", exponent: 2 },
198
+ },
199
+ extra_usage: { ...enterpriseResponse.rate_limits.extra_usage, utilization: 9 },
200
+ },
201
+ };
202
+ expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([
203
+ monthlyWindow,
204
+ ]);
205
+ const utilizationOnly = {
206
+ ...response,
207
+ rate_limits: {
208
+ ...response.rate_limits,
209
+ spend: { enabled: true, used: null, limit: null, percent: 9 },
210
+ extra_usage: {
211
+ ...enterpriseResponse.rate_limits.extra_usage,
212
+ used_credits: null,
213
+ monthly_limit: null,
214
+ },
215
+ },
216
+ };
217
+ expect(
218
+ claudeUsageResponseToLimits({ checkedAt, response: utilizationOnly }).limits.windows,
219
+ ).toEqual([monthlyWindow]);
220
+ });
221
+
222
+ it("omits disabled budgets and refuses invalid money without an enabled fallback", () => {
223
+ const rateLimits = enterpriseResponse.rate_limits;
224
+ const response = {
225
+ ...enterpriseResponse,
226
+ rate_limits: {
227
+ ...rateLimits,
228
+ spend: { ...rateLimits.spend, enabled: false },
229
+ extra_usage: { ...rateLimits.extra_usage, is_enabled: false },
230
+ },
231
+ };
232
+ expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
233
+ response.rate_limits.spend = {
234
+ ...rateLimits.spend,
235
+ limit: { ...rateLimits.spend.limit, currency: "EUR" },
236
+ };
237
+ expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
238
+ response.rate_limits.spend = {
239
+ ...rateLimits.spend,
240
+ limit: { ...rateLimits.spend.limit, amount_minor: 0 },
241
+ };
242
+ expect(claudeUsageResponseToLimits({ checkedAt, response }).limits.windows).toEqual([]);
243
+ });
244
});
245
246
describe("claudeRateLimitEventToUpdate", () => {
apps/server/src/provider/claudeUsageLimits.ts
+58
@@ -8,6 +8,7 @@ import * as DateTime from "effect/DateTime";
8
import * as Effect from "effect/Effect";
9
import * as Option from "effect/Option";
10
import * as Ref from "effect/Ref";
11
+import * as Schema from "effect/Schema";
12
13
import {
14
clampPercent,
@@ -34,6 +35,16 @@ const WINDOWS: Readonly<
35
*/
36
const OVERAGE_INCLUDED_EVENT_TYPE = "seven_day_overage_included";
37
38
+// The SDK does not yet type the spend budget returned by `get_usage`.
39
+const SpendMoney = Schema.Struct({
40
+ amount_minor: Schema.Natural,
41
+ currency: Schema.NonEmptyString,
42
+ exponent: Schema.Natural,
43
+});
44
+const decodeSpendBudget = Schema.decodeUnknownOption(
45
+ Schema.Struct({ enabled: Schema.Boolean, used: SpendMoney, limit: SpendMoney }),
46
+);
47
+
48
export interface ClaudeScopedLimitNames {
49
readonly overageIncluded: string | undefined;
50
}
@@ -61,6 +72,44 @@ function scopedWindow(
72
};
73
}
74
75
+function percentFromAmounts(used: number | null | undefined, limit: number | null | undefined) {
76
+ if (
77
+ typeof used !== "number" ||
78
+ typeof limit !== "number" ||
79
+ !Number.isFinite(used) ||
80
+ !Number.isFinite(limit) ||
81
+ used < 0 ||
82
+ limit <= 0
83
+ ) {
84
+ return undefined;
85
+ }
86
+ const percent = (used / limit) * 100;
87
+ return Number.isFinite(percent) ? clampPercent(percent) : undefined;
88
+}
89
+
90
+function readSpendPercent(rateLimits: NonNullable<SDKControlGetUsageResponse["rate_limits"]>) {
91
+ const spend = Option.getOrUndefined(
92
+ decodeSpendBudget((rateLimits as { readonly spend?: unknown }).spend),
93
+ );
94
+ // Minor-unit ratios require matching currency and scale; spend.percent is rounded.
95
+ if (
96
+ spend?.enabled &&
97
+ spend.used.currency.trim().length > 0 &&
98
+ spend.used.currency.trim() === spend.limit.currency.trim() &&
99
+ spend.used.exponent === spend.limit.exponent
100
+ ) {
101
+ const percent = percentFromAmounts(spend.used.amount_minor, spend.limit.amount_minor);
102
+ if (percent !== undefined) return percent;
103
+ }
104
+
105
+ const extra = rateLimits.extra_usage;
106
+ if (extra?.is_enabled !== true) return undefined;
107
+ const percent = percentFromAmounts(extra.used_credits, extra.monthly_limit) ?? extra.utilization;
108
+ return typeof percent === "number" && Number.isFinite(percent) && percent >= 0
109
+ ? clampPercent(percent)
110
+ : undefined;
111
+}
112
+
113
/**
114
* `model_scoped` shipped in the CLI after the SDK typings we pin, so it is
115
* read structurally until the `.d.ts` catches up.
@@ -165,6 +214,15 @@ export function claudeUsageResponseToLimits(input: {
214
);
215
overageIncluded ??= entry.display_name;
216
}
217
+ const spendPercent = readSpendPercent(response.rate_limits);
218
+ if (spendPercent !== undefined) {
219
+ windows.push({
220
+ id: "monthly_spend",
221
+ kind: "monthly",
222
+ label: "Monthly spend",
223
+ usedPercent: spendPercent,
224
+ });
225
+ }
226
return {
227
limits: makeUsageLimits({ checkedAt, windows }),
228
names: { overageIncluded },