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(slack_app): tag each classifier generation with its classifier (#104273)

Vojta Bartošf2721c7f3fb5
diff --git a/posthog/temporal/ai/slack_app/activities/classifiers.py b/posthog/temporal/ai/slack_app/activities/classifiers.py
index 233be2a0d1b9..d3c71f7e4945 100644
--- a/posthog/temporal/ai/slack_app/activities/classifiers.py
+++ b/posthog/temporal/ai/slack_app/activities/classifiers.py
@@ -39,6 +39,11 @@ prompts = PromptTemplates(Path(__file__).parent / "prompts")
CLASSIFIER_THREAD_HISTORY_MESSAGES = 10
CLASSIFIER_MODEL = "claude-haiku-4-5-20251001"
+# Every classifier here shares `ai_product="slack_app_routing"`, so the captured generation
+# carries this property to say which one of them made the call. An online evaluation scopes
+# itself with it; without it a judge would grade all four.
+CLASSIFIER_PROPERTY = "slack_app_classifier"
+
# The model-override and agent-directed classifiers both run on a reasoning model, which
# draws its reasoning from the same token budget as the reply. The reply is one short JSON
@@ -191,7 +196,11 @@ def classify_task_needs_repo(
)
try:
# The Go gateway refuses a Claude model on chat completions.
- client = build_anthropic_client(product="slack_app_routing", ai_product="slack_app_routing")
+ client = build_anthropic_client(
+ product="slack_app_routing",
+ ai_product="slack_app_routing",
+ properties={CLASSIFIER_PROPERTY: "task_needs_repo"},
+ )
response = client.messages.create(
model=CLASSIFIER_MODEL,
messages=[{"role": "user", "content": prompt}],
@@ -304,9 +313,11 @@ def classify_message_is_agent_directed(
event_text=event_text,
)
try:
- client = build_openai_client(product="slack_app_routing", ai_product="slack_app_routing").with_options(
- timeout=AGENT_DIRECTED_TIMEOUT_SECONDS, max_retries=AGENT_DIRECTED_MAX_RETRIES
- )
+ client = build_openai_client(
+ product="slack_app_routing",
+ ai_product="slack_app_routing",
+ properties={CLASSIFIER_PROPERTY: "agent_directed"},
+ ).with_options(timeout=AGENT_DIRECTED_TIMEOUT_SECONDS, max_retries=AGENT_DIRECTED_MAX_RETRIES)
response = client.chat.completions.create(
model=AGENT_DIRECTED_CLASSIFIER_MODEL,
messages=[{"role": "user", "content": prompt}],
@@ -472,9 +483,11 @@ def classify_slack_app_model_override(
)
try:
- client = build_openai_client(product="slack_app_routing", ai_product="slack_app_routing").with_options(
- timeout=MODEL_OVERRIDE_TIMEOUT_SECONDS, max_retries=MODEL_OVERRIDE_MAX_RETRIES
- )
+ client = build_openai_client(
+ product="slack_app_routing",
+ ai_product="slack_app_routing",
+ properties={CLASSIFIER_PROPERTY: "model_override"},
+ ).with_options(timeout=MODEL_OVERRIDE_TIMEOUT_SECONDS, max_retries=MODEL_OVERRIDE_MAX_RETRIES)
response = client.chat.completions.create(
model=MODEL_OVERRIDE_CLASSIFIER_MODEL,
messages=[{"role": "user", "content": prompt}],
@@ -617,9 +630,11 @@ def classify_slack_app_project_route(
)
try:
- client = build_openai_client(product="slack_app_routing", ai_product="slack_app_routing").with_options(
- timeout=PROJECT_ROUTE_TIMEOUT_SECONDS, max_retries=PROJECT_ROUTE_MAX_RETRIES
- )
+ client = build_openai_client(
+ product="slack_app_routing",
+ ai_product="slack_app_routing",
+ properties={CLASSIFIER_PROPERTY: "project_route"},
+ ).with_options(timeout=PROJECT_ROUTE_TIMEOUT_SECONDS, max_retries=PROJECT_ROUTE_MAX_RETRIES)
response = client.chat.completions.create(
model=PROJECT_ROUTE_CLASSIFIER_MODEL,
messages=[{"role": "user", "content": prompt}],
diff --git a/posthog/temporal/tests/ai/slack_app/activities/test_classifier_gateway_routing.py b/posthog/temporal/tests/ai/slack_app/activities/test_classifier_gateway_routing.py
index 993833a75fa8..7d3b05e69d43 100644
--- a/posthog/temporal/tests/ai/slack_app/activities/test_classifier_gateway_routing.py
+++ b/posthog/temporal/tests/ai/slack_app/activities/test_classifier_gateway_routing.py
@@ -6,9 +6,14 @@ from django.test import override_settings
from parameterized import parameterized
+from posthog.models.integration import Integration
+from posthog.models.organization import Organization
+from posthog.models.team.team import Team
from posthog.temporal.ai.slack_app.activities.classifiers import (
+ CLASSIFIER_PROPERTY,
classify_message_is_agent_directed,
classify_slack_app_model_override,
+ classify_slack_app_project_route,
classify_task_needs_repo,
)
@@ -31,6 +36,10 @@ PYTHON_GATEWAY = {**GO_GATEWAY, "AI_GATEWAY_URL": "", "AI_GATEWAY_API_KEY": ""}
CHOICES = (ModelChoice("claude", "claude-fable-5", "Claude Fable 5", ("low", "medium", "high")),)
NEEDS_REPO_TEXT = "ambiguous ask the heuristic does not catch"
+# Unsaved rows, which is all the project-route classifier reads.
+PROJECTS = [
+ Integration(id=410, kind="slack", team=Team(id=41, name="Staging", organization=Organization(name="Northwind")))
+]
def _classify_needs_repo():
@@ -45,9 +54,14 @@ def _classify_model_override():
return classify_slack_app_model_override("use fable for this", CHOICES)
+def _classify_project_route():
+ return classify_slack_app_project_route("check the error rate on staging", PROJECTS)
+
+
OPENAI_CLASSIFIERS = [
("agent_directed", _classify_agent_directed, '{"agent_directed": true}'),
("model_override", _classify_model_override, '{"model": "claude-fable-5", "reasoning_effort": null}'),
+ ("project_route", _classify_project_route, '{"project_id": 41}'),
]
@@ -63,10 +77,14 @@ def _chat_reply(mock_openai: MagicMock, text: str) -> None:
client.chat.completions.create.return_value.choices = [MagicMock(message=MagicMock(content=text))]
-def _assert_routing_product(headers: dict[str, str]) -> None:
+def _assert_routing_product(headers: dict[str, str], classifier: str) -> None:
# `slack_app` bills the customer.
assert headers["X-PostHog-Product"] == "slack_app_routing"
- assert json.loads(headers["X-PostHog-Properties"])["ai_product"] == "slack_app_routing"
+ properties = json.loads(headers["X-PostHog-Properties"])
+ assert properties["ai_product"] == "slack_app_routing"
+ # All four classifiers share that product, so an online evaluation can only reach one
+ # of them through this label. A call that loses it is graded as the wrong classifier.
+ assert properties[CLASSIFIER_PROPERTY] == classifier
class TestClassifierGatewayRouting:
@@ -80,7 +98,7 @@ class TestClassifierGatewayRouting:
kwargs = mock_anthropic.call_args.kwargs
assert kwargs["base_url"] == "https://ai-gateway.example"
assert kwargs["api_key"] == AI_GATEWAY_KEY
- _assert_routing_product(kwargs["default_headers"])
+ _assert_routing_product(kwargs["default_headers"], "task_needs_repo")
@patch("posthog.llm.gateway_client.httpx.Client")
@patch("posthog.llm.gateway_client.Anthropic")
@@ -96,7 +114,7 @@ class TestClassifierGatewayRouting:
@parameterized.expand(OPENAI_CLASSIFIERS)
@patch("posthog.llm.gateway_client.httpx.Client")
@patch("posthog.llm.gateway_client.OpenAI")
- def test_openai_classifier_uses_go_route_when_configured(self, _name, classify, reply, mock_openai, _mock_httpx):
+ def test_openai_classifier_uses_go_route_when_configured(self, name, classify, reply, mock_openai, _mock_httpx):
_chat_reply(mock_openai, reply)
with override_settings(**GO_GATEWAY):
assert classify()
@@ -104,7 +122,7 @@ class TestClassifierGatewayRouting:
kwargs = mock_openai.call_args.kwargs
assert kwargs["base_url"] == AI_GATEWAY_URL
assert kwargs["api_key"] == AI_GATEWAY_KEY
- _assert_routing_product(kwargs["default_headers"])
+ _assert_routing_product(kwargs["default_headers"], name)
@parameterized.expand(OPENAI_CLASSIFIERS)
@patch("posthog.llm.gateway_client.httpx.Client")
diff --git a/posthog/temporal/tests/ai/test_classify_task_needs_repo.py b/posthog/temporal/tests/ai/test_classify_task_needs_repo.py
index 8d1a276f0931..71de33a5787b 100644
--- a/posthog/temporal/tests/ai/test_classify_task_needs_repo.py
+++ b/posthog/temporal/tests/ai/test_classify_task_needs_repo.py
@@ -7,6 +7,7 @@ from posthog.models.integration import Integration
from posthog.models.repo_routing_rule import RepoRoutingRule
from posthog.temporal.ai.slack_app.activities.classifiers import (
CLASSIFIER_MODEL,
+ CLASSIFIER_PROPERTY,
classify_posthog_code_task_needs_repo_activity,
classify_task_needs_repo,
team_routing_rule_lines,
@@ -195,7 +196,11 @@ class TestClassifyTaskNeedsRepo:
) as build_client:
assert classify_task_needs_repo(text, [SlackThreadMessage(user="Alessandro", text=text)]) is True
- build_client.assert_called_once_with(product="slack_app_routing", ai_product="slack_app_routing")
+ build_client.assert_called_once_with(
+ product="slack_app_routing",
+ ai_product="slack_app_routing",
+ properties={CLASSIFIER_PROPERTY: "task_needs_repo"},
+ )
kwargs = fake_client.messages.create.call_args.kwargs
assert kwargs["model"] == CLASSIFIER_MODEL
assert kwargs["max_tokens"] == 64