Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): usage breakdown drops the % column, cost bars form a waterfall (#18933)

Trang Nguyen62c29dd207bc
diff --git a/web/src/features/traces/components/BreakdownTooltip.stories.tsx b/web/src/features/traces/components/BreakdownTooltip.stories.tsx
index ccc2c5ba30..64f8a90028 100644
--- a/web/src/features/traces/components/BreakdownTooltip.stories.tsx
+++ b/web/src/features/traces/components/BreakdownTooltip.stories.tsx
@@ -281,6 +281,58 @@ export const TestShareOfTotal = meta.story({
},
});
+export const TestWaterfallBars = meta.story({
+ name: "(Test) Cost bars start where the previous row's bar ends",
+ args: {
+ details: cacheCostDetails,
+ children: <span>$0.03782809</span>,
+ isCost: true,
+ },
+ play: async ({ canvasElement }) => {
+ const { tooltip } = await openBreakdownTooltip(
+ canvasElement,
+ "$0.03782809",
+ );
+ const bars = Array.from(
+ tooltip.querySelectorAll<HTMLElement>('[aria-hidden="true"] > span'),
+ );
+
+ await expect(bars).toHaveLength(5);
+ let end = 0;
+ for (const bar of bars) {
+ await expect(parseFloat(bar.style.left)).toBeCloseTo(end, 2);
+ end += parseFloat(bar.style.width);
+ }
+ await expect(end).toBeCloseTo(100, 2);
+ },
+});
+
+export const TestUsageWithoutShares = meta.story({
+ name: "(Test) Usage breakdown shows no share column or bars",
+ args: {
+ details: {
+ input: 120,
+ output: 35,
+ output_reasoning_tokens: 10,
+ total: 165,
+ },
+ children: <span>165 tokens</span>,
+ },
+ play: async ({ canvasElement }) => {
+ const { content, tooltip } = await openBreakdownTooltip(
+ canvasElement,
+ "165 tokens",
+ );
+
+ await expect(content.getByText("Usage breakdown")).toBeInTheDocument();
+ await expect(content.getByText("165")).toBeInTheDocument();
+ await expect(tooltip.textContent ?? "").not.toContain("%");
+ await expect(
+ tooltip.querySelector('[aria-hidden="true"].bg-border'),
+ ).toBeNull();
+ },
+});
+
export const TestCacheHitRate = meta.story({
name: "(Test) Shows cache hit rate after input usage details",
args: {
diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsx
index 63b61a5d75..0abbfe48cd 100644
--- a/web/src/features/traces/components/BreakdownTooltip.tsx
+++ b/web/src/features/traces/components/BreakdownTooltip.tsx
@@ -83,7 +83,12 @@ export const BreakdownTooltip = ({
isCost && !new Decimal(value).isZero()
? usdFormatter(value, costFractionDigits, costFractionDigits)
: undefined;
- const shareOf = createShareOf(contributionEntries);
+ /** Usage rows show bars and percentages only on the cost breakdown. */
+ const showShares = isCost;
+ const shareOf = showShares ? createShareOf(contributionEntries) : undefined;
+ const waterfallOffsets = shareOf
+ ? createWaterfallOffsets(contributionEntries, shareOf)
+ : undefined;
const cacheHitRate = isCost ? null : getCacheHitRate(inputEntries, details);
const displayedTotal =
aggregatedDetails.total ??
@@ -92,7 +97,13 @@ export const BreakdownTooltip = ({
const resolvedCostSource =
costSource ?? (isCost && priceSource ? "calculated" : undefined);
- const sectionProps = { formatValue, formatExactValue, shareOf };
+ const sectionProps = {
+ formatValue,
+ formatExactValue,
+ shareOf,
+ waterfallOffsets,
+ showShares,
+ };
return (
<TooltipProvider>
@@ -104,8 +115,15 @@ export const BreakdownTooltip = ({
{children}
</TooltipTrigger>
<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">
+ <div
+ className={cn(
+ "grid min-w-0 gap-x-3 gap-y-3 max-sm:w-full",
+ showShares
+ ? "grid-cols-[repeat(4,max-content)] max-sm:grid-cols-[minmax(0,1fr)_repeat(3,max-content)]"
+ : "grid-cols-[repeat(2,max-content)] max-sm:grid-cols-[minmax(0,1fr)_max-content]",
+ )}
+ >
+ <div className="col-span-full flex min-w-0 flex-col gap-0.5">
<span className="text-sm font-bold">
{isCost ? "Cost breakdown" : "Usage breakdown"}
</span>
@@ -187,6 +205,7 @@ export const BreakdownTooltip = ({
value={formatValue(displayedTotal)}
exactValue={formatExactValue(displayedTotal)}
variant="total"
+ showShares={showShares}
/>
</div>
</TooltipContent>
@@ -199,7 +218,7 @@ const breakdownRowVariants = cva("min-w-0 items-center text-sm leading-5", {
variants: {
layout: {
inline: "flex gap-3",
- grid: "col-span-4 grid grid-cols-subgrid",
+ grid: "col-span-full grid grid-cols-subgrid",
},
variant: {
item: "text-muted-foreground",
@@ -220,6 +239,8 @@ function BreakdownRow({
variant,
layout = "grid",
share,
+ offset = 0,
+ showShares = false,
}: {
label: string;
value: string;
@@ -228,8 +249,12 @@ function BreakdownRow({
layout?: BreakdownRowVariants["layout"];
/** Share of all components in percent; shown as a bar on item rows. */
share?: number;
+ /** Where the bar starts: the summed share of the rows before it. */
+ offset?: number;
+ /** The grid carries bar and share columns. */
+ showShares?: boolean;
}) {
- const hasBar = layout === "grid" && variant === "item";
+ const hasBar = layout === "grid" && variant === "item" && showShares;
return (
<div className={breakdownRowVariants({ layout, variant })}>
@@ -237,7 +262,8 @@ function BreakdownRow({
className={cn(
"min-w-0 truncate",
layout === "inline" && "flex-1",
- layout === "grid" && (hasBar ? "max-w-52" : "col-span-2"),
+ layout === "grid" && variant === "item" && "max-w-52",
+ layout === "grid" && variant !== "item" && showShares && "col-span-2",
)}
title={label}
>
@@ -245,12 +271,12 @@ function BreakdownRow({
</span>
{hasBar ? (
<span
- className="bg-border relative h-1 w-16 overflow-hidden rounded-sm"
+ className="bg-border relative h-2 w-28 overflow-hidden"
aria-hidden="true"
>
<span
- className="bg-muted-foreground absolute left-0 h-full rounded-sm"
- style={{ width: `${share ?? 0}%` }}
+ className="bg-muted-foreground absolute h-full"
+ style={{ left: `${offset}%`, width: `${share ?? 0}%` }}
/>
</span>
) : null}
@@ -274,7 +300,10 @@ interface SectionProps {
entries: [string, number | undefined][];
formatValue: (value: number | Decimal) => string;
formatExactValue: (value: number | Decimal) => string | undefined;
- shareOf: (value: number | Decimal) => number;
+ /** Omitted when the breakdown shows no shares. */
+ shareOf?: (value: number | Decimal) => number;
+ waterfallOffsets?: Map<string, number>;
+ showShares: boolean;
/** Hairline above the section, matching the total row. */
separated?: boolean;
lastRow?: React.ReactNode;
@@ -286,6 +315,8 @@ const Section = ({
formatValue,
formatExactValue,
shareOf,
+ waterfallOffsets,
+ showShares,
separated = false,
lastRow,
}: SectionProps) => {
@@ -294,7 +325,7 @@ const Section = ({
return (
<div
className={cn(
- "col-span-4 grid min-w-0 grid-cols-subgrid gap-y-1",
+ "col-span-full grid min-w-0 grid-cols-subgrid gap-y-1",
separated && "border-t pt-1.5",
)}
>
@@ -303,7 +334,8 @@ const Section = ({
value={formatValue(sectionTotal)}
exactValue={formatExactValue(sectionTotal)}
variant="section"
- share={shareOf(sectionTotal)}
+ share={shareOf?.(sectionTotal)}
+ showShares={showShares}
/>
{entries.map(([key, value]) => (
<BreakdownRow
@@ -312,7 +344,9 @@ const Section = ({
value={formatValue(value ?? 0)}
exactValue={formatExactValue(value ?? 0)}
variant="item"
- share={shareOf(value ?? 0)}
+ share={shareOf?.(value ?? 0)}
+ offset={waterfallOffsets?.get(key)}
+ showShares={showShares}
/>
))}
{lastRow}
@@ -322,8 +356,8 @@ const Section = ({
function CacheHitRateRow({ rate }: { rate: number }) {
return (
- <div className="text-muted-foreground col-span-4 grid grid-cols-subgrid items-center text-sm leading-5">
- <span className="col-span-2 inline-flex items-center gap-1">
+ <div className="text-muted-foreground col-span-full grid grid-cols-subgrid items-center text-sm leading-5">
+ <span className="inline-flex items-center gap-1">
Cache hit rate
<CacheHitRateInfo />
</span>
@@ -472,6 +506,21 @@ function createShareOf(entries: [string, number | undefined][]) {
};
}
+/** Each row's bar starts where the previous rows' bars end. */
+function createWaterfallOffsets(
+ entries: [string, number | undefined][],
+ shareOf: (value: number | Decimal) => number,
+) {
+ let cumulative = 0;
+ return new Map(
+ entries.map(([key, value]) => {
+ const offset = cumulative;
+ cumulative += shareOf(value ?? 0);
+ return [key, offset];
+ }),
+ );
+}
+
function formatShare(share: number) {
const rounded = Math.round(share);
return share > 0 && rounded === 0 ? "<1%" : `${rounded}%`;