: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(billing): count mcp analytics events in the usage report (#125407)
Georgis Andonis053ce28ce5fd
diff --git a/posthog/constants.py b/posthog/constants.pyindex 105419c3b044..4394fb39a1e3 100644--- a/posthog/constants.py+++ b/posthog/constants.py@@ -11,6 +11,8 @@ POSTHOG_INTERNAL_EMAIL_SUFFIX = "@posthog.com"# it as LLM analytics. nodejs `AI_EVENT_NAME_PREFIX` and Rust `AI_LANE_NAME_PREFIX` pin the same literal.AI_EVENT_NAME_PREFIX = "$ai_"+MCP_EVENT_NAME_PREFIX = "$mcp_"+# N.B. Keep this in sync with frontend enum (types.ts)# AND ensure it is added to the Billing Servicediff --git a/posthog/tasks/test/__snapshots__/test_usage_report.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.ambrindex 85bb1e3d3b92..18275af896e8 100644--- a/posthog/tasks/test/__snapshots__/test_usage_report.ambr+++ b/posthog/tasks/test/__snapshots__/test_usage_report.ambr@@ -70,6 +70,18 @@GROUP BY team_id'''# ---+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.102+ '''++ SELECT team_id,+ count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count+ FROM events+ WHERE timestamp >= '2022-01-10 00:00:00'+ AND timestamp < '2022-01-10 23:59:59'+ AND startsWith(event, '$mcp_')+ GROUP BY team_id+ '''+# ---# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests.11'''diff --git a/posthog/tasks/test/__snapshots__/test_usage_report.new_events_schema.ambr b/posthog/tasks/test/__snapshots__/test_usage_report.new_events_schema.ambrindex 271a3322f60a..3a188086e5db 100644--- a/posthog/tasks/test/__snapshots__/test_usage_report.new_events_schema.ambr+++ b/posthog/tasks/test/__snapshots__/test_usage_report.new_events_schema.ambr@@ -1845,6 +1845,18 @@GROUP BY team_id'''# ---+# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests[new_events_schema.65]+ '''++ SELECT team_id,+ count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count+ FROM events_json+ WHERE timestamp >= '2022-01-10 00:00:00'+ AND timestamp < '2022-01-10 23:59:59'+ AND startsWith(event, '$mcp_')+ GROUP BY team_id+ '''+# ---# name: TestFeatureFlagsUsageReport.test_usage_report_decide_requests[new_events_schema.6]'''diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.pyindex 0399697a20b8..0886ae8e3a6b 100644--- a/posthog/tasks/test/test_usage_report.py+++ b/posthog/tasks/test/test_usage_report.py@@ -76,6 +76,7 @@ from posthog.tasks.usage_report import (get_teams_with_event_count_with_groups_in_period,get_teams_with_logs_distinct_patterns_in_period,get_teams_with_logs_retention_byte_days_in_period,+ get_teams_with_mcp_event_count_in_period,get_teams_with_posthog_code_credits_used_in_period,get_teams_with_query_metric,get_teams_with_sdk_logs_records_in_period,@@ -790,6 +791,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM"rows_synced_in_period": 0,"exceptions_captured_in_period": 0,"ai_event_count_in_period": 7,+ "mcp_event_count_in_period": 0,"hog_function_calls_in_period": 0,"hog_function_fetch_calls_in_period": 0,"cdp_billable_invocations_in_period": 0,@@ -894,6 +896,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM"cdp_billable_invocations_in_period": 0,"rows_exported_in_period": 0,"ai_event_count_in_period": 7,+ "mcp_event_count_in_period": 0,},str(self.org_1_team_2.id): {"event_count_in_period": 11,@@ -988,6 +991,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM"cdp_billable_invocations_in_period": 0,"rows_exported_in_period": 0,"ai_event_count_in_period": 0,+ "mcp_event_count_in_period": 0,},},},@@ -1105,6 +1109,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM"cdp_billable_invocations_in_period": 0,"rows_exported_in_period": 0,"ai_event_count_in_period": 0,+ "mcp_event_count_in_period": 0,"date": "2022-01-09","organization_id": str(self.org_2.id),"organization_name": "Org 2",@@ -1207,6 +1212,7 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM"cdp_billable_invocations_in_period": 0,"rows_exported_in_period": 0,"ai_event_count_in_period": 0,+ "mcp_event_count_in_period": 0,}},},@@ -2467,6 +2473,7 @@ class TestHasNonZeroUsage(SimpleTestCase):[("empty", None),("events", "event_count_in_period"),+ ("mcp_events", "mcp_event_count_in_period"),("logs_bytes", "logs_bytes_in_period"),("logs_retention_mb_days", "logs_retention_mb_days_in_period"),("signals_credits", "signals_credits_used_in_period"),@@ -6411,10 +6418,40 @@ class TestQuerySplitting(ClickhouseDestroyTablesMixin, ClickhouseTestMixin, Testevent_metrics = get_all_event_metrics_in_period(self.begin, reporting_end)self.assertEqual(billable_result_after, [(self.team.id, baseline_count + 3)])+ self.assertEqual(+ get_teams_with_mcp_event_count_in_period(self.begin, reporting_end),+ [(self.team.id, 3)],+ )self.assertEqual(dict(event_metrics[metric_name]).get(self.team.id), 3)self.assertEqual(dict(event_metrics["mcp_tool_call_events"]).get(self.team.id), 2)self.assertIsNone(dict(event_metrics[parent_metric]).get(self.team.id))+ def test_mcp_event_count_deduplicates_copies_hours_apart_on_the_same_day(self) -> None:+ # make sure we don't collapse duplicate rows+ sync_execute("SYSTEM STOP MERGES")+ self.addCleanup(sync_execute, "SYSTEM START MERGES")++ tool_call_event_uuid = create_event(+ event_uuid=uuid4(),+ event="$mcp_tool_call",+ team=self.team,+ distinct_id="mcp_user",+ timestamp=self.begin + relativedelta(hours=1),+ )+ _create_event(+ event="$mcp_tool_call",+ team=self.team,+ distinct_id="mcp_user",+ event_uuid=tool_call_event_uuid,+ timestamp=self.begin + relativedelta(hours=5),+ )+ flush_persons_and_events()++ self.assertEqual(+ get_teams_with_mcp_event_count_in_period(self.begin, self.end),+ [(self.team.id, 1)],+ )+@parameterized.expand([("posthog-godot", "godot_events"),diff --git a/posthog/tasks/usage_report.py b/posthog/tasks/usage_report.pyindex 39108726b446..9cb0f18f1833 100644--- a/posthog/tasks/usage_report.py+++ b/posthog/tasks/usage_report.py@@ -30,7 +30,7 @@ from posthog.clickhouse.client import sync_executefrom posthog.clickhouse.client.connection import ClickHouseUser, Workloadfrom posthog.clickhouse.query_tagging import Feature, Product, tags_contextfrom posthog.cloud_utils import get_cached_instance_license-from posthog.constants import AI_EVENT_NAME_PREFIX, FlagRequestType+from posthog.constants import AI_EVENT_NAME_PREFIX, MCP_EVENT_NAME_PREFIX, FlagRequestTypefrom posthog.dataclasses import frozenfrom posthog.exceptions_capture import capture_exceptionfrom posthog.llm.billing import AI_COST_MARKUP_PERCENT@@ -301,6 +301,9 @@ class UsageReportCounters:ai_ingested_event_count_in_period: int = dataclasses.field(default=0, kw_only=True)ai_offline_eval_metric_count_in_period: int = dataclasses.field(default=0, kw_only=True)+ # MCP analytics+ mcp_event_count_in_period: int+# AI Billing Credits (PostHog AI feature usage)ai_credits_used_in_period: int@@ -861,6 +864,30 @@ def get_teams_with_event_count_with_groups_in_period()+@timed_log()+@retry(tries=QUERY_RETRIES, delay=QUERY_RETRY_DELAY, backoff=QUERY_RETRY_BACKOFF)+def get_teams_with_mcp_event_count_in_period(begin: datetime, end: datetime) -> TeamUsageRows:+ # Same table and dedup key as `get_teams_with_billable_event_count_in_period`, but split at midnight,+ # so two copies of one event a few hours apart still count once.+ # nosemgrep: clickhouse-fstring-param-audit - the events table is an internal fragment+ query_template = f"""+ SELECT team_id, count(distinct toDate(timestamp), event, cityHash64(distinct_id), cityHash64(uuid)) as count+ FROM {events_read_table(use_new_events_schema(None))}+ WHERE timestamp >= %(begin)s AND timestamp < %(end)s+ AND startsWith(event, %(mcp_event_prefix)s)+ GROUP BY team_id+ """++ with tags_context(product=Product.MCP_ANALYTICS, feature=Feature.USAGE_REPORT):+ return _execute_calendar_aligned_split_query(+ begin,+ end,+ query_template,+ {"mcp_event_prefix": MCP_EVENT_NAME_PREFIX},+ num_splits=12,+ )++@frozenclass _AISubSDKEventMetricCounts:counts_by_metric: dict[str, list[tuple[int, int]]]@@ -3015,6 +3042,7 @@ def has_non_zero_usage(report: UsageReportCounters) -> bool:or report.rows_exported_in_period > 0or report.exceptions_captured_in_period > 0or report.ai_event_count_in_period > 0+ or report.mcp_event_count_in_period > 0or report.ai_credits_used_in_period > 0or report.signals_credits_used_in_period > 0or report.posthog_code_credits_used_in_period > 0@@ -3346,6 +3374,7 @@ def _get_all_usage_data("teams_with_apm_tracing_spans_in_period": apm_tracing_usage["spans"],"teams_with_metrics_bytes_in_period": metrics_usage["bytes"],"teams_with_metrics_records_in_period": metrics_usage["records"],+ "teams_with_mcp_event_count_in_period": get_teams_with_mcp_event_count_in_period(period_start, period_end),}@@ -3583,6 +3612,7 @@ def _get_team_report(all_data: dict[str, Any], team: Team) -> UsageReportCounterrust_events_count_in_period=all_data["teams_with_rust_events_count_in_period"].get(team.id, 0),ai_event_count_in_period=all_data["teams_with_ai_event_count_in_period"].get(team.id, 0),ai_ingested_event_count_in_period=all_data["teams_with_ai_event_count_in_period"].get(team.id, 0),+ mcp_event_count_in_period=all_data["teams_with_mcp_event_count_in_period"].get(team.id, 0),ai_credits_used_in_period=all_data["teams_with_ai_credits_used_in_period"].get(team.id, 0),signals_credits_used_in_period=all_data["teams_with_signals_credits_used_in_period"].get(team.id, 0),posthog_code_credits_used_in_period=combine_posthog_code_credits(diff --git a/posthog/temporal/tests/usage_report/test_aggregator.py b/posthog/temporal/tests/usage_report/test_aggregator.pyindex bd3f262f9689..a136f06e87dc 100644--- a/posthog/temporal/tests/usage_report/test_aggregator.py+++ b/posthog/temporal/tests/usage_report/test_aggregator.py@@ -27,6 +27,7 @@ from posthog.tasks.usage_report import (from posthog.temporal.tests.usage_report.test_aggregate_activity import _instance_metadatafrom posthog.temporal.usage_report.aggregator import (add_pre_logs_distinct_patterns_patch_defaults,+ add_pre_mcp_event_count_patch_defaults,add_pre_sandbox_compute_patch_defaults,add_pre_transactional_emails_patch_defaults,build_manifest,@@ -173,6 +174,7 @@ def test_load_all_data_defaults_compute_for_pre_patch_workflow_history() -> None[(add_pre_logs_distinct_patterns_patch_defaults, "teams_with_logs_distinct_patterns_in_period"),(add_pre_transactional_emails_patch_defaults, "teams_with_workflow_transactional_emails_sent_in_period"),+ (add_pre_mcp_event_count_patch_defaults, "teams_with_mcp_event_count_in_period"),],)def test_load_all_data_defaults_single_query_for_pre_patch_workflow_history(diff --git a/posthog/temporal/tests/usage_report/test_parity.py b/posthog/temporal/tests/usage_report/test_parity.pyindex c9a854e030e6..ff82b9bf443f 100644--- a/posthog/temporal/tests/usage_report/test_parity.py+++ b/posthog/temporal/tests/usage_report/test_parity.py@@ -77,6 +77,7 @@ def _seed_all_data(team_a_id: int, team_b_id: int, team_c_id: int) -> dict[str,interesting: dict[str, dict[int, int]] = {"teams_with_event_count_in_period": {team_a_id: 100, team_b_id: 50, team_c_id: 25},"teams_with_enhanced_persons_event_count_in_period": {team_a_id: 80, team_b_id: 40},+ "teams_with_mcp_event_count_in_period": {team_a_id: 30, team_c_id: 5},"teams_with_recording_count_in_period": {team_a_id: 8, team_c_id: 3},"teams_with_recording_bytes_in_period": {team_a_id: 1_234_567, team_c_id: 89_012},"teams_with_decide_requests_count_in_period": {team_a_id: 1_000, team_b_id: 200, team_c_id: 50},@@ -444,6 +445,7 @@ def test_end_to_end_parity_celery_task_vs_temporal_activity(# only has one. If this drifts, the rest of the parity numbers are# suspect.assert temporal_per_org[str(org_a.id)]["event_count_in_period"] == 100 + 50+ assert temporal_per_org[str(org_a.id)]["mcp_event_count_in_period"] == 30assert temporal_per_org[str(org_b.id)]["event_count_in_period"] == 25assert temporal_per_org[str(org_a.id)]["team_count"] == 2assert temporal_per_org[str(org_b.id)]["team_count"] == 1diff --git a/posthog/temporal/tests/usage_report/test_workflow.py b/posthog/temporal/tests/usage_report/test_workflow.pyindex 880271f254dc..8e6133e60b9b 100644--- a/posthog/temporal/tests/usage_report/test_workflow.py+++ b/posthog/temporal/tests/usage_report/test_workflow.py@@ -48,6 +48,7 @@ from posthog.temporal.usage_report.types import (from posthog.temporal.usage_report.workflow import (AGGREGATION_QUEUE_PATCH_ID,LOGS_DISTINCT_PATTERNS_QUERY_NAME,+ MCP_EVENT_COUNT_QUERY_NAME,PARALLEL_COUNTER_QUERIES_PATCH_ID,QUERY_CONCURRENCY,SANDBOX_COMPUTE_QUERY_NAME,@@ -55,6 +56,7 @@ from posthog.temporal.usage_report.workflow import (USAGE_COUNTER_SERVICE_PATCH_ID,RunUsageReportsWorkflow,_queries_for_logs_distinct_patterns_patch,+ _queries_for_mcp_event_count_patch,_queries_for_sandbox_compute_patch,_queries_for_transactional_emails_patch,build_context,@@ -77,6 +79,7 @@ def test_sandbox_compute_query_is_versioned_for_existing_histories() -> None:[("logs_distinct_patterns", _queries_for_logs_distinct_patterns_patch, LOGS_DISTINCT_PATTERNS_QUERY_NAME),("transactional_emails", _queries_for_transactional_emails_patch, TRANSACTIONAL_EMAILS_QUERY_NAME),+ ("mcp_event_count", _queries_for_mcp_event_count_patch, MCP_EVENT_COUNT_QUERY_NAME),])def test_single_query_is_versioned_for_existing_histories(diff --git a/posthog/temporal/usage_report/activities.py b/posthog/temporal/usage_report/activities.pyindex df675eda109e..4854c39dc429 100644--- a/posthog/temporal/usage_report/activities.py+++ b/posthog/temporal/usage_report/activities.py@@ -27,6 +27,7 @@ from posthog.temporal.common.heartbeat import Heartbeaterfrom posthog.temporal.common.metrics import ExecutionTimeRecorderfrom posthog.temporal.usage_report.aggregator import (add_pre_logs_distinct_patterns_patch_defaults,+ add_pre_mcp_event_count_patch_defaults,add_pre_sandbox_compute_patch_defaults,add_pre_transactional_emails_patch_defaults,get_org_user_counts,@@ -251,6 +252,7 @@ async def aggregate_and_chunk_org_reports(inputs: AggregateInputs) -> AggregateRadd_pre_sandbox_compute_patch_defaults(all_data, inputs.query_results)add_pre_logs_distinct_patterns_patch_defaults(all_data, inputs.query_results)add_pre_transactional_emails_patch_defaults(all_data, inputs.query_results)+ add_pre_mcp_event_count_patch_defaults(all_data, inputs.query_results)check_cancelled()counter_report = load_usage_counter_report(inputs)all_data.update({field: dict(rows) for field, rows in counter_report.counts.items()})diff --git a/posthog/temporal/usage_report/aggregator.py b/posthog/temporal/usage_report/aggregator.pyindex 5cc4f4551e3c..3f934528f1f4 100644--- a/posthog/temporal/usage_report/aggregator.py+++ b/posthog/temporal/usage_report/aggregator.py@@ -60,6 +60,7 @@ _SANDBOX_COMPUTE_DESTINATION_KEYS = ()_LOGS_DISTINCT_PATTERNS_QUERY_NAME = "teams_with_logs_distinct_patterns_in_period"_TRANSACTIONAL_EMAILS_QUERY_NAME = "teams_with_workflow_transactional_emails_sent_in_period"+_MCP_EVENT_COUNT_QUERY_NAME = "teams_with_mcp_event_count_in_period"def load_usage_counter_report(inputs: AggregateInputs) -> UsageCounterReport:@@ -113,6 +114,13 @@ def add_pre_transactional_emails_patch_defaults(all_data[_TRANSACTIONAL_EMAILS_QUERY_NAME] = {}+def add_pre_mcp_event_count_patch_defaults(+ all_data: dict[str, dict[int, int]], query_results: list[RunQueryToS3Result]+) -> None:+ if not any(result.query_name == _MCP_EVENT_COUNT_QUERY_NAME for result in query_results):+ all_data[_MCP_EVENT_COUNT_QUERY_NAME] = {}++def iter_chunk_lines(org_reports: Iterable[OrgReport],instance_metadata: InstanceMetadata,diff --git a/posthog/temporal/usage_report/queries.py b/posthog/temporal/usage_report/queries.pyindex d925c2b71c63..ec23f4428699 100644--- a/posthog/temporal/usage_report/queries.py+++ b/posthog/temporal/usage_report/queries.py@@ -73,6 +73,7 @@ from posthog.tasks.usage_report import (get_teams_with_logs_records_in_period,get_teams_with_logs_retention_byte_days_in_period,get_teams_with_logs_retention_bytes_in_period,+ get_teams_with_mcp_event_count_in_period,get_teams_with_metrics_usage_in_period,get_teams_with_mobile_billable_recording_count_in_period,get_teams_with_posthog_code_credits_used_in_period,@@ -276,6 +277,11 @@ QUERIES: list[QuerySpec] = [fn=lambda b, e: get_teams_with_event_count_with_groups_in_period(b, e, count_distinct=True),timeout_minutes=30,),+ QuerySpec(+ name="teams_with_mcp_event_count_in_period",+ fn=get_teams_with_mcp_event_count_in_period,+ timeout_minutes=30,+ ),QuerySpec(name="all_event_metrics",fn=get_all_event_metrics_in_period,diff --git a/posthog/temporal/usage_report/workflow.py b/posthog/temporal/usage_report/workflow.pyindex baf1a5ce400c..200c26a0efc0 100644--- a/posthog/temporal/usage_report/workflow.py+++ b/posthog/temporal/usage_report/workflow.py@@ -60,6 +60,8 @@ LOGS_DISTINCT_PATTERNS_QUERY_PATCH_ID = "usage-report-logs-distinct-patterns-queLOGS_DISTINCT_PATTERNS_QUERY_NAME = "teams_with_logs_distinct_patterns_in_period"TRANSACTIONAL_EMAILS_QUERY_PATCH_ID = "usage-report-transactional-emails-query-2026-10"TRANSACTIONAL_EMAILS_QUERY_NAME = "teams_with_workflow_transactional_emails_sent_in_period"+MCP_EVENT_COUNT_QUERY_PATCH_ID = "usage-report-mcp-event-count-query-2026-10"+MCP_EVENT_COUNT_QUERY_NAME = "teams_with_mcp_event_count_in_period"def _queries_for_sandbox_compute_patch(patch_applied: bool) -> list[QuerySpec]:@@ -80,6 +82,12 @@ def _queries_for_transactional_emails_patch(queries: list[QuerySpec], patch_applreturn [spec for spec in queries if spec.name != TRANSACTIONAL_EMAILS_QUERY_NAME]+def _queries_for_mcp_event_count_patch(queries: list[QuerySpec], patch_applied: bool) -> list[QuerySpec]:+ if patch_applied:+ return queries+ return [spec for spec in queries if spec.name != MCP_EVENT_COUNT_QUERY_NAME]++def build_context(inputs: RunUsageReportsInputs, run_id: str, now: datetime) -> WorkflowContext:"""Compute the period and S3 layout context. Pure function so it's safeto call from the workflow body (no real-time access).@@ -146,6 +154,7 @@ class RunUsageReportsWorkflow(PostHogWorkflow):queries = _queries_for_transactional_emails_patch(queries, workflow.patched(TRANSACTIONAL_EMAILS_QUERY_PATCH_ID))+ queries = _queries_for_mcp_event_count_patch(queries, workflow.patched(MCP_EVENT_COUNT_QUERY_PATCH_ID))if workflow.patched(USAGE_COUNTER_SERVICE_PATCH_ID):ctx.usage_counter_plan = await workflow.execute_activity(plan_usage_counters,