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

usagePricing.test.ts

213 lines7.7 KB

1import { describe, expect, it } from "@effect/vitest";
2​
3import { cursorRateModel } from "./cursorUsageReader.ts";
4import type { UsageSpeed } from "./usageTranscripts.ts";
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)).toEqual({
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"))).toEqual({
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)).toEqual({
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)).toEqual({
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 });
114 }
115 });
116​
117 it("prices fast-mode requests at the model's published fast multiple", () => {
118 const table = parseRateTable({
119 "claude-opus-5-5": { ...rate(4e-6, 2e-7), provider_specific_entry: { fast: 2, us: 1.1 } },
120 "claude-fable-5-1": { ...rate(1e-5, 2.5e-7), provider_specific_entry: { us: 1.1 } },
121 });
122 const overrides = createOverrideRateTable({
123 "claude-opus-5-5": { inputCostPerMillionTokens: 4, outputCostPerMillionTokens: 20 },
124 });
125 const cost = (model: string, speed: UsageSpeed, custom?: typeof overrides) =>
126 priceUsage(table, record(model, null, speed), custom).costUsd;
127​
128 expect(cost("claude-opus-5-5", "fast")).toBeCloseTo(2 * cost("claude-opus-5-5", "standard"));
129 expect(cacheSavingsUsd(table, record("claude-opus-5-5", null, "fast"))).toBeCloseTo(
130 2 * cacheSavingsUsd(table, record("claude-opus-5-5")),
131 );
132 // No published fast tier, and custom prices, both stay at the standard rate.
133 expect(cost("claude-fable-5-1", "fast")).toBe(cost("claude-fable-5-1", "standard"));
134 expect(cost("claude-opus-5-5", "fast", overrides)).toBe(
135 cost("claude-opus-5-5", "standard", overrides),
136 );
137 });
138​
139 it("prices Codex priority and ultrafast requests at their published tier rates", () => {
140 const table = parseRateTable({
141 "gpt-6-astra": {
142 ...rate(1e-5, 1e-6),
143 input_cost_per_token_priority: 2e-5,
144 output_cost_per_token_priority: 1e-4,
145 cache_read_input_token_cost_priority: 2e-6,
146 input_cost_per_token_ultrafast: 6e-5,
147 output_cost_per_token_ultrafast: 3e-4,
148 // No ultrafast cache rate: keeps the standard 10:1 input-to-cache ratio.
149 },
150 "gpt-6-sol": rate(2e-6, 2e-7),
151 });
152 const cost = (model: string, speed: UsageSpeed) =>
153 priceUsage(table, record(model, null, speed)).costUsd;
154 const standard = cost("gpt-6-astra", "standard");
155​
156 expect(cost("gpt-6-astra", "fast")).toBeCloseTo(2 * standard);
157 expect(cost("gpt-6-astra", "ultrafast")).toBeCloseTo(6 * standard);
158 expect(cacheSavingsUsd(table, record("gpt-6-astra", null, "ultrafast"))).toBeCloseTo(
159 6 * cacheSavingsUsd(table, record("gpt-6-astra")),
160 );
161 // A tier the model does not publish bills at the standard rate.
162 expect(cost("gpt-6-sol", "ultrafast")).toBe(cost("gpt-6-sol", "standard"));
163 });
164​
165 it("keeps the canonical Fable rate separate from DeepInfra in either order", () => {
166 const canonical = ["claude-fable-5", rate(1e-5, 1e-6)] as const;
167 const deepInfra = ["deepinfra/anthropic/claude-fable-5", rate(1e-5)] as const;
168​
169 for (const entries of [
170 [canonical, deepInfra],
171 [deepInfra, canonical],
172 ]) {
173 const table = parseRateTable(Object.fromEntries(entries));
174​
175 expect(lookupRate(table, "claude-fable-5")?.cacheReadCostPerToken).toBe(1e-6);
176 expect(lookupRate(table, "deepinfra/anthropic/claude-fable-5")?.cacheReadCostPerToken).toBe(
177 1e-5,
178 );
179 expect(lookupRate(table, "other/claude-fable-5")).toBeNull();
180 }
181 });
182​
183 it("prices a bracketed context-tier variant at the base model's rate", () => {
184 const table = parseRateTable({ "claude-fable-5-1": rate(1e-5, 2.5e-7) });
185​
186 expect(lookupRate(table, "claude-fable-5-1[1m]")).toEqual(
187 lookupRate(table, "claude-fable-5-1"),
188 );
189 expect(lookupRate(table, "anthropic/Claude-Fable-5-1[1m]")).toBeNull();
190 });
191​
192 it("adds a bare alias when every qualified entry has the same rate", () => {
193 const table = parseRateTable({
194 "provider-a/example-model": rate(1),
195 "provider-b/example-model": rate(1),
196 });
197​
198 expect(lookupRate(table, "example-model")).toEqual(
199 lookupRate(table, "provider-a/example-model"),
200 );
201 });
202​
203 it("leaves an ambiguous bare name unpriced", () => {
204 const table = parseRateTable({
205 "provider-a/example-model": rate(1),
206 "provider-b/example-model": rate(3),
207 });
208​
209 expect(lookupRate(table, "provider-a/example-model")?.inputCostPerToken).toBe(1);
210 expect(lookupRate(table, "provider-b/example-model")?.inputCostPerToken).toBe(3);
211 expect(lookupRate(table, "example-model")).toBeNull();
212 });
213});