🪢 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(traces): normalize cost breakdown formatting (#17482)
Hassieb Pakzad054c84f3f8d7
diff --git a/web/src/features/traces/components/BreakdownTooltip.stories.tsx b/web/src/features/traces/components/BreakdownTooltip.stories.tsxindex b6eec55e6f..94c7c5799f 100644--- a/web/src/features/traces/components/BreakdownTooltip.stories.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.stories.tsx@@ -34,6 +34,19 @@ const longUsageTypeCostDetails = {total: 0.03824809,};+const floatingPointCostDetails = {+ input: 0.0000275,+ input_cached_tokens: 0,+ output: 0.00015,+ total: 0.000177499999,+};++const providedTotalCostDetails = {+ input: 0,+ output: 0,+ total: 0.01,+};+const priceSource = {projectId: "project-1",modelId: "gpt-5.6/priority",@@ -155,46 +168,44 @@ export const TestLinksMatchedPricingTier = meta.story({});export const TestProvidedAtIngestionLabel = meta.story({- name: "(Test) Shows provided at ingestion label",+ name: "(Test) Preserves provided total cost",args: {- details: costDetails,- children: <span>$0.001725</span>,+ details: providedTotalCostDetails,+ children: <span>$0.01</span>,isCost: true,costSource: "provided",},play: async ({ canvasElement }) => {- const { content } = await openBreakdownTooltip(canvasElement, "$0.001725");+ const { content } = await openBreakdownTooltip(canvasElement, "$0.01");await expect(content.getByText("Provided at ingestion"),).toBeInTheDocument();await expect(content.queryByRole("link")).not.toBeInTheDocument();+ await expect(content.getByText("$0.01")).toBeInTheDocument();+ await expect(content.getAllByText("—")).toHaveLength(4);},});export const TestCostFormattingAndTruncation = meta.story({- name: "(Test) Formats costs without padded decimals",+ name: "(Test) Aligns cost precision and hides zero costs",args: {- details: cacheCostDetails,- children: <span>$0.03782809</span>,+ details: floatingPointCostDetails,+ children: <span>$0.0001775</span>,isCost: true,},play: async ({ canvasElement }) => {- const { content } = await openBreakdownTooltip(- canvasElement,- "$0.03782809",- );+ const { content } = await openBreakdownTooltip(canvasElement, "$0.0001775");- await expect(content.getAllByText("$0.0066")).not.toHaveLength(0);- await expect(content.queryByText("$0.0066000")).not.toBeInTheDocument();- await expect(content.getByText("$0.03782809")).toBeInTheDocument();- await expect(content.queryByText("$0.0378280")).not.toBeInTheDocument();- await expect(content.getAllByText("$0.00000009")).not.toHaveLength(0);+ await expect(content.getAllByText("$0.0000275")).toHaveLength(2);+ await expect(content.getAllByText("$0.0001500")).toHaveLength(2);+ await expect(content.getByText("$0.0001775")).toBeInTheDocument();+ await expect(+ content.queryByText("$0.000177499999"),+ ).not.toBeInTheDocument();+ await expect(content.getByText("—")).toBeInTheDocument();- const longLabel = content.getByText("cache_creation_input_tokens");+ const longLabel = content.getByText("input_cached_tokens");await expect(longLabel).toHaveClass("truncate");- await expect(longLabel).toHaveAttribute(- "title",- "cache_creation_input_tokens",- );+ await expect(longLabel).toHaveAttribute("title", "input_cached_tokens");},});diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsxindex 957cae08f6..8829e65d1f 100644--- a/web/src/features/traces/components/BreakdownTooltip.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.tsx@@ -54,8 +54,6 @@ export const BreakdownTooltip = ({}, {}): details;- const formatValue = (value: number) =>- isCost ? usdFormatter(value, 2, 12) : value ? value.toLocaleString() : "0";const entries = Object.entries(aggregatedDetails);const inputEntries = sortEntriesByValue(entries.filter(([key]) => key.includes("input")),@@ -75,7 +73,25 @@ export const BreakdownTooltip = ({return acc + value;}, 0);const contributionEntries = inputEntries.concat(outputEntries, otherEntries);+ const costFractionDigits = getCostFractionDigits(+ contributionEntries.length > 0+ ? contributionEntries+ : [["total", aggregatedDetails.total]],+ );+ const formatValue = (value: number | Decimal) => {+ if (!isCost) {+ const numericValue = value instanceof Decimal ? value.toNumber() : value;+ return numericValue ? numericValue.toLocaleString() : "0";+ }++ return new Decimal(value).isZero()+ ? "—"+ : usdFormatter(value, costFractionDigits, costFractionDigits);+ };const waterfallSegments = createWaterfallSegments(contributionEntries);+ const displayedTotal =+ aggregatedDetails.total ??+ (isCost ? sumEntries(contributionEntries) : new Decimal(0));const resolvedCostSource =costSource ?? (isCost && priceSource ? "calculated" : undefined);@@ -179,7 +195,7 @@ export const BreakdownTooltip = ({{/* Total */}<BreakdownRowlabel={isCost ? "Total cost" : "Total usage"}- value={formatValue(aggregatedDetails.total ?? 0)}+ value={formatValue(displayedTotal)}variant="total"/></div>@@ -285,7 +301,7 @@ function BreakdownRow({interface SectionProps {title: string;entries: [string, number | undefined][];- formatValue: (value: number) => string;+ formatValue: (value: number | Decimal) => string;waterfallSegments: Map<string, WaterfallSegment>;}@@ -295,11 +311,7 @@ const Section = ({formatValue,waterfallSegments,}: SectionProps) => {- const sectionTotal = entries.reduce(- (sum, [_, value]) =>- new Decimal(sum).plus(new Decimal(value ?? 0)).toNumber(),- 0,- );+ const sectionTotal = sumEntries(entries);return (<div className="col-span-3 grid min-w-0 grid-cols-subgrid gap-y-2">@@ -325,6 +337,26 @@ function sortEntriesByValue(entries: [string, number | undefined][]) {return entries.toSorted(([, a], [, b]) => (b ?? 0) - (a ?? 0));}+function sumEntries(entries: [string, number | undefined][]) {+ return entries.reduce(+ (sum, [, value]) => sum.plus(value ?? 0),+ new Decimal(0),+ );+}++function getCostFractionDigits(entries: [string, number | undefined][]) {+ return Math.min(+ 12,+ Math.max(+ 2,+ ...entries+ .map(([, value]) => value)+ .filter((value): value is number => Boolean(value))+ .map((value) => new Decimal(value).decimalPlaces()),+ ),+ );+}+function createWaterfallSegments(entries: [string, number | undefined][],): Map<string, WaterfallSegment> {