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.

fix(endpoints): stop counting budget refusals as endpoint errors (#98233)

Jovan Sakovic8ed498c5b10e
diff --git a/products/endpoints/backend/logic/execution.py b/products/endpoints/backend/logic/execution.py
index 904ec6c7febd..fd625299b743 100644
--- a/products/endpoints/backend/logic/execution.py
+++ b/products/endpoints/backend/logic/execution.py
@@ -56,6 +56,7 @@ from posthog.clickhouse.query_tagging import (
from posthog.errors import ExposedCHQueryError
from posthog.event_usage import get_request_analytics_properties, report_user_action
from posthog.exceptions import (
+ APIQueriesBudgetExceeded,
ClickHouseAtCapacity,
ClickHouseEstimatedQueryExecutionTimeTooLong,
ClickHouseQueryMemoryLimitExceeded,
@@ -122,9 +123,13 @@ _QUERY_PERFORMANCE_ERRORS: dict[type[Exception], tuple[str, str]] = {
),
}
-# Cost guardrails + transient capacity: customer problems, not faults. Skipped from capture and
-# re-raised past the materialized/ducklake inline fallback.
-_QUERY_GUARDRAIL_ERRORS: tuple[type[Exception], ...] = (*_QUERY_PERFORMANCE_ERRORS, ClickHouseAtCapacity)
+# Cost guardrails, budget refusals and transient capacity: customer problems, not faults. Skipped
+# from capture and re-raised past the materialized/ducklake inline fallback.
+_QUERY_GUARDRAIL_ERRORS: tuple[type[Exception], ...] = (
+ *_QUERY_PERFORMANCE_ERRORS,
+ ClickHouseAtCapacity,
+ APIQueriesBudgetExceeded,
+)
def _is_query_guardrail_error(error: BaseException) -> bool:
@@ -571,7 +576,9 @@ class EndpointExecutionService(PydanticModelMixin):
limit=limit,
offset=offset,
)
- except ConcurrencyLimitExceeded:
+ except (ConcurrencyLimitExceeded, APIQueriesBudgetExceeded):
+ # A refusal is not a broken table. The inline path meets the same limiter
+ # with the same balance, so falling back cannot succeed.
raise
except Exception:
# Already logged/captured/signaled inside the materialized path. Re-run
@@ -626,6 +633,10 @@ class EndpointExecutionService(PydanticModelMixin):
except ConcurrencyLimitExceeded:
ENDPOINT_CONCURRENCY_REJECTED_TOTAL.labels(team_id=str(self.team.pk)).inc()
raise Throttled(detail="Too many concurrent requests. Please try again later.")
+ except APIQueriesBudgetExceeded:
+ # The platform refused this query on purpose, so it is not an endpoint fault. Leave the
+ # execution counter alone: the budget path counts its own refusals.
+ raise
except tuple(_QUERY_PERFORMANCE_ERRORS) as e:
execution_status = "query_performance"
error_label = type(e).__name__
diff --git a/products/endpoints/backend/tests/test_endpoint_execution.py b/products/endpoints/backend/tests/test_endpoint_execution.py
index ebdd4c216119..32b497437056 100644
--- a/products/endpoints/backend/tests/test_endpoint_execution.py
+++ b/products/endpoints/backend/tests/test_endpoint_execution.py
@@ -16,6 +16,7 @@ from posthog.schema import EventsNode, TrendsQuery
from posthog.hogql.errors import ExposedHogQLError
from posthog.errors import CHQueryErrorNoCommonType
+from posthog.exceptions import APIQueriesBudgetExceeded
from products.data_modeling.backend.facade.models import DataModelingJob, DataWarehouseSavedQuery
from products.endpoints.backend.logic.execution import EndpointExecutionService, _emit_endpoint_failure_signal
@@ -198,6 +199,33 @@ class TestEndpointExecution(ClickhouseTestMixin, APIBaseTest):
if forbidden_detail:
self.assertNotIn(forbidden_detail, detail)
+ def test_budget_refusal_does_not_count_as_an_endpoint_error(self):
+ endpoint = create_endpoint_with_version(
+ name="budget_refused",
+ team=self.team,
+ query={"kind": "HogQLQuery", "query": "SELECT count() FROM events"},
+ created_by=self.user,
+ is_active=True,
+ )
+
+ with (
+ mock.patch(
+ "products.endpoints.backend.logic.execution.process_query_model",
+ side_effect=APIQueriesBudgetExceeded(wait=120),
+ ),
+ mock.patch("products.endpoints.backend.logic.execution.ENDPOINT_EXECUTION_TOTAL") as mock_counter,
+ mock.patch("products.endpoints.backend.logic.execution._emit_endpoint_failure_signal") as mock_signal,
+ mock.patch("products.endpoints.backend.logic.execution.capture_exception") as mock_capture,
+ ):
+ response = self.client.post(
+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/run/", {}, format="json"
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)
+ mock_counter.labels.assert_not_called()
+ mock_signal.assert_not_called()
+ mock_capture.assert_not_called()
+
def test_hogql_endpoint_executes_with_variable_override(self):
endpoint = create_endpoint_with_version(
name="hogql_var_override",