Skip to content

Commit a51164c

Browse files
fix(server): usage prices bare model names at the maker's rate when a reseller marks them up (pingdotgg#28004)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 79f3ee1 commit a51164c

2 files changed

Lines changed: 46 additions & 6 deletions

File tree

‎apps/server/src/usage/usagePricing.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,32 @@ describe("usage pricing", () => {
258258
expect(lookupRate(table, "provider-b/example-model")?.inputCostPerToken).toBe(3);
259259
expect(lookupRate(table, "example-model")).toBeNull();
260260
});
261+
262+
it("prices a bare name at the model maker's rate when a reseller marks it up", () => {
263+
const reseller = { "kyma/gemini-4-argon": rate(5.4e-6) };
264+
const makers = {
265+
"gemini/gemini-4-argon": rate(2e-6, 1e-7),
266+
"vertex_ai/gemini-4-argon": rate(2e-6, 1e-7),
267+
};
268+
269+
for (const document of [
270+
{ ...reseller, ...makers },
271+
{ ...makers, ...reseller },
272+
]) {
273+
const table = parseRateTable(document);
274+
expect(lookupRate(table, "gemini-4-argon")).toEqual(
275+
lookupRate(table, "gemini/gemini-4-argon"),
276+
);
277+
}
278+
});
279+
280+
it("leaves a bare name unpriced when model makers' own entries disagree", () => {
281+
const table = parseRateTable({
282+
"openai/example-model": rate(1),
283+
"xai/example-model": rate(3),
284+
"kyma/example-model": rate(1),
285+
});
286+
287+
expect(lookupRate(table, "example-model")).toBeNull();
288+
});
261289
});

‎apps/server/src/usage/usagePricing.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ function fastMultiplier(entry: LiteLlmEntry): number | null {
8989
return fast !== null && fast > 0 ? fast : null;
9090
}
9191

92+
// LiteLLM prefixes for the model makers' own APIs; their price beats a cloud's or reseller's for a bare name.
93+
const FIRST_PARTY_PROVIDERS = new Set([
94+
"anthropic",
95+
"deepseek",
96+
"gemini",
97+
"meta",
98+
"mistral",
99+
"openai",
100+
"xai",
101+
]);
102+
92103
/** Drops models missing input or output rates to avoid understating cost. */
93104
export function parseRateTable(document: unknown): RateTable {
94105
const table = new Map<string, ModelRate>();
@@ -113,18 +124,19 @@ export function parseRateTable(document: unknown): RateTable {
113124
});
114125
}
115126

116-
const aliasCandidates = new Map<string, ModelRate | null>();
127+
const aliasCandidates = new Map<string, { rate: ModelRate | null; firstParty: boolean }>();
117128
for (const [key, rate] of table) {
118129
const alias = bareModelName(key);
119130
if (alias.length === 0 || alias === key || table.has(alias)) continue;
131+
const firstParty = FIRST_PARTY_PROVIDERS.has(key.slice(0, key.indexOf("/")));
120132
const held = aliasCandidates.get(alias);
121-
if (held === undefined) {
122-
aliasCandidates.set(alias, rate);
123-
} else if (held !== null) {
124-
aliasCandidates.set(alias, mergeAliasRate(held, rate));
133+
if (held === undefined || (firstParty && !held.firstParty)) {
134+
aliasCandidates.set(alias, { rate, firstParty });
135+
} else if (held.rate !== null && firstParty === held.firstParty) {
136+
aliasCandidates.set(alias, { rate: mergeAliasRate(held.rate, rate), firstParty });
125137
}
126138
}
127-
for (const [alias, rate] of aliasCandidates) {
139+
for (const [alias, { rate }] of aliasCandidates) {
128140
if (rate !== null) table.set(alias, rate);
129141
}
130142

0 commit comments

Comments
 (0)