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

usagePricing.test.ts

133 lines4.4 KB

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