Skip to content

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

fix(ui): show dash for missing observation costs (#16287)

Nikita Kabardin1626a0fb74b1
diff --git a/packages/shared/src/server/repositories/observations_converters.test.ts b/packages/shared/src/server/repositories/observations_converters.test.ts
index 5e2c1d8949..6bcc9ebd57 100644
--- a/packages/shared/src/server/repositories/observations_converters.test.ts
+++ b/packages/shared/src/server/repositories/observations_converters.test.ts
@@ -68,16 +68,36 @@ describe("reduceUsageOrCostDetails", () => {
output: null,
total: 5,
});
+ expect(reduceUsageOrCostDetails({})).toEqual({
+ input: null,
+ output: null,
+ total: null,
+ });
expect(reduceUsageOrCostDetails(null)).toEqual({
input: null,
output: null,
- total: 0,
+ total: null,
});
expect(reduceUsageOrCostDetails(undefined)).toEqual({
+ input: null,
+ output: null,
+ total: null,
+ });
+ });
+
+ it("preserves an explicit zero total instead of treating it as missing", () => {
+ expect(reduceUsageOrCostDetails({ total: 0 })).toEqual({
input: null,
output: null,
total: 0,
});
+ expect(reduceUsageOrCostDetails({ input: 0, output: 0, total: 0 })).toEqual(
+ {
+ input: 0,
+ output: 0,
+ total: 0,
+ },
+ );
});
it("does not misclassify the total key", () => {
diff --git a/packages/shared/src/server/repositories/observations_converters.ts b/packages/shared/src/server/repositories/observations_converters.ts
index fd5abc1f9d..e51b212c9c 100644
--- a/packages/shared/src/server/repositories/observations_converters.ts
+++ b/packages/shared/src/server/repositories/observations_converters.ts
@@ -471,6 +471,8 @@ export const reduceUsageOrCostDetails = (
(acc, [, value]) => (acc ?? 0) + Number(value),
null as number | null, // default to null if no output usage is found
),
- total: Number(details?.total ?? 0),
+ // Keep an explicit 0 (deliberate zero cost/usage). A missing `total` key
+ // is null so callers can distinguish "no value" from zero.
+ total: details?.total != null ? Number(details.total) : null,
};
};
diff --git a/web/src/__tests__/server/unit/observations-converters.servertest.ts b/web/src/__tests__/server/unit/observations-converters.servertest.ts
index 69d94736e7..789fe3dec1 100644
--- a/web/src/__tests__/server/unit/observations-converters.servertest.ts
+++ b/web/src/__tests__/server/unit/observations-converters.servertest.ts
@@ -169,5 +169,25 @@ describe("convertEventsObservation", () => {
expect(result).toHaveProperty("release", null);
expect(result).toHaveProperty("tags", null);
});
+
+ it("keeps missing cost as null and preserves an explicit zero", () => {
+ const missing = convertEventsObservation(
+ makeRecord({ cost_details: {} }),
+ DEFAULT_RENDERING_PROPS,
+ true,
+ );
+ expect(missing.totalCost).toBeNull();
+ expect(missing.inputCost).toBeNull();
+ expect(missing.outputCost).toBeNull();
+
+ const explicitZero = convertEventsObservation(
+ makeRecord({ cost_details: { total: 0, input: 0, output: 0 } }),
+ DEFAULT_RENDERING_PROPS,
+ true,
+ );
+ expect(explicitZero.totalCost).toBe(0);
+ expect(explicitZero.inputCost).toBe(0);
+ expect(explicitZero.outputCost).toBe(0);
+ });
});
});
diff --git a/web/src/components/table/use-cases/observations.tsx b/web/src/components/table/use-cases/observations.tsx
index b84ec2b77c..afefad4507 100644
--- a/web/src/components/table/use-cases/observations.tsx
+++ b/web/src/components/table/use-cases/observations.tsx
@@ -53,6 +53,10 @@ import { type LangfuseColumnDef } from "@/src/components/table/types";
import { cn } from "@/src/utils/tailwind";
import { getLevelColors } from "@/src/components/level-colors";
import { numberFormatter, usdFormatter } from "@/src/utils/numbers";
+import {
+ formatObservationCost,
+ isObservationCostDisplayable,
+} from "@/src/utils/observationCost";
import { useOrderByState } from "@/src/features/orderBy/hooks/useOrderByState";
import { useRowHeightLocalStorage } from "@/src/components/table/data-table-row-height-switch";
import { MemoizedIOTableCell } from "../../ui/IOTableCell";
@@ -830,8 +834,13 @@ export default function ObservationsTable({
size: 120,
cell: ({ row }) => {
const value: number | undefined = row.getValue("totalCost");
+ const type = row.original.type;
+
+ if (!isObservationCostDisplayable(value, type)) {
+ return <span>{formatObservationCost(value, type)}</span>;
+ }
- return value !== undefined ? (
+ return (
<BreakdownTooltip
details={row.original.costDetails}
isCost
@@ -842,7 +851,7 @@ export default function ObservationsTable({
<InfoIcon className="h-3 w-3" />
</div>
</BreakdownTooltip>
- ) : undefined;
+ );
},
enableHiding: true,
enableSorting,
@@ -1169,9 +1178,11 @@ export default function ObservationsTable({
outputCost: number | undefined;
} = row.getValue("cost");
- return value.inputCost !== undefined ? (
- <span>{usdFormatter(value.inputCost)}</span>
- ) : undefined;
+ return (
+ <span>
+ {formatObservationCost(value.inputCost, row.original.type)}
+ </span>
+ );
},
enableHiding: true,
defaultHidden: true,
@@ -1189,9 +1200,11 @@ export default function ObservationsTable({
outputCost: number | undefined;
} = row.getValue("cost");
- return value.outputCost !== undefined ? (
- <span>{usdFormatter(value.outputCost)}</span>
- ) : undefined;
+ return (
+ <span>
+ {formatObservationCost(value.outputCost, row.original.type)}
+ </span>
+ );
},
enableHiding: true,
defaultHidden: true,
diff --git a/web/src/features/events/components/EventsTable.tsx b/web/src/features/events/components/EventsTable.tsx
index cf9954aca1..d502556e2c 100644
--- a/web/src/features/events/components/EventsTable.tsx
+++ b/web/src/features/events/components/EventsTable.tsx
@@ -48,6 +48,10 @@ import { filterStateToQueryText } from "@/src/features/search-bar/lib/filter-sta
import { cn } from "@/src/utils/tailwind";
import { getLevelColors } from "@/src/components/level-colors";
import { compactNumberFormatter, usdFormatter } from "@/src/utils/numbers";
+import {
+ formatObservationCost,
+ isObservationCostDisplayable,
+} from "@/src/utils/observationCost";
import { useOrderByState } from "@/src/features/orderBy/hooks/useOrderByState";
import {
getRowHeightIOCharLimit,
@@ -1318,8 +1322,13 @@ export default function ObservationsEventsTable({
size: 120,
cell: ({ row }) => {
const value: number | undefined = row.getValue("totalCost");
+ const type = row.original.type;
+
+ if (!isObservationCostDisplayable(value, type)) {
+ return <span>{formatObservationCost(value, type)}</span>;
+ }
- return value !== undefined ? (
+ return (
<BreakdownTooltip
details={row.original.costDetails}
isCost
@@ -1330,7 +1339,7 @@ export default function ObservationsEventsTable({
<InfoIcon className="h-3 w-3" />
</div>
</BreakdownTooltip>
- ) : undefined;
+ );
},
enableHiding: true,
enableSorting,
@@ -1359,9 +1368,11 @@ export default function ObservationsEventsTable({
outputCost: number | undefined;
};
- return value.inputCost !== undefined ? (
- <span>{usdFormatter(value.inputCost)}</span>
- ) : undefined;
+ return (
+ <span>
+ {formatObservationCost(value.inputCost, row.original.type)}
+ </span>
+ );
},
enableHiding: true,
defaultHidden: true,
@@ -1379,9 +1390,11 @@ export default function ObservationsEventsTable({
outputCost: number | undefined;
};
- return value.outputCost !== undefined ? (
- <span>{usdFormatter(value.outputCost)}</span>
- ) : undefined;
+ return (
+ <span>
+ {formatObservationCost(value.outputCost, row.original.type)}
+ </span>
+ );
},
enableHiding: true,
defaultHidden: true,
diff --git a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx
index ef6b70261e..ddc5cfc18c 100644
--- a/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx
+++ b/web/src/features/traces/components/ObservationMetadataBadgesTooltip.tsx
@@ -21,8 +21,8 @@ export function CostBadge({
costDetails: Record<string, number> | undefined;
priceSource?: PriceSource;
}) {
- // Don't show if no cost data or cost is 0
- if (totalCost == null || totalCost === 0 || !costDetails) return null;
+ // Don't show if no cost data. Explicit 0 is a real value and should render.
+ if (totalCost == null || !costDetails) return null;
return (
<BreakdownTooltip
diff --git a/web/src/utils/observationCost.clienttest.ts b/web/src/utils/observationCost.clienttest.ts
new file mode 100644
index 0000000000..7dbc496ca5
--- /dev/null
+++ b/web/src/utils/observationCost.clienttest.ts
@@ -0,0 +1,60 @@
+import { ObservationType } from "@langfuse/shared";
+import { describe, expect, it } from "vitest";
+
+import {
+ formatObservationCost,
+ isObservationCostDisplayable,
+ isObservationCostSupported,
+ MISSING_OBSERVATION_COST_PLACEHOLDER,
+} from "@/src/utils/observationCost";
+
+describe("formatObservationCost", () => {
+ it("shows a formatted amount for an explicit zero on generations", () => {
+ expect(formatObservationCost(0, ObservationType.GENERATION)).toBe("$0.00");
+ expect(isObservationCostDisplayable(0, ObservationType.GENERATION)).toBe(
+ true,
+ );
+ });
+
+ it("shows a dash when cost is missing on a generation", () => {
+ expect(formatObservationCost(null, ObservationType.GENERATION)).toBe(
+ MISSING_OBSERVATION_COST_PLACEHOLDER,
+ );
+ expect(formatObservationCost(undefined, ObservationType.GENERATION)).toBe(
+ MISSING_OBSERVATION_COST_PLACEHOLDER,
+ );
+ });
+
+ it("shows a dash for spans and events when cost is missing", () => {
+ expect(formatObservationCost(null, ObservationType.SPAN)).toBe(
+ MISSING_OBSERVATION_COST_PLACEHOLDER,
+ );
+ expect(formatObservationCost(undefined, ObservationType.EVENT)).toBe(
+ MISSING_OBSERVATION_COST_PLACEHOLDER,
+ );
+ });
+
+ it("does not hide a persisted cost on generation-like types", () => {
+ expect(isObservationCostSupported(ObservationType.EMBEDDING)).toBe(true);
+ expect(isObservationCostSupported(ObservationType.AGENT)).toBe(true);
+ expect(formatObservationCost(0.12, ObservationType.AGENT)).toBe("$0.12");
+ expect(formatObservationCost(0.0003, ObservationType.EMBEDDING)).toBe(
+ "$0.0003",
+ );
+ });
+
+ it("does not hide a persisted cost on span or event rows", () => {
+ expect(formatObservationCost(0.12, ObservationType.SPAN)).toBe("$0.12");
+ expect(formatObservationCost(0, ObservationType.EVENT)).toBe("$0.00");
+ });
+
+ it("formats a persisted cost when type is missing", () => {
+ expect(formatObservationCost(0.12, undefined)).toBe("$0.12");
+ });
+
+ it("formats a positive generation cost", () => {
+ expect(formatObservationCost(0.0045, ObservationType.GENERATION)).toBe(
+ "$0.0045",
+ );
+ });
+});
diff --git a/web/src/utils/observationCost.ts b/web/src/utils/observationCost.ts
new file mode 100644
index 0000000000..b723d38a54
--- /dev/null
+++ b/web/src/utils/observationCost.ts
@@ -0,0 +1,38 @@
+import { isGenerationLike, type ObservationType } from "@langfuse/shared";
+
+import { usdFormatter } from "@/src/utils/numbers";
+
+export const MISSING_OBSERVATION_COST_PLACEHOLDER = "-";
+
+/**
+ * Types that ingestion can attach usage/cost to (CreateGenerationBody),
+ * matching `isGenerationLike`. Used to describe typical cost-bearing rows;
+ * display still follows a stored amount, not this set alone.
+ */
+export function isObservationCostSupported(
+ observationType: ObservationType | string | undefined,
+): boolean {
+ if (observationType == null) return false;
+ return isGenerationLike(observationType as ObservationType);
+}
+
+/**
+ * True when the cell should show a numeric cost, including an explicit 0.
+ * Missing values render as a dash. A persisted amount is not hidden by type.
+ */
+export function isObservationCostDisplayable(
+ cost: number | null | undefined,
+ _observationType?: ObservationType | string,
+): cost is number {
+ return cost != null;
+}
+
+export function formatObservationCost(
+ cost: number | null | undefined,
+ observationType?: ObservationType | string,
+): string {
+ if (!isObservationCostDisplayable(cost, observationType)) {
+ return MISSING_OBSERVATION_COST_PLACEHOLDER;
+ }
+ return usdFormatter(cost);
+}