spb/chat-spboucher-ai
Public
1import type { AIModel, Usage } from "./types";23/** Estimated cost in USD from verified list prices; null when the model has no verified price. */4export function estimateCost(model: Pick<AIModel, "pricing">, usage: Usage): number | null {5 const p = model.pricing;6 if (!p || p.input == null || p.output == null) return null;7 const cached = usage.cached ?? 0;8 const fresh = Math.max(0, usage.input - cached);9 return (fresh * p.input + cached * (p.cachedInput ?? p.input) + usage.output * p.output) / 1e6;10}1112export function blendedPrice(model: Pick<AIModel, "pricing">): number | null {13 const p = model.pricing;14 if (!p || p.input == null || p.output == null) return null;15 return (3 * p.input + p.output) / 4;16}1718export function fmtUsd(v: number | null | undefined, digits = 4): string {19 if (v == null) return "—";20 if (v === 0) return "$0";21 if (v < 0.01) return `$${v.toFixed(digits)}`;22 return `$${v.toFixed(2)}`;23}24