Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2
3import {
4 cacheSavingsUsd,
5 createOverrideRateTable,
6 lookupRate,
7 normalizeModelName,
8 parseProviderModelRateTable,
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
28 it("uses custom token rates ahead of public and provider-reported costs", () => {
29 const table = parseRateTable({ "example-model": rate(1) });
30 const overrides = createOverrideRateTable({
31 "example-model": {
32 inputCostPerMillionTokens: 2,
33 outputCostPerMillionTokens: 8,
34 cacheReadCostPerMillionTokens: 0.5,
35 cacheWriteCostPerMillionTokens: 3,
36 },
37 });
38
39 for (const reportedCostUsd of [null, 99]) {
40 expect(priceUsage(table, "example-model", totals, reportedCostUsd, overrides)).toEqual({
41 costUsd: 13.5,
42 costSource: "modelPriced",
43 });
44 }
45 expect(cacheSavingsUsd(table, "example-model", totals, overrides)).toBe(1.5);
46 });
47
48 it("prices unknown models offline and uses input prices for omitted cache rates", () => {
49 const table = parseRateTable({});
50 const overrides = createOverrideRateTable({
51 "example-model": { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 },
52 });
53
54 expect(priceUsage(table, "example-model", totals, null, overrides)).toEqual({
55 costUsd: 14,
56 costSource: "modelPriced",
57 });
58 expect(cacheSavingsUsd(table, "example-model", totals, overrides)).toBe(0);
59 });
60
61 it("preserves explicit zero rates and matches only the exact trimmed model ID", () => {
62 const table = parseRateTable({});
63 const overrides = createOverrideRateTable({
64 " vendor/example-model[1m] ": {
65 inputCostPerMillionTokens: 0,
66 outputCostPerMillionTokens: 0,
67 },
68 });
69 expect(priceUsage(table, " vendor/example-model[1m] ", totals, 99, overrides)).toEqual({
70 costUsd: 0,
71 costSource: "modelPriced",
72 });
73 for (const model of [
74 "example-model[1m]",
75 "vendor/example-model",
76 "vendor/Example-model[1m]",
77 "other/example-model[1m]",
78 ]) {
79 expect(priceUsage(table, model, totals, null, overrides).costSource).toBe("unpriced");
80 expect(priceUsage(table, model, totals, 99, overrides)).toEqual({
81 costUsd: 99,
82 costSource: "providerReported",
83 });
84 }
85 });
86
87 it("keeps the canonical Fable rate separate from DeepInfra in either order", () => {
88 const canonical = ["claude-fable-5", rate(1e-5, 1e-6)] as const;
89 const deepInfra = ["deepinfra/anthropic/claude-fable-5", rate(1e-5)] as const;
90
91 for (const entries of [
92 [canonical, deepInfra],
93 [deepInfra, canonical],
94 ]) {
95 const table = parseRateTable(Object.fromEntries(entries));
96
97 expect(lookupRate(table, "claude-fable-5")?.cacheReadCostPerToken).toBe(1e-6);
98 expect(lookupRate(table, "deepinfra/anthropic/claude-fable-5")?.cacheReadCostPerToken).toBe(
99 1e-5,
100 );
101 expect(lookupRate(table, "other/claude-fable-5")).toBeNull();
102 }
103 });
104
105 it("prices a bracketed context-tier variant at the base model's rate", () => {
106 const table = parseRateTable({ "claude-fable-5-1": rate(1e-5, 2.5e-7) });
107
108 expect(lookupRate(table, "claude-fable-5-1[1m]")).toEqual(
109 lookupRate(table, "claude-fable-5-1"),
110 );
111 expect(lookupRate(table, "anthropic/Claude-Fable-5-1[1m]")).toBeNull();
112 });
113
114 it("adds a bare alias when every qualified entry has the same rate", () => {
115 const table = parseRateTable({
116 "provider-a/example-model": rate(1),
117 "provider-b/example-model": rate(1),
118 });
119
120 expect(lookupRate(table, "example-model")).toEqual(
121 lookupRate(table, "provider-a/example-model"),
122 );
123 });
124
125 it("leaves an ambiguous bare name unpriced", () => {
126 const table = parseRateTable({
127 "provider-a/example-model": rate(1),
128 "provider-b/example-model": rate(3),
129 });
130
131 expect(lookupRate(table, "provider-a/example-model")?.inputCostPerToken).toBe(1);
132 expect(lookupRate(table, "provider-b/example-model")?.inputCostPerToken).toBe(3);
133 expect(lookupRate(table, "example-model")).toBeNull();
134 });
135});
136
137describe("parseProviderModelRateTable", () => {
138 it("reads standalone and grouped Devin pricing metadata", () => {
139 const rates = parseProviderModelRateTable({
140 models: [
141 {
142 slug: "adaptive",
143 pricing: {
144 inputPerMillion: 0.5,
145 cachedInputPerMillion: 0.1,
146 outputPerMillion: 2,
147 },
148 },
149 {
150 slug: "glm-5-2",
151 pricingByVariant: {
152 "glm-5-2": {
153 inputPerMillion: 0.7,
154 cachedInputPerMillion: 0.13,
155 outputPerMillion: 2.2,
156 },
157 },
158 },
159 ],
160 });
161
162 expect(lookupRate(rates, "adaptive")).toMatchObject({
163 inputCostPerToken: 0.5e-6,
164 outputCostPerToken: 2e-6,
165 cacheCreationCostPerToken: 0.5e-6,
166 });
167 expect(lookupRate(rates, "adaptive")?.cacheReadCostPerToken).toBeCloseTo(0.1e-6);
168 expect(lookupRate(rates, "glm-5-2")).toMatchObject({
169 inputCostPerToken: 0.7e-6,
170 outputCostPerToken: 2.2e-6,
171 cacheCreationCostPerToken: 0.7e-6,
172 });
173 expect(lookupRate(rates, "glm-5-2")?.cacheReadCostPerToken).toBeCloseTo(0.13e-6);
174 });
175
176 it("ignores malformed or incomplete pricing without crashing", () => {
177 expect(
178 parseProviderModelRateTable({
179 models: [
180 { slug: "missing-output", pricing: { inputPerMillion: 1 } },
181 { slug: "negative", pricing: { inputPerMillion: -1, outputPerMillion: 2 } },
182 { slug: "not-an-object", pricing: "free" },
183 ],
184 }).size,
185 ).toBe(0);
186 expect(parseProviderModelRateTable(null).size).toBe(0);
187 });
188});