Skip to content

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

fix(traces): ignore near-decimal cost noise (#17504)

Hassieb Pakzad5c528311125d
diff --git a/web/src/features/traces/components/BreakdownTooltip.stories.tsx b/web/src/features/traces/components/BreakdownTooltip.stories.tsx
index 94c7c5799f..cfde865c83 100644
--- a/web/src/features/traces/components/BreakdownTooltip.stories.tsx
+++ b/web/src/features/traces/components/BreakdownTooltip.stories.tsx
@@ -47,6 +47,13 @@ const providedTotalCostDetails = {
total: 0.01,
};
+const nearDecimalCostDetails = {
+ input: 0.0390650000001,
+ input_cached_tokens: 0.0034879999999,
+ output: 0.01579,
+ total: 0.058342999997,
+};
+
const priceSource = {
projectId: "project-1",
modelId: "gpt-5.6/priority",
@@ -209,3 +216,24 @@ export const TestCostFormattingAndTruncation = meta.story({
await expect(longLabel).toHaveAttribute("title", "input_cached_tokens");
},
});
+
+export const TestNearDecimalCostFormatting = meta.story({
+ name: "(Test) Ignores near-decimal floating point noise",
+ args: {
+ details: nearDecimalCostDetails,
+ children: <span>$0.058343</span>,
+ isCost: true,
+ },
+ play: async ({ canvasElement }) => {
+ const { content } = await openBreakdownTooltip(canvasElement, "$0.058343");
+
+ await expect(content.getAllByText("$0.039065")).toHaveLength(1);
+ await expect(content.getAllByText("$0.003488")).toHaveLength(1);
+ await expect(content.getAllByText("$0.015790")).toHaveLength(2);
+ await expect(content.getByText("$0.042553")).toBeInTheDocument();
+ await expect(content.getByText("$0.058343")).toBeInTheDocument();
+ await expect(
+ content.queryByText("$0.058342999997"),
+ ).not.toBeInTheDocument();
+ },
+});
diff --git a/web/src/features/traces/components/BreakdownTooltip.tsx b/web/src/features/traces/components/BreakdownTooltip.tsx
index 8829e65d1f..68619149aa 100644
--- a/web/src/features/traces/components/BreakdownTooltip.tsx
+++ b/web/src/features/traces/components/BreakdownTooltip.tsx
@@ -73,11 +73,7 @@ export const BreakdownTooltip = ({
return acc + value;
}, 0);
const contributionEntries = inputEntries.concat(outputEntries, otherEntries);
- const costFractionDigits = getCostFractionDigits(
- contributionEntries.length > 0
- ? contributionEntries
- : [["total", aggregatedDetails.total]],
- );
+ const costFractionDigits = getCostFractionDigits(entries);
const formatValue = (value: number | Decimal) => {
if (!isCost) {
const numericValue = value instanceof Decimal ? value.toNumber() : value;
@@ -345,18 +341,33 @@ function sumEntries(entries: [string, number | undefined][]) {
}
function getCostFractionDigits(entries: [string, number | undefined][]) {
- return Math.min(
- 12,
- Math.max(
- 2,
- ...entries
- .map(([, value]) => value)
- .filter((value): value is number => Boolean(value))
- .map((value) => new Decimal(value).decimalPlaces()),
- ),
+ return Math.max(
+ 2,
+ ...entries
+ .map(([, value]) => value)
+ .filter((value): value is number => Boolean(value))
+ .map(getArtifactFreeFractionDigits),
);
}
+function getArtifactFreeFractionDigits(value: number) {
+ const decimalValue = new Decimal(value);
+
+ for (let fractionDigits = 2; fractionDigits <= 12; fractionDigits++) {
+ const roundingUnit = new Decimal(10).pow(-fractionDigits);
+ const distanceToRoundedValue = decimalValue
+ .minus(decimalValue.toDecimalPlaces(fractionDigits))
+ .abs();
+
+ // Treat values within 1/10,000 of a decimal grid point as transport noise.
+ if (distanceToRoundedValue.lte(roundingUnit.div(10_000))) {
+ return fractionDigits;
+ }
+ }
+
+ return 12;
+}
+
function createWaterfallSegments(
entries: [string, number | undefined][],
): Map<string, WaterfallSegment> {