: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(review_hog): route one-shot review calls through the ai gateway (#103817)
Brandon8cb9786889bd
diff --git a/products/review_hog/backend/reviewer/sandbox/direct_llm.py b/products/review_hog/backend/reviewer/sandbox/direct_llm.pyindex 8b9ec8679049..5d71405a10b8 100644--- a/products/review_hog/backend/reviewer/sandbox/direct_llm.py+++ b/products/review_hog/backend/reviewer/sandbox/direct_llm.py@@ -7,7 +7,7 @@ from anthropic.types import OutputConfigParamfrom pydantic import BaseModelfrom temporalio.exceptions import ApplicationError-from posthog.llm.gateway_client import get_async_anthropic_gateway_client+from posthog.llm.gateway_client import build_async_anthropic_clientfrom products.review_hog.backend.reviewer.constants import ONESHOT_MODEL, ONESHOT_REASONING_EFFORT@@ -44,9 +44,10 @@ async def run_oneshot_review(through the LLM gateway pinned to ``model`` at ``reasoning_effort`` (defaulting to the one-shotpins), with the response schema-constrained to ``model_to_validate`` via structured outputs (sothe bare-JSON failure class the sandbox path retried on cannot occur). Callers that judge on a- different model family (the outcome classifier) pass ``model`` explicitly. Bedrock fallback is- deliberately off: the gateway's Bedrock path forwards only allowlisted params and would strip- ``output_config``, silently losing both the effort pin and the schema constraint.+ different model family (the outcome classifier) pass ``model`` explicitly. Bedrock fallback+ stays off: the Python gateway's Bedrock path forwards only allowlisted params and would strip+ ``output_config``, losing both the effort pin and the schema constraint. The Go gateway drops+ Bedrock targets that cannot serve structured outputs itself, so it needs no opt-out.Raises on failure so the calling Temporal activity retries, mirroring the sandbox contract.Anthropic ``APIError``s are re-raised as compact ``ApplicationError``s — a raw ``APIError``@@ -54,7 +55,10 @@ async def run_oneshot_review(non-retryable. ``step_name`` is stamped on the captured ``$ai_generation`` event as ``ai_stage``so dumps and cost queries can attribute the call to its pipeline stage."""- client = get_async_anthropic_gateway_client(product="review_hog", team_id=team_id)+ # The Go gateway reads ai_stage from the builder; the Python fallback reads the per-call header below.+ client = build_async_anthropic_client(+ product="review_hog", ai_product="review_hog", ai_stage=step_name, team_id=team_id+ )async with client:try:response = await client.messages.parse(diff --git a/products/review_hog/backend/reviewer/tests/test_direct_llm.py b/products/review_hog/backend/reviewer/tests/test_direct_llm.pyindex 842027398a0b..de835e70d0c2 100644--- a/products/review_hog/backend/reviewer/tests/test_direct_llm.py+++ b/products/review_hog/backend/reviewer/tests/test_direct_llm.py@@ -44,11 +44,12 @@ async def test_oneshot_call_pins_model_effort_schema_and_stage() -> None:parsed = IssueDeduplication(duplicates=[])mock_parse = AsyncMock(return_value=MagicMock(parsed_output=parsed))- with patch(f"{_MODULE}.get_async_anthropic_gateway_client", return_value=_mock_client(mock_parse)) as mock_get:+ with patch(f"{_MODULE}.build_async_anthropic_client", return_value=_mock_client(mock_parse)) as mock_get:result = await _call()assert result is parsedassert mock_get.call_args.kwargs["product"] == "review_hog"+ assert mock_get.call_args.kwargs["ai_product"] == "review_hog"assert mock_get.call_args.kwargs["team_id"] == 1kwargs = mock_parse.call_args.kwargsassert kwargs["model"] == ONESHOT_MODEL@@ -74,7 +75,7 @@ async def test_api_errors_map_to_temporal_retryability(status: int, non_retryablmock_parse = AsyncMock(side_effect=_api_error(status))with (- patch(f"{_MODULE}.get_async_anthropic_gateway_client", return_value=_mock_client(mock_parse)),+ patch(f"{_MODULE}.build_async_anthropic_client", return_value=_mock_client(mock_parse)),pytest.raises(ApplicationError) as exc_info,):await _call()@@ -96,7 +97,7 @@ async def test_no_parseable_output_retryability_branches_on_stop_reason(stop_reamock_parse = AsyncMock(return_value=MagicMock(parsed_output=None, stop_reason=stop_reason))with (- patch(f"{_MODULE}.get_async_anthropic_gateway_client", return_value=_mock_client(mock_parse)),+ patch(f"{_MODULE}.build_async_anthropic_client", return_value=_mock_client(mock_parse)),pytest.raises(ApplicationError) as exc_info,):await _call()@@ -122,10 +123,21 @@ async def test_truncated_json_validation_error_becomes_a_compact_application_errmock_parse = AsyncMock(side_effect=_validation_error())with (- patch(f"{_MODULE}.get_async_anthropic_gateway_client", return_value=_mock_client(mock_parse)),+ patch(f"{_MODULE}.build_async_anthropic_client", return_value=_mock_client(mock_parse)),pytest.raises(ApplicationError) as exc_info,):await _call()assert exc_info.value.non_retryable is Falseassert "dedup" in str(exc_info.value)+++async def test_stage_labels_both_gateway_dialects() -> None:+ mock_parse = AsyncMock(return_value=MagicMock(parsed_output=IssueDeduplication(duplicates=[])))++ with patch(f"{_MODULE}.build_async_anthropic_client", return_value=_mock_client(mock_parse)) as mock_get:+ await _call()++ assert mock_get.call_args.kwargs["ai_stage"] == "dedup"+ assert mock_parse.call_args.kwargs["extra_headers"] == {"x-posthog-property-ai_stage": "dedup"}