Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import { cursorRateModel } from "./cursorUsageReader.ts";
4import {
5 cacheSavingsUsd,
6 createOverrideRateTable,
7 lookupRate,
8 parseRateTable,
9 priceUsage,
10} from "./usagePricing.ts";
11
12const rate = (input: number, cacheRead?: number) => ({
13 input_cost_per_token: input,
14 output_cost_per_token: input * 5,
15 ...(cacheRead === undefined ? {} : { cache_read_input_token_cost: cacheRead }),
16});
17
18describe("usage pricing", () => {
19 const totals = {
20 uncachedInputTokens: 1_000_000,
21 cachedInputTokens: 1_000_000,
22 cacheCreationTokens: 1_000_000,
23 outputTokens: 1_000_000,
24 reasoningTokens: 500_000,
25 };
26 const record = (model: string, reportedCostUsd: number | null = null, fast = false) => ({
27 model,
28 totals,
29 reportedCostUsd,
30 fast,
31 });
32
33 it("uses custom token rates ahead of public and provider-reported costs", () => {
34 const table = parseRateTable({ "example-model": rate(1) });
35 const overrides = createOverrideRateTable({
36 "example-model": {
37 inputCostPerMillionTokens: 2,
38 outputCostPerMillionTokens: 8,
39 cacheReadCostPerMillionTokens: 0.5,
40 cacheWriteCostPerMillionTokens: 3,
41 },
42 });
43
44 for (const reportedCostUsd of [null, 99]) {
45 expect(priceUsage(table, record("example-model", reportedCostUsd), overrides)).toEqual({
46 costUsd: 13.5,
47 costSource: "modelPriced",
48 });
49 }
50 expect(cacheSavingsUsd(table, record("example-model"), overrides)).toBe(1.5);
51 });
52
53 it("prices Cursor cache savings at the base model rate", () => {
54 const table = parseRateTable({
55 "claude-fable-5-1": rate(10e-6, 1e-6),
56 "xai/grok-4.7": rate(2e-6, 0.5e-6),
57 "openrouter/x-ai/grok-4.7": rate(3e-6, 0.5e-6),
58 });
59 const cursorRecord = (model: string) => ({
60 ...record(model, 0.25),
61 rateModel: cursorRateModel(model),
62 });
63
64 expect(cacheSavingsUsd(table, cursorRecord("claude-fable-5-1-thinking-high"))).toBeCloseTo(9);
65 expect(cacheSavingsUsd(table, cursorRecord("cursor-grok-4.7-high-fast"))).toBeCloseTo(1.5);
66 expect(cacheSavingsUsd(table, cursorRecord("default"))).toBe(0);
67 expect(priceUsage(table, cursorRecord("grok-4.7-xhigh-fast"))).toEqual({
68 costUsd: 0.25,
69 costSource: "providerReported",
70 });
71 });
72
73 it("prices unknown models offline and uses input prices for omitted cache rates", () => {
74 const table = parseRateTable({});
75 const overrides = createOverrideRateTable({
76 "example-model": { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 },
77 });
78
79 expect(priceUsage(table, record("example-model"), overrides)).toEqual({
80 costUsd: 14,
81 costSource: "modelPriced",
82 });
83 expect(cacheSavingsUsd(table, record("example-model"), overrides)).toBe(0);
84 });
85
86 it("preserves explicit zero rates and matches only the exact trimmed model ID", () => {
87 const table = parseRateTable({});
88 const overrides = createOverrideRateTable({
89 " vendor/example-model[1m] ": {
90 inputCostPerMillionTokens: 0,
91 outputCostPerMillionTokens: 0,
92 },
93 });
94 expect(priceUsage(table, record(" vendor/example-model[1m] ", 99), overrides)).toEqual({
95 costUsd: 0,
96 costSource: "modelPriced",
97 });
98 for (const model of [
99 "example-model[1m]",
100 "vendor/example-model",
101 "vendor/Example-model[1m]",
102 "other/example-model[1m]",
103 ]) {
104 expect(priceUsage(table, record(model), overrides).costSource).toBe("unpriced");
105 expect(priceUsage(table, record(model, 99), overrides)).toEqual({
106 costUsd: 99,
107 costSource: "providerReported",
108 });
109 }
110 });
111
112 it("prices fast-mode requests at the model's published fast multiple", () => {
113 const table = parseRateTable({
114 "claude-opus-5-5": { ...rate(4e-6, 2e-7), provider_specific_entry: { fast: 2, us: 1.1 } },
115 "claude-fable-5-1": { ...rate(1e-5, 2.5e-7), provider_specific_entry: { us: 1.1 } },
116 });
117 const overrides = createOverrideRateTable({
118 "claude-opus-5-5": { inputCostPerMillionTokens: 4, outputCostPerMillionTokens: 20 },
119 });
120 const cost = (model: string, fast: boolean, custom?: typeof overrides) =>
121 priceUsage(table, record(model, null, fast), custom).costUsd;
122
123 expect(cost("claude-opus-5-5", true)).toBeCloseTo(2 * cost("claude-opus-5-5", false));
124 expect(cacheSavingsUsd(table, record("claude-opus-5-5", null, true))).toBeCloseTo(
125 2 * cacheSavingsUsd(table, record("claude-opus-5-5")),
126 );
127 // No published fast tier, and custom prices, both stay at the standard rate.
128 expect(cost("claude-fable-5-1", true)).toBe(cost("claude-fable-5-1", false));
129 expect(cost("claude-opus-5-5", true, overrides)).toBe(
130 cost("claude-opus-5-5", false, overrides),
131 );
132 });
133
134 it("keeps the canonical Fable rate separate from DeepInfra in either order", () => {
135 const canonical = ["claude-fable-5", rate(1e-5, 1e-6)] as const;
136 const deepInfra = ["deepinfra/anthropic/claude-fable-5", rate(1e-5)] as const;
137
138 for (const entries of [
139 [canonical, deepInfra],
140 [deepInfra, canonical],
141 ]) {
142 const table = parseRateTable(Object.fromEntries(entries));
143
144 expect(lookupRate(table, "claude-fable-5")?.cacheReadCostPerToken).toBe(1e-6);
145 expect(lookupRate(table, "deepinfra/anthropic/claude-fable-5")?.cacheReadCostPerToken).toBe(
146 1e-5,
147 );
148 expect(lookupRate(table, "other/claude-fable-5")).toBeNull();
149 }
150 });
151
152 it("prices a bracketed context-tier variant at the base model's rate", () => {
153 const table = parseRateTable({ "claude-fable-5-1": rate(1e-5, 2.5e-7) });
154
155 expect(lookupRate(table, "claude-fable-5-1[1m]")).toEqual(
156 lookupRate(table, "claude-fable-5-1"),
157 );
158 expect(lookupRate(table, "anthropic/Claude-Fable-5-1[1m]")).toBeNull();
159 });
160
161 it("adds a bare alias when every qualified entry has the same rate", () => {
162 const table = parseRateTable({
163 "provider-a/example-model": rate(1),
164 "provider-b/example-model": rate(1),
165 });
166
167 expect(lookupRate(table, "example-model")).toEqual(
168 lookupRate(table, "provider-a/example-model"),
169 );
170 });
171
172 it("leaves an ambiguous bare name unpriced", () => {
173 const table = parseRateTable({
174 "provider-a/example-model": rate(1),
175 "provider-b/example-model": rate(3),
176 });
177
178 expect(lookupRate(table, "provider-a/example-model")?.inputCostPerToken).toBe(1);
179 expect(lookupRate(table, "provider-b/example-model")?.inputCostPerToken).toBe(3);
180 expect(lookupRate(table, "example-model")).toBeNull();
181 });
182});