The best way to control Claude Code, Codex and more from any computer or phone

main
ts 316 lines 10 KB
Raw
1 import { describe, expect, it } from "@effect/vitest";
2
3 import { resolveModelAliases, UsageAggregator } from "./usageAggregation.ts";
4 import type { RateTable } from "./usagePricing.ts";
5 import type { UsageRecord } from "@t3tools/provider-core/server/usage";
6
7 const rates: RateTable = new Map([
8 [
9 "claude-fable-5",
10 {
11 inputCostPerToken: 1e-5,
12 outputCostPerToken: 5e-5,
13 cacheReadCostPerToken: 1e-6,
14 cacheCreationCostPerToken: 1.25e-5,
15 cacheCreation1hCostPerToken: 2e-5,
16 fast: {
17 inputCostPerToken: 2e-5,
18 outputCostPerToken: 1e-4,
19 cacheReadCostPerToken: 2e-6,
20 cacheCreationCostPerToken: 2.5e-5,
21 cacheCreation1hCostPerToken: 4e-5,
22 },
23 ultrafast: null,
24 contextTiers: [],
25 },
26 ],
27 ]);
28
29 function record(overrides: Partial<UsageRecord> = {}): UsageRecord {
30 return {
31 provider: "claude",
32 timestampMs: Date.parse("2026-08-07T04:05:13.944Z"),
33 model: "claude-fable-5",
34 sessionId: "session-a",
35 totals: {
36 uncachedInputTokens: 100,
37 cachedInputTokens: 1000,
38 cacheCreationTokens: 10,
39 outputTokens: 50,
40 reasoningTokens: 0,
41 },
42 reportedCostUsd: null,
43 speed: "standard",
44 dedupeKey: null,
45 ...overrides,
46 };
47 }
48
49 function aggregate(
50 records: readonly UsageRecord[],
51 timeZone = "UTC",
52 resolution: "day" | "hour" = "day",
53 ) {
54 const hourlyBounds =
55 resolution === "hour"
56 ? {
57 sinceTimeMs: Date.parse("2026-08-06T04:37:00.000Z"),
58 untilTimeMs: Date.parse("2026-08-07T04:37:00.000Z"),
59 }
60 : {};
61 const aggregator = new UsageAggregator({
62 timeZone,
63 sinceDay: "2026-08-01",
64 untilDay: "2026-08-31",
65 resolution,
66 ...hourlyBounds,
67 rates,
68 });
69 for (const item of records) aggregator.add(item);
70 return aggregator.finish();
71 }
72
73 describe("UsageAggregator", () => {
74 it("splits a bucket's cost by category and speed", () => {
75 const [bucket] = aggregate([record(), record({ speed: "fast" })]).buckets;
76
77 expect(bucket).toMatchObject({
78 costUsd: expect.closeTo(0.016875),
79 categoryCostUsd: {
80 input: expect.closeTo(0.003),
81 cacheRead: expect.closeTo(0.003),
82 cacheWrite: expect.closeTo(0.000375),
83 output: expect.closeTo(0.0075),
84 },
85 fastCostUsd: expect.closeTo(0.01125),
86 speedPremiumUsd: expect.closeTo(0.005625),
87 });
88 expect(bucket).not.toHaveProperty("ultrafastCostUsd");
89 });
90
91 it("merges partial copies of a key by channel maximum in the first copy's bucket", () => {
92 const zeroTotals = {
93 uncachedInputTokens: 0,
94 cachedInputTokens: 0,
95 cacheCreationTokens: 0,
96 outputTokens: 0,
97 reasoningTokens: 0,
98 };
99 const partial = record({
100 dedupeKey: "msg_1:",
101 speed: "fast",
102 reportedCostUsd: 1,
103 totals: { ...record().totals, cachedInputTokens: 1200, outputTokens: 3, reasoningTokens: 5 },
104 });
105 const complete = record({
106 dedupeKey: "msg_1:",
107 model: "claude-fable-5[1m]",
108 timestampMs: Date.parse("2026-08-08T12:00:00Z"),
109 sessionId: "session-fork",
110 reportedCostUsd: 2,
111 totals: {
112 uncachedInputTokens: 2,
113 cachedInputTokens: 1000,
114 cacheCreationTokens: 40,
115 outputTokens: 264,
116 reasoningTokens: 135,
117 },
118 });
119 const zero = { ...partial, totals: zeroTotals, reportedCostUsd: null };
120 const merged = {
121 reportedCostUsd: 2,
122 totals: { ...complete.totals, uncachedInputTokens: 100, cachedInputTokens: 1200 },
123 };
124
125 for (const copies of [
126 [zero, partial, complete, complete],
127 [complete, partial, zero, zero],
128 ]) {
129 const aggregator = new UsageAggregator({
130 timeZone: "UTC",
131 sinceDay: "2026-08-01",
132 untilDay: "2026-08-31",
133 rates,
134 });
135 copies.forEach((copy, index) => aggregator.add(copy, `${index}.jsonl`));
136 const result = aggregator.finish();
137 const expected = aggregate([{ ...copies[0]!, ...merged }]).buckets[0];
138
139 expect(result.duplicatesDropped).toBe(3);
140 expect(result.buckets).toEqual([{ ...expected, sourcePath: "0.jsonl" }]);
141 }
142 });
143
144 it("keeps a live Grok turn over a saved pre-v17 maximum in either source order", () => {
145 const turn = (timestampMs: number, outputTokens: number) =>
146 record({
147 provider: "grok",
148 model: "grok-4.5",
149 timestampMs,
150 dedupeKey: `s:p:${timestampMs}:grok-4.5`,
151 reportedCostUsd: outputTokens / 10,
152 totals: { ...record().totals, uncachedInputTokens: 0, outputTokens },
153 });
154 const first = turn(Date.parse("2026-08-08T10:00:00Z"), 10);
155 const resumed = turn(Date.parse("2026-08-08T11:00:00Z"), 20);
156 const savedMaximum = { ...first, totals: resumed.totals, reportedCostUsd: 2 };
157
158 const expected = aggregate([first, resumed]).buckets;
159
160 for (const order of [
161 [first, resumed, savedMaximum],
162 [savedMaximum, first, resumed],
163 ]) {
164 const result = aggregate(order);
165 expect(result.duplicatesDropped).toBe(1);
166 expect(result.buckets).toEqual(expected);
167 }
168 });
169
170 it("does not count a key whose first copy fell outside the window", () => {
171 const result = aggregate([
172 record({ dedupeKey: "msg_1:", timestampMs: Date.parse("2026-07-01T12:00:00Z") }),
173 record({ dedupeKey: "msg_1:", totals: { ...record().totals, outputTokens: 500 } }),
174 ]);
175
176 expect(result.duplicatesDropped).toBe(1);
177 expect(result.outOfWindow).toBe(1);
178 expect(result.buckets).toHaveLength(0);
179 });
180
181 it("buckets by the day in the requested time zone", () => {
182 const utc = aggregate([record()], "UTC");
183 const losAngeles = aggregate([record()], "America/Los_Angeles");
184
185 expect(utc.buckets[0]?.day).toBe("2026-08-07");
186 expect(losAngeles.buckets[0]?.day).toBe("2026-08-06");
187 });
188
189 it("finds the day at a quarter-hour zone's midnight across interleaved buckets", () => {
190 const result = aggregate(
191 [
192 record({ timestampMs: Date.parse("2026-08-01T18:14:59.999Z") }),
193 record({ timestampMs: Date.parse("2026-08-01T18:15:00.000Z") }),
194 record({ timestampMs: Date.parse("2026-08-01T18:15:00.000Z"), model: "claude-opus-5" }),
195 record({ timestampMs: Date.parse("2026-08-01T18:16:00.000Z") }),
196 ],
197 "Asia/Kathmandu",
198 );
199
200 expect(result.buckets.map((bucket) => [bucket.day, bucket.model, bucket.records])).toEqual([
201 ["2026-08-01", "claude-fable-5", 1],
202 ["2026-08-02", "claude-fable-5", 2],
203 ["2026-08-02", "claude-opus-5", 1],
204 ]);
205 });
206
207 it("finds the day at a fixed offset's midnight between quarter hours", () => {
208 const result = aggregate(
209 [
210 record({ timestampMs: Date.parse("2026-08-01T23:58:59.999Z") }),
211 record({ timestampMs: Date.parse("2026-08-01T23:59:00.000Z") }),
212 ],
213 "+00:01",
214 );
215
216 expect(result.buckets.map((bucket) => [bucket.day, bucket.records])).toEqual([
217 ["2026-08-01", 1],
218 ["2026-08-02", 1],
219 ]);
220 });
221
222 it("splits an hourly request into fixed buckets anchored to its exact start", () => {
223 const result = aggregate(
224 [
225 record({ timestampMs: Date.parse("2026-08-07T02:40:13.944Z") }),
226 record({ timestampMs: Date.parse("2026-08-07T03:40:13.944Z") }),
227 ],
228 "America/Los_Angeles",
229 "hour",
230 );
231
232 expect(result.buckets.map((bucket) => [bucket.day, bucket.hourStart])).toEqual([
233 ["2026-08-06", "2026-08-07T02:37:00.000Z"],
234 ["2026-08-06", "2026-08-07T03:37:00.000Z"],
235 ]);
236 });
237
238 it("uses an inclusive start and exclusive end for rolling windows", () => {
239 const result = aggregate(
240 [
241 record({ timestampMs: Date.parse("2026-08-06T04:36:59.999Z") }),
242 record({ timestampMs: Date.parse("2026-08-06T04:37:00.000Z") }),
243 record({ timestampMs: Date.parse("2026-08-07T04:36:59.999Z") }),
244 record({ timestampMs: Date.parse("2026-08-07T04:37:00.000Z") }),
245 ],
246 "UTC",
247 "hour",
248 );
249
250 expect(result.outOfWindow).toBe(2);
251 expect(result.buckets.map((bucket) => bucket.hourStart)).toEqual([
252 "2026-08-06T04:37:00.000Z",
253 "2026-08-07T03:37:00.000Z",
254 ]);
255 });
256
257 it("counts tokens but not cost for a model with no rate", () => {
258 const result = aggregate([record({ model: "kimi-k3" })]);
259
260 expect(result.buckets[0]?.costUsd).toBe(0);
261 expect(result.buckets[0]?.costSource).toBe("unpriced");
262 expect(result.buckets[0]?.unpricedRecords).toBe(1);
263 expect(result.buckets[0]?.totals.outputTokens).toBe(50);
264 });
265
266 it("prefers a reported cost over the rate table", () => {
267 const result = aggregate([record({ reportedCostUsd: 1.25 })]);
268
269 expect(result.buckets[0]?.costUsd).toBe(1.25);
270 expect(result.buckets[0]?.costSource).toBe("providerReported");
271 });
272
273 it("folds a mapped model into its target and prices it there", () => {
274 const aggregator = new UsageAggregator({
275 timeZone: "UTC",
276 sinceDay: "2026-08-01",
277 untilDay: "2026-08-31",
278 rates,
279 modelAliases: resolveModelAliases({ "example-preview": "claude-fable-5" }),
280 rateModel: () => "claude-fable",
281 });
282 aggregator.add(record());
283 aggregator.add(record({ model: "example-preview", rateModel: "example-preview-high" }));
284 const result = aggregator.finish();
285
286 expect(result.buckets).toHaveLength(1);
287 expect(result.buckets[0]?.model).toBe("claude-fable-5");
288 expect(result.buckets[0]?.records).toBe(2);
289 expect(result.buckets[0]?.costUsd).toBeCloseTo(0.00925, 9);
290 expect(result.buckets[0]?.unpricedRecords).toBe(0);
291 });
292 });
293
294 describe("resolveModelAliases", () => {
295 it("follows chains to the final model and drops chains that enter a loop", () => {
296 expect([
297 ...resolveModelAliases({
298 "preview[1m]": "preview",
299 preview: "example-model",
300 shared: "preview",
301 intoLoop: "loop",
302 loop: "back",
303 back: "loop",
304 laterIntoLoop: "back",
305 deepIntoLoop: "intoLoop",
306 self: "self",
307 validAfterLoops: "shared",
308 }),
309 ]).toEqual([
310 ["preview[1m]", "example-model"],
311 ["preview", "example-model"],
312 ["shared", "example-model"],
313 ["validAfterLoops", "example-model"],
314 ]);
315 });
316 });