Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

usagePricing.test.ts

246 lines8.8 KB

1import { describe, expect, it } from "@effect/vitest";
2​
3import { cursorRateModel } from "@t3tools/provider-cursor/server/accountUsage";
4import type { UsageSpeed } from "@t3tools/provider-core/server/usage";
5import {
6 cacheSavingsUsd,
7 createOverrideRateTable,
8 lookupRate,
9 parseRateTable,
10 priceUsage,
11} from "./usagePricing.ts";
12​
13const rate = (input: number, cacheRead?: number) => ({
14 input_cost_per_token: input,
15 output_cost_per_token: input * 5,
16 ...(cacheRead === undefined ? {} : { cache_read_input_token_cost: cacheRead }),
17});
18​
19describe("usage pricing", () => {
20 const totals = {
21 uncachedInputTokens: 1_000_000,
22 cachedInputTokens: 1_000_000,
23 cacheCreationTokens: 1_000_000,
24 outputTokens: 1_000_000,
25 reasoningTokens: 500_000,
26 };
27 const record = (
28 model: string,
29 reportedCostUsd: number | null = null,
30 speed: UsageSpeed = "standard",
31 ) => ({
32 model,
33 totals,
34 reportedCostUsd,
35 speed,
36 });
37​
38 it("uses custom token rates ahead of public and provider-reported costs", () => {
39 const table = parseRateTable({ "example-model": rate(1) });
40 const overrides = createOverrideRateTable({
41 "example-model": {
42 inputCostPerMillionTokens: 2,
43 outputCostPerMillionTokens: 8,
44 cacheReadCostPerMillionTokens: 0.5,
45 cacheWriteCostPerMillionTokens: 3,
46 },
47 });
48​
49 for (const reportedCostUsd of [null, 99]) {
50 expect(priceUsage(table, record("example-model", reportedCostUsd), overrides)).toMatchObject({
51 costUsd: 13.5,
52 costSource: "modelPriced",
53 });
54 }
55 expect(cacheSavingsUsd(table, record("example-model"), overrides)).toBe(1.5);
56 });
57​
58 it("prices Cursor cache savings at the base model rate", () => {
59 const table = parseRateTable({
60 "claude-fable-5-1": rate(10e-6, 1e-6),
61 "xai/grok-4.7": rate(2e-6, 0.5e-6),
62 "openrouter/x-ai/grok-4.7": rate(3e-6, 0.5e-6),
63 });
64 const cursorRecord = (model: string) => ({
65 ...record(model, 0.25),
66 rateModel: cursorRateModel(model),
67 });
68​
69 expect(cacheSavingsUsd(table, cursorRecord("claude-fable-5-1-thinking-high"))).toBeCloseTo(9);
70 expect(cacheSavingsUsd(table, cursorRecord("cursor-grok-4.7-high-fast"))).toBeCloseTo(1.5);
71 expect(cacheSavingsUsd(table, cursorRecord("default"))).toBe(0);
72 expect(priceUsage(table, cursorRecord("grok-4.7-xhigh-fast"))).toMatchObject({
73 costUsd: 0.25,
74 costSource: "providerReported",
75 });
76 });
77​
78 it("prices unknown models offline and uses input prices for omitted cache rates", () => {
79 const table = parseRateTable({});
80 const overrides = createOverrideRateTable({
81 "example-model": { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 },
82 });
83​
84 expect(priceUsage(table, record("example-model"), overrides)).toMatchObject({
85 costUsd: 14,
86 costSource: "modelPriced",
87 });
88 expect(cacheSavingsUsd(table, record("example-model"), overrides)).toBe(0);
89 });
90​
91 it("preserves explicit zero rates and matches only the exact trimmed model ID", () => {
92 const table = parseRateTable({});
93 const overrides = createOverrideRateTable({
94 " vendor/example-model[1m] ": {
95 inputCostPerMillionTokens: 0,
96 outputCostPerMillionTokens: 0,
97 },
98 });
99 expect(priceUsage(table, record(" vendor/example-model[1m] ", 99), overrides)).toMatchObject({
100 costUsd: 0,
101 costSource: "modelPriced",
102 });
103 for (const model of [
104 "example-model[1m]",
105 "vendor/example-model",
106 "vendor/Example-model[1m]",
107 "other/example-model[1m]",
108 ]) {
109 expect(priceUsage(table, record(model), overrides).costSource).toBe("unpriced");
110 expect(priceUsage(table, record(model, 99), overrides)).toEqual({
111 costUsd: 99,
112 costSource: "providerReported",
113 categoryCostUsd: null,
114 speedPremiumUsd: 0,
115 });
116 }
117 });
118​
119 it("prices fast-mode requests at the model's published fast multiple", () => {
120 const table = parseRateTable({
121 "claude-opus-5-5": { ...rate(4e-6, 2e-7), provider_specific_entry: { fast: 2, us: 1.1 } },
122 "claude-fable-5-1": { ...rate(1e-5, 2.5e-7), provider_specific_entry: { us: 1.1 } },
123 });
124 const overrides = createOverrideRateTable({
125 "claude-opus-5-5": { inputCostPerMillionTokens: 4, outputCostPerMillionTokens: 20 },
126 });
127 const cost = (model: string, speed: UsageSpeed, custom?: typeof overrides) =>
128 priceUsage(table, record(model, null, speed), custom).costUsd;
129​
130 expect(cost("claude-opus-5-5", "fast")).toBeCloseTo(2 * cost("claude-opus-5-5", "standard"));
131 expect(cacheSavingsUsd(table, record("claude-opus-5-5", null, "fast"))).toBeCloseTo(
132 2 * cacheSavingsUsd(table, record("claude-opus-5-5")),
133 );
134 expect(cost("claude-fable-5-1", "fast")).toBe(cost("claude-fable-5-1", "standard"));
135 expect(cost("claude-opus-5-5", "fast", overrides)).toBe(
136 cost("claude-opus-5-5", "standard", overrides),
137 );
138 });
139​
140 it("splits cost by category and prices the speed premium", () => {
141 const table = parseRateTable({
142 "claude-opus-5-5": {
143 ...rate(4e-6, 4e-7),
144 cache_creation_input_token_cost: 5e-6,
145 provider_specific_entry: { fast: 2 },
146 },
147 });
148 const split = (input: number, cacheRead: number, cacheWrite: number, output: number) => ({
149 input: expect.closeTo(input),
150 cacheRead: expect.closeTo(cacheRead),
151 cacheWrite: expect.closeTo(cacheWrite),
152 output: expect.closeTo(output),
153 });
154​
155 expect(priceUsage(table, record("claude-opus-5-5", null, "fast"))).toEqual({
156 costUsd: expect.closeTo(58.8),
157 costSource: "modelPriced",
158 categoryCostUsd: split(8, 0.8, 10, 40),
159 speedPremiumUsd: expect.closeTo(29.4),
160 });
161 expect(priceUsage(table, record("claude-opus-5-5", 29.4, "fast"))).toEqual({
162 costUsd: 29.4,
163 costSource: "providerReported",
164 categoryCostUsd: split(4, 0.4, 5, 20),
165 speedPremiumUsd: expect.closeTo(14.7),
166 });
167 expect(priceUsage(table, record("unknown-model", 29.4, "fast"))).toMatchObject({
168 costUsd: 29.4,
169 categoryCostUsd: null,
170 speedPremiumUsd: 0,
171 });
172 });
173​
174 it("prices Codex priority and ultrafast requests at their published tier rates", () => {
175 const table = parseRateTable({
176 "gpt-6-astra": {
177 ...rate(1e-5, 1e-6),
178 input_cost_per_token_priority: 2e-5,
179 output_cost_per_token_priority: 1e-4,
180 cache_read_input_token_cost_priority: 2e-6,
181 input_cost_per_token_ultrafast: 6e-5,
182 output_cost_per_token_ultrafast: 3e-4,
183 },
184 "gpt-6-sol": rate(2e-6, 2e-7),
185 });
186 const cost = (model: string, speed: UsageSpeed) =>
187 priceUsage(table, record(model, null, speed)).costUsd;
188 const standard = cost("gpt-6-astra", "standard");
189​
190 expect(cost("gpt-6-astra", "fast")).toBeCloseTo(2 * standard);
191 expect(cost("gpt-6-astra", "ultrafast")).toBeCloseTo(6 * standard);
192 expect(cacheSavingsUsd(table, record("gpt-6-astra", null, "ultrafast"))).toBeCloseTo(
193 6 * cacheSavingsUsd(table, record("gpt-6-astra")),
194 );
195 expect(cost("gpt-6-sol", "ultrafast")).toBe(cost("gpt-6-sol", "standard"));
196 });
197​
198 it("keeps the canonical Fable rate separate from DeepInfra in either order", () => {
199 const canonical = ["claude-fable-5", rate(1e-5, 1e-6)] as const;
200 const deepInfra = ["deepinfra/anthropic/claude-fable-5", rate(1e-5)] as const;
201​
202 for (const entries of [
203 [canonical, deepInfra],
204 [deepInfra, canonical],
205 ]) {
206 const table = parseRateTable(Object.fromEntries(entries));
207​
208 expect(lookupRate(table, "claude-fable-5")?.cacheReadCostPerToken).toBe(1e-6);
209 expect(lookupRate(table, "deepinfra/anthropic/claude-fable-5")?.cacheReadCostPerToken).toBe(
210 1e-5,
211 );
212 expect(lookupRate(table, "other/claude-fable-5")).toBeNull();
213 }
214 });
215​
216 it("prices a bracketed context-tier variant at the base model's rate", () => {
217 const table = parseRateTable({ "claude-fable-5-1": rate(1e-5, 2.5e-7) });
218​
219 expect(lookupRate(table, "claude-fable-5-1[1m]")).toEqual(
220 lookupRate(table, "claude-fable-5-1"),
221 );
222 expect(lookupRate(table, "anthropic/Claude-Fable-5-1[1m]")).toBeNull();
223 });
224​
225 it("adds a bare alias when every qualified entry has the same rate", () => {
226 const table = parseRateTable({
227 "provider-a/example-model": rate(1),
228 "provider-b/example-model": rate(1),
229 });
230​
231 expect(lookupRate(table, "example-model")).toEqual(
232 lookupRate(table, "provider-a/example-model"),
233 );
234 });
235​
236 it("leaves an ambiguous bare name unpriced", () => {
237 const table = parseRateTable({
238 "provider-a/example-model": rate(1),
239 "provider-b/example-model": rate(3),
240 });
241​
242 expect(lookupRate(table, "provider-a/example-model")?.inputCostPerToken).toBe(1);
243 expect(lookupRate(table, "provider-b/example-model")?.inputCostPerToken).toBe(3);
244 expect(lookupRate(table, "example-model")).toBeNull();
245 });
246});