🪢 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): quieter cost and usage breakdown tooltip (#18788)
Trang Nguyen3983d93c2fb5
diff --git a/web/src/features/traces/components/BreakdownTooltip.stories.tsx b/web/src/features/traces/components/BreakdownTooltip.stories.tsxindex cfde865c83..d1515ac07a 100644--- a/web/src/features/traces/components/BreakdownTooltip.stories.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.stories.tsx@@ -170,7 +170,7 @@ export const TestLinksMatchedPricingTier = meta.story({"href","/project/project-1/settings/models/gpt-5.6%2Fpriority?pricingTier=tier-priority",);- await expect(link).toHaveClass("text-xs", "italic");+ await expect(link).toHaveClass("text-xs");},});@@ -194,7 +194,7 @@ export const TestProvidedAtIngestionLabel = meta.story({});export const TestCostFormattingAndTruncation = meta.story({- name: "(Test) Aligns cost precision and hides zero costs",+ name: "(Test) Flags sub-cent costs, keeps exact values in titles",args: {details: floatingPointCostDetails,children: <span>$0.0001775</span>,@@ -203,11 +203,12 @@ export const TestCostFormattingAndTruncation = meta.story({play: async ({ canvasElement }) => {const { content } = await openBreakdownTooltip(canvasElement, "$0.0001775");- 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.getAllByText("<$0.01")).toHaveLength(5);+ await expect(content.getAllByTitle("$0.0000275")).toHaveLength(2);+ await expect(content.getAllByTitle("$0.0001500")).toHaveLength(2);+ await expect(content.getByTitle("$0.0001775")).toBeInTheDocument();await expect(- content.queryByText("$0.000177499999"),+ content.queryByTitle("$0.000177499999"),).not.toBeInTheDocument();await expect(content.getByText("—")).toBeInTheDocument();@@ -227,13 +228,39 @@ export const TestNearDecimalCostFormatting = meta.story({play: async ({ canvasElement }) => {const { content } = await openBreakdownTooltip(canvasElement, "$0.058343");- await expect(content.getAllByText("$0.039065")).toHaveLength(1);- await expect(content.getAllByText("$0.003488")).toHaveLength(1);- await expect(content.getAllByText("$0.015790")).toHaveLength(2);- await expect(content.getByText("$0.042553")).toBeInTheDocument();- await expect(content.getByText("$0.058343")).toBeInTheDocument();+ await expect(content.getAllByText("$0.04")).toHaveLength(2);+ await expect(content.getByText("<$0.01")).toBeInTheDocument();+ await expect(content.getAllByText("$0.02")).toHaveLength(2);+ await expect(content.getByText("$0.06")).toBeInTheDocument();+ await expect(content.getByTitle("$0.039065")).toBeInTheDocument();+ await expect(content.getByTitle("$0.003488")).toBeInTheDocument();+ await expect(content.getAllByTitle("$0.015790")).toHaveLength(2);+ await expect(content.getByTitle("$0.042553")).toBeInTheDocument();+ await expect(content.getByTitle("$0.058343")).toBeInTheDocument();await expect(- content.queryByText("$0.058342999997"),+ content.queryByTitle("$0.058342999997"),).not.toBeInTheDocument();},});++export const TestShareOfTotal = meta.story({+ name: "(Test) Shows each row's share of the total",+ args: {+ details: cacheCostDetails,+ children: <span>$0.03782809</span>,+ isCost: true,+ },+ play: async ({ canvasElement }) => {+ const { content } = await openBreakdownTooltip(+ canvasElement,+ "$0.03782809",+ );++ await expect(content.getByText("70%")).toBeInTheDocument();+ await expect(content.getByText("83%")).toBeInTheDocument();+ await expect(content.getAllByText("17%")).toHaveLength(2);+ // Other section: subtotal and its only row.+ await expect(content.getAllByText("<1%")).toHaveLength(2);+ await expect(content.getByText("$0.04")).toBeInTheDocument();+ },+});diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsxindex 488825cd9c..65fc66c98d 100644--- a/web/src/features/traces/components/BreakdownTooltip.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.tsx@@ -1,4 +1,3 @@-/* eslint-disable no-nested-ternary */import {Tooltip,TooltipContent,@@ -10,8 +9,9 @@ import Decimal from "decimal.js";import Link from "next/link";import { type Details } from "@/src/features/traces/fns/calculateAggregatedUsage";import { ExternalLink } from "lucide-react";-import { usdFormatter } from "@/src/utils/numbers";+import { centCostFormatter, usdFormatter } from "@/src/utils/numbers";import { cva, type VariantProps } from "class-variance-authority";+import { cn } from "@/src/utils/tailwind";export interface PriceSource {projectId: string;@@ -68,24 +68,20 @@ export const BreakdownTooltip = ({!key.includes("input") && !key.includes("output") && key !== "total",).sort(([, a], [, b]) => (b ?? 0) - (a ?? 0));- const otherTotal = otherEntries.reduce((acc, [, value]) => {- if (typeof value !== "number") return acc;-- return acc + value;- }, 0);const contributionEntries = inputEntries.concat(outputEntries, otherEntries);const costFractionDigits = getCostFractionDigits(entries);const formatValue = (value: number | Decimal) => {- if (!isCost) {- const numericValue = value instanceof Decimal ? value.toNumber() : value;- return numericValue ? numericValue.toLocaleString() : "0";- }+ const numericValue = value instanceof Decimal ? value.toNumber() : value;+ if (!isCost) return numericValue ? numericValue.toLocaleString() : "0";- return new Decimal(value).isZero()- ? "—"- : usdFormatter(value, costFractionDigits, costFractionDigits);+ return numericValue === 0 ? "—" : centCostFormatter(numericValue);};- const waterfallSegments = createWaterfallSegments(contributionEntries);+ /** Full-precision cost for the value's `title`. */+ const formatExactValue = (value: number | Decimal) =>+ isCost && !new Decimal(value).isZero()+ ? usdFormatter(value, costFractionDigits, costFractionDigits)+ : undefined;+ const shareOf = createShareOf(contributionEntries);const displayedTotal =aggregatedDetails.total ??(isCost ? sumEntries(contributionEntries) : new Decimal(0));@@ -93,6 +89,8 @@ export const BreakdownTooltip = ({const resolvedCostSource =costSource ?? (isCost && priceSource ? "calculated" : undefined);+ const sectionProps = { formatValue, formatExactValue, shareOf };+return (<TooltipProvider><Tooltip open={isOpen} onOpenChange={setIsOpen}>@@ -102,15 +100,15 @@ export const BreakdownTooltip = ({>{children}</TooltipTrigger>- <TooltipContent className="w-fit max-w-[calc(100vw-2rem)] p-4">- <div className="grid min-w-0 grid-cols-[max-content_7rem_max-content] gap-x-3 gap-y-4 max-sm:w-full max-sm:grid-cols-[minmax(0,1fr)_6rem_max-content]">- <div className="col-span-3 flex min-w-0 flex-col gap-1">- <span className="font-bold">+ <TooltipContent className="w-fit max-w-[calc(100vw-2rem)] p-3">+ <div className="grid min-w-0 grid-cols-[repeat(4,max-content)] gap-x-3 gap-y-3 max-sm:w-full max-sm:grid-cols-[minmax(0,1fr)_repeat(3,max-content)]">+ <div className="col-span-4 flex min-w-0 flex-col gap-0.5">+ <span className="text-sm font-bold">{isCost ? "Cost breakdown" : "Usage breakdown"}</span>{isCost && resolvedCostSource === "provided" ? (- <span className="text-muted-foreground text-xs italic">+ <span className="text-muted-foreground text-xs">Provided at ingestion</span>) : null}@@ -118,7 +116,7 @@ export const BreakdownTooltip = ({{isCost && resolvedCostSource === "calculated" && priceSource ? (<Linkhref={`/project/${encodeURIComponent(priceSource.projectId)}/settings/models/${encodeURIComponent(priceSource.modelId)}?pricingTier=${encodeURIComponent(priceSource.pricingTierId)}`}- className="text-muted-foreground flex min-w-0 flex-row gap-1 text-xs italic underline-offset-4 hover:underline"+ className="text-muted-foreground inline-flex min-w-0 items-center gap-1 text-xs underline-offset-4 hover:underline"target="_blank"rel="noopener noreferrer">@@ -133,13 +131,13 @@ export const BreakdownTooltip = ({) : null}{isCost && resolvedCostSource === "calculated" && !priceSource ? (- <span className="text-muted-foreground text-xs italic">+ <span className="text-muted-foreground text-xs">Calculated from model pricing</span>) : null}{Array.isArray(details) && details.length > 0 && (- <span className="text-muted-foreground text-xs italic">+ <span className="text-muted-foreground text-xs">Aggregate across {details.length}{" "}{details.length === 1 ? "generation" : "generations"}</span>@@ -149,50 +147,37 @@ export const BreakdownTooltip = ({label="Pricing Tier:"value={pricingTierName}variant="item"+ layout="inline"/>)}</div>- {/* Input Section */}<Sectiontitle={isCost ? "Input cost" : "Input usage"}entries={inputEntries}- formatValue={formatValue}- waterfallSegments={waterfallSegments}+ {...sectionProps}/>- {/* Output Section */}<Sectiontitle={isCost ? "Output cost" : "Output usage"}entries={outputEntries}- formatValue={formatValue}- waterfallSegments={waterfallSegments}+ separated+ {...sectionProps}/>- {/* Other Section */}{otherEntries.length > 0 && (- <div className="col-span-3 grid min-w-0 grid-cols-subgrid gap-y-2">- <BreakdownRow- label={isCost ? "Other cost" : "Other usage"}- value={formatValue(otherTotal)}- variant="section"- />- {otherEntries.map(([key, value]) => (- <BreakdownRow- key={key}- label={key}- value={formatValue(value ?? 0)}- variant="item"- waterfallSegment={waterfallSegments.get(key)}- />- ))}- </div>+ <Section+ title={isCost ? "Other cost" : "Other usage"}+ entries={otherEntries}+ separated+ {...sectionProps}+ />)}- {/* Total */}<BreakdownRowlabel={isCost ? "Total cost" : "Total usage"}value={formatValue(displayedTotal)}+ exactValue={formatExactValue(displayedTotal)}variant="total"/></div>@@ -202,95 +187,76 @@ export const BreakdownTooltip = ({);};-const breakdownRowVariants = cva("min-w-0 items-center text-xs", {+const breakdownRowVariants = cva("min-w-0 items-center text-sm leading-5", {variants: {layout: {- default: "col-span-3 flex gap-3",- waterfall: "col-span-3 grid grid-cols-subgrid",+ inline: "flex gap-3",+ grid: "col-span-4 grid grid-cols-subgrid",},variant: {item: "text-muted-foreground",- section: "border-b pb-1 font-bold",- total: "border-t border-b-4 border-double py-1 font-bold",+ section: "font-bold",+ total: "border-t pt-1.5 font-bold",},},defaultVariants: {- layout: "default",+ layout: "grid",variant: "item",},});-const breakdownLabelVariants = cva("min-w-0 truncate", {- variants: {- layout: {- default: "flex-1",- waterfall: "max-w-52",- },- },-});--const waterfallSegmentVariants = cva("bg-primary/60 absolute h-full", {- variants: {- edge: {- only: "rounded-sm",- first: "rounded-l-sm",- middle: "",- last: "rounded-r-sm",- },- },-});--interface WaterfallSegment {- left: number;- width: number;- edge: NonNullable<VariantProps<typeof waterfallSegmentVariants>["edge"]>;-}-function BreakdownRow({label,value,+ exactValue,variant,- waterfallSegment,+ layout = "grid",+ share,}: {label: string;value: string;- variant: NonNullable<VariantProps<typeof breakdownRowVariants>["variant"]>;- waterfallSegment?: WaterfallSegment;+ exactValue?: string;+ variant: NonNullable<BreakdownRowVariants["variant"]>;+ layout?: BreakdownRowVariants["layout"];+ /** Share of all components in percent; shown as a bar on item rows. */+ share?: number;}) {- const layout = waterfallSegment ? "waterfall" : "default";+ const hasBar = layout === "grid" && variant === "item";return (- <div- className={breakdownRowVariants({- layout,- variant,- })}- >- <span className={breakdownLabelVariants({ layout })} title={label}>+ <div className={breakdownRowVariants({ layout, variant })}>+ <span+ className={cn(+ "min-w-0 truncate",+ layout === "inline" && "flex-1",+ layout === "grid" && (hasBar ? "max-w-52" : "col-span-2"),+ )}+ title={label}+ >{label}</span>- {waterfallSegment ? (+ {hasBar ? (<span- className="bg-muted relative h-2 overflow-hidden rounded-sm"+ className="bg-border relative h-1 w-16 overflow-hidden rounded-sm"aria-hidden="true"><span- className={waterfallSegmentVariants({- edge: waterfallSegment.edge,- })}- style={{- left: `${waterfallSegment.left}%`,- width: `${waterfallSegment.width}%`,- }}+ className="bg-muted-foreground absolute left-0 h-full rounded-sm"+ style={{ width: `${share ?? 0}%` }}/></span>) : null}<span- className="text-right font-mono whitespace-nowrap tabular-nums"- title={value}+ className="text-right font-mono text-xs whitespace-nowrap tabular-nums"+ title={exactValue ?? value}>{value}</span>+ {share !== undefined ? (+ <span className="text-muted-foreground text-right font-mono text-xs font-normal tabular-nums">+ {formatShare(share)}+ </span>+ ) : null}</div>);}@@ -299,31 +265,44 @@ interface SectionProps {title: string;entries: [string, number | undefined][];formatValue: (value: number | Decimal) => string;- waterfallSegments: Map<string, WaterfallSegment>;+ formatExactValue: (value: number | Decimal) => string | undefined;+ shareOf: (value: number | Decimal) => number;+ /** Hairline above the section, matching the total row. */+ separated?: boolean;}const Section = ({title,entries,formatValue,- waterfallSegments,+ formatExactValue,+ shareOf,+ separated = false,}: SectionProps) => {const sectionTotal = sumEntries(entries);return (- <div className="col-span-3 grid min-w-0 grid-cols-subgrid gap-y-2">+ <div+ className={cn(+ "col-span-4 grid min-w-0 grid-cols-subgrid gap-y-1",+ separated && "border-t pt-1.5",+ )}+ ><BreakdownRowlabel={title}value={formatValue(sectionTotal)}+ exactValue={formatExactValue(sectionTotal)}variant="section"+ share={shareOf(sectionTotal)}/>{entries.map(([key, value]) => (<BreakdownRowkey={key}label={key}value={formatValue(value ?? 0)}+ exactValue={formatExactValue(value ?? 0)}variant="item"- waterfallSegment={waterfallSegments.get(key)}+ share={shareOf(value ?? 0)}/>))}</div>@@ -369,41 +348,23 @@ function getArtifactFreeFractionDigits(value: number) {return 12;}-function createWaterfallSegments(- entries: [string, number | undefined][],-): Map<string, WaterfallSegment> {+/** Percent of the summed components, so bars and shares agree. */+function createShareOf(entries: [string, number | undefined][]) {const total = entries.reduce((sum, [, value]) => sum + Math.max(value ?? 0, 0),0,);- const contributionCount = entries.reduce(- (count, [, value]) => count + ((value ?? 0) > 0 ? 1 : 0),- 0,- );- if (total === 0) return new Map();-- let cumulative = 0;- let contributionIndex = 0;- return new Map(- entries.map(([key, value]) => {- const contribution = Math.max(value ?? 0, 0);- const edge: WaterfallSegment["edge"] =- contributionCount === 1- ? "only"- : contributionIndex === 0- ? "first"- : contributionIndex === contributionCount - 1- ? "last"- : "middle";- const segment = {- left: (cumulative / total) * 100,- width: (contribution / total) * 100,- edge,- };- cumulative += contribution;- if (contribution > 0) contributionIndex += 1;- return [key, segment];- }),- );+ return (value: number | Decimal) => {+ if (total === 0) return 0;+ const numericValue = value instanceof Decimal ? value.toNumber() : value;+ return (Math.max(numericValue, 0) / total) * 100;+ };+}++function formatShare(share: number) {+ const rounded = Math.round(share);+ return share > 0 && rounded === 0 ? "<1%" : `${rounded}%`;}++type BreakdownRowVariants = VariantProps<typeof breakdownRowVariants>;diff --git a/web/src/utils/numbers.clienttest.ts b/web/src/utils/numbers.clienttest.tsindex 8edf8c0e9f..9443bff480 100644--- a/web/src/utils/numbers.clienttest.ts+++ b/web/src/utils/numbers.clienttest.ts@@ -2,7 +2,7 @@import { describe, expect, it } from "vitest";-import { costFormatter } from "@/src/utils/numbers";+import { centCostFormatter, costFormatter } from "@/src/utils/numbers";describe("costFormatter", () => {it("formats dashboard costs with cent precision", () => {@@ -33,3 +33,17 @@ describe("costFormatter", () => {expect(costFormatter(-4.402973)).toBe("-$4.40");});});++describe("centCostFormatter", () => {+ it("rounds to cents and flags sub-cent costs instead of showing $0.00", () => {+ expect(centCostFormatter(0.36674048)).toBe("$0.37");+ expect(centCostFormatter(0.005)).toBe("$0.01");+ expect(centCostFormatter(0.0049)).toBe("<$0.01");+ expect(centCostFormatter(0)).toBe("$0.00");+ });++ it("keeps the sign on negative costs", () => {+ expect(centCostFormatter(-0.37)).toBe("-$0.37");+ expect(centCostFormatter(-0.0049)).toBe("-<$0.01");+ });+});diff --git a/web/src/utils/numbers.ts b/web/src/utils/numbers.tsindex 72306822a2..a3387f7ebc 100644--- a/web/src/utils/numbers.ts+++ b/web/src/utils/numbers.ts@@ -95,6 +95,15 @@ export const costFormatter = (totalCost?: number) => {return usdFormatter(cost, 2, 2);};+/** Cent precision; a non-zero cost that would round to $0.00 reads "<$0.01". */+export const centCostFormatter = (cost: number) => {+ if (cost !== 0 && Math.abs(cost) < SUB_CENT_THRESHOLD / 2) {+ return `${cost < 0 ? "-" : ""}<${usdFormatter(SUB_CENT_THRESHOLD, 2, 2)}`;+ }++ 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(