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.

feat(aio): distinguish billable personal ai spend (#125399)

Bernat Torresdb8f35a2bca7
diff --git a/products/ai_observability/backend/api/personal_spend.py b/products/ai_observability/backend/api/personal_spend.py
index f3a3813e5bd6..b2ed34c0f453 100644
--- a/products/ai_observability/backend/api/personal_spend.py
+++ b/products/ai_observability/backend/api/personal_spend.py
@@ -50,6 +50,7 @@ from posthog.clickhouse.query_tagging import Feature, Product, tags_context
from posthog.models import Team, User
from posthog.permissions import APIScopePermission
from posthog.rate_limit import PersonalSpendBurstThrottle, PersonalSpendDailyThrottle, PersonalSpendSustainedThrottle
+from posthog.tasks.usage_report import POSTHOG_AI_PRODUCTS, POSTHOG_CODE_AI_PRODUCTS
from posthog.utils import relative_date_parse
logger = structlog.get_logger(__name__)
@@ -58,6 +59,7 @@ MAX_PRODUCT_KEY_LENGTH = 64
MAX_DATE_STRING_LENGTH = 32
SUPPORTED_PRODUCTS = frozenset({"posthog_code"})
+BILLABLE_AI_PRODUCTS = frozenset(POSTHOG_AI_PRODUCTS + POSTHOG_CODE_AI_PRODUCTS)
DEFAULT_DATE_FROM = "-30d"
MAX_WINDOW_DAYS = 90
@@ -77,7 +79,7 @@ MAX_LIMIT = 200
DEFAULT_LIMIT = 50
CACHE_TIMEOUT_SECONDS = 300
-CACHE_VERSION = "v2"
+CACHE_VERSION = "v3"
UTC = ZoneInfo("UTC")
@@ -238,6 +240,9 @@ class _ProductBreakdownRowSerializer(serializers.Serializer):
help_text="Number of $ai_generation + $ai_embedding events for this product."
)
cost_usd = serializers.FloatField(help_text="Total cost in USD for this product over the lookback window.")
+ billable = serializers.BooleanField(
+ help_text="Whether this product is eligible for PostHog AI or Desktop credit billing, independent of event-level billing rules."
+ )
class _ToolBreakdownRowSerializer(serializers.Serializer):
@@ -381,6 +386,12 @@ class _SummarySerializer(serializers.Serializer):
total_cost_usd = serializers.FloatField(
help_text="Total LLM cost in USD across every `ai_product` for the user — independent of the `product` filter."
)
+ billable_cost_usd = serializers.FloatField(
+ help_text=(
+ "Raw LLM cost in USD for products eligible for AI or Desktop credit billing, independent of the "
+ "product filter and breakdown limit. Excludes credit markup and does not represent an invoiced amount."
+ )
+ )
event_count = serializers.IntegerField(
help_text="Total $ai_generation + $ai_embedding events captured across every product."
)
@@ -566,7 +577,8 @@ def _fetch_summary(
countIf({product_filter}) AS scoped_event_count,
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
+ round(sum(toFloat(properties.$ai_total_cost_usd)), 6) AS total_cost_usd,
+ round(sumIf(toFloat(properties.$ai_total_cost_usd), properties.ai_product IN {billable_products}), 6) AS billable_cost_usd
FROM events
WHERE {event_in} AND {email_filter} AND {timestamp_filter}
"""
@@ -575,6 +587,7 @@ def _fetch_summary(
query=query,
placeholders={
"product_filter": _product_filter(product),
+ "billable_products": ast.Tuple(exprs=[ast.Constant(value=value) for value in sorted(BILLABLE_AI_PRODUCTS)]),
"event_in": _event_in(["$ai_generation", "$ai_embedding"]),
"email_filter": _email_filter(email),
"timestamp_filter": _timestamp_filter(from_dt, to_dt),
@@ -582,7 +595,7 @@ def _fetch_summary(
team=team,
query_type="PersonalSpendSummary",
)
- row = (result.results or [(0, 0.0, 0, 0.0)])[0]
+ row = (result.results or [(0, 0.0, 0, 0.0, 0.0)])[0]
return {
"date_from": from_dt,
"date_to": to_dt,
@@ -591,6 +604,7 @@ def _fetch_summary(
"scoped_cost_usd": float(row[1] or 0.0),
"event_count": int(row[2] or 0),
"total_cost_usd": float(row[3] or 0.0),
+ "billable_cost_usd": float(row[4] or 0.0),
}
@@ -624,6 +638,7 @@ def _fetch_by_product(
rows = [
{
"product": row[0] if row[0] is not None else None,
+ "billable": row[0] in BILLABLE_AI_PRODUCTS,
"event_count": int(row[1] or 0),
"cost_usd": float(row[2] or 0.0),
}
diff --git a/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.ambr b/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.ambr
index 690a5e36c40d..601afc235127 100644
--- a/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.ambr
+++ b/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.ambr
@@ -5,7 +5,8 @@
SELECT countIf(ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'ai_product'), ''), 'null'), '^"|"$', ''), 'posthog_code'), 0)) AS scoped_event_count,
round(sumIf(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'ai_product'), ''), 'null'), '^"|"$', ''), 'posthog_code'), 0)), 6) AS scoped_cost_usd,
count() AS event_count,
- round(sum(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64')), 6) AS total_cost_usd
+ round(sum(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64')), 6) AS total_cost_usd,
+ round(sumIf(accurateCastOrNull(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, '$ai_total_cost_usd'), ''), 'null'), '^"|"$', ''), 'Float64'), in(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.properties, 'ai_product'), ''), 'null'), '^"|"$', ''), tuple('alert_investigation_agent', 'alert_llm_detector', 'hogql_decide', 'posthog_ai', 'posthog_code', 'product_analytics', 'replay_vision', 'slack_app', 'subscriptions', 'surveys', 'workflows'))), 6) AS billable_cost_usd
FROM events
WHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_generation', '$ai_embedding')), ifNull(equals(replaceRegexpAll(nullIf(nullIf(JSONExtractRaw(events.person_properties, 'email'), ''), 'null'), '^"|"$', ''), '[email protected]'), 0), and(greaterOrEquals(events.timestamp, toDateTime64('2025-12-16 12:00:00.000000', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC'))))
LIMIT 100 SETTINGS readonly=2,
diff --git a/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.new_events_schema.ambr b/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.new_events_schema.ambr
index 60f7e8ad546a..55335a4f8076 100644
--- a/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.new_events_schema.ambr
+++ b/products/ai_observability/backend/api/test/__snapshots__/test_personal_spend.new_events_schema.ambr
@@ -169,7 +169,8 @@
SELECT countIf(ifNull(equals(CAST(events.properties.ai_product, 'Nullable(String)'), 'posthog_code'), 0)) AS scoped_event_count,
round(sumIf(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), ifNull(equals(CAST(events.properties.ai_product, 'Nullable(String)'), 'posthog_code'), 0)), 6) AS scoped_cost_usd,
count() AS event_count,
- round(sum(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64')), 6) AS total_cost_usd
+ round(sum(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64')), 6) AS total_cost_usd,
+ round(sumIf(accurateCastOrNull(events.properties.`$ai_total_cost_usd`, 'Float64'), in(CAST(events.properties.ai_product, 'Nullable(String)'), tuple('alert_investigation_agent', 'alert_llm_detector', 'hogql_decide', 'posthog_ai', 'posthog_code', 'product_analytics', 'replay_vision', 'slack_app', 'subscriptions', 'surveys', 'workflows'))), 6) AS billable_cost_usd
FROM events_json AS events
WHERE and(equals(events.team_id, 99999), in(events.event, tuple('$ai_generation', '$ai_embedding')), ifNull(equals(CAST(events.person_properties.email, 'Nullable(String)'), '[email protected]'), 0), and(greaterOrEquals(events.timestamp, toDateTime64('2025-12-16 12:00:00.000000', 6, 'UTC')), less(events.timestamp, toDateTime64('today', 6, 'UTC'))))
LIMIT 100 SETTINGS readonly=2,
diff --git a/products/ai_observability/backend/api/test/test_personal_spend.py b/products/ai_observability/backend/api/test/test_personal_spend.py
index 8de549ff9ad8..423fe6024434 100644
--- a/products/ai_observability/backend/api/test/test_personal_spend.py
+++ b/products/ai_observability/backend/api/test/test_personal_spend.py
@@ -279,6 +279,7 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
assert response.status_code == status.HTTP_200_OK
body = response.json()
assert body["summary"]["total_cost_usd"] == 0
+ assert body["summary"]["billable_cost_usd"] == 0
assert body["summary"]["event_count"] == 0
assert body["summary"]["scoped_cost_usd"] == 0
assert body["summary"]["scoped_event_count"] == 0
@@ -293,6 +294,9 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
self._create_generation(ai_product="posthog_code", cost=2.0)
self._create_generation(ai_product="background_agents", cost=1.0)
self._create_generation(ai_product="posthog_code", cost=0.5, event_name="$ai_embedding")
+ self._create_generation(ai_product="posthog_ai", cost=4.0)
+ self._create_generation(ai_product="signals_scout", cost=8.0)
+ self._create_generation(cost=0.25, extra_props={"ai_product": None})
flush_persons_and_events()
response = self.client.get(ENDPOINT_OK)
@@ -300,8 +304,9 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
summary = body["summary"]
assert summary["product"] == "posthog_code"
# `event_count` / `total_cost_usd` stay cross-product even with the product filter.
- assert summary["event_count"] == 3
- assert summary["total_cost_usd"] == 3.5
+ assert summary["event_count"] == 6
+ assert summary["total_cost_usd"] == 15.75
+ assert summary["billable_cost_usd"] == 6.5
# `scoped_*` is filtered to posthog_code only.
assert summary["scoped_event_count"] == 2
assert summary["scoped_cost_usd"] == 2.5
@@ -312,6 +317,13 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
bg_row = _by_product(body["by_product"]["items"], "background_agents")
assert bg_row is not None
assert bg_row["cost_usd"] == 1.0
+ assert {row["product"]: row["billable"] for row in body["by_product"]["items"]} == {
+ "posthog_code": True,
+ "posthog_ai": True,
+ "background_agents": False,
+ "signals_scout": False,
+ None: False,
+ }
assert body["by_product"]["truncated"] is False
def test_product_filter_scopes_summary_and_breakdowns(self) -> None:
@@ -446,13 +458,15 @@ class TestPersonalSpendQueries(ClickhouseTestMixin, APIBaseTest):
# ai_products still show up.
self._create_generation(ai_product="a", cost=3.0)
self._create_generation(ai_product="b", cost=2.0)
- self._create_generation(ai_product="c", cost=1.0)
+ self._create_generation(ai_product="posthog_code", cost=1.0)
flush_persons_and_events()
response = self.client.get(f"{ENDPOINT}?{PRODUCT_QS}&limit=2")
by_product = response.json()["by_product"]
assert len(by_product["items"]) == 2
assert by_product["truncated"] is True
+ assert all(row["billable"] is False for row in by_product["items"])
+ assert response.json()["summary"]["billable_cost_usd"] == 1.0
def test_by_tool_share_of_scoped(self) -> None:
self._create_generation(tool="Bash", cost=2.0)
diff --git a/products/ai_observability/frontend/generated/api.schemas.ts b/products/ai_observability/frontend/generated/api.schemas.ts
index 1355c1e2368a..b76e649d8b1f 100644
--- a/products/ai_observability/frontend/generated/api.schemas.ts
+++ b/products/ai_observability/frontend/generated/api.schemas.ts
@@ -16,6 +16,8 @@ export interface _SummaryApi {
product: string
/** Total LLM cost in USD across every `ai_product` for the user — independent of the `product` filter. */
total_cost_usd: number
+ /** Raw LLM cost in USD for products eligible for AI or Desktop credit billing, independent of the product filter and breakdown limit. Excludes credit markup and does not represent an invoiced amount. */
+ billable_cost_usd: number
/** Total $ai_generation + $ai_embedding events captured across every product. */
event_count: number
/** Total cost in USD for the product filter. Matches the cost summed across `by_tool` / `by_model` for the scoped slice. */
@@ -34,6 +36,8 @@ export interface _ProductBreakdownRowApi {
event_count: number
/** Total cost in USD for this product over the lookback window. */
cost_usd: number
+ /** Whether this product is eligible for PostHog AI or Desktop credit billing, independent of event-level billing rules. */
+ billable: boolean
}
export interface _ProductBreakdownApi {
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 4f1b9224e85d..3648274c40cd 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -95136,6 +95136,8 @@ export namespace Schemas {
product: string;
/** Total LLM cost in USD across every `ai_product` for the user — independent of the `product` filter. */
total_cost_usd: number;
+ /** Raw LLM cost in USD for products eligible for AI or Desktop credit billing, independent of the product filter and breakdown limit. Excludes credit markup and does not represent an invoiced amount. */
+ billable_cost_usd: number;
/** Total $ai_generation + $ai_embedding events captured across every product. */
event_count: number;
/** Total cost in USD for the product filter. Matches the cost summed across `by_tool` / `by_model` for the scoped slice. */
@@ -95154,6 +95156,8 @@ export namespace Schemas {
event_count: number;
/** Total cost in USD for this product over the lookback window. */
cost_usd: number;
+ /** Whether this product is eligible for PostHog AI or Desktop credit billing, independent of event-level billing rules. */
+ billable: boolean;
}
export interface _ProductBreakdown {