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.

chore(experiments): delete the dev-only apply_llm_metric command (#106617)

Anders3e5a6234a90e
diff --git a/posthog/management/commands/apply_llm_metric.py b/posthog/management/commands/apply_llm_metric.py
deleted file mode 100644
index d4ef77459641..000000000000
--- a/posthog/management/commands/apply_llm_metric.py
+++ /dev/null
@@ -1,61 +0,0 @@
-from django.conf import settings
-from django.core.management.base import BaseCommand, CommandError
-
-from posthog.models import User
-
-from products.experiments.backend.llm_metric_templates import TEMPLATE_NAMES, apply_metric_to_experiment
-from products.experiments.backend.models.experiment import Experiment
-
-
-class Command(BaseCommand):
- help = "Apply an LLM metric template to an existing experiment (dev-only)."
-
- def add_arguments(self, parser):
- parser.add_argument("--experiment-id", type=int, required=True, help="Experiment row id")
- parser.add_argument(
- "--template",
- type=str,
- choices=TEMPLATE_NAMES,
- default="cost",
- help=f"Template name. One of: {', '.join(TEMPLATE_NAMES)}",
- )
- parser.add_argument(
- "--prompt-name",
- type=str,
- required=True,
- help="Value to use in the $ai_prompt_name filter",
- )
- parser.add_argument(
- "--replace",
- action="store_true",
- help="Replace all existing primary metrics instead of appending",
- )
-
- def handle(self, *args, **options):
- if not settings.DEBUG:
- raise CommandError("This command should only be run in development! DEBUG must be True.")
-
- try:
- experiment = Experiment.objects.get(id=options["experiment_id"])
- except Experiment.DoesNotExist:
- raise CommandError(f"Experiment id={options['experiment_id']} not found")
-
- user = User.objects.filter(last_login__isnull=False).order_by("-last_login").first()
- if user is None:
- user = User.objects.first()
- if user is None:
- raise CommandError("No User found to attribute the change to. Create one and try again.")
-
- experiment = apply_metric_to_experiment(
- experiment,
- options["template"],
- options["prompt_name"],
- user=user,
- replace=options["replace"],
- )
-
- self.stdout.write(
- f"Applied template '{options['template']}' (prompt_name='{options['prompt_name']}') "
- f"to experiment id={experiment.id}. "
- f"Metric count after apply: {len(experiment.metrics or [])}"
- )
diff --git a/products/experiments/backend/llm_metric_templates.py b/products/experiments/backend/llm_metric_templates.py
index 883a6c9f61b0..77341297f564 100644
--- a/products/experiments/backend/llm_metric_templates.py
+++ b/products/experiments/backend/llm_metric_templates.py
@@ -1,5 +1,4 @@
from collections.abc import Callable
-from typing import TYPE_CHECKING
from posthog.schema import (
EventPropertyFilter,
@@ -12,12 +11,6 @@ from posthog.schema import (
PropertyOperator,
)
-from products.experiments.backend.experiment_service import ExperimentService
-from products.experiments.backend.models.experiment import Experiment
-
-if TYPE_CHECKING:
- from posthog.models import User
-
LLMMetric = ExperimentMeanMetric | ExperimentRatioMetric
@@ -120,25 +113,3 @@ def build_template(name: str, prompt_name: str) -> LLMMetric:
def list_templates() -> list[dict[str, str]]:
return [{"key": name, **_TEMPLATE_METADATA[name]} for name in _TEMPLATES]
-
-
-def apply_metric_to_experiment(
- experiment: Experiment,
- template_name: str,
- prompt_name: str,
- *,
- user: "User",
- replace: bool = False,
-) -> Experiment:
- new_metric = build_template(template_name, prompt_name).model_dump(exclude_none=True)
-
- if replace:
- next_metrics = [new_metric]
- else:
- next_metrics = [*(experiment.metrics or []), new_metric]
-
- service = ExperimentService(team=experiment.team, user=user)
- # The templates hardcode known LLM event names, so the typo-guard validation in
- # update_experiment adds no value but rejects the common case of attaching a
- # metric before any matching event has been ingested.
- return service.update_experiment(experiment, {"metrics": next_metrics}, allow_unknown_events=True)