Skip to content

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

fix(traces): stop clipping cost breakdown tooltips (#16688)

Nikita Kabardin16a6bedb528f
diff --git a/web/src/features/traces/components/BreakdownTooltip.stories.tsx b/web/src/features/traces/components/BreakdownTooltip.stories.tsx
index 181aa56198..6d0934cf7e 100644
--- a/web/src/features/traces/components/BreakdownTooltip.stories.tsx
+++ b/web/src/features/traces/components/BreakdownTooltip.stories.tsx
@@ -19,6 +19,21 @@ const costDetails = {
total: 0.001725,
};
+const cacheCostDetails = {
+ cache_read_input_tokens: 0.0266155,
+ cache_creation_input_tokens: 0.0034625,
+ input: 0.00115,
+ output: 0.0066,
+ cached_tokens: 9e-8,
+ total: 0.03782809,
+};
+
+const longUsageTypeCostDetails = {
+ ...cacheCostDetails,
+ prompt_cache_creation_ephemeral_5m_input_tokens: 0.00042,
+ total: 0.03824809,
+};
+
const priceSource = {
projectId: "project-1",
modelId: "gpt-5.6/priority",
@@ -63,6 +78,39 @@ export const AggregatedCost = meta.story({
},
});
+export const CostWithLongUsageTypes = meta.story({
+ args: {
+ details: longUsageTypeCostDetails,
+ children: <span>$0.03824809</span>,
+ isCost: true,
+ priceSource: {
+ ...priceSource,
+ pricingTierName: "Standard",
+ },
+ },
+});
+
+async function openBreakdownTooltip(
+ canvasElement: HTMLElement,
+ triggerName: string,
+) {
+ const canvas = within(canvasElement);
+ const trigger = canvas.getByRole("button", { name: triggerName });
+ trigger.click();
+ await waitFor(() =>
+ expect(trigger.getAttribute("data-state")).toMatch(/-open$/),
+ );
+ await waitFor(() => expect(trigger).toHaveAttribute("aria-describedby"));
+
+ const tooltipId = trigger.getAttribute("aria-describedby");
+ const tooltip = tooltipId
+ ? canvasElement.ownerDocument.getElementById(tooltipId)
+ : null;
+ if (!tooltip) throw new Error("Tooltip content was not rendered");
+
+ return { trigger, content: within(tooltip) };
+}
+
export const TestLinksMatchedPricingTier = meta.story({
name: "(Test) Opens matched pricing tier breakdown",
args: {
@@ -72,21 +120,7 @@ export const TestLinksMatchedPricingTier = meta.story({
priceSource,
},
play: async ({ canvasElement }) => {
- const canvas = within(canvasElement);
- const trigger = canvas.getByRole("button", { name: "$0.001725" });
- trigger.click();
- await waitFor(() =>
- expect(trigger.getAttribute("data-state")).toMatch(/-open$/),
- );
- await waitFor(() => expect(trigger).toHaveAttribute("aria-describedby"));
-
- const tooltipId = trigger.getAttribute("aria-describedby");
- const tooltip = tooltipId
- ? canvasElement.ownerDocument.getElementById(tooltipId)
- : null;
- if (!tooltip) throw new Error("Tooltip content was not rendered");
-
- const content = within(tooltip);
+ const { content } = await openBreakdownTooltip(canvasElement, "$0.001725");
await expect(content.getByText("Cost breakdown")).toBeInTheDocument();
await expect(content.getByText("Total cost")).toBeInTheDocument();
const link = content.getByRole("link");
@@ -98,3 +132,31 @@ export const TestLinksMatchedPricingTier = meta.story({
await expect(link).toHaveClass("text-xs", "italic");
},
});
+
+export const TestCostFormattingAndTruncation = meta.story({
+ name: "(Test) Formats costs without padded decimals",
+ args: {
+ details: cacheCostDetails,
+ children: <span>$0.03782809</span>,
+ isCost: true,
+ },
+ play: async ({ canvasElement }) => {
+ const { content } = await openBreakdownTooltip(
+ canvasElement,
+ "$0.03782809",
+ );
+
+ 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);
+
+ const longLabel = content.getByText("cache_creation_input_tokens");
+ await expect(longLabel).toHaveClass("truncate");
+ await expect(longLabel).toHaveAttribute(
+ "title",
+ "cache_creation_input_tokens",
+ );
+ },
+});
diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsx
index 58f833e416..18b1234a48 100644
--- a/web/src/features/traces/components/BreakdownTooltip.tsx
+++ b/web/src/features/traces/components/BreakdownTooltip.tsx
@@ -7,9 +7,10 @@ import {
import { useState } from "react";
import Decimal from "decimal.js";
import Link from "next/link";
-import { getMaxDecimals } from "@/src/features/models/fns/getMaxDecimals";
import { type Details } from "@/src/features/traces/fns/calculateAggregatedUsage";
import { ExternalLink } from "lucide-react";
+import { usdFormatter } from "@/src/utils/numbers";
+import { cva, type VariantProps } from "class-variance-authority";
export interface PriceSource {
projectId: string;
@@ -48,19 +49,8 @@ export const BreakdownTooltip = ({
}, {})
: details;
- const formatValueWithPadding = (value: number, maxDecimals: number) => {
- return !value
- ? "0"
- : isCost
- ? `$${value.toFixed(maxDecimals)}`
- : value.toLocaleString();
- };
-
- const maxDecimals = isCost
- ? Math.max(
- ...Object.values(aggregatedDetails).map((v) => getMaxDecimals(v)),
- )
- : 0;
+ const formatValue = (value: number) =>
+ isCost ? usdFormatter(value, 2, 12) : value ? value.toLocaleString() : "0";
return (
<TooltipProvider>
@@ -71,8 +61,8 @@ export const BreakdownTooltip = ({
>
{children}
</TooltipTrigger>
- <TooltipContent className="w-64 p-4">
- <div className="flex flex-col gap-4">
+ <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">
<span className="font-bold">
{isCost ? "Cost breakdown" : "Usage breakdown"}
@@ -81,12 +71,17 @@ export const BreakdownTooltip = ({
{isCost && priceSource && (
<Link
href={`/project/${encodeURIComponent(priceSource.projectId)}/settings/models/${encodeURIComponent(priceSource.modelId)}?pricingTier=${encodeURIComponent(priceSource.pricingTierId)}`}
- className="text-muted-foreground flex flex-row gap-1 text-xs italic underline-offset-4 hover:underline"
+ className="text-muted-foreground flex min-w-0 flex-row gap-1 text-xs italic underline-offset-4 hover:underline"
target="_blank"
rel="noopener noreferrer"
>
- {priceSource.pricingTierName} Tier Pricing
- <ExternalLink className="h-3 w-3" />
+ <span
+ className="min-w-0 truncate"
+ title={`${priceSource.pricingTierName} Tier Pricing`}
+ >
+ {priceSource.pricingTierName} Tier Pricing
+ </span>
+ <ExternalLink className="h-3 w-3 shrink-0" />
</Link>
)}
{Array.isArray(details) && details.length > 0 && (
@@ -96,10 +91,11 @@ export const BreakdownTooltip = ({
</span>
)}
{pricingTierName && (
- <div className="text-muted-foreground flex justify-between text-xs">
- <span>Pricing Tier:</span>
- <span className="font-mono">{pricingTierName}</span>
- </div>
+ <BreakdownRow
+ label="Pricing Tier:"
+ value={pricingTierName}
+ variant="item"
+ />
)}
</div>
@@ -108,7 +104,7 @@ export const BreakdownTooltip = ({
title={isCost ? "Input cost" : "Input usage"}
details={aggregatedDetails}
filterFn={(key) => key.includes("input")}
- formatValue={(v) => formatValueWithPadding(v, maxDecimals)}
+ formatValue={formatValue}
/>
{/* Output Section */}
@@ -116,28 +112,22 @@ export const BreakdownTooltip = ({
title={isCost ? "Output cost" : "Output usage"}
details={aggregatedDetails}
filterFn={(key) => key.includes("output")}
- formatValue={(v) => formatValueWithPadding(v, maxDecimals)}
+ formatValue={formatValue}
/>
{/* Other Section */}
<OtherSection
details={aggregatedDetails}
isCost={isCost}
- formatValue={(v) => formatValueWithPadding(v, maxDecimals)}
+ formatValue={formatValue}
/>
{/* Total */}
- <div className="flex justify-between border-t border-b-4 border-double py-1">
- <span className="text-xs font-bold">
- {isCost ? "Total cost" : "Total usage"}
- </span>
- <span className="font-mono text-xs font-bold">
- {formatValueWithPadding(
- aggregatedDetails.total ?? 0,
- maxDecimals,
- )}
- </span>
- </div>
+ <BreakdownRow
+ label={isCost ? "Total cost" : "Total usage"}
+ value={formatValue(aggregatedDetails.total ?? 0)}
+ variant="total"
+ />
</div>
</TooltipContent>
</Tooltip>
@@ -145,6 +135,43 @@ export const BreakdownTooltip = ({
);
};
+const breakdownRowVariants = cva("flex min-w-0 items-center gap-3 text-xs", {
+ variants: {
+ variant: {
+ item: "text-muted-foreground",
+ section: "border-b pb-1 font-bold",
+ total: "border-t border-b-4 border-double py-1 font-bold",
+ },
+ },
+ defaultVariants: {
+ variant: "item",
+ },
+});
+
+function BreakdownRow({
+ label,
+ value,
+ variant,
+}: {
+ label: string;
+ value: string;
+ variant: NonNullable<VariantProps<typeof breakdownRowVariants>["variant"]>;
+}) {
+ return (
+ <div className={breakdownRowVariants({ variant })}>
+ <span className="min-w-0 flex-1 truncate" title={label}>
+ {label}
+ </span>
+ <span
+ className="max-w-[50%] min-w-0 truncate text-right font-mono tabular-nums"
+ title={value}
+ >
+ {value}
+ </span>
+ </div>
+ );
+}
+
interface SectionProps {
title: string;
details: Details;
@@ -164,21 +191,19 @@ const Section = ({ title, details, filterFn, formatValue }: SectionProps) => {
);
return (
- <div className="flex flex-col gap-2">
- <div className="flex justify-between border-b pb-1">
- <span className="text-xs font-bold">{title}</span>
- <span className="text-right font-mono text-xs font-bold">
- {formatValue(sectionTotal)}
- </span>
- </div>
+ <div className="flex min-w-0 flex-col gap-2">
+ <BreakdownRow
+ label={title}
+ value={formatValue(sectionTotal)}
+ variant="section"
+ />
{filteredEntries.map(([key, value]) => (
- <div
+ <BreakdownRow
key={key}
- className="text-muted-foreground flex justify-between text-xs"
- >
- <span className="mr-4">{key}</span>
- <span className="font-mono">{formatValue(value ?? 0)}</span>
- </div>
+ label={key}
+ value={formatValue(value ?? 0)}
+ variant="item"
+ />
))}
</div>
);
@@ -207,23 +232,19 @@ const OtherSection = ({ details, isCost, formatValue }: OtherSectionProps) => {
}, 0);
return (
- <div className="flex flex-col gap-2">
- <div className="flex justify-between border-b pb-2">
- <span className="text-xs font-bold">
- {isCost ? "Other cost" : "Other usage"}
- </span>
- <span className="text-right font-mono text-xs font-bold">
- {formatValue(otherTotal)}
- </span>
- </div>
+ <div className="flex min-w-0 flex-col gap-2">
+ <BreakdownRow
+ label={isCost ? "Other cost" : "Other usage"}
+ value={formatValue(otherTotal)}
+ variant="section"
+ />
{otherEntries.map(([key, value]) => (
- <div
+ <BreakdownRow
key={key}
- className="text-muted-foreground flex justify-between text-xs"
- >
- <span className="mr-4">{key}</span>
- <span className="font-mono">{formatValue(value ?? 0)}</span>
- </div>
+ label={key}
+ value={formatValue(value ?? 0)}
+ variant="item"
+ />
))}
</div>
);