: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(signals): opt into bedrock fallbacks for llm gateway (#65492)
Joshua Snyder9b7e6c34fe3d
diff --git a/posthog/llm/gateway_client.py b/posthog/llm/gateway_client.pyindex 13a2b5fac8e0..dde80dbda85e 100644--- a/posthog/llm/gateway_client.py+++ b/posthog/llm/gateway_client.py@@ -109,7 +109,11 @@ def get_async_llm_client(product: Product = "django", team_id: int | None = None)-def get_async_anthropic_gateway_client(product: Product = "django", team_id: int | None = None) -> AsyncAnthropic:+def get_async_anthropic_gateway_client(+ product: Product = "django",+ team_id: int | None = None,+ use_bedrock_fallback: bool = False,+) -> AsyncAnthropic:"""Get an Anthropic-native async client pointed at the internal LLM gateway.@@ -128,14 +132,22 @@ def get_async_anthropic_gateway_client(product: Product = "django", team_id: intrationale — it is sent identically as a default `x-posthog-property-team_id` header. Forper-call tags, pass `extra_headers={"x-posthog-property-<key>": "<value>"}` on the individual`messages.create(...)` call, and the user identifier as `metadata={"user_id": ...}`.++ Set `use_bedrock_fallback=True` to opt into the gateway's Bedrock fallback: if Anthropic+ returns a 5xx/429 (or its circuit breaker is open) the gateway retries the request against+ Bedrock instead of failing. Sent as the `x-posthog-use-bedrock-fallback` default header."""if not settings.LLM_GATEWAY_URL or not settings.LLM_GATEWAY_API_KEY:raise ValueError("LLM_GATEWAY_URL and LLM_GATEWAY_API_KEY must be configured")+ default_headers = _team_id_header(team_id) if team_id is not None else {}+ if use_bedrock_fallback:+ default_headers["x-posthog-use-bedrock-fallback"] = "true"+base_url = f"{settings.LLM_GATEWAY_URL.rstrip('/')}/{product}"return AsyncAnthropic(base_url=base_url,api_key=settings.LLM_GATEWAY_API_KEY,- default_headers=_team_id_header(team_id) if team_id is not None else None,+ default_headers=default_headers or None,http_client=httpx.AsyncClient(trust_env=False),)diff --git a/posthog/llm/test_gateway_client.py b/posthog/llm/test_gateway_client.pyindex 4edd1a7d9f46..1ef0f9c128af 100644--- a/posthog/llm/test_gateway_client.py+++ b/posthog/llm/test_gateway_client.py@@ -139,3 +139,21 @@ class TestGetAsyncAnthropicGatewayClient:client = get_async_anthropic_gateway_client(product="signals")assert client.default_headers.get("x-posthog-property-team_id") is None++ @pytest.mark.parametrize(+ "use_bedrock_fallback, expected_header_value",+ [+ (True, "true"),+ (False, None),+ ],+ )+ @patch("posthog.llm.gateway_client.settings")+ def test_bedrock_fallback_header(self, mock_settings, use_bedrock_fallback: bool, expected_header_value):+ mock_settings.LLM_GATEWAY_URL = "http://gateway:8080"+ mock_settings.LLM_GATEWAY_API_KEY = "test-key"++ client = get_async_anthropic_gateway_client(+ product="signals", team_id=42, use_bedrock_fallback=use_bedrock_fallback+ )++ assert client.default_headers.get("x-posthog-use-bedrock-fallback") == expected_header_valuediff --git a/products/signals/backend/temporal/llm.py b/products/signals/backend/temporal/llm.pyindex 60ab18913b37..5ec8e452e6c6 100644--- a/products/signals/backend/temporal/llm.py+++ b/products/signals/backend/temporal/llm.py@@ -89,7 +89,7 @@ async def call_llm() -> T:# Native Anthropic Messages endpoint so prefilling and extended thinking carry over unchanged.thinking = thinking and MATCHING_MODEL in ANTHROPIC_THINKING_MODELS- client = get_async_anthropic_gateway_client(product="signals", team_id=team_id)+ client = get_async_anthropic_gateway_client(product="signals", team_id=team_id, use_bedrock_fallback=True)messages: list[MessageParam] = [{"role": "user", "content": user_prompt},