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(aio): let project secret API keys fetch prompts (#129971)

Juraj Majerik9d5abf927345
diff --git a/frontend/src/lib/scopes.tsx b/frontend/src/lib/scopes.tsx
index b41c23e90194..150bd77fec67 100644
--- a/frontend/src/lib/scopes.tsx
+++ b/frontend/src/lib/scopes.tsx
@@ -429,6 +429,7 @@ export const PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION = [
'messaging_preference:write',
'transactional_email:read',
'transactional_email:write',
+ 'llm_prompt:read',
] as const
export type ProjectSecretAPIKeyAllowedScope = (typeof PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION)[number]
diff --git a/posthog/api/llm_prompt.py b/posthog/api/llm_prompt.py
index b5f14303918a..51422fe94d13 100644
--- a/posthog/api/llm_prompt.py
+++ b/posthog/api/llm_prompt.py
@@ -61,13 +61,19 @@ from posthog.api.services.llm_prompt import (
set_prompt_label,
unarchive_prompt,
)
-from posthog.auth import DelegatedOAuthAccessTokenAuthentication, OAuthAccessTokenAuthentication, SessionAuthentication
+from posthog.auth import (
+ DelegatedOAuthAccessTokenAuthentication,
+ OAuthAccessTokenAuthentication,
+ ProjectSecretAPIKeyAuthentication,
+ SessionAuthentication,
+)
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,
+ LLMPromptFetchProjectSecretApiKeyTeamRateThrottle,
LLMPromptFetchRateThrottle,
LLMPromptPublishBurstRateThrottle,
SustainedRateThrottle,
@@ -98,6 +104,10 @@ class LLMPromptViewSet(
viewsets.GenericViewSet,
):
scope_object = "llm_prompt"
+ # Extends the default authenticators. TeamAndOrgViewSetMixin keeps session, personal key and OAuth.
+ authentication_classes = [ProjectSecretAPIKeyAuthentication]
+ # The two reads the SDKs make. Every other action stays tied to a person.
+ psak_allowed_actions = ["list", "get_by_name"]
queryset = LLMPrompt.objects.all()
serializer_class = LLMPromptSerializer
permission_classes = [AccessControlPermission]
@@ -111,7 +121,7 @@ class LLMPromptViewSet(
# 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 [LLMPromptFetchRateThrottle(), LLMPromptFetchProjectSecretApiKeyTeamRateThrottle()]
return super().get_throttles()
diff --git a/posthog/api/test/test_llm_prompt.py b/posthog/api/test/test_llm_prompt.py
index f7b56d65d281..4a40c5781ee2 100644
--- a/posthog/api/test/test_llm_prompt.py
+++ b/posthog/api/test/test_llm_prompt.py
@@ -23,19 +23,22 @@ from posthog.api.llm_prompt_serializers import (
validate_prompt_label_name_value,
)
from posthog.api.services.llm_prompt import MAX_PROMPT_VERSION
-from posthog.auth import DelegatedOAuthAccessTokenAuthentication
+from posthog.auth import DelegatedOAuthAccessTokenAuthentication, ProjectSecretAPIKeyAuthentication
from posthog.jwt import PosthogJwtAudience, encode_jwt
from posthog.models import PersonalAPIKey
from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.models.oauth import OAuthAccessToken, OAuthApplication
+from posthog.models.organization import OrganizationMembership
from posthog.models.utils import generate_random_token_personal, hash_key_value
from posthog.rate_limit import (
BurstRateThrottle,
+ LLMPromptFetchProjectSecretApiKeyTeamRateThrottle,
LLMPromptFetchRateThrottle,
LLMPromptPublishBurstRateThrottle,
SustainedRateThrottle,
)
from posthog.storage.llm_prompt_cache import get_prompt_by_name_from_cache
+from posthog.test.api_keys import create_project_secret_api_key
from posthog.test.authentication_checks import covers_authentication
from products.ai_observability.backend.models.llm_prompt import LLMPrompt, LLMPromptDependency, LLMPromptLabel
@@ -931,6 +934,65 @@ class TestLLMPromptAPI(APIBaseTest):
assert response.status_code == status.HTTP_200_OK
+ @parameterized.expand(
+ [
+ ("get_by_name", "name/test-prompt/"),
+ ("list", ""),
+ ]
+ )
+ @covers_authentication(ProjectSecretAPIKeyAuthentication)
+ def test_project_secret_key_created_in_settings_fetches_prompts(self, _name: str, path: str) -> None:
+ self.create_prompt_version(name="test-prompt", prompt="Prompt text")
+ self.organization_membership.level = OrganizationMembership.Level.ADMIN
+ self.organization_membership.save()
+ created = self.client.post(
+ f"/api/projects/{self.team.id}/project_secret_api_keys",
+ {"label": "sdk", "scopes": ["llm_prompt:read"]},
+ )
+ assert created.status_code == status.HTTP_201_CREATED, created.content
+ self.client.logout()
+
+ response = self.client.get(
+ f"/api/environments/{self.team.id}/llm_prompts/{path}",
+ headers={"authorization": f"Bearer {created.json()['value']}"},
+ )
+
+ assert response.status_code == status.HTTP_200_OK, response.content
+ body = response.json()
+ prompt = body["results"][0] if "results" in body else body
+ assert prompt["name"] == "test-prompt"
+ assert prompt["prompt"] == "Prompt text"
+
+ @parameterized.expand(
+ [
+ ("resolve_by_name", ["llm_prompt:read"], True, "get", "resolve/name/test-prompt/"),
+ ("publish", ["llm_prompt:read"], True, "patch", "name/test-prompt/"),
+ ("create", ["llm_prompt:read"], True, "post", ""),
+ ("archive", ["llm_prompt:read"], True, "post", "name/test-prompt/archive/"),
+ ("key_without_the_scope", ["feature_flag:read"], True, "get", "name/test-prompt/"),
+ ("key_of_another_project", ["llm_prompt:read"], False, "get", "name/test-prompt/"),
+ ]
+ )
+ def test_project_secret_key_is_denied_outside_sdk_reads(
+ self, _name: str, scopes: list[str], key_is_for_this_project: bool, method: str, path: str
+ ) -> None:
+ self.create_prompt_version(name="test-prompt")
+ key_team = (
+ self.team if key_is_for_this_project else self.create_team_with_organization(organization=self.organization)
+ )
+ _, token = create_project_secret_api_key(key_team, scopes=scopes)
+ self.client.logout()
+
+ response = getattr(self.client, method)(
+ f"/api/environments/{self.team.id}/llm_prompts/{path}",
+ data={"name": "new-prompt", "prompt": "v2", "base_version": 1} if method != "get" else None,
+ format="json",
+ headers={"authorization": f"Bearer {token}"},
+ )
+
+ assert response.status_code == status.HTTP_403_FORBIDDEN, response.content
+ assert LLMPrompt.objects.filter(team=self.team).count() == 1
+
@override_settings(TEST=False)
@patch("posthog.api.llm_prompt.capture_internal")
def test_fetch_prompt_by_name_emits_version_metadata(self, mock_capture_internal):
@@ -1074,8 +1136,9 @@ class TestLLMPromptAPI(APIBaseTest):
throttles = view.get_throttles()
- assert len(throttles) == 1
+ assert len(throttles) == 2
assert isinstance(throttles[0], LLMPromptFetchRateThrottle)
+ assert isinstance(throttles[1], LLMPromptFetchProjectSecretApiKeyTeamRateThrottle)
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 c4b86ba687fc..5581fb1c8211 100644
--- a/posthog/rate_limit.py
+++ b/posthog/rate_limit.py
@@ -1116,15 +1116,22 @@ class LLMPromptPublishBurstRateThrottle(PersonalApiKeyOrUserRateThrottle):
rate = "30/minute"
-class LLMPromptFetchRateThrottle(PersonalApiKeyRateThrottle):
+class LLMPromptFetchRateThrottle(PersonalOrProjectSecretApiKeyRateThrottle):
# 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.
+ # feature_flag_remote_config throttle. Project secret keys get the same budget per key.
scope = "llm_prompt_fetch"
rate = "600/minute"
+class LLMPromptFetchProjectSecretApiKeyTeamRateThrottle(ProjectSecretApiKeyTeamRateThrottle):
+ # One budget for all of a project's secret keys together, the same size as the per-key
+ # budget, so creating more keys does not raise a project's total.
+ scope = "llm_prompt_fetch_psak_team"
+ rate = "600/minute"
+
+
class EventValuesBurstThrottle(PersonalApiKeyRateThrottle):
scope = "event_values_burst"
rate = "60/minute"
diff --git a/posthog/scopes.py b/posthog/scopes.py
index f7044cae4817..643a170b2b50 100644
--- a/posthog/scopes.py
+++ b/posthog/scopes.py
@@ -291,6 +291,9 @@ PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION: list[tuple[APIScopeObject, APIS
("messaging_preference", "write"),
("transactional_email", "read"),
("transactional_email", "write"),
+ # SDK prompt fetches by name and by list, so a service does not need a credential tied
+ # to one person's account. Publishing, labels and archiving stay closed to these keys.
+ ("llm_prompt", "read"),
]
# Server-side scope assignment string-set constants (see RFC: server-side scope
diff --git a/posthog/test/test_rate_limit.py b/posthog/test/test_rate_limit.py
index 1bc1026a6df3..c2dba37c7656 100644
--- a/posthog/test/test_rate_limit.py
+++ b/posthog/test/test_rate_limit.py
@@ -913,10 +913,16 @@ class TestPersonalOrProjectSecretApiKeyRateThrottle(SimpleTestCase):
auth.project_secret_api_key = Mock(id=key_id)
return Mock(successful_authenticator=auth)
- def test_psak_requests_do_not_bypass_throttle(self):
+ @parameterized.expand(
+ [
+ ("base", _PSAKThrottleForTest),
+ ("llm_prompt_fetch", rate_limit.LLMPromptFetchRateThrottle),
+ ]
+ )
+ def test_psak_requests_do_not_bypass_throttle(self, _name, throttle_class):
# PSAK carries no personal API key; the throttle must not let it through the
# personal_api_key_only gate that PersonalApiKeyRateThrottle uses.
- throttle = _PSAKThrottleForTest()
+ throttle = throttle_class()
request = self._psak_request()
with patch.object(throttle, "_allow_request_internal", return_value=True) as spy:
throttle.allow_request(request, Mock())