import type { AIModel, Usage } from "./types"; /** Estimated cost in USD from verified list prices; null when the model has no verified price. */ export function estimateCost(model: Pick, usage: Usage): number | null { const p = model.pricing; if (!p || p.input == null || p.output == null) return null; const cached = usage.cached ?? 0; const fresh = Math.max(0, usage.input - cached); return (fresh * p.input + cached * (p.cachedInput ?? p.input) + usage.output * p.output) / 1e6; } export function blendedPrice(model: Pick): number | null { const p = model.pricing; if (!p || p.input == null || p.output == null) return null; return (3 * p.input + p.output) / 4; } export function fmtUsd(v: number | null | undefined, digits = 4): string { if (v == null) return "—"; if (v === 0) return "$0"; if (v < 0.01) return `$${v.toFixed(digits)}`; return `$${v.toFixed(2)}`; }