{
  "__inputs": [
    {
      "name": "DS_PROMETHEUS",
      "label": "Prometheus",
      "type": "datasource",
      "pluginId": "prometheus"
    }
  ],
  "title": "Pi coding agent",
  "uid": "pi-prometheus",
  "tags": ["pi", "pi-prometheus"],
  "timezone": "browser",
  "time": { "from": "now-3h", "to": "now" },
  "refresh": "15s",
  "templating": {
    "list": [
      {
        "name": "session",
        "type": "query",
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "query": "label_values(pi_session_info, session_id)",
        "includeAll": true,
        "multi": true,
        "refresh": 2
      }
    ]
  },
  "panels": [
    {
      "id": 1,
      "type": "stat",
      "title": "Sessions online",
      "gridPos": { "x": 0, "y": 0, "w": 4, "h": 4 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "count(up{job=\"pi\"} == 1)", "refId": "A" }]
    },
    {
      "id": 2,
      "type": "stat",
      "title": "Total cost (USD)",
      "gridPos": { "x": 4, "y": 0, "w": 4, "h": 4 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "sum(pi_cost_usd_total{session_id=~\"$session\"})", "refId": "A" }],
      "fieldConfig": { "defaults": { "unit": "currencyUSD", "decimals": 4 }, "overrides": [] }
    },
    {
      "id": 3,
      "type": "gauge",
      "title": "Context usage",
      "gridPos": { "x": 8, "y": 0, "w": 8, "h": 4 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "pi_context_tokens{session_id=~\"$session\"} / pi_context_window_tokens{session_id=~\"$session\"}",
          "legendFormat": "{{session_id}}",
          "refId": "A"
        }
      ],
      "fieldConfig": {
        "defaults": {
          "unit": "percentunit",
          "max": 1,
          "thresholds": {
            "mode": "absolute",
            "steps": [
              { "color": "green", "value": null },
              { "color": "yellow", "value": 0.7 },
              { "color": "red", "value": 0.9 }
            ]
          }
        },
        "overrides": []
      }
    },
    {
      "id": 4,
      "type": "stat",
      "title": "Turns",
      "gridPos": { "x": 16, "y": 0, "w": 4, "h": 4 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [{ "expr": "sum(pi_turns_total{session_id=~\"$session\"})", "refId": "A" }]
    },
    {
      "id": 5,
      "type": "timeseries",
      "title": "Token rate by type (tokens/min)",
      "gridPos": { "x": 0, "y": 4, "w": 12, "h": 8 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "sum by (type) (rate(pi_tokens_total{session_id=~\"$session\"}[5m])) * 60",
          "legendFormat": "{{type}}",
          "refId": "A"
        }
      ]
    },
    {
      "id": 6,
      "type": "timeseries",
      "title": "Tool calls by tool (calls/min)",
      "gridPos": { "x": 12, "y": 4, "w": 12, "h": 8 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "sum by (tool) (rate(pi_tool_calls_total{session_id=~\"$session\"}[5m])) * 60",
          "legendFormat": "{{tool}}",
          "refId": "A"
        }
      ]
    },
    {
      "id": 7,
      "type": "timeseries",
      "title": "Turn duration p50 / p90 (s)",
      "gridPos": { "x": 0, "y": 12, "w": 12, "h": 8 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "histogram_quantile(0.5, sum by (le) (rate(pi_turn_duration_seconds_bucket{session_id=~\"$session\"}[15m])))",
          "legendFormat": "p50",
          "refId": "A"
        },
        {
          "expr": "histogram_quantile(0.9, sum by (le) (rate(pi_turn_duration_seconds_bucket{session_id=~\"$session\"}[15m])))",
          "legendFormat": "p90",
          "refId": "B"
        }
      ],
      "fieldConfig": { "defaults": { "unit": "s" }, "overrides": [] }
    },
    {
      "id": 8,
      "type": "timeseries",
      "title": "Tool errors (errors/min)",
      "gridPos": { "x": 12, "y": 12, "w": 12, "h": 8 },
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "targets": [
        {
          "expr": "sum by (tool) (rate(pi_tool_errors_total{session_id=~\"$session\"}[5m])) * 60",
          "legendFormat": "{{tool}}",
          "refId": "A"
        }
      ]
    }
  ],
  "schemaVersion": 39,
  "version": 1
}
