: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
feat(insights): teach MCP agents to use aggregationAxisFormat (#58561)
Paul D'Ambraff5d329c1e3d
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex a046d2a1ae9b..9ad99938aeaa 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -38037,12 +38037,15 @@"properties": {"aggregationAxisFormat": {"$ref": "#/definitions/AggregationAxisFormat",- "default": "numeric"+ "default": "numeric",+ "description": "Y-axis value formatter. Picks a human-friendly unit per value at render time without changing the underlying series values.\n\n- `numeric` (default): raw numbers, e.g. `1,234`.\n- `duration`: values are in seconds; rendered as friendly units per value (`45s`, `2m 12s`, `1h 4m`). Use this whenever the series is in seconds (latency, session length, time-to-event) instead of dividing in `formula` to force minutes or hours.\n- `duration_ms`: values are in milliseconds; rendered as friendly units (`850ms`, `1.5s`, `1m 4s`).\n- `percentage`: values are already in the 0-100 range; appends `%`.\n- `percentage_scaled`: values are a 0-1 ratio; multiplied and rendered as `%`.\n- `currency`: values are in the project's base currency (set in project settings, defaults to USD); rendered with that currency symbol. For values pinned to a specific currency regardless of project base (e.g. `$ai_total_cost_usd` is always USD), use `aggregationAxisPrefix` instead.\n- `short`: compact notation for large counts (`1.2K`, `3.4M`)."},"aggregationAxisPostfix": {+ "description": "Literal suffix applied to every value (e.g. ` req`). Reserve for units that `aggregationAxisFormat` cannot express. Do not use ` mins`, ` s`, ` ms`, `%` etc. — pick the matching `aggregationAxisFormat` instead so the underlying values stay numerically correct for breakdowns, formulas, and alerts. Include any leading space yourself.","type": "string"},"aggregationAxisPrefix": {+ "description": "Literal prefix applied to every value (e.g. `$`). Use to pin a unit or currency symbol that does not depend on `aggregationAxisFormat` — for example, when values are denominated in a fixed currency regardless of the project's base currency. Include any trailing space yourself.","type": "string"},"breakdown_histogram_bin_count": {@@ -38052,6 +38055,7 @@"type": "number"},"decimalPlaces": {+ "description": "Maximum number of decimal places shown. 1 or 2 is usually right for percentages and currency.","type": "number"},"detailedResultsAggregationType": {diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex 5741f0fcd00b..bed638515f9d 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -1379,10 +1379,29 @@ export type TrendsFilter = {/** @default false */showAlertThresholdLines?: booleanbreakdown_histogram_bin_count?: TrendsFilterLegacy['breakdown_histogram_bin_count'] // TODO: fully move into BreakdownFilter- /** @default numeric */+ // The aggregation-axis description below is duplicated, intentionally and divergently, on+ // `AssistantTrendsFilter` in schema-assistant-queries.ts. The assistant copy is deliberately+ // more cautious ("do not use unless absolutely sure") because Max picks formats without explicit+ // query context; this copy is more prescriptive because MCP callers know the units up front.+ // Keep both in sync when the format set changes, but treat their wording as audience-specific.+ /**+ * Y-axis value formatter. Picks a human-friendly unit per value at render time without changing the underlying series values.+ *+ * - `numeric` (default): raw numbers, e.g. `1,234`.+ * - `duration`: values are in seconds; rendered as friendly units per value (`45s`, `2m 12s`, `1h 4m`). Use this whenever the series is in seconds (latency, session length, time-to-event) instead of dividing in `formula` to force minutes or hours.+ * - `duration_ms`: values are in milliseconds; rendered as friendly units (`850ms`, `1.5s`, `1m 4s`).+ * - `percentage`: values are already in the 0-100 range; appends `%`.+ * - `percentage_scaled`: values are a 0-1 ratio; multiplied and rendered as `%`.+ * - `currency`: values are in the project's base currency (set in project settings, defaults to USD); rendered with that currency symbol. For values pinned to a specific currency regardless of project base (e.g. `$ai_total_cost_usd` is always USD), use `aggregationAxisPrefix` instead.+ * - `short`: compact notation for large counts (`1.2K`, `3.4M`).+ * @default numeric+ */aggregationAxisFormat?: TrendsFilterLegacy['aggregation_axis_format']+ /** Literal prefix applied to every value (e.g. `$`). Use to pin a unit or currency symbol that does not depend on `aggregationAxisFormat` — for example, when values are denominated in a fixed currency regardless of the project's base currency. Include any trailing space yourself. */aggregationAxisPrefix?: TrendsFilterLegacy['aggregation_axis_prefix']+ /** Literal suffix applied to every value (e.g. ` req`). Reserve for units that `aggregationAxisFormat` cannot express. Do not use ` mins`, ` s`, ` ms`, `%` etc. — pick the matching `aggregationAxisFormat` instead so the underlying values stay numerically correct for breakdowns, formulas, and alerts. Include any leading space yourself. */aggregationAxisPostfix?: TrendsFilterLegacy['aggregation_axis_postfix']+ /** Maximum number of decimal places shown. 1 or 2 is usually right for percentages and currency. */decimalPlaces?: TrendsFilterLegacy['decimal_places']minDecimalPlaces?: TrendsFilterLegacy['min_decimal_places']/** @default false */diff --git a/posthog/schema.py b/posthog/schema.pyindex f00ac7b066f8..bd9903aaba1f 100644--- a/posthog/schema.py+++ b/posthog/schema.py@@ -8879,12 +8879,51 @@ class TrendsFilter(BaseModel):model_config = ConfigDict(extra="forbid",)- aggregationAxisFormat: AggregationAxisFormat | None = AggregationAxisFormat.NUMERIC- aggregationAxisPostfix: str | None = None- aggregationAxisPrefix: str | None = None+ aggregationAxisFormat: AggregationAxisFormat | None = Field(+ default=AggregationAxisFormat.NUMERIC,+ description=(+ "Y-axis value formatter. Picks a human-friendly unit per value at render"+ " time without changing the underlying series values.\n\n- `numeric`"+ " (default): raw numbers, e.g. `1,234`.\n- `duration`: values are in"+ " seconds; rendered as friendly units per value (`45s`, `2m 12s`, `1h 4m`)."+ " Use this whenever the series is in seconds (latency, session length,"+ " time-to-event) instead of dividing in `formula` to force minutes or"+ " hours.\n- `duration_ms`: values are in milliseconds; rendered as friendly"+ " units (`850ms`, `1.5s`, `1m 4s`).\n- `percentage`: values are already in"+ " the 0-100 range; appends `%`.\n- `percentage_scaled`: values are a 0-1"+ " ratio; multiplied and rendered as `%`.\n- `currency`: values are in the"+ " project's base currency (set in project settings, defaults to USD);"+ " rendered with that currency symbol. For values pinned to a specific"+ " currency regardless of project base (e.g. `$ai_total_cost_usd` is always"+ " USD), use `aggregationAxisPrefix` instead.\n- `short`: compact notation"+ " for large counts (`1.2K`, `3.4M`)."+ ),+ )+ aggregationAxisPostfix: str | None = Field(+ default=None,+ description=(+ "Literal suffix applied to every value (e.g. ` req`). Reserve for units"+ " that `aggregationAxisFormat` cannot express. Do not use ` mins`, ` s`, `"+ " ms`, `%` etc. — pick the matching `aggregationAxisFormat` instead so the"+ " underlying values stay numerically correct for breakdowns, formulas, and"+ " alerts. Include any leading space yourself."+ ),+ )+ aggregationAxisPrefix: str | None = Field(+ default=None,+ description=(+ "Literal prefix applied to every value (e.g. `$`). Use to pin a unit or"+ " currency symbol that does not depend on `aggregationAxisFormat` — for"+ " example, when values are denominated in a fixed currency regardless of"+ " the project's base currency. Include any trailing space yourself."+ ),+ )breakdown_histogram_bin_count: float | None = NoneconfidenceLevel: float | None = None- decimalPlaces: float | None = None+ decimalPlaces: float | None = Field(+ default=None,+ description=("Maximum number of decimal places shown. 1 or 2 is usually right for percentages and currency."),+ )detailedResultsAggregationType: DetailedResultsAggregationType | None = Field(default=None, description="detailed results table")diff --git a/products/product_analytics/frontend/generated/api.schemas.ts b/products/product_analytics/frontend/generated/api.schemas.tsindex 96e47046adb0..78d46d680b3a 100644--- a/products/product_analytics/frontend/generated/api.schemas.ts+++ b/products/product_analytics/frontend/generated/api.schemas.ts@@ -1483,11 +1483,23 @@ export type TrendsFilterApiResultCustomizations =| nullexport interface TrendsFilterApi {+ /** Y-axis value formatter. Picks a human-friendly unit per value at render time without changing the underlying series values.++ - `numeric` (default): raw numbers, e.g. `1,234`.+ - `duration`: values are in seconds; rendered as friendly units per value (`45s`, `2m 12s`, `1h 4m`). Use this whenever the series is in seconds (latency, session length, time-to-event) instead of dividing in `formula` to force minutes or hours.+ - `duration_ms`: values are in milliseconds; rendered as friendly units (`850ms`, `1.5s`, `1m 4s`).+ - `percentage`: values are already in the 0-100 range; appends `%`.+ - `percentage_scaled`: values are a 0-1 ratio; multiplied and rendered as `%`.+ - `currency`: values are in the project's base currency (set in project settings, defaults to USD); rendered with that currency symbol. For values pinned to a specific currency regardless of project base (e.g. `$ai_total_cost_usd` is always USD), use `aggregationAxisPrefix` instead.+ - `short`: compact notation for large counts (`1.2K`, `3.4M`). */aggregationAxisFormat?: AggregationAxisFormatApi | null+ /** Literal suffix applied to every value (e.g. ` req`). Reserve for units that `aggregationAxisFormat` cannot express. Do not use ` mins`, ` s`, ` ms`, `%` etc. — pick the matching `aggregationAxisFormat` instead so the underlying values stay numerically correct for breakdowns, formulas, and alerts. Include any leading space yourself. */aggregationAxisPostfix?: string | null+ /** Literal prefix applied to every value (e.g. `$`). Use to pin a unit or currency symbol that does not depend on `aggregationAxisFormat` — for example, when values are denominated in a fixed currency regardless of the project's base currency. Include any trailing space yourself. */aggregationAxisPrefix?: string | nullbreakdown_histogram_bin_count?: number | nullconfidenceLevel?: number | null+ /** Maximum number of decimal places shown. 1 or 2 is usually right for percentages and currency. */decimalPlaces?: number | null/** detailed results table */detailedResultsAggregationType?: DetailedResultsAggregationTypeApi | nulldiff --git a/products/product_analytics/skills/formatting-insight-axes/SKILL.md b/products/product_analytics/skills/formatting-insight-axes/SKILL.mdnew file mode 100644index 000000000000..ce7b54f02c77--- /dev/null+++ b/products/product_analytics/skills/formatting-insight-axes/SKILL.md@@ -0,0 +1,180 @@+---+name: formatting-insight-axes+description: >+ Pick the right y-axis unit when creating or updating a TrendsQuery insight+ via `posthog:insight-create` or `posthog:insight-update`. Use when the agent+ is about to add a `formula` purely to convert units (e.g. dividing seconds+ by 60 to display minutes), when a `math_property` is a duration, currency,+ ratio, or large count, or whenever the user mentions "format the y-axis",+ "duration", "seconds", "minutes", "hours", "milliseconds", "ms",+ "percentage", "currency", "decimals", "axis label", or "axis unit" in the+ context of a graph insight.+---++# Formatting insight axes++PostHog renders TrendsQuery insights with a built-in axis formatter. Use it+instead of contorting `formula` or `aggregationAxisPostfix` to fake units.++## The anti-pattern++If you are reaching for any of these, stop and pick a format below first:++- `formula: "A / 60"` with `aggregationAxisPostfix: " mins"` — manual seconds -> minutes+- `formula: "A / 1000"` with `aggregationAxisPostfix: " s"` — manual ms -> seconds+- `formula: "A * 100"` with `aggregationAxisPostfix: "%"` — manual ratio -> percent+- `aggregationAxisPostfix: "ms"` / `"s"` / `"min"` / `"hr"` on raw values++These freeze the unit at one scale. The built-in formatter picks a friendly+unit per value (1.5s, 2m 12s, 1h 4m) and keeps the underlying series numerically+correct for further math, breakdowns, and alerts.++## Available formats++Set `trendsFilter.aggregationAxisFormat` on the TrendsQuery:++| Value | Use when the series is... | Renders as |+| ------------------- | ---------------------------------------- | --------------------------- |+| `numeric` (default) | a plain count | `1,234` |+| `duration` | **seconds** (any scale) | `45s`, `2m 12s`, `1h 4m` |+| `duration_ms` | **milliseconds** | `850ms`, `1.5s`, `1m 4s` |+| `percentage` | already 0-100 | `47.3%` |+| `percentage_scaled` | a ratio 0-1 | `47.3%` |+| `currency` | money in the **project's base currency** | `$1,234.56` (or local code) |+| `short` | large counts you want compacted | `1.2K`, `3.4M` |++Companion fields on `trendsFilter`:++- `aggregationAxisPrefix` — literal prefix (e.g. `"$"`) when you need a symbol+ pinned to a specific currency or unit, regardless of project settings+- `aggregationAxisPostfix` — literal suffix; reserve for genuine units the+ format can't express (e.g. `" req"`, `" events"`), never for `"mins"` / `"s"`+- `decimalPlaces` — cap decimals (1 or 2 is usually right for currency / ratios)++### Currency — pick `format` or `prefix` carefully++`aggregationAxisFormat: "currency"` renders with the **project's base currency**+(set in project settings, defaults to USD). Use it when the underlying values+are in that same currency — e.g. revenue events that PostHog auto-converts to+the project's base currency.++If the values are pinned to a specific currency regardless of project (e.g.+`$ai_total_cost_usd` is always USD, even on a EUR-base project), use+`aggregationAxisPrefix: "$"` + `decimalPlaces: 2` so the symbol matches the+data. Using `format: "currency"` here would render USD values with `€` on a+EUR project.++## When the series is in seconds++If the series is in seconds (latency, session length, time-to-first-event,+processing time, page load, etc.), silently default to+`aggregationAxisFormat: "duration"`. Do not stop to ask — the formatter is+non-destructive (the underlying values stay in seconds either way, only the+labels change), so picking it is always at least as good as raw seconds.++Only confirm with the user when they have **explicitly** named a fixed unit+they want pinned ("show this in minutes", "graph the average in hours"):++> "I can pin the y-axis to minutes by dividing the series by 60, or use+> PostHog's `duration` formatter which auto-picks seconds / minutes / hours+> per value — `90s` renders as `1m 30s` and `5400s` as `1h 30m`. Which would+> you prefer?"++In one-shot MCP contexts where no user is in the loop, just pick `duration`+and move on.++## Examples++### Latency — duration in milliseconds++```json+{+ "kind": "TrendsQuery",+ "series": [+ {+ "kind": "EventsNode",+ "event": "$pageview",+ "math": "p95",+ "math_property": "$performance_page_loaded"+ }+ ],+ "trendsFilter": {+ "aggregationAxisFormat": "duration_ms"+ }+}+```++### Average session length — duration in seconds++```json+{+ "kind": "TrendsQuery",+ "series": [+ {+ "kind": "EventsNode",+ "event": "$pageleave",+ "math": "avg",+ "math_property": "$session_duration"+ }+ ],+ "trendsFilter": {+ "aggregationAxisFormat": "duration"+ }+}+```++### Revenue — currency in the project's base currency++```json+{+ "trendsFilter": {+ "aggregationAxisFormat": "currency",+ "decimalPlaces": 2+ }+}+```++### Fixed-currency value (e.g. LLM cost in USD) — pin the symbol++```json+{+ "trendsFilter": {+ "aggregationAxisPrefix": "$",+ "decimalPlaces": 2+ }+}+```++### Conversion rate — percentage from a 0-1 formula++```json+{+ "kind": "TrendsQuery",+ "series": [+ {+ "kind": "EventsNode",+ "event": "checkout_completed",+ "math": "dau"+ },+ {+ "kind": "EventsNode",+ "event": "checkout_started",+ "math": "dau"+ }+ ],+ "trendsFilter": {+ "formula": "A / B",+ "aggregationAxisFormat": "percentage_scaled",+ "decimalPlaces": 1+ }+}+```++## Updating an existing insight++If you are updating an insight and notice it already uses the+`formula`/`postfix` anti-pattern, fix it in the same `posthog:insight-update`+call — drop the divide-by-N, drop the `aggregationAxisPostfix`, and set the+matching `aggregationAxisFormat`. The series values stay the same, only the+labels change. Do not go scanning unrelated insights for this pattern —+fix only the ones you are already touching.diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex a50d7e7dd194..64685bfba26a 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -1928,11 +1928,23 @@ export namespace Schemas {export type TrendsFilterResultCustomizations = {[key: string]: ResultCustomizationByValue} | {[key: string]: ResultCustomizationByPosition} | null;export interface TrendsFilter {+ /** Y-axis value formatter. Picks a human-friendly unit per value at render time without changing the underlying series values.++ - `numeric` (default): raw numbers, e.g. `1,234`.+ - `duration`: values are in seconds; rendered as friendly units per value (`45s`, `2m 12s`, `1h 4m`). Use this whenever the series is in seconds (latency, session length, time-to-event) instead of dividing in `formula` to force minutes or hours.+ - `duration_ms`: values are in milliseconds; rendered as friendly units (`850ms`, `1.5s`, `1m 4s`).+ - `percentage`: values are already in the 0-100 range; appends `%`.+ - `percentage_scaled`: values are a 0-1 ratio; multiplied and rendered as `%`.+ - `currency`: values are in the project's base currency (set in project settings, defaults to USD); rendered with that currency symbol. For values pinned to a specific currency regardless of project base (e.g. `$ai_total_cost_usd` is always USD), use `aggregationAxisPrefix` instead.+ - `short`: compact notation for large counts (`1.2K`, `3.4M`). */aggregationAxisFormat?: AggregationAxisFormat | null;+ /** Literal suffix applied to every value (e.g. ` req`). Reserve for units that `aggregationAxisFormat` cannot express. Do not use ` mins`, ` s`, ` ms`, `%` etc. — pick the matching `aggregationAxisFormat` instead so the underlying values stay numerically correct for breakdowns, formulas, and alerts. Include any leading space yourself. */aggregationAxisPostfix?: string | null;+ /** Literal prefix applied to every value (e.g. `$`). Use to pin a unit or currency symbol that does not depend on `aggregationAxisFormat` — for example, when values are denominated in a fixed currency regardless of the project's base currency. Include any trailing space yourself. */aggregationAxisPrefix?: string | null;breakdown_histogram_bin_count?: number | null;confidenceLevel?: number | null;+ /** Maximum number of decimal places shown. 1 or 2 is usually right for percentages and currency. */decimalPlaces?: number | null;/** detailed results table */detailedResultsAggregationType?: DetailedResultsAggregationType | null;