🪢 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): show cost source in breakdown tooltip (#17051)
Hassieb Pakzad74c6064fee0f
diff --git a/web/src/features/traces/components/BreakdownTooltip.stories.tsx b/web/src/features/traces/components/BreakdownTooltip.stories.tsxindex 6d0934cf7e..b6eec55e6f 100644--- a/web/src/features/traces/components/BreakdownTooltip.stories.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.stories.tsx@@ -61,15 +61,34 @@ export const Cost = meta.story({},});-export const CostWithMatchedPricingTier = meta.story({+export const CostCalculatedFromPricing = meta.story({args: {details: costDetails,children: <span>$0.001725</span>,isCost: true,+ costSource: "calculated",priceSource,},});+export const CostCalculatedWithoutTier = meta.story({+ args: {+ details: costDetails,+ children: <span>$0.001725</span>,+ isCost: true,+ costSource: "calculated",+ },+});++export const CostProvidedAtIngestion = meta.story({+ args: {+ details: costDetails,+ children: <span>$0.001725</span>,+ isCost: true,+ costSource: "provided",+ },+});+export const AggregatedCost = meta.story({args: {details: [costDetails, costDetails],@@ -83,6 +102,7 @@ export const CostWithLongUsageTypes = meta.story({details: longUsageTypeCostDetails,children: <span>$0.03824809</span>,isCost: true,+ costSource: "calculated",priceSource: {...priceSource,pricingTierName: "Standard",@@ -112,11 +132,12 @@ async function openBreakdownTooltip(}export const TestLinksMatchedPricingTier = meta.story({- name: "(Test) Opens matched pricing tier breakdown",+ name: "(Test) Opens calculated cost source with pricing tier",args: {details: costDetails,children: <span>$0.001725</span>,isCost: true,+ costSource: "calculated",priceSource,},play: async ({ canvasElement }) => {@@ -124,7 +145,7 @@ export const TestLinksMatchedPricingTier = meta.story({await expect(content.getByText("Cost breakdown")).toBeInTheDocument();await expect(content.getByText("Total cost")).toBeInTheDocument();const link = content.getByRole("link");- await expect(link).toHaveTextContent("Priority");+ await expect(link).toHaveTextContent("Calculated · Priority Tier Pricing");await expect(link).toHaveAttribute("href","/project/project-1/settings/models/gpt-5.6%2Fpriority?pricingTier=tier-priority",@@ -133,6 +154,23 @@ export const TestLinksMatchedPricingTier = meta.story({},});+export const TestProvidedAtIngestionLabel = meta.story({+ name: "(Test) Shows provided at ingestion label",+ args: {+ details: costDetails,+ children: <span>$0.001725</span>,+ isCost: true,+ costSource: "provided",+ },+ play: async ({ canvasElement }) => {+ const { content } = await openBreakdownTooltip(canvasElement, "$0.001725");+ await expect(+ content.getByText("Provided at ingestion"),+ ).toBeInTheDocument();+ await expect(content.queryByRole("link")).not.toBeInTheDocument();+ },+});+export const TestCostFormattingAndTruncation = meta.story({name: "(Test) Formats costs without padded decimals",args: {diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsxindex 98bed12dc6..9617063271 100644--- a/web/src/features/traces/components/BreakdownTooltip.tsx+++ b/web/src/features/traces/components/BreakdownTooltip.tsx@@ -20,12 +20,16 @@ export interface PriceSource {pricingTierName: string;}+export type CostSource = "calculated" | "provided";+interface BreakdownTooltipProps {details: Details | Details[];children: React.ReactNode;isCost?: boolean;pricingTierName?: string;priceSource?: PriceSource;+ /** Whether cost was calculated by Langfuse or provided at ingestion. */+ costSource?: CostSource;}export const BreakdownTooltip = ({@@ -34,6 +38,7 @@ export const BreakdownTooltip = ({isCost = false,pricingTierName,priceSource,+ costSource,}: BreakdownTooltipProps) => {const [isOpen, setIsOpen] = useState(false);@@ -63,6 +68,9 @@ export const BreakdownTooltip = ({return acc + value;}, 0);+ const resolvedCostSource =+ costSource ?? (isCost && priceSource ? "calculated" : undefined);+return (<TooltipProvider><Tooltip open={isOpen} onOpenChange={setIsOpen}>@@ -79,7 +87,13 @@ export const BreakdownTooltip = ({{isCost ? "Cost breakdown" : "Usage breakdown"}</span>- {isCost && priceSource && (+ {isCost && resolvedCostSource === "provided" ? (+ <span className="text-muted-foreground text-xs italic">+ Provided at ingestion+ </span>+ ) : null}++ {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"@@ -88,13 +102,20 @@ export const BreakdownTooltip = ({><spanclassName="min-w-0 truncate"- title={`${priceSource.pricingTierName} Tier Pricing`}+ title={`Calculated · ${priceSource.pricingTierName} Tier Pricing`}>- {priceSource.pricingTierName} Tier Pricing+ Calculated · {priceSource.pricingTierName} Tier Pricing</span><ExternalLink className="h-3 w-3 shrink-0" /></Link>- )}+ ) : null}++ {isCost && resolvedCostSource === "calculated" && !priceSource ? (+ <span className="text-muted-foreground text-xs italic">+ Calculated from model pricing+ </span>+ ) : null}+{Array.isArray(details) && details.length > 0 && (<span className="text-muted-foreground text-xs italic">Aggregate across {details.length}{" "}diff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsx b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsxindex fbe47e67ee..dc27bd5d8a 100644--- a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsx+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsx@@ -46,6 +46,7 @@ import {CostBadge,UsageBadge,} from "@/src/features/traces/components/ObservationMetadataBadgesTooltip";+import { resolveObservationCostSource } from "@/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource";import { ModelBadge } from "@/src/features/traces/components/ObservationDetailView/components/ModelBadge";import { ModelParametersBadges } from "@/src/features/traces/components/ObservationDetailView/components/ModelParametersBadges";import {@@ -191,6 +192,29 @@ export const ObservationDetailViewHeader = memo(: totalCost;const displayedCostDetails =subtreeMetrics?.costDetails ?? observation.costDetails;+ const showsOwnObservationCost = !subtreeMetrics;+ const hasProvidedCostDetails =+ Object.keys(observation.providedCostDetails).length > 0;+ const costSource = resolveObservationCostSource({+ hasSubtreeMetrics: Boolean(subtreeMetrics),+ hasProvidedCostDetails,+ });+ const priceSource =+ isGenerationLike(observation.type) &&+ observation.internalModelId &&+ observation.model &&+ observation.usagePricingTierId &&+ observation.usagePricingTierName &&+ !hasProvidedCostDetails &&+ showsOwnObservationCost+ ? {+ projectId,+ modelId: observation.internalModelId,+ modelName: observation.model,+ pricingTierId: observation.usagePricingTierId,+ pricingTierName: observation.usagePricingTierName,+ }+ : undefined;return (<div className="@container shrink-0 space-y-2 border-b p-2">@@ -727,24 +751,8 @@ export const ObservationDetailViewHeader = memo(<CostBadgetotalCost={displayedTotalCost}costDetails={displayedCostDetails}- priceSource={- isGenerationLike(observation.type) &&- observation.internalModelId &&- observation.model &&- observation.usagePricingTierId &&- observation.usagePricingTierName &&- Object.keys(observation.providedCostDetails).length === 0 &&- (!subtreeMetrics ||- treeNodeTotalCost?.eq(totalCost ?? 0) === true)- ? {- projectId,- modelId: observation.internalModelId,- modelName: observation.model,- pricingTierId: observation.usagePricingTierId,- pricingTierName: observation.usagePricingTierName,- }- : undefined- }+ costSource={costSource}+ priceSource={priceSource}/>)}{subtreeMetricsdiff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.clienttest.ts b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.clienttest.tsnew file mode 100644index 0000000000..e3729e6edb--- /dev/null+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.clienttest.ts@@ -0,0 +1,29 @@+import { describe, expect, it } from "vitest";++import { resolveObservationCostSource } from "./costSource";++describe("resolveObservationCostSource", () => {+ it("omits the source for subtree rollups", () => {+ expect(+ resolveObservationCostSource({+ hasSubtreeMetrics: true,+ hasProvidedCostDetails: false,+ }),+ ).toBeUndefined();+ });++ it.each([+ [false, "calculated"],+ [true, "provided"],+ ] as const)(+ "labels a non-subtree observation with provided costs %s as %s",+ (hasProvidedCostDetails, expected) => {+ expect(+ resolveObservationCostSource({+ hasSubtreeMetrics: false,+ hasProvidedCostDetails,+ }),+ ).toBe(expected);+ },+ );+});diff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.ts b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.tsnew file mode 100644index 0000000000..30789761c0--- /dev/null+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.ts@@ -0,0 +1,13 @@+import type { CostSource } from "@/src/features/traces/components/BreakdownTooltip";++export function resolveObservationCostSource({+ hasSubtreeMetrics,+ hasProvidedCostDetails,+}: {+ hasSubtreeMetrics: boolean;+ hasProvidedCostDetails: boolean;+}): CostSource | undefined {+ if (hasSubtreeMetrics) return undefined;++ return hasProvidedCostDetails ? "provided" : "calculated";+}diff --git a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsxindex 9d00e28953..76a63e6c02 100644--- a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx+++ b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx@@ -6,6 +6,7 @@import { Badge, BadgeShell } from "@/src/components/design-system/Badge/Badge";import {BreakdownTooltip,+ type CostSource,type PriceSource,} from "@/src/features/traces/components/BreakdownTooltip";import { usdFormatter, formatTokenCounts } from "@/src/utils/numbers";@@ -15,16 +16,19 @@ export function CostBadge({totalCost,costDetails,priceSource,+ costSource,}: {totalCost: number;costDetails: Record<string, number>;priceSource?: PriceSource;+ costSource?: CostSource;}) {return (<BreakdownTooltipdetails={costDetails}isCost={true}priceSource={priceSource}+ costSource={costSource}><Badge text={usdFormatter(totalCost)} trailingIcon={InfoIcon} /></BreakdownTooltip>