: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(posthog-ai): inspect the complete metric catalog (#92902)
Thiago Salvatore823ab28adee8
diff --git a/products/posthog_ai/backend/services/system_prompt/prompt.py b/products/posthog_ai/backend/services/system_prompt/prompt.pyindex 93524bd87d53..c4c8503b0826 100644--- a/products/posthog_ai/backend/services/system_prompt/prompt.py+++ b/products/posthog_ai/backend/services/system_prompt/prompt.py@@ -6,6 +6,19 @@ capabilities; this layers PostHog AI's product-engineering identity and PostHog-knowledge on top, so it is written to sit after that base prompt rather than to stand alone."""++def governed_metrics_catalog_prompt() -> str:+ """Return instructions for using the complete governed-metric catalog."""+ return """# Governed metrics catalog++For any named business or operational measure, call `metric-list` before making a data-bearing call. It lists the complete governed catalog and outranks typed domain tools, `query-*` tools, product skills, and raw SQL. Use `metric-describe` to inspect any candidate's full definition, including its stored HogQL or SQL, before adapting it. When an approved, non-drifted metric exactly answers the request, run it with `data-catalog-metric-run` rather than re-deriving the number.++- For a request that needs a drill-down, run the canonical metric for the headline first. You may then provide a label-level breakdown, but describe that breakdown as noncanonical.+- When materially different catalog matches could answer the request, ask one clarifying question and end your turn. Do not acknowledge ambiguity and then run one of the alternatives anyway.+- If no metric matches, say that you consulted the catalog and label any derived result noncanonical.+"""++POSTHOG_AI_SYSTEM_PROMPT = """# PostHog AIYou are operating as PostHog AI – PostHog's product-engineering agent. The harness identity and capabilities above remain fully in force: you work in a sandbox, read and edit the customer's code, run commands, and use every tool exactly as Claude Code does. This section adds one defining trait on top of that: you make product-engineering decisions from evidence, not assumptions.diff --git a/products/posthog_ai/backend/services/system_prompt/service.py b/products/posthog_ai/backend/services/system_prompt/service.pyindex 5b39d2601456..ff319b532b2d 100644--- a/products/posthog_ai/backend/services/system_prompt/service.py+++ b/products/posthog_ai/backend/services/system_prompt/service.py@@ -9,7 +9,8 @@ bare string would replace the preset entirely. Project context, groups, billing,reachable via the PostHog MCP server, so they are not duplicated here; per-turn context is deliveredseparately via the ``<posthog_context>`` wrapper.-The service is pure over its inputs — no side effects.+The metric catalog is inspected through its MCP tools at the moment it is needed, so this service+does not embed a partial catalog snapshot in a Run's system prompt."""from typing import Literal@@ -17,7 +18,10 @@ from typing import Literalfrom typing_extensions import TypedDictfrom products.posthog_ai.backend.helpers import BaseSandboxService-from products.posthog_ai.backend.services.system_prompt.prompt import POSTHOG_AI_SYSTEM_PROMPT+from products.posthog_ai.backend.services.system_prompt.prompt import (+ POSTHOG_AI_SYSTEM_PROMPT,+ governed_metrics_catalog_prompt,+)class ClaudeCodeSystemPrompt(TypedDict):@@ -33,7 +37,7 @@ class ClaudeCodeSystemPrompt(TypedDict):class PromptService(BaseSandboxService):- """Provide the systemPrompt suffix for a PostHog AI sandbox Run. Stateless over its inputs."""+ """Provide the systemPrompt suffix for a PostHog AI sandbox Run."""def build(self) -> ClaudeCodeSystemPrompt:"""Return the PostHog AI systemPrompt as a Claude Code preset-plus-append suffix.@@ -42,4 +46,5 @@ class PromptService(BaseSandboxService):``clientConnection.newSession({ _meta: { systemPrompt } })``; the sandbox appends ``append``after Claude Code's own system prompt rather than replacing it."""- return {"type": "preset", "preset": "claude_code", "append": POSTHOG_AI_SYSTEM_PROMPT}+ prompt = POSTHOG_AI_SYSTEM_PROMPT + governed_metrics_catalog_prompt()+ return {"type": "preset", "preset": "claude_code", "append": prompt}diff --git a/products/posthog_ai/backend/tests/test_system_prompt.py b/products/posthog_ai/backend/tests/test_system_prompt.pyindex 9af05dde9f6f..35618ef33ab4 100644--- a/products/posthog_ai/backend/tests/test_system_prompt.py+++ b/products/posthog_ai/backend/tests/test_system_prompt.py@@ -11,11 +11,19 @@ class TestPostHogAISystemPrompt(APIBaseTest):def test_build_returns_preset_append_suffix(self):# The object form makes the agent-server append to Claude Code's prompt (a suffix), rather# than the bare-string form, which would replace it.- assert self._build() == {- "type": "preset",- "preset": "claude_code",- "append": POSTHOG_AI_SYSTEM_PROMPT,- }+ prompt = self._build()+ assert prompt["type"] == "preset"+ assert prompt["preset"] == "claude_code"+ assert prompt["append"].startswith(POSTHOG_AI_SYSTEM_PROMPT)++ def test_instructs_the_agent_to_inspect_the_complete_metric_catalog(self) -> None:+ prompt = self._build()["append"]++ assert "# Governed metrics catalog" in prompt+ assert "`metric-list`" in prompt+ assert "`metric-describe`" in prompt+ assert "`data-catalog-metric-run`" in prompt+ assert "complete governed catalog" in promptdef test_includes_core_sections(self):prompt = self._build()["append"]