: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(alerts): set ai_product on alert investigation agent LLM calls (#59099)
Andrew Maguire756a4696a16d
diff --git a/ee/hogai/llm.py b/ee/hogai/llm.pyindex 9c44516ebe91..e39d7cb4debd 100644--- a/ee/hogai/llm.py+++ b/ee/hogai/llm.py@@ -169,7 +169,7 @@ class MaxChatMixin(BaseModel):posthog_props = dict(self.posthog_properties or {})posthog_props["$ai_billable"] = self._get_effective_billable()posthog_props["team_id"] = self.team.id- posthog_props["ai_product"] = "posthog_ai"+ posthog_props.setdefault("ai_product", "posthog_ai")metadata["posthog_properties"] = posthog_propsnew_kwargs["metadata"] = metadatadiff --git a/ee/hogai/test/test_llm.py b/ee/hogai/test/test_llm.pyindex de111e3fa84d..72371f43a801 100644--- a/ee/hogai/test/test_llm.py+++ b/ee/hogai/test/test_llm.py@@ -294,6 +294,31 @@ class TestMaxChatOpenAI(BaseTest):self.assertEqual(call_kwargs["metadata"]["posthog_properties"]["$ai_billable"], True)self.assertEqual(call_kwargs["metadata"]["posthog_properties"]["team_id"], self.team.id)+ def test_ai_product_defaults_to_posthog_ai(self):+ llm = MaxChatOpenAI(user=self.user, team=self.team, use_responses_api=False)++ mock_result = LLMResult(generations=[[Generation(text="Response")]])+ with patch("langchain_openai.ChatOpenAI.generate", return_value=mock_result) as mock_generate:+ llm.generate([[HumanMessage(content="Test query")]])++ call_kwargs = mock_generate.call_args.kwargs+ self.assertEqual(call_kwargs["metadata"]["posthog_properties"]["ai_product"], "posthog_ai")++ def test_caller_supplied_ai_product_overrides_default(self):+ llm = MaxChatOpenAI(+ user=self.user,+ team=self.team,+ use_responses_api=False,+ posthog_properties={"ai_product": "alert_investigation_agent"},+ )++ mock_result = LLMResult(generations=[[Generation(text="Response")]])+ with patch("langchain_openai.ChatOpenAI.generate", return_value=mock_result) as mock_generate:+ llm.generate([[HumanMessage(content="Test query")]])++ call_kwargs = mock_generate.call_args.kwargs+ self.assertEqual(call_kwargs["metadata"]["posthog_properties"]["ai_product"], "alert_investigation_agent")+@parameterized.expand([# (model_billable, is_agent_billable, expected_effective_billable, should_increment_counter)diff --git a/posthog/temporal/ai/anomaly_investigation/runner.py b/posthog/temporal/ai/anomaly_investigation/runner.pyindex 6b5ae1eb57b8..b7eede57f439 100644--- a/posthog/temporal/ai/anomaly_investigation/runner.py+++ b/posthog/temporal/ai/anomaly_investigation/runner.py@@ -136,6 +136,7 @@ async def run_investigation(max_retries=2,temperature=0,default_request_timeout=LLM_REQUEST_TIMEOUT_SECONDS,+ posthog_properties={"ai_product": "alert_investigation_agent"},)llm_with_tools = llm.bind_tools([