: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
fix(experiments): use the team default minimum detectable effect on create (#103201)
Marcel Poelker9602633e9703
diff --git a/products/experiments/backend/experiment_service.py b/products/experiments/backend/experiment_service.pyindex cdbb4e618b1c..4d48ecc49960 100644--- a/products/experiments/backend/experiment_service.py+++ b/products/experiments/backend/experiment_service.py@@ -1345,6 +1345,17 @@ class ExperimentService:if only_count_matured_users is None:only_count_matured_users = team_config.default_only_count_matured_users+ # A duplicate or a copy keeps the source's value, including "none", so it stays like its source.+ if (+ creation_mode == "new"+ and running_time_calculation.get("minimum_detectable_effect") is None+ and team_config.default_minimum_detectable_effect is not None+ ):+ running_time_calculation = {+ **running_time_calculation,+ "minimum_detectable_effect": team_config.default_minimum_detectable_effect,+ }+stats_method = "bayesian" if stats_config is None else stats_config.get("method", "bayesian")if metrics is not None:for metric in metrics:diff --git a/products/experiments/backend/max_tools.py b/products/experiments/backend/max_tools.pyindex 71cc94426602..9c65473bd2c9 100644--- a/products/experiments/backend/max_tools.py+++ b/products/experiments/backend/max_tools.py@@ -146,9 +146,6 @@ class CreateExperimentTool(MaxTool):feature_flag_key=feature_flag_key,description=description or "",type=type,- running_time_calculation={- "minimum_detectable_effect": 30,- },event_source=EventSource.POSTHOG_AI,)diff --git a/products/experiments/backend/test/test_experiment_service.py b/products/experiments/backend/test/test_experiment_service.pyindex d8d46b7e6f6f..dbfbb685fbb3 100644--- a/products/experiments/backend/test/test_experiment_service.py+++ b/products/experiments/backend/test/test_experiment_service.py@@ -286,6 +286,73 @@ class TestExperimentService(APIBaseTest):assert experiment.only_count_matured_users is False+ # ------------------------------------------------------------------+ # Minimum detectable effect defaults+ # ------------------------------------------------------------------++ def _set_default_minimum_detectable_effect(self, value: int | None) -> None:+ config = get_or_create_team_extension(self.team, TeamExperimentsConfig)+ config.default_minimum_detectable_effect = value+ config.save()++ def test_minimum_detectable_effect_defaults_from_team(self):+ self._set_default_minimum_detectable_effect(10)++ self._create_flag(key="mde-default")+ service = self._service()++ experiment = service.create_experiment(name="MDE Default", feature_flag_key="mde-default")++ assert experiment.running_time_calculation == {"minimum_detectable_effect": 10}++ def test_minimum_detectable_effect_keeps_provided_value(self):+ self._set_default_minimum_detectable_effect(10)++ self._create_flag(key="mde-provided")+ service = self._service()++ experiment = service.create_experiment(+ name="MDE Provided",+ feature_flag_key="mde-provided",+ running_time_calculation={"minimum_detectable_effect": 5},+ )++ assert experiment.running_time_calculation == {"minimum_detectable_effect": 5}++ def test_minimum_detectable_effect_not_stored_without_team_default(self):+ self._set_default_minimum_detectable_effect(None)++ self._create_flag(key="mde-unset")+ service = self._service()++ experiment = service.create_experiment(name="MDE Unset", feature_flag_key="mde-unset")++ assert "minimum_detectable_effect" not in (experiment.running_time_calculation or {})++ @parameterized.expand(+ [+ ("duplicate",),+ ("copy_to_project",),+ ]+ )+ def test_minimum_detectable_effect_does_not_apply_to_clone(self, clone_mode: str):+ self._create_flag(key=f"mde-source-{clone_mode}")+ service = self._service()+ source = service.create_experiment(name="MDE Source", feature_flag_key=f"mde-source-{clone_mode}")++ self._set_default_minimum_detectable_effect(10)++ if clone_mode == "duplicate":+ clone = service.duplicate_experiment(source)+ else:+ target_team = Team.objects.create(organization=self.organization, name="MDE Target Team")+ target_config = get_or_create_team_extension(target_team, TeamExperimentsConfig)+ target_config.default_minimum_detectable_effect = 10+ target_config.save()+ clone = service.copy_experiment_to_project(source, target_team)++ assert "minimum_detectable_effect" not in (clone.running_time_calculation or {})+# ------------------------------------------------------------------# Metric fingerprints# ------------------------------------------------------------------diff --git a/products/experiments/backend/test/test_max_tools.py b/products/experiments/backend/test/test_max_tools.pyindex e688eb76e90e..f4e885e562cc 100644--- a/products/experiments/backend/test/test_max_tools.py+++ b/products/experiments/backend/test/test_max_tools.py@@ -1,6 +1,8 @@from posthog.test.base import APIBaseTestfrom unittest.mock import ANY, AsyncMock, MagicMock, patch+from asgiref.sync import sync_to_async+from posthog.schema import (MaxExperimentMetricResult,MaxExperimentVariantResultBayesian,@@ -8,10 +10,12 @@ from posthog.schema import ()from posthog.event_usage import EventSource+from posthog.models.team.extensions import get_or_create_team_extensionfrom products.experiments.backend.experiment_summary_data_service import ExperimentSummaryDatafrom products.experiments.backend.max_tools import CreateExperimentTool, ExperimentSummaryToolfrom products.experiments.backend.models.experiment import Experiment+from products.experiments.backend.models.team_experiments_config import TeamExperimentsConfigfrom products.feature_flags.backend.models.feature_flag import FeatureFlagfrom ee.hogai.tool_errors import MaxToolAccessDeniedError@@ -218,10 +222,28 @@ class TestCreateExperimentTool(APIBaseTest):{"key": "test", "name": "Test", "rollout_percentage": 50},]assert "minimum_detectable_effect" not in (experiment.parameters or {})- assert experiment.running_time_calculation == {"minimum_detectable_effect": 30}+ assert experiment.running_time_calculation == {}assert experiment.metrics == []assert experiment.metrics_secondary == []+ async def test_create_experiment_uses_team_default_minimum_detectable_effect(self):+ config = await sync_to_async(get_or_create_team_extension)(self.team, TeamExperimentsConfig)+ config.default_minimum_detectable_effect = 10+ await config.asave()++ await self._create_multivariate_flag(key="mde-test", name="MDE Test Flag")+ tool = self._create_tool()++ result, _artifact = await tool._arun_impl(+ name="MDE Test",+ feature_flag_key="mde-test",+ )++ assert "Successfully created" in result++ experiment = await Experiment.objects.aget(name="MDE Test", team=self.team)+ assert experiment.running_time_calculation == {"minimum_detectable_effect": 10}+async def test_create_experiment_missing_flag(self):tool = self._create_tool()diff --git a/products/experiments/mcp/tools.yaml b/products/experiments/mcp/tools.yamlindex e73e428ce604..a7cabbfaf803 100644--- a/products/experiments/mcp/tools.yaml+++ b/products/experiments/mcp/tools.yaml@@ -286,7 +286,8 @@ tools:Persist a running-time / sample-size plan onto the experiment (the planning target shown in theexperiment's running-time panel). Object with optional keys: minimum_detectable_effect (percentage,e.g. 20 for a 20% lift), recommended_sample_size (total across all variants),- recommended_running_time (days), and exposure_estimate_config.+ recommended_running_time (days), and exposure_estimate_config. When minimum_detectable_effect is+ omitted, the project's default MDE is stored instead, if the project has one.ui_app: experimentresponse:include:diff --git a/services/mcp/src/tools/generated/experiments.ts b/services/mcp/src/tools/generated/experiments.tsindex 63a4c7efe999..79bb15b14f13 100644--- a/services/mcp/src/tools/generated/experiments.ts+++ b/services/mcp/src/tools/generated/experiments.ts@@ -240,7 +240,7 @@ const ExperimentCreateSchema = () => {'Variant split, rollout scope, payloads, and experience continuity for the auto-created feature flag, in the flag\'s own filters shape. This is the canonical input for flag config. If the user mentions a specific percentage, load the configuring-experiment-rollout skill and clarify before setting these values. Set filters.multivariate.variants (each with key and rollout_percentage; percentages must sum to 100) to customize the variant split. Set filters.groups to a single group [{"properties": [], "rollout_percentage": N}] (0-100) to control the overall fraction of users entering the experiment. Default: 50/50 control/test, 100% rollout. Omit this parameter entirely when feature_flag_key refers to a pre-existing flag: the experiment links to that flag as-is and explicit config is rejected. No specific variant key is required. The analysis baseline defaults to the variant keyed `control` (lowercase) when present, else the first variant; override with stats_config.baseline_variant_key. Convention: when the user describes variants as "A/B", "old/new", "original/redesign", or any other natural-language pair without naming explicit keys, key the baseline `control` and keep their wording in the variant `name`. When the user asks for specific keys, use them as-is and put the baseline first.'),running_time_calculation: ExperimentsCreateBody.shape['running_time_calculation'].describe(- "Persist a running-time / sample-size plan onto the experiment (the planning target shown in the experiment's running-time panel). Object with optional keys: minimum_detectable_effect (percentage, e.g. 20 for a 20% lift), recommended_sample_size (total across all variants), recommended_running_time (days), and exposure_estimate_config."+ "Persist a running-time / sample-size plan onto the experiment (the planning target shown in the experiment's running-time panel). Object with optional keys: minimum_detectable_effect (percentage, e.g. 20 for a 20% lift), recommended_sample_size (total across all variants), recommended_running_time (days), and exposure_estimate_config. When minimum_detectable_effect is omitted, the project's default MDE is stored instead, if the project has one."),})}diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-create.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-create.jsonindex 2a722800eb00..f9f1318d9270 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-create.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/experiment-create.json@@ -5938,7 +5938,7 @@"type": "null"}],- "description": "Persist a running-time / sample-size plan onto the experiment (the planning target shown in the experiment's running-time panel). Object with optional keys: minimum_detectable_effect (percentage, e.g. 20 for a 20% lift), recommended_sample_size (total across all variants), recommended_running_time (days), and exposure_estimate_config."+ "description": "Persist a running-time / sample-size plan onto the experiment (the planning target shown in the experiment's running-time panel). Object with optional keys: minimum_detectable_effect (percentage, e.g. 20 for a 20% lift), recommended_sample_size (total across all variants), recommended_running_time (days), and exposure_estimate_config. When minimum_detectable_effect is omitted, the project's default MDE is stored instead, if the project has one."},"stats_config": {},"tags": {