🪢 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
feat(traces): add waterfall to breakdown tooltips (#17407)
Hassieb Pakzadc3f89fe466bd
diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsxindex 9617063271..957cae08f6 100644--- a/web/src/features/traces/components/BreakdownTooltip.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.tsx@@ -56,7 +56,14 @@ export const BreakdownTooltip = ({const formatValue = (value: number) =>isCost ? usdFormatter(value, 2, 12) : value ? value.toLocaleString() : "0";- const otherEntries = Object.entries(aggregatedDetails)+ const entries = Object.entries(aggregatedDetails);+ const inputEntries = sortEntriesByValue(+ entries.filter(([key]) => key.includes("input")),+ );+ const outputEntries = sortEntriesByValue(+ entries.filter(([key]) => key.includes("output")),+ );+ const otherEntries = entries.filter(([key]) =>!key.includes("input") && !key.includes("output") && key !== "total",@@ -67,6 +74,8 @@ export const BreakdownTooltip = ({return acc + value;}, 0);+ const contributionEntries = inputEntries.concat(outputEntries, otherEntries);+ const waterfallSegments = createWaterfallSegments(contributionEntries);const resolvedCostSource =costSource ?? (isCost && priceSource ? "calculated" : undefined);@@ -80,9 +89,9 @@ export const BreakdownTooltip = ({>{children}</TooltipTrigger>- <TooltipContent className="w-max max-w-80 min-w-52 p-4">- <div className="flex min-w-0 flex-col gap-4">- <div className="flex flex-col gap-1">+ <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">{isCost ? "Cost breakdown" : "Usage breakdown"}</span>@@ -134,22 +143,22 @@ export const BreakdownTooltip = ({{/* Input Section */}<Sectiontitle={isCost ? "Input cost" : "Input usage"}- details={aggregatedDetails}- filterFn={(key) => key.includes("input")}+ entries={inputEntries}formatValue={formatValue}+ waterfallSegments={waterfallSegments}/>{/* Output Section */}<Sectiontitle={isCost ? "Output cost" : "Output usage"}- details={aggregatedDetails}- filterFn={(key) => key.includes("output")}+ entries={outputEntries}formatValue={formatValue}+ waterfallSegments={waterfallSegments}/>{/* Other Section */}{otherEntries.length > 0 && (- <div className="flex min-w-0 flex-col gap-2">+ <div className="col-span-3 grid min-w-0 grid-cols-subgrid gap-y-2"><BreakdownRowlabel={isCost ? "Other cost" : "Other usage"}value={formatValue(otherTotal)}@@ -161,6 +170,7 @@ export const BreakdownTooltip = ({label={key}value={formatValue(value ?? 0)}variant="item"+ waterfallSegment={waterfallSegments.get(key)}/>))}</div>@@ -179,8 +189,12 @@ export const BreakdownTooltip = ({);};-const breakdownRowVariants = cva("flex min-w-0 items-center gap-3 text-xs", {+const breakdownRowVariants = cva("min-w-0 items-center text-xs", {variants: {+ layout: {+ default: "col-span-3 flex gap-3",+ waterfall: "col-span-3 grid grid-cols-subgrid",+ },variant: {item: "text-muted-foreground",section: "border-b pb-1 font-bold",@@ -188,26 +202,78 @@ const breakdownRowVariants = cva("flex min-w-0 items-center gap-3 text-xs", {},},defaultVariants: {+ layout: "default",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,variant,+ waterfallSegment,}: {label: string;value: string;variant: NonNullable<VariantProps<typeof breakdownRowVariants>["variant"]>;+ waterfallSegment?: WaterfallSegment;}) {+ const layout = waterfallSegment ? "waterfall" : "default";+return (- <div className={breakdownRowVariants({ variant })}>- <span className="min-w-0 flex-1 truncate" title={label}>+ <div+ className={breakdownRowVariants({+ layout,+ variant,+ })}+ >+ <span className={breakdownLabelVariants({ layout })} title={label}>{label}</span>+ {waterfallSegment ? (+ <span+ className="bg-muted relative h-2 overflow-hidden rounded-sm"+ aria-hidden="true"+ >+ <span+ className={waterfallSegmentVariants({+ edge: waterfallSegment.edge,+ })}+ style={{+ left: `${waterfallSegment.left}%`,+ width: `${waterfallSegment.width}%`,+ }}+ />+ </span>+ ) : null}<span- className="max-w-[50%] min-w-0 truncate text-right font-mono tabular-nums"+ className="text-right font-mono whitespace-nowrap tabular-nums"title={value}>{value}@@ -218,37 +284,82 @@ function BreakdownRow({interface SectionProps {title: string;- details: Details;- filterFn: (key: string) => boolean;+ entries: [string, number | undefined][];formatValue: (value: number) => string;+ waterfallSegments: Map<string, WaterfallSegment>;}-const Section = ({ title, details, filterFn, formatValue }: SectionProps) => {- const filteredEntries = Object.entries(details)- .filter(([key]) => filterFn(key))- .sort(([, a], [, b]) => (b ?? 0) - (a ?? 0));-- const sectionTotal = filteredEntries.reduce(+const Section = ({+ title,+ entries,+ formatValue,+ waterfallSegments,+}: SectionProps) => {+ const sectionTotal = entries.reduce((sum, [_, value]) =>new Decimal(sum).plus(new Decimal(value ?? 0)).toNumber(),0,);return (- <div className="flex min-w-0 flex-col gap-2">+ <div className="col-span-3 grid min-w-0 grid-cols-subgrid gap-y-2"><BreakdownRowlabel={title}value={formatValue(sectionTotal)}variant="section"/>- {filteredEntries.map(([key, value]) => (+ {entries.map(([key, value]) => (<BreakdownRowkey={key}label={key}value={formatValue(value ?? 0)}variant="item"+ waterfallSegment={waterfallSegments.get(key)}/>))}</div>);};++function sortEntriesByValue(entries: [string, number | undefined][]) {+ return entries.toSorted(([, a], [, b]) => (b ?? 0) - (a ?? 0));+}++function createWaterfallSegments(+ entries: [string, number | undefined][],+): Map<string, WaterfallSegment> {+ 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];+ }),+ );+}