From 7a3cb6312e94c9276546d93b65b2524fc513e7ff Mon Sep 17 00:00:00 2001 From: Michael Yu Date: Sat, 13 Jun 2026 06:01:04 +0800 Subject: [PATCH] fix(ai): normalize generated model costs (#5634) --- packages/ai/scripts/generate-models.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/ai/scripts/generate-models.ts b/packages/ai/scripts/generate-models.ts index a3892cf1..c74a6afc 100644 --- a/packages/ai/scripts/generate-models.ts +++ b/packages/ai/scripts/generate-models.ts @@ -372,6 +372,10 @@ function normalizeNvidiaModelId(modelId: string): string { return modelId.toLowerCase().replaceAll("_", "."); } +function roundCost(value: number): number { + return Number(value.toFixed(6)); +} + async function fetchNvidiaNimModelIds(): Promise> { try { console.log("Fetching models from NVIDIA NIM API..."); @@ -417,10 +421,10 @@ async function fetchOpenRouterModels(): Promise[]> { } // Convert pricing from $/token to $/million tokens - const inputCost = parseFloat(model.pricing?.prompt || "0") * 1_000_000; - const outputCost = parseFloat(model.pricing?.completion || "0") * 1_000_000; - const cacheReadCost = parseFloat(model.pricing?.input_cache_read || "0") * 1_000_000; - const cacheWriteCost = parseFloat(model.pricing?.input_cache_write || "0") * 1_000_000; + const inputCost = roundCost(parseFloat(model.pricing?.prompt || "0") * 1_000_000); + const outputCost = roundCost(parseFloat(model.pricing?.completion || "0") * 1_000_000); + const cacheReadCost = roundCost(parseFloat(model.pricing?.input_cache_read || "0") * 1_000_000); + const cacheWriteCost = roundCost(parseFloat(model.pricing?.input_cache_write || "0") * 1_000_000); const normalizedModel: Model = { id: modelKey, @@ -476,10 +480,10 @@ async function fetchAiGatewayModels(): Promise[]> { input.push("image"); } - const inputCost = toNumber(model.pricing?.input) * 1_000_000; - const outputCost = toNumber(model.pricing?.output) * 1_000_000; - const cacheReadCost = toNumber(model.pricing?.input_cache_read) * 1_000_000; - const cacheWriteCost = toNumber(model.pricing?.input_cache_write) * 1_000_000; + const inputCost = roundCost(toNumber(model.pricing?.input) * 1_000_000); + const outputCost = roundCost(toNumber(model.pricing?.output) * 1_000_000); + const cacheReadCost = roundCost(toNumber(model.pricing?.input_cache_read) * 1_000_000); + const cacheWriteCost = roundCost(toNumber(model.pricing?.input_cache_write) * 1_000_000); models.push({ id: model.id,