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(llma): route personal_spend through events table to dodge ai_events shim (#59784)

Paul D'Ambra57ddec3061e3
diff --git a/products/llm_analytics/backend/api/personal_spend.py b/products/llm_analytics/backend/api/personal_spend.py
index dd857cad2d7b..53e5c5f637e4 100644
--- a/products/llm_analytics/backend/api/personal_spend.py
+++ b/products/llm_analytics/backend/api/personal_spend.py
@@ -31,9 +31,10 @@ from rest_framework.response import Response
from posthog.hogql import ast
from posthog.hogql.parser import parse_select
+from posthog.hogql.query import execute_hogql_query
from posthog.auth import OAuthAccessTokenAuthentication, PersonalAPIKeyAuthentication, SessionAuthentication
-from posthog.hogql_queries.ai.ai_table_resolver import execute_with_ai_events_fallback
+from posthog.clickhouse.query_tagging import Product, tags_context
from posthog.models import Team, User
from posthog.permissions import APIScopePermission
from posthog.rate_limit import PersonalSpendBurstThrottle, PersonalSpendDailyThrottle, PersonalSpendSustainedThrottle
@@ -286,10 +287,11 @@ class PersonalSpendAnalysisResponseSerializer(serializers.Serializer):
def _email_filter(email: str) -> ast.Expr:
- # With person-on-events mode `person.properties.email` is a denormalized column on
- # the events row, so this is a single column read rather than a join — it also catches
- # every event the user identified with, including historical distinct_ids merged into
- # the same person.
+ # With person-on-events, the event row carries `person_id` directly; HogQL then joins to
+ # `person` to read `properties.email`. The join is bounded to one person per event (not a
+ # full pdi walk), and on the main cluster the printer transparently uses the materialized
+ # `pmat_email` column when registered. Same shape the LLM Analytics "Users" tab uses
+ # against `events` -- see `products/llm_analytics/frontend/tabs/llmAnalyticsUsersLogic.ts`.
return ast.CompareOperation(
op=ast.CompareOperationOp.Eq,
left=ast.Field(chain=["person", "properties", "email"]),
@@ -354,11 +356,11 @@ def _fetch_summary(
round(sumIf(toFloat(properties.$ai_total_cost_usd), {product_filter}), 6) AS scoped_cost_usd,
count() AS event_count,
round(sum(toFloat(properties.$ai_total_cost_usd)), 6) AS total_cost_usd
- FROM posthog.ai_events
+ FROM events
WHERE {event_in} AND {email_filter} AND {timestamp_filter}
"""
)
- result = execute_with_ai_events_fallback(
+ result = execute_hogql_query(
query=query,
placeholders={
"product_filter": _product_filter(product),
@@ -390,14 +392,14 @@ def _fetch_by_product(
properties.ai_product AS product,
count() AS event_count,
round(sum(toFloat(properties.$ai_total_cost_usd)), 6) AS cost_usd
- FROM posthog.ai_events
+ FROM events
WHERE {event_in} AND {email_filter} AND {timestamp_filter}
GROUP BY product
ORDER BY cost_usd DESC
LIMIT {limit}
"""
)
- result = execute_with_ai_events_fallback(
+ result = execute_hogql_query(
query=query,
placeholders={
"event_in": _event_in(["$ai_generation", "$ai_embedding"]),
@@ -438,7 +440,7 @@ def _fetch_by_tool(
count() AS generation_count,
round(sum(toFloat(properties.$ai_total_cost_usd)), 6) AS cost_usd,
round(avg(toFloat(properties.$ai_input_tokens)), 0) AS avg_input_tokens
- FROM posthog.ai_events
+ FROM events
WHERE equals(event, '$ai_generation')
AND {product_filter}
AND {email_filter}
@@ -448,7 +450,7 @@ def _fetch_by_tool(
LIMIT {limit}
"""
)
- result = execute_with_ai_events_fallback(
+ result = execute_hogql_query(
query=query,
placeholders={
"product_filter": _product_filter(product),
@@ -487,7 +489,7 @@ def _fetch_by_model(
round(sum(toFloat(properties.$ai_total_cost_usd)), 6) AS cost_usd,
sum(toFloat(properties.$ai_input_tokens)) AS input_tokens,
sum(toFloat(properties.$ai_output_tokens)) AS output_tokens
- FROM posthog.ai_events
+ FROM events
WHERE {event_in}
AND {product_filter}
AND {email_filter}
@@ -497,7 +499,7 @@ def _fetch_by_model(
LIMIT {limit}
"""
)
- result = execute_with_ai_events_fallback(
+ result = execute_hogql_query(
query=query,
placeholders={
"event_in": _event_in(["$ai_generation", "$ai_embedding"]),
@@ -537,7 +539,7 @@ def _fetch_top_traces(
count() AS generation_count,
round(sum(toFloat(properties.$ai_total_cost_usd)), 6) AS cost_usd,
min(timestamp) AS started_at
- FROM posthog.ai_events
+ FROM events
WHERE equals(event, '$ai_generation')
AND {product_filter}
AND {email_filter}
@@ -547,7 +549,7 @@ def _fetch_top_traces(
LIMIT {limit}
"""
)
- result = execute_with_ai_events_fallback(
+ result = execute_hogql_query(
query=query,
placeholders={
"product_filter": _product_filter(product),
@@ -579,10 +581,20 @@ class PersonalSpendViewSet(viewsets.ViewSet):
authenticated user's email (read off the events row via person-on-events) —
callers cannot pivot to other users' data. Authorization model is "any
authenticated PostHog user may read their own spend" via `user:read` (the
- same scope that covers `/api/users/@me/`). Routes through
- `execute_with_ai_events_fallback` so reads hit the
- dedicated `ai_events` table when enabled, with the shared `events` table as
- a fallback. Optionally filter tool / model / trace breakdowns to a single
+ same scope that covers `/api/users/@me/`). Queries the shared `events`
+ table directly -- same pattern as the LLM Analytics "Users" tab and every
+ other person-property filter on AI events. We don't route through
+ `execute_with_ai_events_fallback` because the satellite `ai_events`
+ cluster's Distributed `person` shim doesn't declare materialized columns
+ like `pmat_email`, and the helper's fallback only catches empty results,
+ not the unresolved-identifier exception that would fire there. The
+ `events`-table WHERE clauses lead with the sort-key columns (`team_id`,
+ `event`, `timestamp`) so the scan narrows before the person join fires,
+ and the HogQL printer uses `pmat_email` on the main cluster's `person`
+ when registered -- so this path should be comparable to what the
+ satellite would have served. The endpoint is cached for 5 minutes per
+ user (see `CACHE_TIMEOUT_SECONDS`). Optionally filter tool /
+ model / trace breakdowns to a single
`ai_product` via the `product` query param; `by_product` always returns
the full cross-product breakdown. The endpoint is only registered on US
Cloud + dev/test envs; hobby / self-hosted deploys never see this URL;
@@ -660,22 +672,26 @@ class PersonalSpendViewSet(viewsets.ViewSet):
logger.exception("personal_spend.team_missing", team_id=team_id)
raise exceptions.NotFound("Internal analytics team is not provisioned on this deployment.")
- summary = _fetch_summary(team, email, from_dt, to_dt, product)
- by_tool = _fetch_by_tool(team, email, from_dt, to_dt, product, limit)
- scoped = summary["scoped_cost_usd"] or 0.0
- # `cost_usd` in by_tool can exceed scoped_cost_usd because multi-tool generations
- # contribute to every tool. `share_of_scoped` is independent per row, so agents can
- # present headline percentages directly without reconciling sums.
- for row in by_tool["items"]:
- row["share_of_scoped"] = (row["cost_usd"] / scoped) if scoped > 0 else 0.0
-
- payload = {
- "summary": summary,
- "by_product": _fetch_by_product(team, email, from_dt, to_dt, limit),
- "by_tool": by_tool,
- "by_model": _fetch_by_model(team, email, from_dt, to_dt, product, limit),
- "top_traces": _fetch_top_traces(team, email, from_dt, to_dt, product, limit),
- }
+ # Tag the underlying ClickHouse reads with the LLM_ANALYTICS product so they show up
+ # in the existing per-product Prometheus + cost-attribution dashboards alongside the
+ # rest of AI observability traffic. Wraps every call into `_fetch_*` -> HogQL.
+ with tags_context(product=Product.LLM_ANALYTICS):
+ summary = _fetch_summary(team, email, from_dt, to_dt, product)
+ by_tool = _fetch_by_tool(team, email, from_dt, to_dt, product, limit)
+ scoped = summary["scoped_cost_usd"] or 0.0
+ # `cost_usd` in by_tool can exceed scoped_cost_usd because multi-tool generations
+ # contribute to every tool. `share_of_scoped` is independent per row, so agents can
+ # present headline percentages directly without reconciling sums.
+ for row in by_tool["items"]:
+ row["share_of_scoped"] = (row["cost_usd"] / scoped) if scoped > 0 else 0.0
+
+ payload = {
+ "summary": summary,
+ "by_product": _fetch_by_product(team, email, from_dt, to_dt, limit),
+ "by_tool": by_tool,
+ "by_model": _fetch_by_model(team, email, from_dt, to_dt, product, limit),
+ "top_traces": _fetch_top_traces(team, email, from_dt, to_dt, product, limit),
+ }
response_data = PersonalSpendAnalysisResponseSerializer(payload).data
cache.set(cache_key, response_data, timeout=CACHE_TIMEOUT_SECONDS)
diff --git a/products/llm_analytics/backend/api/test/__snapshots__/test_personal_spend.ambr b/products/llm_analytics/backend/api/test/__snapshots__/test_personal_spend.ambr
index bc1cdb18c520..c1962a1090ff 100644
--- a/products/llm_analytics/backend/api/test/__snapshots__/test_personal_spend.ambr
+++ b/products/llm_analytics/backend/api/test/__snapshots__/test_personal_spend.ambr
@@ -143,7 +143,7 @@
GROUP BY person.id
HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))), 0))))) SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_generation', '$ai_embedding')), ifNull(equals(events__person.properties___email, '[email protected]'), 0), and(greaterOrEquals(events.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC'))))
- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_model'), ''), 'null'), '^"|"$', '')
+ GROUP BY model
ORDER BY cost_usd DESC
LIMIT 51 SETTINGS readonly=2,
max_execution_time=60,
@@ -183,7 +183,7 @@
GROUP BY person.id
HAVING and(ifNull(equals(argMax(person.is_deleted, person.version), 0), 0), ifNull(less(argMax(toTimeZone(person.created_at, 'UTC'), person.version), plus(now64(6, 'UTC'), toIntervalDay(1))), 0))))) SETTINGS optimize_aggregation_in_order=1) AS events__person ON equals(if(not(empty(events__override.distinct_id)), events__override.person_id, events.person_id), events__person.id)
WHERE and(equals(events.team_id, 99999), equals(events.event, '$ai_generation'), ifNull(equals(events__person.properties___email, '[email protected]'), 0), and(greaterOrEquals(events.timestamp, toDateTime64('explicit_redacted_timestamp', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC'))))
- GROUP BY replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_trace_id'), ''), 'null'), '^"|"$', '')
+ GROUP BY trace_id
ORDER BY cost_usd DESC
LIMIT 51 SETTINGS readonly=2,
max_execution_time=60,
diff --git a/products/llm_analytics/backend/api/test/test_personal_spend.py b/products/llm_analytics/backend/api/test/test_personal_spend.py
index 506d5ba6f131..d27577b3c925 100644
--- a/products/llm_analytics/backend/api/test/test_personal_spend.py
+++ b/products/llm_analytics/backend/api/test/test_personal_spend.py
@@ -310,7 +310,7 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
assert response.json()["summary"]["total_cost_usd"] == 1.0
def test_date_window_passes_through_to_query_layer(self) -> None:
- with patch("products.llm_analytics.backend.api.personal_spend.execute_with_ai_events_fallback") as mock_exec:
+ with patch("products.llm_analytics.backend.api.personal_spend.execute_hogql_query") as mock_exec:
mock_exec.return_value.results = []
response = self.client.get(f"{ENDPOINT}?date_from=-7d")
@@ -319,7 +319,7 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
assert mock_exec.call_count == 5
def test_second_call_serves_from_cache(self) -> None:
- with patch("products.llm_analytics.backend.api.personal_spend.execute_with_ai_events_fallback") as mock_exec:
+ with patch("products.llm_analytics.backend.api.personal_spend.execute_hogql_query") as mock_exec:
mock_exec.return_value.results = []
self.client.get(ENDPOINT)
first = mock_exec.call_count
@@ -327,7 +327,7 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
assert mock_exec.call_count == first
def test_refresh_bypasses_cache(self) -> None:
- with patch("products.llm_analytics.backend.api.personal_spend.execute_with_ai_events_fallback") as mock_exec:
+ with patch("products.llm_analytics.backend.api.personal_spend.execute_hogql_query") as mock_exec:
mock_exec.return_value.results = []
self.client.get(ENDPOINT)
first = mock_exec.call_count
@@ -335,7 +335,7 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
assert mock_exec.call_count == first * 2
def test_cache_key_includes_date_from(self) -> None:
- with patch("products.llm_analytics.backend.api.personal_spend.execute_with_ai_events_fallback") as mock_exec:
+ with patch("products.llm_analytics.backend.api.personal_spend.execute_hogql_query") as mock_exec:
mock_exec.return_value.results = []
self.client.get(f"{ENDPOINT}?date_from=-7d")
first = mock_exec.call_count
@@ -343,7 +343,7 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
assert mock_exec.call_count == first * 2
def test_cache_key_includes_product(self) -> None:
- with patch("products.llm_analytics.backend.api.personal_spend.execute_with_ai_events_fallback") as mock_exec:
+ with patch("products.llm_analytics.backend.api.personal_spend.execute_hogql_query") as mock_exec:
mock_exec.return_value.results = []
self.client.get(ENDPOINT)
first = mock_exec.call_count
@@ -351,7 +351,7 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
assert mock_exec.call_count == first * 2
def test_cache_key_includes_limit(self) -> None:
- with patch("products.llm_analytics.backend.api.personal_spend.execute_with_ai_events_fallback") as mock_exec:
+ with patch("products.llm_analytics.backend.api.personal_spend.execute_hogql_query") as mock_exec:
mock_exec.return_value.results = []
self.client.get(f"{ENDPOINT}?limit=10")
first = mock_exec.call_count