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.

feat(llm-analytics): dedicated rate limit for prompt fetch endpoints (#108384)

Juraj Majerikf7d967c64afb
diff --git a/posthog/api/llm_prompt.py b/posthog/api/llm_prompt.py
index a2098647fdfd..f987a709166d 100644
--- a/posthog/api/llm_prompt.py
+++ b/posthog/api/llm_prompt.py
@@ -69,7 +69,12 @@ from posthog.event_usage import report_team_action, report_user_action
from posthog.exceptions_capture import capture_exception
from posthog.models import User
from posthog.permissions import AccessControlPermission
-from posthog.rate_limit import BurstRateThrottle, LLMPromptPublishBurstRateThrottle, SustainedRateThrottle
+from posthog.rate_limit import (
+ BurstRateThrottle,
+ LLMPromptFetchRateThrottle,
+ LLMPromptPublishBurstRateThrottle,
+ SustainedRateThrottle,
+)
from posthog.storage.llm_prompt_cache import get_prompt_by_name_from_cache
from products.access_control.backend.presentation.access_control import AccessControlViewSetMixin
@@ -109,8 +114,10 @@ class LLMPromptViewSet(
def get_throttles(self):
if self.action == "update_by_name":
return [LLMPromptPublishBurstRateThrottle(), BurstRateThrottle(), SustainedRateThrottle()]
- if self.action in ["get_by_name", "resolve_by_name"]:
- return [BurstRateThrottle(), SustainedRateThrottle()]
+ # SDK read paths (get_all() hits list) get a dedicated per-minute budget, so a
+ # polling fleet cannot exhaust the shared sustained budget for the whole API key.
+ if self.action in ["list", "get_by_name", "resolve_by_name"]:
+ return [LLMPromptFetchRateThrottle()]
return super().get_throttles()
diff --git a/posthog/api/test/test_llm_prompt.py b/posthog/api/test/test_llm_prompt.py
index d3957b6b9145..adc23f429f1f 100644
--- a/posthog/api/test/test_llm_prompt.py
+++ b/posthog/api/test/test_llm_prompt.py
@@ -28,7 +28,12 @@ from posthog.models import PersonalAPIKey
from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.models.oauth import OAuthAccessToken, OAuthApplication
from posthog.models.utils import generate_random_token_personal, hash_key_value
-from posthog.rate_limit import BurstRateThrottle, LLMPromptPublishBurstRateThrottle, SustainedRateThrottle
+from posthog.rate_limit import (
+ BurstRateThrottle,
+ LLMPromptFetchRateThrottle,
+ LLMPromptPublishBurstRateThrottle,
+ SustainedRateThrottle,
+)
from posthog.storage.llm_prompt_cache import get_prompt_by_name_from_cache
from products.ai_observability.backend.models.llm_prompt import LLMPrompt, LLMPromptDependency, LLMPromptLabel
@@ -947,15 +952,15 @@ class TestLLMPromptAPI(APIBaseTest):
assert isinstance(throttles[1], BurstRateThrottle)
assert isinstance(throttles[2], SustainedRateThrottle)
- def test_get_by_name_uses_default_burst_and_sustained_throttles(self):
+ @parameterized.expand(["list", "get_by_name", "resolve_by_name"])
+ def test_fetch_actions_use_dedicated_throttle_instead_of_shared_budget(self, action):
view = LLMPromptViewSet()
- view.action = "get_by_name"
+ view.action = action
throttles = view.get_throttles()
- assert len(throttles) == 2
- assert isinstance(throttles[0], BurstRateThrottle)
- assert isinstance(throttles[1], SustainedRateThrottle)
+ assert len(throttles) == 1
+ assert isinstance(throttles[0], LLMPromptFetchRateThrottle)
def test_duplicate_prompt_creates_new_prompt_with_latest_content(self):
self.create_prompt_version(name="original", version=1, is_latest=False, prompt="v1")
diff --git a/posthog/rate_limit.py b/posthog/rate_limit.py
index b2c6056d1c69..98505b88b358 100644
--- a/posthog/rate_limit.py
+++ b/posthog/rate_limit.py
@@ -1102,6 +1102,15 @@ class LLMPromptPublishBurstRateThrottle(PersonalApiKeyOrUserRateThrottle):
rate = "30/minute"
+class LLMPromptFetchRateThrottle(PersonalApiKeyRateThrottle):
+ # SDK fleets poll prompt fetches on a fixed interval, so the shared sustained budget
+ # (4800/hour) rejects steady polling that the burst budget allows. A per-minute-only
+ # bucket keeps prompt fetches out of the general API budget, mirroring the dedicated
+ # feature_flag_remote_config throttle.
+ scope = "llm_prompt_fetch"
+ rate = "600/minute"
+
+
class EventValuesBurstThrottle(PersonalApiKeyRateThrottle):
scope = "event_values_burst"
rate = "60/minute"