: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
chore(experiments/mcp): guide agents to reuse shared metrics (#65751)
Marcel Poelker5a81f0bc4a13
diff --git a/products/experiments/skills/configuring-experiment-analytics/SKILL.md b/products/experiments/skills/configuring-experiment-analytics/SKILL.mdindex f3d651ab0139..ab03ddacef8c 100644--- a/products/experiments/skills/configuring-experiment-analytics/SKILL.md+++ b/products/experiments/skills/configuring-experiment-analytics/SKILL.md@@ -53,13 +53,54 @@ skill to resolve it to a concrete ID before proceeding.## Metrics-Metrics are added via `experiment-update` after creation. The `metrics` array **replaces** the entire list, so always get the current experiment first via `experiment-get` to preserve existing metrics.+A metric reaches an experiment one of two ways, both via `experiment-update`:-### Step 1: Discover available events (REQUIRED — always do this first)+- **Inline metric** — defined directly on the experiment. Sent in the `metrics` array, which+ **replaces** the entire inline list, so always get the current experiment first via `experiment-get`+ to preserve existing metrics.+- **Shared (saved) metric** — a reusable metric object that can be attached to many experiments.+ Attached by ID via `saved_metrics_ids` (this list also **replaces** the experiment's existing+ saved-metric links, so resend the full set — see Step 1).-Before suggesting or configuring ANY metric, you MUST call `read-data-schema` to discover+**Prefer reusing a shared metric over duplicating it inline.** Build a new inline metric only when+no suitable shared metric already exists.++### Step 1: Check for an existing shared metric (REQUIRED — always do this first)++Before building any new inline metric, you MUST call `experiment-saved-metrics-list` to see what+reusable metrics already exist in the project. Do NOT skip this step — duplicating a metric that is+already set up as a shared metric fragments measurement and is exactly what we want to avoid.++**Workflow:**++1. Call `experiment-saved-metrics-list` (pass `search` to resolve by name, e.g.+ `{ "search": "checkout conversion" }`) and scan the results (`id`, `name`, `description`, `query`)+ for one that matches the user's intent. Results are paginated — if you're browsing without a+ `search` term and the project has many, page through with `limit`/`offset` before concluding+ nothing matches.+2. **If a shared metric clearly matches** — confirm the match with the user by name/description,+ then attach it instead of building a new one:+ - Call `experiment-get` to read the experiment's current `saved_metrics`.+ - Call `experiment-update` with `saved_metrics_ids` set to the full desired set — it **replaces**+ existing links, so include the already-attached ones plus the new entry. Each entry has shape+ `{ "id": <saved-metric id>, "metadata": { "type": "primary" } }` — set `type` to `"primary"` or+ `"secondary"`. `metadata` is optional and defaults to primary.+ - **Watch the id when rebuilding the set:** each item in the `saved_metrics` you just read has a+ top-level `id` (the _link_ id) AND a `saved_metric` field (the _metric_ id). `saved_metrics_ids`+ wants the **`saved_metric`** value, not the link `id` — sending the link `id` attaches the wrong+ metric or fails validation.+ - You do not need to discover events (Step 2) — the shared metric already encodes them.+3. **If the list is empty or nothing matches** — fall through to Step 2 and build an inline metric.++When a new inline metric you're about to build is likely to be reused across experiments, offer to+create it as a shared metric instead, via `experiment-saved-metrics-create`, then attach it as above.++### Step 2: Discover available events (REQUIRED before building an inline metric)++Before suggesting or building any new inline metric, you MUST call `read-data-schema` to discoverwhat events actually exist in the project. Do NOT skip this step. Do NOT suggest event namesbased on what you think the project might track — only use events you have confirmed exist.+(Attaching an existing shared metric from Step 1 does not need this — it already encodes its events.)This applies even when:@@ -90,7 +131,7 @@ RIGHT: *calls read-data-schema* → "Here are the events in your project`order_confirmed`. Which of these represents a successful checkout?"```-### Step 2: Choose metric type+### Step 3: Choose metric typeThere are four metric types. Each has `kind: "ExperimentMetric"`:@@ -130,7 +171,7 @@ The start occurrence never counts as its own completion (only a distinct later eSee `references/metric-configuration.md` for the full rendered `ExperimentMetric` schema (all four metric types, with required fields per type) plus WRONG/RIGHT JSON pairs for the failure modes that come up most often (ratio with `is_set` filter instead of `math: "sum"` + `math_property`; retention without `retention_window_start` / `start_handling`). Read it before assembling a ratio or retention payload — the required fields are authoritative.-### Step 3: Primary vs secondary+### Step 4: Primary vs secondary- **Primary metrics** — the main success criteria for the experiment. These drive the ship/end decision.- **Secondary metrics** — additional measurements for context. Useful for guardrail metrics (e.g., ensuring a conversion improvement doesn't increase error rates).diff --git a/products/experiments/skills/configuring-experiment-analytics/references/metric-configuration.md.j2 b/products/experiments/skills/configuring-experiment-analytics/references/metric-configuration.md.j2index 976584fd1b6a..43b8e264d0d4 100644--- a/products/experiments/skills/configuring-experiment-analytics/references/metric-configuration.md.j2+++ b/products/experiments/skills/configuring-experiment-analytics/references/metric-configuration.md.j2@@ -241,8 +241,14 @@ required on every retention metric — the schema is the source of truth.## Adding metrics to an experiment+A metric reaches an experiment via one of two independent `experiment-update`+fields. Attaching a shared metric does **not** touch the inline `metrics` array,+and vice versa.++### Inline metric — `metrics`+Call `experiment-update` with the full `metrics` array. This **replaces** the-entire list.+entire inline list.To add a metric without losing existing ones:@@ -250,6 +256,40 @@ To add a metric without losing existing ones:2. Append the new metric to the existing array3. Call `experiment-update` with the combined array+### Shared (saved) metric — `saved_metrics_ids`++Reuse a metric that already exists in the project instead of duplicating it+inline. Resolve the id with `experiment-saved-metrics-list`, then attach it:++1. Call `experiment-saved-metrics-list` to find the metric and its `id` (pass a+ `search` term to resolve by name; results are paginated, so use `limit`/`offset`+ when browsing a large project)+2. Call `experiment-get` to read the experiment's current `saved_metrics`+3. Call `experiment-update` with `saved_metrics_ids` — this **replaces** all+ existing saved-metric links, so send the full desired set:++```json+{+ "saved_metrics_ids": [+ { "id": 42, "metadata": { "type": "primary" } },+ { "id": 57, "metadata": { "type": "secondary" } }+ ]+}+```++The `id` here is the **saved-metric id**. Note the read/write asymmetry when you+rebuild the set from `experiment-get`: each entry in the returned `saved_metrics`+exposes a top-level `id` (the *link* row) and a separate `saved_metric` (the+*metric* id). Map each existing entry's **`saved_metric`** into the `id` you+resend — sending the link `id` attaches the wrong metric or fails validation.++`metadata` is optional and defaults to `primary`. Pass an empty array to detach+all shared metrics.++To promote a one-off inline metric into a reusable shared metric, call+`experiment-saved-metrics-create` with the same `query` (the `ExperimentMetric`+object), then attach it via `saved_metrics_ids` as above.+## Property filtersAny `EventsNode` can include property filters to narrow *which* events count.diff --git a/products/experiments/skills/creating-experiments/SKILL.md b/products/experiments/skills/creating-experiments/SKILL.mdindex 5e55ec244ea1..4fa21e3693c9 100644--- a/products/experiments/skills/creating-experiments/SKILL.md+++ b/products/experiments/skills/creating-experiments/SKILL.md@@ -45,6 +45,8 @@ If the user doesn't mention rollout specifics, use defaults: 50/50 control/test,### Step 3: How to measure impact?This is about analytics and metrics. **Load the `configuring-experiment-analytics` skill** for guidance.+That skill's first step checks for an existing **shared metric** to reuse before building a new one —+don't duplicate a metric the project already has set up.**Do NOT configure metrics on creation.** Metrics are not passed to `experiment-create` — they are addedafterwards via `experiment-update`. This keeps the creation call lightweight.