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(feature-flags): attribute PostHog AI's flag writes to PostHog AI (#98632)

Patricioc4d9328beec1
diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.py
index 57a6c24ed873..4605260e8dd9 100644
--- a/products/feature_flags/backend/api/feature_flag.py
+++ b/products/feature_flags/backend/api/feature_flag.py
@@ -183,6 +183,10 @@ def _flag_write_source(request: Any) -> str:
# below would read it as an unidentified caller.
if getattr(request, "is_system", False):
return "internal"
+ # PostHog AI builds its own request shim with no authenticator, so without this it lands
+ # in the same bucket as a caller we failed to identify.
+ if getattr(request, "is_posthog_ai", False):
+ return "posthog_ai"
headers = getattr(request, "headers", None) or {}
if headers.get("x-posthog-mcp-user-agent") or "posthog-mcp" in (headers.get("User-Agent") or ""):
return "mcp"
diff --git a/products/feature_flags/backend/max_tools.py b/products/feature_flags/backend/max_tools.py
index 9e906c1de620..d5b08bde0bf6 100644
--- a/products/feature_flags/backend/max_tools.py
+++ b/products/feature_flags/backend/max_tools.py
@@ -282,6 +282,7 @@ class CreateFeatureFlagTool(MaxTool):
user=self._user,
method="POST",
successful_authenticator=None,
+ is_posthog_ai=True,
session={},
data=serializer_data,
META={},
diff --git a/products/feature_flags/backend/test/test_max_tools.py b/products/feature_flags/backend/test/test_max_tools.py
index 6c56821837e5..200b9b924cd7 100644
--- a/products/feature_flags/backend/test/test_max_tools.py
+++ b/products/feature_flags/backend/test/test_max_tools.py
@@ -8,6 +8,7 @@ from posthog.schema import FeatureFlagGroupType, GroupPropertyFilter, PersonProp
from posthog.sync import database_sync_to_async
from posthog.test.persons import create_group_type_mapping
+from products.feature_flags.backend.api.feature_flag import FLAG_FILTERS_WRITE_COUNTER
from products.feature_flags.backend.max_tools import (
CreateFeatureFlagTool,
FeatureFlagCreationSchema,
@@ -36,6 +37,16 @@ class TestCreateFeatureFlagTool(APIBaseTest):
state=AssistantState(messages=[]),
)
+ async def test_flag_writes_are_attributed_to_posthog_ai(self):
+ counted = FLAG_FILTERS_WRITE_COUNTER.labels(operation="create", outcome="accepted", source="posthog_ai")
+ before = counted._value.get()
+
+ await self._create_tool()._arun_impl(
+ feature_flag=FeatureFlagCreationSchema(key="attributed-flag", name="Attributed", groups=[ALL_USERS_GROUP])
+ )
+
+ assert counted._value.get() == before + 1
+
async def test_create_flag_minimal(self):
tool = self._create_tool()