: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(api): let the read budget flag target free organizations (#122870)
Andy Zhao01623f75e37a
diff --git a/posthog/hogql_queries/query_runner.py b/posthog/hogql_queries/query_runner.pyindex fb0e3c46a568..816d38710dc0 100644--- a/posthog/hogql_queries/query_runner.py+++ b/posthog/hogql_queries/query_runner.py@@ -524,6 +524,12 @@ def shared_insights_execution_mode(execution_mode: ExecutionMode) -> SharedExecu)+def _organization_plan(team: Team) -> str:+ if team.organization.has_active_subscription is None:+ return "unknown"+ return "paid" if team.organization.has_active_subscription else "free"++def _api_queries_budget_enforcement_enabled(team: Team) -> bool:org_id = str(team.organization_id)try:@@ -532,6 +538,7 @@ def _api_queries_budget_enforcement_enabled(team: Team) -> bool:API_QUERIES_BUDGET_ENFORCEMENT_FLAG,org_id,groups={"organization": org_id},+ person_properties={"organization_plan": _organization_plan(team)},group_properties={"organization": {"id": org_id}},only_evaluate_locally=False,send_feature_flag_events=False,@@ -606,6 +613,7 @@ def enforce_api_queries_budget(team: Team) -> None:{"outcome": outcome,"team_id": team.pk,+ "organization_plan": _organization_plan(team),"bytes_per_hour": status.spec.bytes_per_hour,"remaining_bytes": status.remaining_bytes,"retry_after_seconds": status.retry_after_seconds,diff --git a/posthog/hogql_queries/test/test_api_queries_budget.py b/posthog/hogql_queries/test/test_api_queries_budget.pyindex 0eed53e5c4a9..d92c885fa3ff 100644--- a/posthog/hogql_queries/test/test_api_queries_budget.py+++ b/posthog/hogql_queries/test/test_api_queries_budget.py@@ -35,9 +35,9 @@ class TestApiQueriesBudgetEnforcement(BaseTest):runner.is_query_service = is_query_servicereturn runner- def _drain(self) -> None:+ def _drain(self, read_bytes: int = 10_000) -> None:refill_and_read(str(self.team.pk), budget_spec_for(self.organization))- debit(str(self.team.pk), 10_000)+ debit(str(self.team.pk), read_bytes)def test_status_reports_remaining_and_retry_after(self):self._drain()@@ -97,6 +97,7 @@ class TestApiQueriesBudgetEnforcement(BaseTest):assert event == "api queries budget limited"assert properties["outcome"] == outcomeassert properties["team_id"] == self.team.pk+ assert properties["organization_plan"] == "free"assert properties["bytes_per_hour"] == 3600assert properties["remaining_bytes"] <= 0assert properties["retry_after_seconds"] >= 1@@ -130,6 +131,24 @@ class TestApiQueriesBudgetEnforcement(BaseTest):):self._runner()._enforce_api_queries_budget()+ @parameterized.expand([("free", False, True), ("paid", True, False), ("never_synced", None, False)])+ def test_flag_can_enforce_only_free_organizations(self, _name, has_active_subscription, refused):+ self.organization.has_active_subscription = has_active_subscription+ self.organization.save()+ self._drain(read_bytes=1_000_000)++ def flag_on_for_free_plans(*_args, person_properties=None, **_kwargs):+ return (person_properties or {}).get("organization_plan") == "free"++ with patch(+ "posthog.hogql_queries.query_runner.posthoganalytics.feature_enabled", side_effect=flag_on_for_free_plans+ ):+ if refused:+ with pytest.raises(APIQueriesBudgetExceeded):+ self._runner()._enforce_api_queries_budget()+ else:+ self._runner()._enforce_api_queries_budget()+@parameterized.expand([("api_key_query_unbudgeted", True), ("app_query", False)])def test_call_with_rate_limits_admits_unbudgeted_queries(self, _name, is_query_service):self._drain()