Skip to content

Commit 2a1ea4b

Browse files
committed
fix(pricing): preserve native schedules through resolution
Merge prepared costs beneath authored display prices, retain validated native free schedules under exact owner policy, and compile OpenRouter overrides with strict thresholds and source-order inheritance for each price key. Preserve zero, partial, flat and tiered user pricing without new settings or storage. Follow-up repairs for #133695.
1 parent 40a62bc commit 2a1ea4b

11 files changed

Lines changed: 718 additions & 151 deletions

File tree

‎docs/plugins/sdk-provider-plugins.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,9 +412,13 @@ catalog, API-key auth, and dynamic model resolution.
412412
The same subpath exposes `normalizeOpenRouterModelPricing(pricing)` for
413413
native OpenRouter pricing objects. It converts per-token rates and static
414414
prompt-length overrides into a complete per-million cost schedule, without
415-
network access or prices from another source. Missing cache rates become zero;
416-
invalid required rates or conflicting context tiers return `undefined`.
417-
Time-based overrides are not represented as static context tiers.
415+
network access or prices from another source. Overrides apply strictly above
416+
`min_prompt_tokens`, counting uncached input, cache reads, and cache writes.
417+
Matching entries apply in source order: later entries win per price key,
418+
including at equal thresholds; omitted keys inherit the native base or an
419+
earlier matching entry. Cache rates absent from the base default to zero.
420+
Invalid effective token rates return `undefined`. Entries with time-based or
421+
unknown conditions are skipped; other known charge dimensions are ignored.
418422

419423
When `ctx.providerIds` is present, it contains the normalized provider
420424
identities selected for that catalog owner. Return `null` before resolving

