: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
fix(support-sidebar-max): add explicit Anthropic client timeout (#65816)
Sam Penningtonfe2f3fd1e629
diff --git a/ee/support_sidebar_max/views.py b/ee/support_sidebar_max/views.pyindex 563e6f5bb3b7..05addb27f2d7 100644--- a/ee/support_sidebar_max/views.py+++ b/ee/support_sidebar_max/views.py@@ -47,6 +47,10 @@ class MaxChatViewSet(viewsets.ViewSet):CONVERSATION_TIMEOUT = 3600 # one hourMAX_BACKOFF = 40 # Maximum backoff in seconds to prevent gateway timeouts+ # Cap how long any single Anthropic call may block a worker. The SDK defaults to 600s,+ # which lets stalled requests pile up (each holding conversation history + a connection)+ # and exhaust worker memory when Anthropic is slow or erroring.+ ANTHROPIC_TIMEOUT_SECONDS = 30.0basename = "max"def _convert_headers(self, headers: MutableMapping[str, str]) -> dict[str, str]:@@ -79,7 +83,7 @@ class MaxChatViewSet(viewsets.ViewSet):return self._handle_rate_limit(retry_after, limit_type)# Initialize Anthropic client (non-blocking)- client = anthropic.Anthropic(api_key=settings.ANTHROPIC_API_KEY)+ client = anthropic.Anthropic(api_key=settings.ANTHROPIC_API_KEY, timeout=self.ANTHROPIC_TIMEOUT_SECONDS)data = request.dataif not data or "message" not in data:@@ -351,6 +355,15 @@ class MaxChatViewSet(viewsets.ViewSet):except Exception as header_error:django_logger.warning(f"✨🦔 Rate limit handling error: {str(header_error)}")return self._handle_rate_limit(self.MAX_BACKOFF) # Default to MAX_BACKOFF+ except anthropic.APITimeoutError:+ # Raised once a call exceeds ANTHROPIC_TIMEOUT_SECONDS. Surface a clean 504 rather than+ # letting it fall through to the generic handler, which would leak the raw SDK error and+ # mislabel an upstream timeout as a 500.+ django_logger.warning(f"✨🦔 Anthropic API call timed out after {self.ANTHROPIC_TIMEOUT_SECONDS}s")+ return Response(+ {"error": "The AI service took too long to respond. Please try again."},+ status=status.HTTP_504_GATEWAY_TIMEOUT,+ )except Exception as e:django_logger.error(f"✨🦔 Request to Anthropic API failed: {str(e)}", exc_info=True)return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)diff --git a/posthog/temporal/tests/product_analytics/__snapshots__/test_upgrade_queries_workflow.ambr b/posthog/temporal/tests/product_analytics/__snapshots__/test_upgrade_queries_workflow.ambrindex 0839c952c78f..a1c0325ab25a 100644--- a/posthog/temporal/tests/product_analytics/__snapshots__/test_upgrade_queries_workflow.ambr+++ b/posthog/temporal/tests/product_analytics/__snapshots__/test_upgrade_queries_workflow.ambr@@ -11,6 +11,14 @@OR query @? '$.** ? (@.kind == "InsightVizNode" &&(!exists(@.version) || @.version == null || @.version < 4)+ )'+ OR query @? '$.** ? (+ @.kind == "RetentionQuery" &&+ (!exists(@.version) || @.version == null || @.version < 2)+ )'+ OR query @? '$.** ? (+ @.kind == "StickinessQuery" &&+ (!exists(@.version) || @.version == null || @.version < 2))'OR query @? '$.** ? (@.kind == "TrendsQuery" &&