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 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
27 it("uses custom token rates ahead of public and provider-reported costs", () => {
28 const table = parseRateTable({ "example-model": rate(1) });
29 const overrides = createOverrideRateTable({
30 "example-model": {
31 inputCostPerMillionTokens: 2,
32 outputCostPerMillionTokens: 8,
33 cacheReadCostPerMillionTokens: 0.5,
34 cacheWriteCostPerMillionTokens: 3,
35 },
36 });
37
38 for (const reportedCostUsd of [null, 99]) {
39 expect(priceUsage(table, "example-model", totals, reportedCostUsd, overrides)).toEqual({
40 costUsd: 13.5,
41 costSource: "modelPriced",
42 });
43 }
44 expect(cacheSavingsUsd(table, "example-model", totals, overrides)).toBe(1.5);
45 });
46
47 it("prices unknown models offline and uses input prices for omitted cache rates", () => {
48 const table = parseRateTable({});
49 const overrides = createOverrideRateTable({
50 "example-model": { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 },
51 });
52
53 expect(priceUsage(table, "example-model", totals, null, overrides)).toEqual({
54 costUsd: 14,
55 costSource: "modelPriced",
56 });
57 expect(cacheSavingsUsd(table, "example-model", totals, overrides)).toBe(0);
58 });
59
60 it("preserves explicit zero rates and matches only the exact trimmed model ID", () => {
61 const table = parseRateTable({});
62 const overrides = createOverrideRateTable({
63 " vendor/example-model[1m] ": {
64 inputCostPerMillionTokens: 0,
65 outputCostPerMillionTokens: 0,
66 },
67 });
68 expect(priceUsage(table, " vendor/example-model[1m] ", totals, 99, overrides)).toEqual({
69 costUsd: 0,
70 costSource: "modelPriced",
71 });
72 for (const model of [
73 "example-model[1m]",
74 "vendor/example-model",
75 "vendor/Example-model[1m]",
76 "other/example-model[1m]",
77 ]) {
78 expect(priceUsage(table, model, totals, null, overrides).costSource).toBe("unpriced");
79 expect(priceUsage(table, model, totals, 99, overrides)).toEqual({
80 costUsd: 99,
81 costSource: "providerReported",
82 });
83 }
84 });
85
86 it("keeps the existing model-name normalization contract", () => {
87 expect(normalizeModelName(" Anthropic/Claude-Opus-5 ")).toBe("claude-opus-5");
88 });
89
90 it("keeps the canonical Fable rate separate from DeepInfra in either order", () => {
91 const canonical = ["claude-fable-5", rate(1e-5, 1e-6)] as const;
92 const deepInfra = ["deepinfra/anthropic/claude-fable-5", rate(1e-5)] as const;
93
94 for (const entries of [
95 [canonical, deepInfra],
96 [deepInfra, canonical],
97 ]) {
98 const table = parseRateTable(Object.fromEntries(entries));
99
100 expect(lookupRate(table, "claude-fable-5")?.cacheReadCostPerToken).toBe(1e-6);
101 expect(lookupRate(table, "deepinfra/anthropic/claude-fable-5")?.cacheReadCostPerToken).toBe(
102 1e-5,
103 );
104 expect(lookupRate(table, "other/claude-fable-5")).toBeNull();
105 }
106 });
107
108 it("prices a bracketed context-tier variant at the base model's rate", () => {
109 const table = parseRateTable({ "claude-fable-5-1": rate(1e-5, 2.5e-7) });
110
111 expect(lookupRate(table, "claude-fable-5-1[1m]")).toEqual(
112 lookupRate(table, "claude-fable-5-1"),
113 );
114 expect(lookupRate(table, "anthropic/Claude-Fable-5-1[1m]")).toBeNull();
115 });
116
117 it("adds a bare alias when every qualified entry has the same rate", () => {
118 const table = parseRateTable({
119 "provider-a/example-model": rate(1),
120 "provider-b/example-model": rate(1),
121 });
122
123 expect(lookupRate(table, "example-model")).toEqual(
124 lookupRate(table, "provider-a/example-model"),
125 );
126 });
127
128 it("leaves an ambiguous bare name unpriced", () => {
129 const table = parseRateTable({
130 "provider-a/example-model": rate(1),
131 "provider-b/example-model": rate(3),
132 });
133
134 expect(lookupRate(table, "provider-a/example-model")?.inputCostPerToken).toBe(1);
135 expect(lookupRate(table, "provider-b/example-model")?.inputCostPerToken).toBe(3);
136 expect(lookupRate(table, "example-model")).toBeNull();
137 });
138});