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