🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(web): keep sub-cent dashboard costs visible instead of $0.00 (#18582)
Der Gerät27f0656d47c7
diff --git a/web/src/utils/numbers.clienttest.ts b/web/src/utils/numbers.clienttest.tsindex 2ade9155f8..8edf8c0e9f 100644--- a/web/src/utils/numbers.clienttest.ts+++ b/web/src/utils/numbers.clienttest.ts@@ -11,4 +11,25 @@ describe("costFormatter", () => {expect(costFormatter(0.015427)).toBe("$0.02");expect(costFormatter(0)).toBe("$0.00");});++ it("keeps sub-cent costs visible instead of rounding them to $0.00", () => {+ expect(costFormatter(0.000601)).toBe("$0.000601");+ });++ it("uses cent precision from one cent up", () => {+ expect(costFormatter(0.01)).toBe("$0.01");+ // Three significant digits can still round up to a full cent.+ expect(costFormatter(0.009999)).toBe("$0.01");+ });++ it("stops widening at ten fraction digits", () => {+ expect(costFormatter(0.0000000001)).toBe("$0.0000000001");+ expect(costFormatter(0.00000000001)).toBe("$0.00");+ });++ it("formats missing and negative costs", () => {+ expect(costFormatter(undefined)).toBe("$0.00");+ expect(costFormatter(-0.000601)).toBe("-$0.000601");+ expect(costFormatter(-4.402973)).toBe("-$4.40");+ });});diff --git a/web/src/utils/numbers.ts b/web/src/utils/numbers.tsindex 1dde00ee10..72306822a2 100644--- a/web/src/utils/numbers.ts+++ b/web/src/utils/numbers.ts@@ -76,10 +76,32 @@ export const usdFormatter = (}).format(numberToFormat ?? 0);};+/** Below this, two fraction digits would render a real cost as "$0.00". */+const SUB_CENT_THRESHOLD = 0.01;+/** Upper bound on the widening, so a dust value cannot stretch a table cell. */+const MAX_SUB_CENT_FRACTION_DIGITS = 10;+export const costFormatter = (totalCost?: number) => {- return usdFormatter(totalCost, 2, 2);+ const cost = totalCost ?? 0;++ if (+ typeof cost === "number" &&+ cost !== 0 &&+ Math.abs(cost) < SUB_CENT_THRESHOLD+ ) {+ return usdFormatter(cost, 2, subCentFractionDigits(cost));+ }++ return usdFormatter(cost, 2, 2);};+/** Returns the fraction digits that render a sub-cent cost to ~3 significant digits. */+const subCentFractionDigits = (cost: number) =>+ Math.min(+ 2 + Math.ceil(-Math.log10(Math.abs(cost))),+ MAX_SUB_CENT_FRACTION_DIGITS,+ );+export const formatTokenCounts = (inputUsage?: number | null,outputUsage?: number | null,