{
  "__inputs": [
    {
      "name": "DS_PROMETHEUS",
      "label": "Prometheus",
      "description": "Prometheus datasource for Agentic Gateway metrics",
      "type": "datasource",
      "pluginId": "prometheus",
      "pluginName": "Prometheus"
    }
  ],
  "__requires": [
    { "type": "grafana", "id": "grafana", "name": "Grafana", "version": "9.0.0" },
    { "type": "datasource", "id": "prometheus", "name": "Prometheus", "version": "1.0.0" },
    { "type": "panel", "id": "timeseries", "name": "Time series", "version": "" },
    { "type": "panel", "id": "histogram", "name": "Histogram", "version": "" },
    { "type": "panel", "id": "stat", "name": "Stat", "version": "" }
  ],
  "annotations": {
    "list": [
      {
        "builtIn": 1,
        "datasource": "-- Grafana --",
        "enable": true,
        "hide": true,
        "iconColor": "rgba(0, 211, 255, 1)",
        "name": "Annotations & Alerts",
        "type": "dashboard"
      }
    ]
  },
  "description": "Agentic Gateway — Hierarchical Budget Engine (Spec 003) observability dashboard",
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 1,
  "id": null,
  "links": [],
  "panels": [
    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": { "lineWidth": 2, "fillOpacity": 10 },
          "unit": "s"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 0 },
      "id": 1,
      "options": {
        "legend": { "calcs": ["mean", "max", "p99"], "displayMode": "table", "placement": "bottom" },
        "tooltip": { "mode": "single" }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "histogram_quantile(0.99, sum(rate(budget_evaluate_duration_seconds_bucket[5m])) by (le))",
          "legendFormat": "p99 evaluate latency",
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "histogram_quantile(0.50, sum(rate(budget_evaluate_duration_seconds_bucket[5m])) by (le))",
          "legendFormat": "p50 evaluate latency",
          "refId": "B"
        }
      ],
      "title": "evaluate_duration_p99 — Cascade Budget Evaluate Latency",
      "type": "timeseries",
      "description": "P99 evaluate latency must stay below 1ms. Alert fires if > 1ms for 5 min."
    },
    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": { "fillOpacity": 30 },
          "unit": "short"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 0 },
      "id": 2,
      "options": {
        "fillOpacity": 30,
        "gradientMode": "none",
        "bucketSize": "auto"
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum(rate(budget_cascade_depth_bucket[5m])) by (le)",
          "legendFormat": "depth {{le}}",
          "refId": "A",
          "format": "heatmap"
        }
      ],
      "title": "cascade_depth_histogram — Cascade Depth Distribution",
      "type": "histogram",
      "description": "Distribution of cascade resolution depths per request. Depth > 6 is unusual."
    },
    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "thresholds": {
            "mode": "percentage",
            "steps": [
              { "color": "green", "value": null },
              { "color": "yellow", "value": 2 },
              { "color": "red", "value": 5 }
            ]
          },
          "unit": "percentunit",
          "mappings": []
        }
      },
      "gridPos": { "h": 6, "w": 6, "x": 0, "y": 8 },
      "id": 3,
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "reduceOptions": { "calcs": ["lastNotNull"] }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "rate(budget_sub_limit_rejected_total[5m]) / rate(budget_evaluate_total[5m])",
          "legendFormat": "Sub-limit hit rate",
          "refId": "A"
        }
      ],
      "title": "sub_limit_hit_rate — Sub-Limit Rejection Rate",
      "type": "stat",
      "description": "Rate of requests rejected by sub-limit constraints. Alert fires if > 5% on same sub-limit for 10 min."
    },
    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green", "value": null },
              { "color": "yellow", "value": 1 },
              { "color": "red", "value": 10 }
            ]
          },
          "unit": "short"
        }
      },
      "gridPos": { "h": 6, "w": 6, "x": 6, "y": 8 },
      "id": 4,
      "options": {
        "colorMode": "background",
        "graphMode": "area",
        "reduceOptions": { "calcs": ["sum"] }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "increase(budget_mode_switch_total[1h])",
          "legendFormat": "Mode switches (1h)",
          "refId": "A"
        }
      ],
      "title": "mode_switch_count — Pool↔PerUser Mode Switches",
      "type": "stat",
      "description": "Count of Pool↔PerUser mode-switch operations. High frequency indicates unstable budget configuration."
    },
    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green", "value": null },
              { "color": "red", "value": 1 }
            ]
          },
          "unit": "short"
        }
      },
      "gridPos": { "h": 6, "w": 6, "x": 12, "y": 8 },
      "id": 5,
      "options": {
        "colorMode": "background",
        "graphMode": "none",
        "reduceOptions": { "calcs": ["sum"] }
      },
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "increase(budget_migration_drift_warnings_total[24h])",
          "legendFormat": "Drift warnings (24h)",
          "refId": "A"
        }
      ],
      "title": "migration_drift_warnings — Counter Drift Warnings",
      "type": "stat",
      "description": "Migration v4 counter drift warnings. Any value > 0 requires investigation. See runbook: docs/runbooks/budgets-rollout.md"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "custom": { "lineWidth": 2 },
          "unit": "reqps"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 14 },
      "id": 6,
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "rate(budget_evaluate_total[5m])",
          "legendFormat": "evaluate/s",
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "rate(budget_settle_total[5m])",
          "legendFormat": "settle/s",
          "refId": "B"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "rate(budget_rollback_total[5m])",
          "legendFormat": "rollback/s",
          "refId": "C"
        }
      ],
      "title": "Budget Engine Throughput",
      "type": "timeseries"
    },
    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "palette-classic" },
          "unit": "short"
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 14 },
      "id": 7,
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "rate(budget_cap_exceeded_total[5m])",
          "legendFormat": "429 cap exceeded/s",
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "rate(budget_rollback_total[5m]) / rate(budget_evaluate_total[5m])",
          "legendFormat": "rollback rate",
          "refId": "B"
        }
      ],
      "title": "Cap Violations & Rollback Rate",
      "type": "timeseries"
    }
  ],
  "refresh": "30s",
  "schemaVersion": 36,
  "style": "dark",
  "tags": ["agentic-gateway", "budgets", "spec-003"],
  "templating": {
    "list": [
      {
        "current": {},
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "definition": "label_values(budget_evaluate_total, tenant_id)",
        "hide": 0,
        "includeAll": true,
        "multi": true,
        "name": "tenant_id",
        "options": [],
        "query": "label_values(budget_evaluate_total, tenant_id)",
        "refresh": 1,
        "sort": 1,
        "type": "query"
      }
    ]
  },
  "time": { "from": "now-1h", "to": "now" },
  "timepicker": {},
  "timezone": "browser",
  "title": "Agentic Gateway — Hierarchical Budgets (Spec 003)",
  "uid": "agentic-gateway-budgets-003",
  "version": 1,

  "_alert_rules_comments": {
    "note": "The following alert rules should be configured in Grafana Alerting or PrometheusRule CRDs.",
    "rules": [
      {
        "name": "BudgetEvaluateP99TooHigh",
        "condition": "histogram_quantile(0.99, sum(rate(budget_evaluate_duration_seconds_bucket[5m])) by (le)) > 0.001",
        "for": "5m",
        "severity": "page",
        "summary": "Budget evaluate P99 latency > 1ms for 5 minutes",
        "runbook": "docs/runbooks/budgets-rollout.md#evaluate-latency"
      },
      {
        "name": "BudgetMigrationDriftWarning",
        "condition": "increase(budget_migration_drift_warnings_total[5m]) / increase(budget_evaluate_total[5m]) > 0.005",
        "for": "5m",
        "severity": "ticket",
        "summary": "Migration v4 counter drift warnings > 0.5% of requests",
        "runbook": "docs/runbooks/budgets-rollout.md#counter-drift"
      },
      {
        "name": "SubLimitHighRejectionRate",
        "condition": "rate(budget_sub_limit_rejected_total[5m]) / rate(budget_evaluate_total[5m]) > 0.05",
        "for": "10m",
        "severity": "notify-tenant-admin",
        "summary": "Sub-limit rejection rate > 5% — tenant admin notification",
        "runbook": "docs/runbooks/budgets-rollout.md#sub-limit-rejection"
      }
    ]
  }
}
