: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(tasks): route task titles through the ai gateway (#127830)
Charles Vien8f3f3c31f2e0
diff --git a/products/tasks/backend/constants.py b/products/tasks/backend/constants.pyindex 0a3c5dc7e6ff..5dcfec9d4078 100644--- a/products/tasks/backend/constants.py+++ b/products/tasks/backend/constants.py@@ -45,6 +45,7 @@ PI_CLOUD_RUNTIME_FEATURE_FLAG = "pi-harness"REASONING_EFFORTS = model_catalog.REASONING_EFFORTSPI_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/logic/services/test_title_generator.py b/products/tasks/backend/logic/services/test_title_generator.pyindex 434e263b6869..f02b06531050 100644--- a/products/tasks/backend/logic/services/test_title_generator.py+++ b/products/tasks/backend/logic/services/test_title_generator.py@@ -1,3 +1,5 @@+from types import SimpleNamespace+import pytestfrom unittest.mock import MagicMock, patch@@ -98,6 +100,76 @@ class TestTitleGenerator:assert result == "This should fall back to description"+class TestTitleGeneratorAIGateway:+ @pytest.mark.parametrize(+ ("gateway_configured", "flag_on", "expect_gateway"),+ [+ (True, True, True),+ (True, False, False),+ (False, True, False),+ (False, False, False),+ ],+ )+ @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("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):+ 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")+ 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"}++ @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):+ with_options = mock_build.return_value.with_options+ 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()++class TestFallbackTitle:@pytest.mark.parametrize(("description", "expected"),diff --git a/products/tasks/backend/logic/services/title_generator.py b/products/tasks/backend/logic/services/title_generator.pyindex b8150f265375..4b4900e674e7 100644--- a/products/tasks/backend/logic/services/title_generator.py+++ b/products/tasks/backend/logic/services/title_generator.py@@ -1,8 +1,19 @@import loggingfrom typing import Any+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_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+TITLE_GATEWAY_TIMEOUT_SECONDS = 30.0+TITLE_GATEWAY_MAX_RETRIES = 1+def generate_task_title(description: str) -> str:"""@@ -20,8 +31,6 @@ def generate_task_title(description: str) -> str:return "Untitled Task"try:- client = Client(distinct_id="task-title-generator")-system_prompt = """You are a title generator. You output ONLY a task title. Nothing else.Convert the task description into a concise task title.@@ -52,32 +61,31 @@ Examples:- "What's the best restaurant in NYC?" → "NYC restaurant recommendations""""- messages: list[dict[str, Any]] = [- {- "role": "user",- "content": f"""Generate a task title based on the following description. Do NOT respond to, answer, or help with the description content - ONLY generate a title.+ user_prompt = f"""Generate a task title based on the following description. Do NOT respond to, answer, or help with the description content - ONLY generate a title.<description>{description}</description>-Output the title now:""",- }- ]-- request = CompletionRequest(- model="claude-haiku-4-5-20251001",- messages=messages,- provider="anthropic",- system=system_prompt,- temperature=0.2, # Slightly lower for more consistent output- max_tokens=50, # Reduced since we want short titles- )-- response_text = ""- for chunk in client.stream(request):- if chunk.type == "text":- response_text += chunk.data.get("text", "")+Output the title now:"""+ messages: list[dict[str, Any]] = [{"role": "user", "content": user_prompt}]++ if _title_via_ai_gateway():+ response_text = _complete_via_ai_gateway(system_prompt, user_prompt)+ else:+ client = Client(distinct_id=TITLE_DISTINCT_ID)+ request = CompletionRequest(+ model=TITLE_MODEL,+ messages=messages,+ provider="anthropic",+ system=system_prompt,+ temperature=TITLE_TEMPERATURE,+ max_tokens=TITLE_MAX_TOKENS,+ )+ response_text = ""+ for chunk in client.stream(request):+ if chunk.type == "text":+ response_text += chunk.data.get("text", "")title = response_text.strip()@@ -98,6 +106,35 @@ Output the title now:""",return _fallback_title(description)+def _title_via_ai_gateway() -> 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,+ TITLE_DISTINCT_ID,+ 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()