Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(aio): distinguish zero from unknown in trace header sums (#91579)

Bernat Torres7ce058d14e5b
diff --git a/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambr b/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambr
index 4c042b4c1faf..97c5e8d8a757 100644
--- a/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambr
+++ b/posthog/hogql_queries/ai/test/__snapshots__/test_trace_query_runner.ambr
@@ -8,11 +8,11 @@
ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,
round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)
and isNull(deduped.trace_id))))), 2) AS total_latency,
- nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,
- nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,
- nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,
- nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,
+ if(greater(countIf(and(isNotNull(deduped.input_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS input_tokens,
+ if(greater(countIf(and(isNotNull(deduped.output_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS output_tokens,
+ if(greater(countIf(and(isNotNull(deduped.input_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS input_cost,
+ if(greater(countIf(and(isNotNull(deduped.output_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS output_cost,
+ if(greater(countIf(and(isNotNull(deduped.total_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS total_cost,
arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,
argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,
argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,
@@ -67,11 +67,11 @@
ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,
round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)
and isNull(deduped.trace_id))))), 2) AS total_latency,
- nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,
- nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,
- nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,
- nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,
+ if(greater(countIf(and(isNotNull(deduped.input_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS input_tokens,
+ if(greater(countIf(and(isNotNull(deduped.output_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS output_tokens,
+ if(greater(countIf(and(isNotNull(deduped.input_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS input_cost,
+ if(greater(countIf(and(isNotNull(deduped.output_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS output_cost,
+ if(greater(countIf(and(isNotNull(deduped.total_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS total_cost,
arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,
argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,
argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,
@@ -126,11 +126,11 @@
ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,
round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)
and isNull(deduped.trace_id))))), 2) AS total_latency,
- nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,
- nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,
- nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,
- nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,
+ if(greater(countIf(and(isNotNull(deduped.input_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS input_tokens,
+ if(greater(countIf(and(isNotNull(deduped.output_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS output_tokens,
+ if(greater(countIf(and(isNotNull(deduped.input_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS input_cost,
+ if(greater(countIf(and(isNotNull(deduped.output_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS output_cost,
+ if(greater(countIf(and(isNotNull(deduped.total_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS total_cost,
arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,
argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,
argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,
@@ -185,11 +185,11 @@
ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,
round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)
and isNull(deduped.trace_id))))), 2) AS total_latency,
- nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,
- nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,
- nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,
- nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,
+ if(greater(countIf(and(isNotNull(deduped.input_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS input_tokens,
+ if(greater(countIf(and(isNotNull(deduped.output_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS output_tokens,
+ if(greater(countIf(and(isNotNull(deduped.input_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS input_cost,
+ if(greater(countIf(and(isNotNull(deduped.output_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS output_cost,
+ if(greater(countIf(and(isNotNull(deduped.total_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS total_cost,
arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,
argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,
argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,
@@ -244,11 +244,11 @@
ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,
round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)
and isNull(deduped.trace_id))))), 2) AS total_latency,
- nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,
- nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,
- nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,
- nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,
+ if(greater(countIf(and(isNotNull(deduped.input_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS input_tokens,
+ if(greater(countIf(and(isNotNull(deduped.output_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS output_tokens,
+ if(greater(countIf(and(isNotNull(deduped.input_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS input_cost,
+ if(greater(countIf(and(isNotNull(deduped.output_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS output_cost,
+ if(greater(countIf(and(isNotNull(deduped.total_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS total_cost,
arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,
argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,
argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,
@@ -321,11 +321,11 @@
ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,
round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)
and isNull(deduped.trace_id))))), 2) AS total_latency,
- nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,
- nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,
- nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,
- nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,
+ if(greater(countIf(and(isNotNull(deduped.input_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS input_tokens,
+ if(greater(countIf(and(isNotNull(deduped.output_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS output_tokens,
+ if(greater(countIf(and(isNotNull(deduped.input_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS input_cost,
+ if(greater(countIf(and(isNotNull(deduped.output_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS output_cost,
+ if(greater(countIf(and(isNotNull(deduped.total_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS total_cost,
arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,
argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,
argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,
@@ -398,11 +398,11 @@
ifNull(nullIf(argMinIf(deduped.distinct_id, deduped.timestamp, equals(deduped.event, '$ai_trace')), ''), argMin(deduped.distinct_id, deduped.timestamp)) AS first_distinct_id,
round(if(and(equals(countIf(and(ifNull(greater(deduped.latency, 0), 0), notEquals(deduped.event, '$ai_generation'))), 0), greater(countIf(and(ifNull(greater(deduped.latency, 0), 0), equals(deduped.event, '$ai_generation'))), 0)), sumIf(deduped.latency, and(equals(deduped.event, '$ai_generation'), ifNull(greater(deduped.latency, 0), 0))), sumIf(deduped.latency, or(isNull(deduped.parent_id), ifNull(equals(deduped.parent_id, deduped.trace_id), isNull(deduped.parent_id)
and isNull(deduped.trace_id))))), 2) AS total_latency,
- nullIf(sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 0) AS output_tokens,
- nullIf(round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS input_cost,
- nullIf(round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS output_cost,
- nullIf(round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), 0) AS total_cost,
+ if(greater(countIf(and(isNotNull(deduped.input_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.input_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS input_tokens,
+ if(greater(countIf(and(isNotNull(deduped.output_tokens), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), sumIf(deduped.output_tokens, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), NULL) AS output_tokens,
+ if(greater(countIf(and(isNotNull(deduped.input_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.input_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS input_cost,
+ if(greater(countIf(and(isNotNull(deduped.output_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.output_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS output_cost,
+ if(greater(countIf(and(isNotNull(deduped.total_cost_usd), in(deduped.event, tuple('$ai_generation', '$ai_embedding')))), 0), round(sumIf(deduped.total_cost_usd, in(deduped.event, tuple('$ai_generation', '$ai_embedding'))), 10), NULL) AS total_cost,
arrayDistinct(arraySort(x -> x.3, groupArrayIf(tuple(deduped.uuid, deduped.event, deduped.timestamp, deduped.properties, deduped.input, deduped.output, deduped.output_choices, deduped.input_state, deduped.output_state, deduped.tools), notEquals(deduped.event, '$ai_trace')))) AS events,
argMinIf(deduped.input_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS input_state,
argMinIf(deduped.output_state, deduped.timestamp, equals(deduped.event, '$ai_trace')) AS output_state,
diff --git a/posthog/hogql_queries/ai/test/test_trace_query_runner.py b/posthog/hogql_queries/ai/test/test_trace_query_runner.py
index 0fb037b39806..00c160e8d7cc 100644
--- a/posthog/hogql_queries/ai/test/test_trace_query_runner.py
+++ b/posthog/hogql_queries/ai/test/test_trace_query_runner.py
@@ -966,6 +966,57 @@ class TestTraceQueryRunner(ClickhouseTestMixin, BaseTest):
self.assertEqual(trace.totalLatency, 3.9)
self.assertEqual(trace.events[0].properties.get("$ai_input"), [{"role": "user", "content": "Foo"}])
+ def test_sums_distinguish_reported_zero_from_no_report(self):
+ zero_trace_id = str(uuid.uuid4())
+ unpriced_trace_id = str(uuid.uuid4())
+ timestamp = datetime(2024, 12, 1, 0, 0, tzinfo=UTC)
+ _create_ai_generation_event(
+ distinct_id="person1",
+ trace_id=zero_trace_id,
+ team=self.team,
+ timestamp=timestamp,
+ properties={
+ "$ai_input_tokens": 0,
+ "$ai_output_tokens": 0,
+ "$ai_input_cost_usd": 0,
+ "$ai_output_cost_usd": 0,
+ "$ai_total_cost_usd": 0,
+ },
+ )
+ # A generation whose provider never reported usage carries no token or
+ # cost properties at all.
+ _create_event(
+ event="$ai_generation",
+ distinct_id="person1",
+ team=self.team,
+ timestamp=timestamp,
+ properties={
+ "$ai_trace_id": unpriced_trace_id,
+ "$ai_latency": 1,
+ "$ai_input": [{"role": "user", "content": "Foo"}],
+ "$ai_output_choices": [{"role": "assistant", "content": "Bar"}],
+ },
+ )
+
+ date_range = DateRange(date_from="2024-12-01T00:00:00Z", date_to="2024-12-01T00:10:00Z")
+ zero_trace = (
+ TraceQueryRunner(team=self.team, query=TraceQuery(traceId=zero_trace_id, dateRange=date_range))
+ .calculate()
+ .results[0]
+ )
+ unpriced_trace = (
+ TraceQueryRunner(team=self.team, query=TraceQuery(traceId=unpriced_trace_id, dateRange=date_range))
+ .calculate()
+ .results[0]
+ )
+
+ self.assertEqual(zero_trace.totalCost, 0)
+ self.assertEqual(zero_trace.inputCost, 0)
+ self.assertEqual(zero_trace.inputTokens, 0)
+ self.assertIsNone(unpriced_trace.totalCost)
+ self.assertIsNone(unpriced_trace.inputCost)
+ self.assertIsNone(unpriced_trace.inputTokens)
+
def test_trace_name_from_trace_event(self):
"""Test that trace_name comes from $ai_trace events when they exist."""
_create_person(distinct_ids=["person1"], team=self.team)
diff --git a/posthog/hogql_queries/ai/test/test_traces_query_runner.py b/posthog/hogql_queries/ai/test/test_traces_query_runner.py
index c2bce44a1512..678276427a62 100644
--- a/posthog/hogql_queries/ai/test/test_traces_query_runner.py
+++ b/posthog/hogql_queries/ai/test/test_traces_query_runner.py
@@ -346,6 +346,48 @@ class TestTracesQueryRunner(ClickhouseTestMixin, BaseTest):
# test_trace_id_filter removed - TracesQuery no longer supports traceId parameter
+ @freeze_time("2025-01-16T00:00:00Z")
+ def test_sums_distinguish_reported_zero_from_no_report(self):
+ _create_person(distinct_ids=["person1"], team=self.team)
+ _create_ai_generation_event(
+ distinct_id="person1",
+ trace_id="trace_zero",
+ team=self.team,
+ timestamp=datetime(2025, 1, 15, 0),
+ properties={
+ "$ai_input_tokens": 0,
+ "$ai_output_tokens": 0,
+ "$ai_input_cost_usd": 0,
+ "$ai_output_cost_usd": 0,
+ "$ai_total_cost_usd": 0,
+ },
+ )
+ # A generation whose provider never reported usage carries no token or
+ # cost properties at all.
+ _create_event(
+ event="$ai_generation",
+ distinct_id="person1",
+ team=self.team,
+ timestamp=datetime(2025, 1, 15, 1),
+ properties={
+ "$ai_trace_id": "trace_unpriced",
+ "$ai_latency": 1,
+ },
+ )
+
+ response = TracesQueryRunner(team=self.team, query=TracesQuery()).calculate()
+ traces = {trace.id: trace for trace in response.results}
+
+ zero_trace = traces["trace_zero"]
+ self.assertEqual(zero_trace.totalCost, 0)
+ self.assertEqual(zero_trace.inputCost, 0)
+ self.assertEqual(zero_trace.inputTokens, 0)
+
+ unpriced_trace = traces["trace_unpriced"]
+ self.assertIsNone(unpriced_trace.totalCost)
+ self.assertIsNone(unpriced_trace.inputCost)
+ self.assertIsNone(unpriced_trace.inputTokens)
+
@freeze_time("2025-01-16T00:00:00Z")
def test_stored_sentiment_evaluations_are_mapped_to_trace_and_generation(self):
event_uuid = uuid.uuid4()
diff --git a/posthog/hogql_queries/ai/trace_query_runner.py b/posthog/hogql_queries/ai/trace_query_runner.py
index 8f541457032d..fbcbc99e4b53 100644
--- a/posthog/hogql_queries/ai/trace_query_runner.py
+++ b/posthog/hogql_queries/ai/trace_query_runner.py
@@ -151,27 +151,43 @@ class TraceQueryRunner(AnalyticsQueryRunner[TraceQueryResponse]):
)
END, 2
) AS total_latency,
- nullIf(sumIf(deduped.input_tokens,
- deduped.event IN ('$ai_generation', '$ai_embedding')
- ), 0) AS input_tokens,
- nullIf(sumIf(deduped.output_tokens,
- deduped.event IN ('$ai_generation', '$ai_embedding')
- ), 0) AS output_tokens,
- nullIf(round(
- sumIf(deduped.input_cost_usd,
- deduped.event IN ('$ai_generation', '$ai_embedding')
- ), 10
- ), 0) AS input_cost,
- nullIf(round(
- sumIf(deduped.output_cost_usd,
- deduped.event IN ('$ai_generation', '$ai_embedding')
- ), 10
- ), 0) AS output_cost,
- nullIf(round(
- sumIf(deduped.total_cost_usd,
- deduped.event IN ('$ai_generation', '$ai_embedding')
- ), 10
- ), 0) AS total_cost,
+ -- NULL means no event carried the field, 0 is a reported zero.
+ -- nullIf(sum, 0) would collapse a real zero into NULL.
+ if(countIf(isNotNull(deduped.input_tokens)
+ AND deduped.event IN ('$ai_generation', '$ai_embedding')) > 0,
+ sumIf(deduped.input_tokens,
+ deduped.event IN ('$ai_generation', '$ai_embedding')
+ ),
+ NULL
+ ) AS input_tokens,
+ if(countIf(isNotNull(deduped.output_tokens)
+ AND deduped.event IN ('$ai_generation', '$ai_embedding')) > 0,
+ sumIf(deduped.output_tokens,
+ deduped.event IN ('$ai_generation', '$ai_embedding')
+ ),
+ NULL
+ ) AS output_tokens,
+ if(countIf(isNotNull(deduped.input_cost_usd)
+ AND deduped.event IN ('$ai_generation', '$ai_embedding')) > 0,
+ round(sumIf(deduped.input_cost_usd,
+ deduped.event IN ('$ai_generation', '$ai_embedding')
+ ), 10),
+ NULL
+ ) AS input_cost,
+ if(countIf(isNotNull(deduped.output_cost_usd)
+ AND deduped.event IN ('$ai_generation', '$ai_embedding')) > 0,
+ round(sumIf(deduped.output_cost_usd,
+ deduped.event IN ('$ai_generation', '$ai_embedding')
+ ), 10),
+ NULL
+ ) AS output_cost,
+ if(countIf(isNotNull(deduped.total_cost_usd)
+ AND deduped.event IN ('$ai_generation', '$ai_embedding')) > 0,
+ round(sumIf(deduped.total_cost_usd,
+ deduped.event IN ('$ai_generation', '$ai_embedding')
+ ), 10),
+ NULL
+ ) AS total_cost,
arrayDistinct(
arraySort(
x -> x.3,
diff --git a/products/ai_observability/frontend/traceExportUtils.test.ts b/products/ai_observability/frontend/traceExportUtils.test.ts
index 3df96c093aaa..420d5c6b14ba 100644
--- a/products/ai_observability/frontend/traceExportUtils.test.ts
+++ b/products/ai_observability/frontend/traceExportUtils.test.ts
@@ -316,6 +316,34 @@ describe('traceExportUtils', () => {
expect(result.events[0].metrics).toBeUndefined()
})
+ it('should keep a genuine zero cost at both the trace and event level', () => {
+ const zeroCostTrace: LLMTrace = { ...mockTrace, totalCost: 0 }
+ const zeroCostEvent: LLMTraceEvent = {
+ id: 'event-zero',
+ event: '$ai_generation',
+ properties: {
+ $ai_model: 'claude-3',
+ $ai_latency: 120,
+ $ai_total_cost_usd: 0,
+ },
+ createdAt: '2024-01-01T12:00:06Z',
+ }
+ const tree: EnrichedTraceTreeNode[] = [
+ {
+ event: zeroCostEvent,
+ displayTotalCost: 0,
+ displayLatency: 120,
+ displayUsage: null,
+ attachedFeedback: [],
+ },
+ ]
+ const result = buildMinimalTraceJSON(zeroCostTrace, tree)
+
+ // Falsy zero used to drop these keys entirely.
+ expect(result.total_cost).toBe(0)
+ expect(result.events[0].metrics).toEqual({ latency: 120, cost: 0 })
+ })
+
it('should handle generation with output_choices instead of output', () => {
const choicesEvent: LLMTraceEvent = {
id: 'event-6',
diff --git a/products/ai_observability/frontend/traceExportUtils.ts b/products/ai_observability/frontend/traceExportUtils.ts
index 6e0ae1ca9621..fb3abae91822 100644
--- a/products/ai_observability/frontend/traceExportUtils.ts
+++ b/products/ai_observability/frontend/traceExportUtils.ts
@@ -120,7 +120,9 @@ function buildEventExport(event: LLMTraceEvent, children?: EnrichedTraceTreeNode
output: event.properties.$ai_output_tokens || 0,
}
}
- if (event.properties.$ai_total_cost_usd) {
+ // `!= null` keeps a genuine $0 cost, which ingestion now reports for a
+ // generation that priced to zero, rather than dropping it as falsy.
+ if (event.properties.$ai_total_cost_usd != null) {
metrics.cost = event.properties.$ai_total_cost_usd
}
@@ -152,8 +154,9 @@ export function buildMinimalTraceJSON(trace: LLMTrace, tree: EnrichedTraceTreeNo
result.name = trace.traceName
}
- // Add total cost if available
- if (trace.totalCost) {
+ // Add total cost if reported. `!= null` keeps a genuine $0, which the trace
+ // header now shows, so the export agrees with it.
+ if (trace.totalCost != null) {
result.total_cost = trace.totalCost
}