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): Activity-log prompt label changes (#71952)

Juraj Majerikf22bf81ee40b
diff --git a/posthog/api/services/llm_prompt.py b/posthog/api/services/llm_prompt.py
index bc8d6ceabd34..ccdfd729fcb3 100644
--- a/posthog/api/services/llm_prompt.py
+++ b/posthog/api/services/llm_prompt.py
@@ -273,7 +273,9 @@ def archive_prompt(team: Team, prompt_name: str) -> list[int]:
is_latest=False,
)
- LLMPromptLabel.objects.filter(team=team, prompt_name=prompt_name).delete()
+ # Instance-level deletes so ModelActivityMixin logs each label removal.
+ for label in LLMPromptLabel.objects.filter(team=team, prompt_name=prompt_name):
+ label.delete()
def invalidate_caches_on_commit() -> None:
invalidate_prompt_latest_cache(team.id, prompt_name)
diff --git a/posthog/api/test/test_llm_prompt.py b/posthog/api/test/test_llm_prompt.py
index e7334f01c464..9bdf7694bccb 100644
--- a/posthog/api/test/test_llm_prompt.py
+++ b/posthog/api/test/test_llm_prompt.py
@@ -17,6 +17,7 @@ from posthog.api.llm_prompt_serializers import (
validate_prompt_label_name_value,
)
from posthog.api.services.llm_prompt import MAX_PROMPT_VERSION
+from posthog.models.activity_logging.activity_log import ActivityLog
from posthog.rate_limit import BurstRateThrottle, LLMPromptPublishBurstRateThrottle, SustainedRateThrottle
from products.ai_observability.backend.models.llm_prompt import LLMPrompt, LLMPromptLabel
@@ -1148,6 +1149,33 @@ class TestLLMPromptLabelsAPI(APIBaseTest):
self.client.post(f"/api/environments/{self.team.id}/llm_prompts/name/my-prompt/archive/")
assert self._fetch_by_label("my-prompt", "production").status_code == status.HTTP_404_NOT_FOUND
+ def test_label_changes_are_activity_logged_with_version_movement(self):
+ self.create_prompt_version(version=1, is_latest=False)
+ self.create_prompt_version(version=2)
+
+ self._set_label("my-prompt", "production", 1)
+ self._set_label("my-prompt", "production", 2)
+ self._set_label("my-prompt", "production", 2) # no-op move must not log
+ self.client.delete(self._label_url("my-prompt", "production"))
+
+ entries = list(ActivityLog.objects.filter(team_id=self.team.id, scope="LLMPromptLabel").order_by("created_at"))
+ assert [entry.activity for entry in entries] == ["created", "updated", "deleted"]
+ move_detail = entries[1].detail
+ assert move_detail is not None
+ assert move_detail["name"] == "my-prompt: production"
+ assert move_detail["changes"][0]["before"] == 1
+ assert move_detail["changes"][0]["after"] == 2
+ assert entries[1].user is not None and entries[1].user.id == self.user.id
+
+ def test_archive_logs_label_deletion(self):
+ self.create_prompt_version(version=1)
+ self._set_label("my-prompt", "production", 1)
+
+ self.client.post(f"/api/environments/{self.team.id}/llm_prompts/name/my-prompt/archive/")
+
+ deletions = ActivityLog.objects.filter(team_id=self.team.id, scope="LLMPromptLabel", activity="deleted")
+ assert deletions.count() == 1
+
def test_label_writes_forbidden_for_read_only_personal_api_key(self):
self.create_prompt_version(version=1)
api_key = self.create_personal_api_key_with_scopes(["llm_prompt:read"])
diff --git a/posthog/models/activity_logging/activity_log.py b/posthog/models/activity_logging/activity_log.py
index fb82bb9eea26..48661e132697 100644
--- a/posthog/models/activity_logging/activity_log.py
+++ b/posthog/models/activity_logging/activity_log.py
@@ -80,6 +80,7 @@ ActivityScope = Literal[
"ExternalDataSource",
"ExternalDataSchema",
"Evaluation",
+ "LLMPromptLabel",
"LLMTrace",
"AIGatewayCredit",
"WebAnalyticsFilterPreset",
diff --git a/posthog/test/setup_receivers_baseline.txt b/posthog/test/setup_receivers_baseline.txt
index 9f0b672443cf..9bef458e9e80 100644
--- a/posthog/test/setup_receivers_baseline.txt
+++ b/posthog/test/setup_receivers_baseline.txt
@@ -19,6 +19,7 @@ model_activity:posthog.models.activity_logging.signal_handlers.handle_tagged_ite
model_activity:posthog.models.activity_logging.signal_handlers.log_user_change_activity
model_activity:products.actions.backend.activity_logging.handle_action_change
model_activity:products.ai_observability.backend.activity_logging.handle_evaluation_change
+model_activity:products.ai_observability.backend.activity_logging.handle_llm_prompt_label_change
model_activity:products.alerts.backend.activity_logging.handle_alert_configuration_change
model_activity:products.alerts.backend.activity_logging.handle_alert_subscription_change
model_activity:products.alerts.backend.activity_logging.handle_threshold_change
diff --git a/products/ai_observability/backend/activity_logging.py b/products/ai_observability/backend/activity_logging.py
index 8b333bdec14d..e1ab9928b7e7 100644
--- a/products/ai_observability/backend/activity_logging.py
+++ b/products/ai_observability/backend/activity_logging.py
@@ -1,10 +1,11 @@
import copy
from typing import Any
-from posthog.models.activity_logging.activity_log import Detail, changes_between, log_activity
+from posthog.models.activity_logging.activity_log import Change, ChangeAction, Detail, changes_between, log_activity
from posthog.models.signals import model_activity_signal, mutable_receiver
from products.ai_observability.backend.models.evaluations import Evaluation
+from products.ai_observability.backend.models.llm_prompt import LLMPromptLabel
# Lives here, not in api/evaluations.py, so it can wire at AppConfig.ready() without dragging the
# evaluations viewset (which pulls scipy / google.genai / the ai_observability Temporal worker) onto
@@ -59,3 +60,45 @@ def handle_evaluation_change(
name=after_update.name,
),
)
+
+
+@mutable_receiver(model_activity_signal, sender=LLMPromptLabel)
+def handle_llm_prompt_label_change(
+ sender, scope, before_update, after_update, activity, user, was_impersonated=False, **kwargs
+):
+ # A label move changes which prompt version production traffic receives, with no code
+ # deploy — the from/to version numbers are the audit trail, so build the change by hand
+ # instead of diffing the raw FK (which would only show opaque version-row UUIDs).
+ instance = after_update if after_update is not None else before_update
+ if instance is None:
+ return
+
+ before_version = before_update.prompt.version if before_update is not None else None
+ after_version = after_update.prompt.version if after_update is not None else None
+ action: ChangeAction = "changed"
+ if activity == "created":
+ action = "created"
+ elif activity == "deleted":
+ action = "deleted"
+
+ log_activity(
+ organization_id=instance.team.organization_id,
+ team_id=instance.team_id,
+ user=user,
+ was_impersonated=was_impersonated,
+ item_id=instance.id,
+ scope="LLMPromptLabel",
+ activity=activity,
+ detail=Detail(
+ name=f"{instance.prompt_name}: {instance.name}",
+ changes=[
+ Change(
+ type="LLMPromptLabel",
+ action=action,
+ field="version",
+ before=before_version,
+ after=after_version,
+ )
+ ],
+ ),
+ )
diff --git a/products/ai_observability/backend/models/llm_prompt.py b/products/ai_observability/backend/models/llm_prompt.py
index 44cffaa5e57c..b05102e6bdaf 100644
--- a/products/ai_observability/backend/models/llm_prompt.py
+++ b/products/ai_observability/backend/models/llm_prompt.py
@@ -8,6 +8,7 @@ from django.dispatch import receiver
from django.utils import timezone
from posthog.exceptions_capture import capture_exception
+from posthog.models.activity_logging.model_activity import ModelActivityMixin
from posthog.models.utils import UUIDModel
from products.ai_observability.backend.markdown_outline import get_markdown_outline
@@ -74,7 +75,7 @@ class LLMPrompt(UUIDModel):
deleted = models.BooleanField(default=False)
-class LLMPromptLabel(UUIDModel):
+class LLMPromptLabel(ModelActivityMixin, UUIDModel):
"""A movable pointer from a name (e.g. "production") to exactly one version of a prompt.
Version rows are immutable; releasing a version means pointing a label at it and
@@ -96,6 +97,8 @@ class LLMPromptLabel(UUIDModel):
]
db_table = "posthog_llmpromptlabel"
+ activity_logging_on_delete = True
+
name = models.CharField(max_length=128)
# Prompts have no parent entity — version rows are grouped by name, so the label
# keys the prompt family by name and points at one version row via the FK below.
diff --git a/products/platform_features/frontend/generated/api.schemas.ts b/products/platform_features/frontend/generated/api.schemas.ts
index bf5c535ca959..86adfab75cbb 100644
--- a/products/platform_features/frontend/generated/api.schemas.ts
+++ b/products/platform_features/frontend/generated/api.schemas.ts
@@ -982,6 +982,7 @@ export type ActivityLogListParams = {
* * `ExternalDataSource` - ExternalDataSource
* * `ExternalDataSchema` - ExternalDataSchema
* * `Evaluation` - Evaluation
+ * * `LLMPromptLabel` - LLMPromptLabel
* * `LLMTrace` - LLMTrace
* * `AIGatewayCredit` - AIGatewayCredit
* * `WebAnalyticsFilterPreset` - WebAnalyticsFilterPreset
@@ -1069,6 +1070,7 @@ export const ActivityLogListScope = {
ExternalDataSource: 'ExternalDataSource',
ExternalDataSchema: 'ExternalDataSchema',
Evaluation: 'Evaluation',
+ LLMPromptLabel: 'LLMPromptLabel',
LLMTrace: 'LLMTrace',
AIGatewayCredit: 'AIGatewayCredit',
WebAnalyticsFilterPreset: 'WebAnalyticsFilterPreset',
@@ -1143,6 +1145,7 @@ export const ActivityLogListScope = {
* * `ExternalDataSource` - ExternalDataSource
* * `ExternalDataSchema` - ExternalDataSchema
* * `Evaluation` - Evaluation
+ * * `LLMPromptLabel` - LLMPromptLabel
* * `LLMTrace` - LLMTrace
* * `AIGatewayCredit` - AIGatewayCredit
* * `WebAnalyticsFilterPreset` - WebAnalyticsFilterPreset
@@ -1218,6 +1221,7 @@ export const ActivityLogListScopesItem = {
ExternalDataSource: 'ExternalDataSource',
ExternalDataSchema: 'ExternalDataSchema',
Evaluation: 'Evaluation',
+ LLMPromptLabel: 'LLMPromptLabel',
LLMTrace: 'LLMTrace',
AIGatewayCredit: 'AIGatewayCredit',
WebAnalyticsFilterPreset: 'WebAnalyticsFilterPreset',
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index 2de38fe33db6..afd497efd921 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -71267,6 +71267,7 @@ export namespace Schemas {
* * `ExternalDataSource` - ExternalDataSource
* * `ExternalDataSchema` - ExternalDataSchema
* * `Evaluation` - Evaluation
+ * * `LLMPromptLabel` - LLMPromptLabel
* * `LLMTrace` - LLMTrace
* * `AIGatewayCredit` - AIGatewayCredit
* * `WebAnalyticsFilterPreset` - WebAnalyticsFilterPreset
@@ -71355,6 +71356,7 @@ export namespace Schemas {
ExternalDataSource: 'ExternalDataSource',
ExternalDataSchema: 'ExternalDataSchema',
Evaluation: 'Evaluation',
+ LLMPromptLabel: 'LLMPromptLabel',
LLMTrace: 'LLMTrace',
AIGatewayCredit: 'AIGatewayCredit',
WebAnalyticsFilterPreset: 'WebAnalyticsFilterPreset',
@@ -71429,6 +71431,7 @@ export namespace Schemas {
* * `ExternalDataSource` - ExternalDataSource
* * `ExternalDataSchema` - ExternalDataSchema
* * `Evaluation` - Evaluation
+ * * `LLMPromptLabel` - LLMPromptLabel
* * `LLMTrace` - LLMTrace
* * `AIGatewayCredit` - AIGatewayCredit
* * `WebAnalyticsFilterPreset` - WebAnalyticsFilterPreset
@@ -71505,6 +71508,7 @@ export namespace Schemas {
ExternalDataSource: 'ExternalDataSource',
ExternalDataSchema: 'ExternalDataSchema',
Evaluation: 'Evaluation',
+ LLMPromptLabel: 'LLMPromptLabel',
LLMTrace: 'LLMTrace',
AIGatewayCredit: 'AIGatewayCredit',
WebAnalyticsFilterPreset: 'WebAnalyticsFilterPreset',