‎extensions/openrouter/provider-catalog.test.ts‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe("OpenRouter provider catalog", () => {
1616
response: Response.json({
1717
data: [
1818
{
19-
id: "google/gemini-3.6-flash",
20-
name: "Google: Gemini 3.6 Flash",
19+
id: "acme/partial-pricing",
20+
name: "Partial Pricing Fixture",
2121
architecture: {
2222
input_modalities: ["text", "image", "audio", "video"],
2323
output_modalities: ["text"],
@@ -32,13 +32,11 @@ describe("OpenRouter provider catalog", () => {
3232
prompt: "0.0000015",
3333
completion: "0.0000075",
3434
input_cache_read: "0.00000015",
35+
input_cache_write: "0.0000025",
3536
overrides: [
3637
{
3738
min_prompt_tokens: 272_000,
3839
prompt: "0.000004",
39-
completion: "0.000015",
40-
input_cache_read: "0.0000004",
41-
input_cache_write: "0.000005",
4240
},
4341
],
4442
},
@@ -73,12 +71,12 @@ describe("OpenRouter provider catalog", () => {
7371
expect.arrayContaining([
7472
"openrouter/auto",
7573
"google/gemini-3.5-flash-lite",
76-
"google/gemini-3.6-flash",
74+
"acme/partial-pricing",
7775
]),
7876
);
7977
expect(provider.models.map((model) => model.id)).not.toContain("google/gemini-3.1-flash-image");
80-
expect(provider.models.find((model) => model.id === "google/gemini-3.6-flash")).toMatchObject({
81-
name: "Google: Gemini 3.6 Flash",
78+
expect(provider.models.find((model) => model.id === "acme/partial-pricing")).toMatchObject({
79+
name: "Partial Pricing Fixture",
8280
reasoning: true,
8381
input: ["text", "image"],
8482
contextWindow: 1_048_576,
@@ -87,15 +85,15 @@ describe("OpenRouter provider catalog", () => {
8785
input: 1.5,
8886
output: 7.5,
8987
cacheRead: 0.15,
90-
cacheWrite: 0,
88+
cacheWrite: 2.5,
9189
tieredPricing: [
92-
{ input: 1.5, output: 7.5, cacheRead: 0.15, cacheWrite: 0, range: [0, 272_000] },
90+
{ input: 1.5, output: 7.5, cacheRead: 0.15, cacheWrite: 2.5, range: [0, 272_001] },
9391
{
9492
input: 4,
95-
output: 15,
96-
cacheRead: expect.closeTo(0.4, 12),
97-
cacheWrite: 5,
98-
range: [272_000],
93+
output: 7.5,
94+
cacheRead: 0.15,
95+
cacheWrite: 2.5,
96+
range: [272_001],
9997
},
10098
],
10199
},

‎packages/model-catalog-core/src/model-catalog-pricing.test.ts‎

Lines changed: 179 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { calculateUsageCost } from "@openclaw/llm-core";
12
import { describe, expect, it } from "vitest";
23
import {
34
normalizeModelPricingProvider,
@@ -85,12 +86,17 @@ describe.each([
8586
});
8687

8788
describe("OpenRouter native pricing", () => {
88-
it("sorts static prompt tiers and ignores scheduled overrides without borrowing foreign prices", () => {
89+
const base = {
90+
prompt: "0.000002",
91+
completion: "0.00001",
92+
input_cache_read: "0.00000025",
93+
input_cache_write: "0.0000025",
94+
};
95+
const cost = { input: 2, output: 10, cacheRead: 0.25, cacheWrite: 2.5 };
96+
97+
it("compiles unordered overrides in source order per key without borrowing foreign prices", () => {
8998
const pricing = {
90-
prompt: "0.000002",
91-
completion: "0.00001",
92-
input_cache_read: "0.0000002",
93-
input_cache_write: "0.0000025",
99+
...base,
94100
// Other feed formats must not supply native OpenRouter rates or tiers.
95101
input: 99,
96102
context_over_200k: { input: 99, output: 99 },
@@ -107,31 +113,190 @@ describe("OpenRouter native pricing", () => {
107113
},
108114
],
109115
};
110-
const cost = { input: 2, output: 10, cacheRead: expect.closeTo(0.2, 12), cacheWrite: 2.5 };
111116
expect(normalizeOpenRouterModelPricing(pricing)).toEqual({
112117
...cost,
113118
tieredPricing: [
114-
{ ...cost, range: [0, 272_000] },
119+
{ ...cost, range: [0, 272_001] },
115120
{
116121
input: 4,
117122
output: 15,
118123
cacheRead: expect.closeTo(0.4, 12),
119124
cacheWrite: 5,
120-
range: [272_000, 500_000],
125+
range: [272_001, 500_001],
121126
},
122-
{ input: 6, output: 20, cacheRead: 0, cacheWrite: 0, range: [500_000] },
127+
{
128+
input: 4,
129+
output: 15,
130+
cacheRead: expect.closeTo(0.4, 12),
131+
cacheWrite: 5,
132+
range: [500_001],
133+
},
134+
],
135+
});
136+
});
137+
138+
it.each([
139+
{ prices: { prompt: "0.000004" }, expected: { input: 4 } },
140+
{ prices: { completion: "0.00002" }, expected: { output: 20 } },
141+
{ prices: { input_cache_read: "0.0000005" }, expected: { cacheRead: 0.5 } },
142+
{ prices: { input_cache_write: "0.000005" }, expected: { cacheWrite: 5 } },
143+
{ prices: { prompt: "0", input_cache_read: "0" }, expected: { input: 0, cacheRead: 0 } },
144+
])("inherits omitted native rates for partial overrides: $prices", ({ prices, expected }) => {
145+
expect(
146+
normalizeOpenRouterModelPricing({
147+
...base,
148+
overrides: [{ min_prompt_tokens: 100, ...prices }],
149+
}),
150+
).toEqual({
151+
...cost,
152+
tieredPricing: [
153+
{ ...cost, range: [0, 101] },
154+
{ ...cost, ...expected, range: [101] },
155+
],
156+
});
157+
});
158+
159+
it("lets equal and lower thresholds override only their supplied keys in source order", () => {
160+
expect(
161+
normalizeOpenRouterModelPricing({
162+
...base,
163+
overrides: [
164+
{ min_prompt_tokens: 500, prompt: "0.000006", completion: "0.00002" },
165+
{ min_prompt_tokens: 200, prompt: "0.000004", input_cache_read: "0.0000005" },
166+
{ min_prompt_tokens: 200, input_cache_write: "0" },
167+
],
168+
}),
169+
).toEqual({
170+
...cost,
171+
tieredPricing: [
172+
{ ...cost, range: [0, 201] },
173+
{ input: 4, output: 10, cacheRead: 0.5, cacheWrite: 0, range: [201, 501] },
174+
{ input: 4, output: 20, cacheRead: 0.5, cacheWrite: 0, range: [501] },
175+
],
176+
});
177+
});
178+
179+
it.each([
180+
{ input: 39, output: 7, cacheRead: 30, cacheWrite: 30, expected: 0.0002305 },
181+
{ input: 40, output: 7, cacheRead: 30, cacheWrite: 30, expected: 0.0002325 },
182+
{ input: 41, output: 7, cacheRead: 30, cacheWrite: 30, expected: 0.0003865 },
183+
{ input: 0, output: 7, cacheRead: 101, cacheWrite: 0, expected: 0.00016525 },
184+
{ input: 0, output: 7, cacheRead: 0, cacheWrite: 101, expected: 0.0003925 },
185+
{ input: 0, output: 1_000, cacheRead: 0, cacheWrite: 0, expected: 0.01 },
186+
])(
187+
"bills strict total-prompt boundaries with inherited cache prices: %j",
188+
({ expected, ...usage }) => {
189+
const pricing = normalizeOpenRouterModelPricing({
190+
...base,
191+
overrides: [{ min_prompt_tokens: 100, prompt: "0.000004", completion: "0.00002" }],
192+
});
193+
expect(pricing).toBeDefined();
194+
if (!pricing) {
195+
throw new Error("Expected a complete native schedule");
196+
}
197+
expect(calculateUsageCost(usage, pricing).total).toBeCloseTo(expected, 12);
198+
},
199+
);
200+
201+
it.each([0, 100.5, Number.MAX_SAFE_INTEGER - 1])(
202+
"compiles a strict threshold %j to the first matching integral prompt size",
203+
(threshold) => {
204+
const start = Math.floor(threshold) + 1;
205+
expect(
206+
normalizeOpenRouterModelPricing({
207+
...base,
208+
overrides: [{ min_prompt_tokens: threshold, prompt: "0" }],
209+
}),
210+
).toEqual({
211+
...cost,
212+
tieredPricing: [
213+
{ ...cost, range: [0, start] },
214+
{ ...cost, input: 0, range: [start] },
215+
],
216+
});
217+
},
218+
);
219+
220+
it.each([
221+
{ future_condition: true },
222+
{ future_price: "0" },
223+
{ utc_start: 1630 },
224+
{ utc_end: 30 },
225+
{ utc_days: ["monday"] },
226+
...[undefined, null, -1, "100", Infinity, Number.NaN, Number.MAX_SAFE_INTEGER].map(
227+
(min_prompt_tokens) => ({ min_prompt_tokens }),
228+
),
229+
])("skips unsupported or invalid predicates: %j", (condition) => {
230+
expect(
231+
normalizeOpenRouterModelPricing({
232+
...base,
233+
overrides: [{ min_prompt_tokens: 100, prompt: "0", ...condition }],
234+
}),
235+
).toEqual(cost);
236+
});
237+
238+
it("ignores known non-token charge dimensions without dropping token overrides", () => {
239+
expect(
240+
normalizeOpenRouterModelPricing({
241+
...base,
242+
overrides: [
243+
{
244+
min_prompt_tokens: 100,
245+
prompt: "0.000004",
246+
request: "1",
247+
image: "1",
248+
web_search: "1",
249+
internal_reasoning: "1",
250+
audio: "1",
251+
input_audio_cache: "1",
252+
input_cache_write_1h: "1",
253+
image_output: "1",
254+
audio_output: "1",
255+
},
256+
],
257+
}),
258+
).toEqual({
259+
...cost,
260+
tieredPricing: [
261+
{ ...cost, range: [0, 101] },
262+
{ ...cost, input: 4, range: [101] },
123263
],
124264
});
125265
});
126266

127-
it("rejects an incomplete native context override and per-million overflow", () => {
267+
it.each([null, "", "1usd", -1, Infinity, "1e308"])(
268+
"invalidates the schedule for malformed effective prices %j",
269+
(invalid) => {
270+
for (const field of Object.keys(base)) {
271+
expect(
272+
normalizeOpenRouterModelPricing({
273+
...base,
274+
overrides: [{ min_prompt_tokens: 100, [field]: invalid }],
275+
}),
276+
).toBeUndefined();
277+
}
278+
},
279+
);
280+
281+
it("validates effective prices after later matching entries replace malformed fields", () => {
128282
expect(
129283
normalizeOpenRouterModelPricing({
130-
prompt: "0.000002",
131-
completion: "0.00001",
132-
overrides: [{ min_prompt_tokens: 272_000, prompt: "0.000004" }],
284+
...base,
285+
overrides: [
286+
{ min_prompt_tokens: 100, prompt: "bad" },
287+
{ min_prompt_tokens: 100, prompt: "0" },
288+
],
133289
}),
134-
).toBeUndefined();
290+
).toEqual({
291+
...cost,
292+
tieredPricing: [
293+
{ ...cost, range: [0, 101] },
294+
{ ...cost, input: 0, range: [101] },
295+
],
296+
});
297+
});
298+
299+
it("rejects per-million overflow and foreign base prices", () => {
135300
expect(normalizeOpenRouterModelPricing({ prompt: "1e308", completion: "0" })).toBeUndefined();
136301
expect(normalizeOpenRouterModelPricing({ input: 2, output: 10 })).toBeUndefined();
137302
});

0 commit comments

Comments
 (0)