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(tasks): route task titles only through the go ai gateway (#128201)

Jonathan Mieloo734a9b4d11da
diff --git a/products/tasks/backend/constants.py b/products/tasks/backend/constants.py
index 5dcfec9d4078..0a3c5dc7e6ff 100644
--- a/products/tasks/backend/constants.py
+++ b/products/tasks/backend/constants.py
@@ -45,7 +45,6 @@ PI_CLOUD_RUNTIME_FEATURE_FLAG = "pi-harness"
REASONING_EFFORTS = model_catalog.REASONING_EFFORTS
PI_ACP_FEATURE_FLAG = "pi-acp"
TASK_ANALYSIS_FEATURE_FLAG = "posthog-code-task-analysis"
-TASK_TITLE_AI_GATEWAY_FEATURE_FLAG = "tasks-title-ai-gateway"
ANALYSIS_TARGET_TASK_ID_STATE_KEY = "analysis_target_task_id"
ANALYSIS_TARGET_RUN_ID_STATE_KEY = "analysis_target_run_id"
diff --git a/products/tasks/backend/facade/api.py b/products/tasks/backend/facade/api.py
index 2f7558ae03ac..918cfa180a77 100644
--- a/products/tasks/backend/facade/api.py
+++ b/products/tasks/backend/facade/api.py
@@ -8010,8 +8010,6 @@ def create_task(
if user_id is not None:
validated_data["created_by"] = User.objects.get(id=user_id)
- created_by = validated_data.get("created_by")
- title_distinct_id = created_by.distinct_id if created_by else None
if validated_data.get("repository") and not validated_data.get("github_integration"):
default_integration = IntegrationManager.github.oauth.find(team.id)
@@ -8131,7 +8129,7 @@ def create_task(
warm_task.title_manually_set = True
update_fields += ["title", "title_manually_set"]
elif description and not (warm_task.title or "").strip():
- warm_task.title = generate_task_title(naming_source or description, distinct_id=title_distinct_id)
+ warm_task.title = generate_task_title(naming_source or description)
warm_task.title_manually_set = False
update_fields += ["title", "title_manually_set"]
if description and not (warm_task.description or "").strip():
@@ -8265,9 +8263,7 @@ def create_task(
title = (validated_data.get("title") or "").strip()
if not title and (naming_source or validated_data.get("description")):
- validated_data["title"] = generate_task_title(
- naming_source or validated_data["description"], distinct_id=title_distinct_id
- )
+ validated_data["title"] = generate_task_title(naming_source or validated_data["description"])
validated_data.setdefault("title_manually_set", False)
elif title:
validated_data.setdefault("title_manually_set", True)
diff --git a/products/tasks/backend/logic/services/test_title_generator.py b/products/tasks/backend/logic/services/test_title_generator.py
index 3c2b3b81e4e9..b163e10c17d2 100644
--- a/products/tasks/backend/logic/services/test_title_generator.py
+++ b/products/tasks/backend/logic/services/test_title_generator.py
@@ -1,195 +1,71 @@
from types import SimpleNamespace
import pytest
-from unittest.mock import MagicMock, patch
+from unittest.mock import patch
-from products.ai_observability.backend.llm.types import StreamChunk
from products.tasks.backend.logic.services.title_generator import _fallback_title, generate_task_title
+BUILDER = "posthog.llm.gateway_client.build_ai_gateway_anthropic_client"
-class TestTitleGenerator:
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_success(self, mock_client_cls):
- mock_client = MagicMock()
- mock_client_cls.return_value = mock_client
-
- mock_client.stream.return_value = [
- StreamChunk(type="text", data={"text": "Fix login bug"}),
- ]
-
- result = generate_task_title("Users are experiencing login issues on the authentication page")
-
- assert result == "Fix login bug"
- mock_client_cls.assert_called_once_with(distinct_id="task-title-generator")
-
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_multipart_response(self, mock_client_cls):
- mock_client = MagicMock()
- mock_client_cls.return_value = mock_client
-
- mock_client.stream.return_value = [
- StreamChunk(type="text", data={"text": "Add "}),
- StreamChunk(type="text", data={"text": "dashboard "}),
- StreamChunk(type="text", data={"text": "metrics"}),
- ]
-
- result = generate_task_title("Need to add metrics to the dashboard")
-
- assert result == "Add dashboard metrics"
-
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_falls_back_on_error(self, mock_client_cls):
- mock_client_cls.side_effect = Exception("API error")
-
- result = generate_task_title("This is a test description that should be used as fallback")
-
- assert result == "This is a test description that should be used as fallback"
-
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_empty_description(self, mock_client_cls):
- result = generate_task_title("")
-
- assert result == "Untitled Task"
- mock_client_cls.assert_not_called()
-
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_whitespace_only(self, mock_client_cls):
- result = generate_task_title(" \n\t ")
-
- assert result == "Untitled Task"
- mock_client_cls.assert_not_called()
-
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_truncates_long_response(self, mock_client_cls):
- mock_client = MagicMock()
- mock_client_cls.return_value = mock_client
-
- long_title = "A" * 300
- mock_client.stream.return_value = [
- StreamChunk(type="text", data={"text": long_title}),
- ]
- result = generate_task_title("Some description")
+def _blocks(*blocks: tuple[str, str]) -> SimpleNamespace:
+ return SimpleNamespace(content=[SimpleNamespace(type=kind, text=text) for kind, text in blocks])
- assert len(result) == 60
- assert result == "A" * 60
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_ignores_non_text_chunks(self, mock_client_cls):
- mock_client = MagicMock()
- mock_client_cls.return_value = mock_client
-
- mock_client.stream.return_value = [
- StreamChunk(type="usage", data={"input_tokens": 10, "output_tokens": 5}),
- StreamChunk(type="text", data={"text": "Valid title"}),
- ]
-
- result = generate_task_title("Description")
-
- assert result == "Valid title"
-
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_generate_task_title_empty_response(self, mock_client_cls):
- mock_client = MagicMock()
- mock_client_cls.return_value = mock_client
-
- mock_client.stream.return_value = []
-
- result = generate_task_title("This should fall back to description")
-
- assert result == "This should fall back to description"
-
-
-class TestTitleGeneratorAIGateway:
+class TestTitleGenerator:
@pytest.mark.parametrize(
- ("gateway_configured", "flag_on", "expect_gateway"),
+ ("response", "expected"),
[
- (True, True, True),
- (True, False, False),
- (False, True, False),
- (False, False, False),
+ (_blocks(("text", "Fix login bug")), "Fix login bug"),
+ (_blocks(("text", "Add "), ("text", "dashboard "), ("text", "metrics")), "Add dashboard metrics"),
+ (_blocks(("thinking", "ignored"), ("text", "Valid title")), "Valid title"),
+ (_blocks(("text", "A" * 300)), "A" * 60),
+ (_blocks(), "Users are experiencing login issues"),
],
)
- @patch("posthog.llm.gateway_client.build_ai_gateway_anthropic_client")
- @patch("products.tasks.backend.logic.services.title_generator.feature_enabled_or_false")
- @patch("posthog.llm.gateway_client.resolve_ai_gateway_config")
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_routes_by_gateway_config_and_flag(
- self, mock_client_cls, mock_resolve, mock_flag, mock_build, gateway_configured, flag_on, expect_gateway
- ):
- mock_resolve.return_value = MagicMock() if gateway_configured else None
- mock_flag.return_value = flag_on
- gateway = mock_build.return_value.with_options.return_value.__enter__.return_value
- gateway.messages.create.return_value = SimpleNamespace(
- content=[SimpleNamespace(type="text", text="Gateway title")]
- )
- mock_client_cls.return_value.stream.return_value = [StreamChunk(type="text", data={"text": "Direct title"})]
-
- result = generate_task_title("Users are experiencing login issues")
-
- assert result == ("Gateway title" if expect_gateway else "Direct title")
- assert mock_build.called is expect_gateway
- assert mock_client_cls.called is not expect_gateway
+ @patch(BUILDER)
+ def test_generate_task_title_from_gateway_response(self, mock_build, response, expected):
+ create = mock_build.return_value.with_options.return_value.__enter__.return_value.messages.create
+ create.return_value = response
- @pytest.mark.parametrize(
- ("distinct_id", "expected_flag_distinct_id"),
- [
- ("user-distinct-id", "user-distinct-id"),
- ("", "random-id"),
- (None, "random-id"),
- ],
- )
- @patch("products.tasks.backend.logic.services.title_generator.uuid4", return_value="random-id")
- @patch("products.tasks.backend.logic.services.title_generator.feature_enabled_or_false", return_value=False)
- @patch("posthog.llm.gateway_client.resolve_ai_gateway_config", return_value=MagicMock())
- @patch("products.ai_observability.backend.llm.client.Client")
- def test_flag_buckets_by_caller_distinct_id(
- self, mock_client_cls, _mock_resolve, mock_flag, _mock_uuid4, distinct_id, expected_flag_distinct_id
- ):
- mock_client_cls.return_value.stream.return_value = [StreamChunk(type="text", data={"text": "Direct title"})]
-
- generate_task_title("Users are experiencing login issues", distinct_id=distinct_id)
-
- assert mock_flag.call_args.args == ("tasks-title-ai-gateway", expected_flag_distinct_id)
- mock_client_cls.assert_called_once_with(distinct_id="task-title-generator")
-
- @patch("posthog.llm.gateway_client.build_ai_gateway_anthropic_client")
- @patch("products.tasks.backend.logic.services.title_generator.feature_enabled_or_false", return_value=True)
- @patch("posthog.llm.gateway_client.resolve_ai_gateway_config", return_value=MagicMock())
- def test_gateway_request_keeps_model_and_attribution(self, _mock_resolve, _mock_flag, mock_build):
+ assert generate_task_title("Users are experiencing login issues") == expected
+
+ @pytest.mark.parametrize("description", ["", " \n\t "])
+ @patch(BUILDER)
+ def test_generate_task_title_blank_description(self, mock_build, description):
+ assert generate_task_title(description) == "Untitled Task"
+ mock_build.assert_not_called()
+
+ @patch(BUILDER)
+ def test_gateway_request_keeps_model_and_attribution(self, mock_build):
with_options = mock_build.return_value.with_options
create = with_options.return_value.__enter__.return_value.messages.create
- create.return_value = SimpleNamespace(
- content=[
- SimpleNamespace(type="thinking", text="ignored"),
- SimpleNamespace(type="text", text="Fix "),
- SimpleNamespace(type="text", text="login bug"),
- ]
- )
-
- result = generate_task_title("Users are experiencing login issues")
-
- assert result == "Fix login bug"
- mock_build.assert_called_once_with(ai_product="posthog_code", distinct_id="task-title-generator")
+ create.return_value = _blocks(("text", "Fix login bug"))
+
+ generate_task_title("Users are experiencing login issues")
+
+ mock_build.assert_called_once_with(ai_product="tasks_title", distinct_id="task-title-generator")
with_options.assert_called_once_with(timeout=30.0, max_retries=1)
- with_options.return_value.__exit__.assert_called_once()
kwargs = create.call_args.kwargs
assert kwargs["model"] == "claude-haiku-4-5-20251001"
assert kwargs["max_tokens"] == 50
assert kwargs["temperature"] == 0.2
- assert kwargs["extra_headers"] == {"X-PostHog-Billable": "false", "X-PostHog-Privacy-Mode": "true"}
+ assert kwargs["extra_headers"] == {"X-PostHog-Billable": "false"}
- @patch("posthog.llm.gateway_client.build_ai_gateway_anthropic_client")
- @patch("products.tasks.backend.logic.services.title_generator.feature_enabled_or_false", return_value=True)
- @patch("posthog.llm.gateway_client.resolve_ai_gateway_config", return_value=MagicMock())
- def test_gateway_error_falls_back_to_description(self, _mock_resolve, _mock_flag, mock_build):
+ @pytest.mark.parametrize("failure", ["build", "create"])
+ @patch(BUILDER)
+ def test_gateway_error_falls_back_to_description(self, mock_build, failure):
with_options = mock_build.return_value.with_options
- with_options.return_value.__enter__.return_value.messages.create.side_effect = Exception("gateway down")
+ if failure == "build":
+ mock_build.side_effect = ValueError("AI_GATEWAY_URL and AI_GATEWAY_API_KEY must be configured")
+ else:
+ with_options.return_value.__enter__.return_value.messages.create.side_effect = Exception("gateway down")
result = generate_task_title("This is a test description that should be used as fallback")
assert result == "This is a test description that should be used as fallback"
- with_options.return_value.__exit__.assert_called_once()
+ if failure == "create":
+ with_options.return_value.__exit__.assert_called_once()
class TestFallbackTitle:
diff --git a/products/tasks/backend/logic/services/title_generator.py b/products/tasks/backend/logic/services/title_generator.py
index f23b71904a24..7190b3f8376d 100644
--- a/products/tasks/backend/logic/services/title_generator.py
+++ b/products/tasks/backend/logic/services/title_generator.py
@@ -1,14 +1,9 @@
import logging
-from typing import Any
-from uuid import uuid4
-
-from posthog.ph_client import feature_enabled_or_false
-
-from products.tasks.backend.constants import TASK_TITLE_AI_GATEWAY_FEATURE_FLAG
logger = logging.getLogger(__name__)
TITLE_DISTINCT_ID = "task-title-generator"
+TITLE_AI_PRODUCT = "tasks_title"
TITLE_MODEL = "claude-haiku-4-5-20251001"
TITLE_TEMPERATURE = 0.2 # Slightly lower for more consistent output
TITLE_MAX_TOKENS = 50 # Reduced since we want short titles
@@ -16,7 +11,7 @@ TITLE_GATEWAY_TIMEOUT_SECONDS = 30.0
TITLE_GATEWAY_MAX_RETRIES = 1
-def generate_task_title(description: str, *, distinct_id: str | None = None) -> str:
+def generate_task_title(description: str) -> str:
"""
Generate a concise task title from a description using Claude Haiku.
@@ -25,8 +20,7 @@ def generate_task_title(description: str, *, distinct_id: str | None = None) ->
# Deferred: the llm client pulls google.genai (Gemini SDK), and this module is reachable from a
# tasks serializer that the file-system registrations import at AppConfig.ready() — a module-level
# import here would drag the SDK onto the django.setup() path that every process pays for.
- from products.ai_observability.backend.llm.client import Client # noqa: PLC0415
- from products.ai_observability.backend.llm.types import CompletionRequest # noqa: PLC0415
+ from posthog.llm.gateway_client import build_ai_gateway_anthropic_client # noqa: PLC0415
if not description or not description.strip():
return "Untitled Task"
@@ -69,24 +63,19 @@ Examples:
</description>
Output the title now:"""
- messages: list[dict[str, Any]] = [{"role": "user", "content": user_prompt}]
-
- if _title_via_ai_gateway(distinct_id):
- response_text = _complete_via_ai_gateway(system_prompt, user_prompt)
- else:
- client = Client(distinct_id=TITLE_DISTINCT_ID)
- request = CompletionRequest(
+ client = build_ai_gateway_anthropic_client(ai_product=TITLE_AI_PRODUCT, distinct_id=TITLE_DISTINCT_ID)
+ with client.with_options(
+ timeout=TITLE_GATEWAY_TIMEOUT_SECONDS, max_retries=TITLE_GATEWAY_MAX_RETRIES
+ ) as gateway:
+ response = gateway.messages.create(
model=TITLE_MODEL,
- messages=messages,
- provider="anthropic",
system=system_prompt,
+ messages=[{"role": "user", "content": user_prompt}],
temperature=TITLE_TEMPERATURE,
max_tokens=TITLE_MAX_TOKENS,
+ extra_headers={"X-PostHog-Billable": "false"},
)
- response_text = ""
- for chunk in client.stream(request):
- if chunk.type == "text":
- response_text += chunk.data.get("text", "")
+ response_text = "".join(block.text for block in response.content if block.type == "text")
title = response_text.strip()
@@ -107,35 +96,6 @@ Output the title now:"""
return _fallback_title(description)
-def _title_via_ai_gateway(distinct_id: str | None) -> bool:
- from posthog.llm.gateway_client import resolve_ai_gateway_config # noqa: PLC0415
-
- if resolve_ai_gateway_config() is None:
- return False
- return feature_enabled_or_false(
- TASK_TITLE_AI_GATEWAY_FEATURE_FLAG,
- distinct_id or str(uuid4()),
- only_evaluate_locally=False,
- send_feature_flag_events=False,
- )
-
-
-def _complete_via_ai_gateway(system_prompt: str, user_prompt: str) -> str:
- from posthog.llm.gateway_client import build_ai_gateway_anthropic_client # noqa: PLC0415
-
- client = build_ai_gateway_anthropic_client(ai_product="posthog_code", distinct_id=TITLE_DISTINCT_ID)
- with client.with_options(timeout=TITLE_GATEWAY_TIMEOUT_SECONDS, max_retries=TITLE_GATEWAY_MAX_RETRIES) as gateway:
- response = gateway.messages.create(
- model=TITLE_MODEL,
- system=system_prompt,
- messages=[{"role": "user", "content": user_prompt}],
- temperature=TITLE_TEMPERATURE,
- max_tokens=TITLE_MAX_TOKENS,
- extra_headers={"X-PostHog-Billable": "false", "X-PostHog-Privacy-Mode": "true"},
- )
- return "".join(block.text for block in response.content if block.type == "text")
-
-
def _fallback_title(description: str) -> str:
"""Generate a fallback title from the description."""
clean_desc = description.strip()
diff --git a/products/tasks/backend/tests/test_facade.py b/products/tasks/backend/tests/test_facade.py
index 05cc5af09dd1..38058dacffbd 100644
--- a/products/tasks/backend/tests/test_facade.py
+++ b/products/tasks/backend/tests/test_facade.py
@@ -1894,7 +1894,7 @@ class TestFacadeReadsAndMappers(TestCase):
# When a client pastes text (stored as an attachment), it sends the pasted content as
# naming_source so the title reads well, while description stays the bare prompt the
# agent — and the reload transcript dedup — expect.
- mock_title.side_effect = lambda text, **_: f"title:{text}"
+ mock_title.side_effect = lambda text: f"title:{text}"
dto = facade.create_task(
team_id=self.team.id,
user_id=self.user.id,
@@ -1908,11 +1908,11 @@ class TestFacadeReadsAndMappers(TestCase):
self.assertEqual(task.description, "Attached files: pasted-text.txt")
self.assertEqual(task.title, "title:Deploy blocks on a stale lockfile")
self.assertFalse(task.title_manually_set)
- mock_title.assert_called_once_with("Deploy blocks on a stale lockfile", distinct_id=self.user.distinct_id)
+ mock_title.assert_called_once_with("Deploy blocks on a stale lockfile")
@patch("products.tasks.backend.logic.services.title_generator.generate_task_title")
def test_create_task_falls_back_to_description_without_naming_source(self, mock_title):
- mock_title.side_effect = lambda text, **_: f"title:{text}"
+ mock_title.side_effect = lambda text: f"title:{text}"
dto = facade.create_task(
team_id=self.team.id,
user_id=self.user.id,
@@ -1923,7 +1923,7 @@ class TestFacadeReadsAndMappers(TestCase):
)
task = Task.objects.get(id=dto.id)
self.assertEqual(task.title, "title:Fix the login redirect")
- mock_title.assert_called_once_with("Fix the login redirect", distinct_id=self.user.distinct_id)
+ mock_title.assert_called_once_with("Fix the login redirect")
@patch("products.tasks.backend.temporal.client.execute_task_processing_workflow")
def test_create_and_run_persists_dispatch_params_for_reconcile(self, _mock_workflow):