Skip to content

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

fix(web): show a muted no-cost chip on generations with tokens but no cost (#18686)

Trang Nguyend31d1617d364
diff --git a/web/src/components/design-system/Badge/Badge.tsx b/web/src/components/design-system/Badge/Badge.tsx
index a8acc181a5..3769182c54 100644
--- a/web/src/components/design-system/Badge/Badge.tsx
+++ b/web/src/components/design-system/Badge/Badge.tsx
@@ -112,6 +112,7 @@ type BadgeProps = Omit<BadgeShellProps, "asChild" | "children"> & {
text: string;
/** Key shown muted before the value, e.g. `latency` before `0.71s`. */
label?: string;
+ leadingIcon?: LucideIcon;
trailingIcon?: LucideIcon;
/** Link badges tint the arrow and underline the text on hover. */
trailingIconTone?: "default" | "link";
@@ -124,12 +125,19 @@ export function Badge({
title,
interactive,
shouldWrap,
+ leadingIcon: LeadingIcon,
trailingIcon: TrailingIcon,
trailingIconTone = "default",
...props
}: BadgeProps) {
return (
<BadgeShell color={color} shouldWrap={shouldWrap} {...props}>
+ {LeadingIcon && (
+ <LeadingIcon
+ aria-hidden
+ className="icon-sm text-foreground-tertiary shrink-0"
+ />
+ )}
{label && <span className="shrink-0 py-0.5">{label}:</span>}
<span
className={cn(
diff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsx b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsx
index 0db723e52a..d80a2790fe 100644
--- a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsx
+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/ObservationDetailViewHeader.tsx
@@ -37,7 +37,10 @@ import {
UsageBadge,
hasBreakdown,
} from "@/src/features/traces/components/ObservationMetadataBadgesTooltip";
-import { resolveObservationCostSource } from "@/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource";
+import {
+ isGenerationMissingCost,
+ resolveObservationCostSource,
+} from "@/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource";
import { ModelBadge } from "@/src/features/traces/components/ObservationDetailView/components/ModelBadge";
import {
type WithStringifiedMetadata,
@@ -153,6 +156,12 @@ export const ObservationDetailViewHeader = memo(
const displayedCostDetails =
subtreeMetrics?.costDetails ?? observation.costDetails;
const showsOwnObservationCost = !subtreeMetrics;
+ const showsNoCost = isGenerationMissingCost({
+ type: observation.type,
+ totalUsage,
+ totalCost: displayedTotalCost,
+ costDetails: displayedCostDetails,
+ });
const hasProvidedCostDetails =
Object.keys(observation.providedCostDetails).length > 0;
const costSource = resolveObservationCostSource({
@@ -176,6 +185,20 @@ export const ObservationDetailViewHeader = memo(
}
: undefined;
+ const renderCostBadge = () => {
+ if (showsNoCost)
+ return <CostBadge variant="missing" projectId={projectId} />;
+ if (displayedTotalCost == null || !displayedCostDetails) return null;
+ return (
+ <CostBadge
+ totalCost={displayedTotalCost}
+ costDetails={displayedCostDetails}
+ costSource={costSource}
+ priceSource={priceSource}
+ />
+ );
+ };
+
const timestampBadge = preparedDate && (
<Badge
font="mono"
@@ -494,14 +517,7 @@ export const ObservationDetailViewHeader = memo(
projectId={projectId}
/>
)}
- {displayedTotalCost != null && displayedCostDetails && (
- <CostBadge
- totalCost={displayedTotalCost}
- costDetails={displayedCostDetails}
- costSource={costSource}
- priceSource={priceSource}
- />
- )}
+ {renderCostBadge()}
{subtreeMetrics
? subtreeMetrics.hasGenerationLike &&
subtreeMetrics.totalUsage > 0 &&
diff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.clienttest.ts b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.clienttest.ts
index e3729e6edb..302a6e8b46 100644
--- a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.clienttest.ts
+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.clienttest.ts
@@ -1,6 +1,44 @@
import { describe, expect, it } from "vitest";
-import { resolveObservationCostSource } from "./costSource";
+import {
+ isGenerationMissingCost,
+ resolveObservationCostSource,
+} from "./costSource";
+
+describe("isGenerationMissingCost", () => {
+ const unpriced = {
+ type: "GENERATION",
+ totalUsage: 1113,
+ totalCost: null,
+ costDetails: {},
+ };
+
+ it("flags a generation with tokens and no cost", () => {
+ expect(isGenerationMissingCost(unpriced)).toBe(true);
+ expect(isGenerationMissingCost({ ...unpriced, totalCost: 0 })).toBe(true);
+ });
+
+ it("ignores generations without tokens, e.g. image models", () => {
+ expect(isGenerationMissingCost({ ...unpriced, totalUsage: 0 })).toBe(false);
+ });
+
+ it("ignores priced generations, including a matched zero price", () => {
+ expect(isGenerationMissingCost({ ...unpriced, totalCost: 0.28 })).toBe(
+ false,
+ );
+ expect(
+ isGenerationMissingCost({
+ ...unpriced,
+ totalCost: 0,
+ costDetails: { input: 0, total: 0 },
+ }),
+ ).toBe(false);
+ });
+
+ it("ignores non-generation types", () => {
+ expect(isGenerationMissingCost({ ...unpriced, type: "SPAN" })).toBe(false);
+ });
+});
describe("resolveObservationCostSource", () => {
it("omits the source for subtree rollups", () => {
diff --git a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.ts b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.ts
index 30789761c0..e256c1d487 100644
--- a/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.ts
+++ b/web/src/features/traces/components/ObservationDetailView/components/ObservationDetailViewHeader/costSource.ts
@@ -1,5 +1,24 @@
+import { ObservationType } from "@langfuse/shared";
import type { CostSource } from "@/src/features/traces/components/BreakdownTooltip";
+/** A GENERATION that recorded tokens but nothing priced them. */
+export function isGenerationMissingCost({
+ type,
+ totalUsage,
+ totalCost,
+ costDetails,
+}: {
+ type: string;
+ totalUsage: number;
+ totalCost: number | null | undefined;
+ costDetails: Record<string, number> | undefined;
+}): boolean {
+ if (type !== ObservationType.GENERATION) return false;
+ if (totalUsage <= 0) return false;
+ if (totalCost) return false;
+ return Object.keys(costDetails ?? {}).length === 0;
+}
+
export function resolveObservationCostSource({
hasSubtreeMetrics,
hasProvidedCostDetails,
diff --git a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx
index 9201d7128b..89d1e25df3 100644
--- a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx
+++ b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx
@@ -1,6 +1,10 @@
/** Cost and token metrics for the trace, observation and session headers; breakdowns open on hover. */
+import { TriangleAlert } from "lucide-react";
+
import { Badge } from "@/src/components/design-system/Badge/Badge";
+import { CustomTooltip } from "@/src/components/design-system/CustomTooltip/CustomTooltip";
+import { TextLink } from "@/src/components/design-system/TextLink/TextLink";
import {
BreakdownTooltip,
type CostSource,
@@ -15,17 +19,22 @@ export const formatMetricCost = (cost: number) => usdFormatter(cost, 2, 3);
export const formatMetricTokens = (tokens: number) =>
`${compactNumberFormatter(tokens, 0).toLowerCase()} tokens`;
-export function CostBadge({
- totalCost,
- costDetails,
- priceSource,
- costSource,
-}: {
- totalCost: number;
- costDetails: Record<string, number>;
- priceSource?: PriceSource;
- costSource?: CostSource;
-}) {
+type CostBadgeProps =
+ | {
+ variant?: "cost";
+ totalCost: number;
+ costDetails: Record<string, number>;
+ priceSource?: PriceSource;
+ costSource?: CostSource;
+ }
+ /** A generation with tokens but no cost. */
+ | { variant: "missing"; projectId: string };
+
+export function CostBadge(props: CostBadgeProps) {
+ if (props.variant === "missing") {
+ return <MissingCostBadge projectId={props.projectId} />;
+ }
+ const { totalCost, costDetails, priceSource, costSource } = props;
if (!hasBreakdown(costDetails)) {
return (
<Badge
@@ -53,6 +62,37 @@ export function CostBadge({
);
}
+function MissingCostBadge({ projectId }: { projectId: string }) {
+ return (
+ <CustomTooltip
+ content={
+ <div className="flex flex-col gap-1">
+ <p className="text-xs">
+ No model price matched this generation. Add a model price, or send
+ the cost from your SDK.
+ </p>
+ <TextLink
+ path={`/project/${encodeURIComponent(projectId)}/settings/models`}
+ value="Model settings"
+ />
+ </div>
+ }
+ >
+ {({ getTriggerProps }) => (
+ <span {...getTriggerProps()} className="inline-flex" tabIndex={0}>
+ <Badge
+ font="mono"
+ color="ghost"
+ interactive
+ leadingIcon={TriangleAlert}
+ text="no cost"
+ />
+ </span>
+ )}
+ </CustomTooltip>
+ );
+}
+
/** A breakdown of nothing but zeros has nothing to say. */
export const hasBreakdown = (details: Record<string, number>) =>
Object.values(details).some((value) => value > 0);