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 session runner sums (#92409)

Bernat Torresad18aca859f9
diff --git a/posthog/hogql_queries/ai/session_query_runner.py b/posthog/hogql_queries/ai/session_query_runner.py
index 9a5f8da620d4..517205333d22 100644
--- a/posthog/hogql_queries/ai/session_query_runner.py
+++ b/posthog/hogql_queries/ai/session_query_runner.py
@@ -173,37 +173,41 @@ class SessionQueryRunner(AnalyticsQueryRunner[SessionQueryResponse]):
)
END, 2
) AS total_latency,
- nullIf(sumIf(input_tokens,
+ -- NULL means no event carried the field, 0 is a reported zero.
+ -- These columns are Nullable, so a bare sum already returns NULL
+ -- when nothing reported and 0 for a reported zero; nullIf(sum, 0)
+ -- would collapse a real zero into NULL. Matches traces_query_runner.
+ sumIf(input_tokens,
event IN ('$ai_generation', '$ai_embedding')
- ), 0) AS input_tokens,
- nullIf(sumIf(output_tokens,
+ ) AS input_tokens,
+ sumIf(output_tokens,
event IN ('$ai_generation', '$ai_embedding')
- ), 0) AS output_tokens,
- nullIf(round(
+ ) AS output_tokens,
+ round(
sumIf(input_cost_usd,
event IN ('$ai_generation', '$ai_embedding')
), 10
- ), 0) AS input_cost,
- nullIf(round(
+ ) AS input_cost,
+ round(
sumIf(output_cost_usd,
event IN ('$ai_generation', '$ai_embedding')
), 10
- ), 0) AS output_cost,
- nullIf(round(
+ ) AS output_cost,
+ round(
sumIf(request_cost_usd,
event IN ('$ai_generation', '$ai_embedding')
), 10
- ), 0) AS request_cost,
- nullIf(round(
+ ) AS request_cost,
+ round(
sumIf(web_search_cost_usd,
event IN ('$ai_generation', '$ai_embedding')
), 10
- ), 0) AS web_search_cost,
- nullIf(round(
+ ) AS web_search_cost,
+ round(
sumIf(total_cost_usd,
event IN ('$ai_generation', '$ai_embedding')
), 10
- ), 0) AS total_cost,
+ ) AS total_cost,
arrayDistinct(
arraySort(
x -> x.3,
diff --git a/posthog/hogql_queries/ai/test/test_session_query_runner.py b/posthog/hogql_queries/ai/test/test_session_query_runner.py
index 796ba3b0b4af..3e18ab37b4d1 100644
--- a/posthog/hogql_queries/ai/test/test_session_query_runner.py
+++ b/posthog/hogql_queries/ai/test/test_session_query_runner.py
@@ -132,6 +132,54 @@ class TestSessionQueryRunner(ClickhouseTestMixin, BaseTest):
self.assertAlmostEqual(trace.totalCost or 0, 0.01)
self.assertEqual(trace.totalLatency, 3)
+ def test_sums_distinguish_reported_zero_from_no_report(self) -> None:
+ bulk_create_ai_events(
+ [
+ {
+ "event": "$ai_generation",
+ "distinct_id": "person1",
+ "team": self.team,
+ "timestamp": datetime(2025, 1, 15, 0, 0, tzinfo=UTC),
+ "properties": {
+ "$ai_session_id": "session-zero",
+ "$ai_trace_id": "trace_zero",
+ "$ai_latency": 1,
+ "$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.
+ {
+ "event": "$ai_generation",
+ "distinct_id": "person1",
+ "team": self.team,
+ "timestamp": datetime(2025, 1, 15, 0, 1, tzinfo=UTC),
+ "properties": {
+ "$ai_session_id": "session-zero",
+ "$ai_trace_id": "trace_unpriced",
+ "$ai_latency": 1,
+ },
+ },
+ ]
+ )
+
+ runner = SessionQueryRunner(team=self.team, query=SessionQuery(sessionId="session-zero"))
+ traces = {trace.id: trace for trace in runner.calculate().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)
+
def test_paginates_session_traces(self) -> None:
bulk_create_ai_events(
[
diff --git a/products/ai_observability/AGENTS.md b/products/ai_observability/AGENTS.md
index 3c69ad0ee8b5..1ae3bb5abc5c 100644
--- a/products/ai_observability/AGENTS.md
+++ b/products/ai_observability/AGENTS.md
@@ -10,23 +10,24 @@ This file is only for rules that are specific to `products/ai_observability/`
for the directory map. Skip it if you already know where things live —
it's orientation, not a rulebook.
-## Update both query runners together
+## Update all three query runners together
-`LLMTrace` and `LLMTraceEvent` are built by two parallel query runners:
+`LLMTrace` and `LLMTraceEvent` are built by three parallel query runners:
- `posthog/hogql_queries/ai/trace_query_runner.py` (single trace read)
- `posthog/hogql_queries/ai/traces_query_runner.py` (list of traces)
+- `posthog/hogql_queries/ai/session_query_runner.py` (traces in a session)
-Both define their own `_map_trace` and `_map_event`. Neither imports
-from the other. Any time you add, remove, or rename a field on
+Each defines its own `_map_trace` and `_map_event`. None imports
+from the others. Any time you add, remove, or rename a field on
`LLMTrace` or `LLMTraceEvent`, or change how an event is shaped into
-either, **change both runners in the same PR** and cover the change in
-`posthog/hogql_queries/ai/test/`. The type checker will not catch drift
-here.
+any of them, **change all three runners in the same PR** and cover the
+change in `posthog/hogql_queries/ai/test/`. The type checker will not
+catch drift here.
The same rule applies to routing via `ai_events` vs `events`. If one
-runner adopts a new code path, the other needs the matching change so
-single-trace and list reads don't diverge.
+runner adopts a new code path, the others need the matching change so
+single-trace, list, and session reads don't diverge.
## Product docs go under `docs/`
diff --git a/products/ai_observability/README.md b/products/ai_observability/README.md
index 5e6a7de446a9..7eccf9cf4927 100644
--- a/products/ai_observability/README.md
+++ b/products/ai_observability/README.md
@@ -42,8 +42,9 @@ products/ai_observability/
```
Query runners for the product live **outside** this folder in
-`posthog/hogql_queries/ai/`. Two runners matter: `trace_query_runner.py`
-(single trace) and `traces_query_runner.py` (list). See
+`posthog/hogql_queries/ai/`. Three runners matter: `trace_query_runner.py`
+(single trace), `traces_query_runner.py` (list), and
+`session_query_runner.py` (traces in a session). See
[AGENTS.md](./AGENTS.md) for why that detail matters.
## Running tests locally