{
  "__inputs": [
    {
      "name": "DS_PROMETHEUS",
      "label": "Prometheus",
      "description": "Prometheus datasource for RactoGateway metrics",
      "type": "datasource",
      "pluginId": "prometheus",
      "pluginName": "Prometheus"
    }
  ],
  "__requires": [
    { "type": "grafana",    "id": "grafana",    "name": "Grafana",    "version": "10.0.0" },
    { "type": "datasource", "id": "prometheus", "name": "Prometheus", "version": "1.0.0" },
    { "type": "panel",      "id": "stat",       "name": "Stat",       "version": "" },
    { "type": "panel",      "id": "timeseries", "name": "Time series","version": "" },
    { "type": "panel",      "id": "gauge",      "name": "Gauge",      "version": "" },
    { "type": "panel",      "id": "table",      "name": "Table",      "version": "" },
    { "type": "panel",      "id": "piechart",   "name": "Pie chart",  "version": "" }
  ],
  "annotations": { "list": [] },
  "description": "RactoGateway LLM observability dashboard — latency, tokens, cost, cache, and errors across OpenAI / Google / Anthropic.",
  "editable": true,
  "fiscalYearStartMonth": 0,
  "graphTooltip": 1,
  "id": null,
  "links": [],
  "liveNow": false,
  "panels": [

    {
      "collapsed": false,
      "gridPos": { "h": 1, "w": 24, "x": 0, "y": 0 },
      "id": 100,
      "title": "Overview",
      "type": "row"
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "thresholds": { "mode": "absolute", "steps": [
            { "color": "green", "value": null },
            { "color": "yellow", "value": 100 },
            { "color": "red",    "value": 1000 }
          ]}
        }
      },
      "gridPos": { "h": 4, "w": 4, "x": 0, "y": 1 },
      "id": 1,
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "orientation": "auto",
                   "textMode": "auto", "colorMode": "background" },
      "title": "Total Requests",
      "type": "stat",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum(increase(ractogateway_requests_total[$__range]))",
        "legendFormat": "requests",
        "refId": "A"
      }]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "unit": "s",
          "thresholds": { "mode": "absolute", "steps": [
            { "color": "green",  "value": null },
            { "color": "yellow", "value": 2 },
            { "color": "red",    "value": 10 }
          ]}
        }
      },
      "gridPos": { "h": 4, "w": 4, "x": 4, "y": 1 },
      "id": 2,
      "options": { "reduceOptions": { "calcs": ["mean"] }, "orientation": "auto",
                   "textMode": "auto", "colorMode": "background" },
      "title": "Avg Latency",
      "type": "stat",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum(rate(ractogateway_request_duration_seconds_sum[$__rate_interval])) / sum(rate(ractogateway_request_duration_seconds_count[$__rate_interval]))",
        "legendFormat": "latency",
        "refId": "A"
      }]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "unit": "currencyUSD",
          "decimals": 4,
          "thresholds": { "mode": "absolute", "steps": [
            { "color": "green",  "value": null },
            { "color": "yellow", "value": 1 },
            { "color": "red",    "value": 10 }
          ]}
        }
      },
      "gridPos": { "h": 4, "w": 4, "x": 8, "y": 1 },
      "id": 3,
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "orientation": "auto",
                   "textMode": "auto", "colorMode": "background" },
      "title": "Total Cost (USD)",
      "type": "stat",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum(increase(ractogateway_cost_usd_total[$__range]))",
        "legendFormat": "cost",
        "refId": "A"
      }]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "unit": "percentunit",
          "min": 0, "max": 1,
          "thresholds": { "mode": "absolute", "steps": [
            { "color": "red",    "value": null },
            { "color": "yellow", "value": 0.2 },
            { "color": "green",  "value": 0.5 }
          ]}
        }
      },
      "gridPos": { "h": 4, "w": 4, "x": 12, "y": 1 },
      "id": 4,
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "orientation": "auto",
                   "textMode": "auto", "colorMode": "background" },
      "title": "Cache Hit Rate",
      "type": "stat",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum(increase(ractogateway_cache_hits_total[$__range])) / clamp_min(sum(increase(ractogateway_cache_hits_total[$__range])) + sum(increase(ractogateway_cache_misses_total[$__range])), 1)",
        "legendFormat": "hit rate",
        "refId": "A"
      }]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": {
          "color": { "mode": "thresholds" },
          "unit": "short",
          "thresholds": { "mode": "absolute", "steps": [
            { "color": "green",  "value": null },
            { "color": "yellow", "value": 1 },
            { "color": "red",    "value": 10 }
          ]}
        }
      },
      "gridPos": { "h": 4, "w": 4, "x": 16, "y": 1 },
      "id": 5,
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "orientation": "auto",
                   "textMode": "auto", "colorMode": "background" },
      "title": "Errors",
      "type": "stat",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum(increase(ractogateway_requests_total{status=\"error\"}[$__range]))",
        "legendFormat": "errors",
        "refId": "A"
      }]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": { "defaults": { "color": { "mode": "thresholds" },
        "unit": "short",
        "thresholds": { "mode": "absolute", "steps": [{ "color": "blue", "value": null }]}
      }},
      "gridPos": { "h": 4, "w": 4, "x": 20, "y": 1 },
      "id": 6,
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "orientation": "auto",
                   "textMode": "auto", "colorMode": "background" },
      "title": "Tool Calls",
      "type": "stat",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum(increase(ractogateway_tool_calls_total[$__range]))",
        "legendFormat": "tool calls",
        "refId": "A"
      }]
    },

    {
      "collapsed": false,
      "gridPos": { "h": 1, "w": 24, "x": 0, "y": 5 },
      "id": 101,
      "title": "Latency",
      "type": "row"
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "s",
          "custom": { "lineWidth": 2, "fillOpacity": 8 }
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 6 },
      "id": 10,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Request Latency by Provider (p50 / p95 / p99)",
      "type": "timeseries",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "histogram_quantile(0.50, sum by (provider, le) (rate(ractogateway_request_duration_seconds_bucket[$__rate_interval])))",
          "legendFormat": "p50 {{provider}}",
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "histogram_quantile(0.95, sum by (provider, le) (rate(ractogateway_request_duration_seconds_bucket[$__rate_interval])))",
          "legendFormat": "p95 {{provider}}",
          "refId": "B"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "histogram_quantile(0.99, sum by (provider, le) (rate(ractogateway_request_duration_seconds_bucket[$__rate_interval])))",
          "legendFormat": "p99 {{provider}}",
          "refId": "C"
        }
      ]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "s",
          "custom": { "lineWidth": 2, "fillOpacity": 8 }
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 6 },
      "id": 11,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Request Latency by Model (p95)",
      "type": "timeseries",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "histogram_quantile(0.95, sum by (model, le) (rate(ractogateway_request_duration_seconds_bucket[$__rate_interval])))",
        "legendFormat": "p95 {{model}}",
        "refId": "A"
      }]
    },

    {
      "collapsed": false,
      "gridPos": { "h": 1, "w": 24, "x": 0, "y": 14 },
      "id": 102,
      "title": "Token Usage & Cost",
      "type": "row"
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "short",
          "custom": { "lineWidth": 2, "fillOpacity": 12, "stacking": { "mode": "normal" } }
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 15 },
      "id": 20,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Token Rate (input + output) by Provider",
      "type": "timeseries",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum by (provider, token_type) (rate(ractogateway_tokens_total[$__rate_interval]))",
          "legendFormat": "{{provider}} {{token_type}}",
          "refId": "A"
        }
      ]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "currencyUSD",
          "decimals": 6,
          "custom": { "lineWidth": 2, "fillOpacity": 8 }
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 15 },
      "id": 21,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Cost Rate (USD/s) by Model",
      "type": "timeseries",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum by (model) (rate(ractogateway_cost_usd_total[$__rate_interval]))",
        "legendFormat": "{{model}}",
        "refId": "A"
      }]
    },

    {
      "collapsed": false,
      "gridPos": { "h": 1, "w": 24, "x": 0, "y": 23 },
      "id": 103,
      "title": "Cache",
      "type": "row"
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "short",
          "custom": { "lineWidth": 2, "fillOpacity": 8 }
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 24 },
      "id": 30,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Cache Hits vs Misses",
      "type": "timeseries",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum by (cache_type) (rate(ractogateway_cache_hits_total[$__rate_interval]))",
          "legendFormat": "hit {{cache_type}}",
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum by (cache_type) (rate(ractogateway_cache_misses_total[$__rate_interval]))",
          "legendFormat": "miss {{cache_type}}",
          "refId": "B"
        }
      ]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "percentunit",
          "min": 0, "max": 1
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 24 },
      "id": 31,
      "options": { "reduceOptions": { "calcs": ["lastNotNull"] }, "minVizWidth": 75,
                   "minVizHeight": 75, "mode": "basic" },
      "title": "Cache Hit Rate Gauge",
      "type": "gauge",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum(increase(ractogateway_cache_hits_total{cache_type=\"exact\"}[$__range])) / clamp_min(sum(increase(ractogateway_cache_hits_total{cache_type=\"exact\"}[$__range])) + sum(increase(ractogateway_cache_misses_total{cache_type=\"exact\"}[$__range])), 1)",
          "legendFormat": "exact",
          "refId": "A"
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum(increase(ractogateway_cache_hits_total{cache_type=\"semantic\"}[$__range])) / clamp_min(sum(increase(ractogateway_cache_hits_total{cache_type=\"semantic\"}[$__range])) + sum(increase(ractogateway_cache_misses_total{cache_type=\"semantic\"}[$__range])), 1)",
          "legendFormat": "semantic",
          "refId": "B"
        }
      ]
    },

    {
      "collapsed": false,
      "gridPos": { "h": 1, "w": 24, "x": 0, "y": 32 },
      "id": 104,
      "title": "Request Throughput & Errors",
      "type": "row"
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "reqps",
          "custom": { "lineWidth": 2, "fillOpacity": 8 }
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 33 },
      "id": 40,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Request Throughput by Provider",
      "type": "timeseries",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum by (provider, operation) (rate(ractogateway_requests_total[$__rate_interval]))",
        "legendFormat": "{{provider}} {{operation}}",
        "refId": "A"
      }]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "short",
          "custom": { "lineWidth": 2, "fillOpacity": 20 }
        },
        "overrides": [{ "matcher": { "id": "byName", "options": "errors" },
                        "properties": [{ "id": "color", "value": { "fixedColor": "red", "mode": "fixed" }}]}]
      },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 33 },
      "id": 41,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Error Rate",
      "type": "timeseries",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum by (provider) (rate(ractogateway_requests_total{status=\"error\"}[$__rate_interval]))",
        "legendFormat": "{{provider}} errors",
        "refId": "A"
      }]
    },

    {
      "collapsed": false,
      "gridPos": { "h": 1, "w": 24, "x": 0, "y": 41 },
      "id": 105,
      "title": "Tool Calls",
      "type": "row"
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "palette-classic" }, "unit": "short",
          "custom": { "lineWidth": 2, "fillOpacity": 8 }
        }
      },
      "gridPos": { "h": 8, "w": 12, "x": 0, "y": 42 },
      "id": 50,
      "options": { "tooltip": { "mode": "multi" } },
      "title": "Tool Call Rate by Function",
      "type": "timeseries",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum by (tool_name) (rate(ractogateway_tool_calls_total[$__rate_interval]))",
        "legendFormat": "{{tool_name}}",
        "refId": "A"
      }]
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": { "defaults": { "color": { "mode": "palette-classic" } } },
      "gridPos": { "h": 8, "w": 12, "x": 12, "y": 42 },
      "id": 51,
      "options": { "tooltip": { "mode": "single" }, "legend": { "placement": "right" } },
      "title": "Tool Call Distribution",
      "type": "piechart",
      "targets": [{
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "expr": "sum by (tool_name) (increase(ractogateway_tool_calls_total[$__range]))",
        "legendFormat": "{{tool_name}}",
        "refId": "A"
      }]
    },

    {
      "collapsed": false,
      "gridPos": { "h": 1, "w": 24, "x": 0, "y": 50 },
      "id": 106,
      "title": "Per-Model Summary",
      "type": "row"
    },

    {
      "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
      "fieldConfig": {
        "defaults": { "color": { "mode": "thresholds" } },
        "overrides": [
          { "matcher": { "id": "byName", "options": "Total Requests" },
            "properties": [{ "id": "custom.width", "value": 130 }] },
          { "matcher": { "id": "byName", "options": "Avg Latency (s)" },
            "properties": [{ "id": "unit", "value": "s" }, { "id": "custom.width", "value": 140 }] },
          { "matcher": { "id": "byName", "options": "Total Cost (USD)" },
            "properties": [{ "id": "unit", "value": "currencyUSD" }, { "id": "decimals", "value": 4 }] },
          { "matcher": { "id": "byName", "options": "Error Rate" },
            "properties": [{ "id": "unit", "value": "percentunit" }, { "id": "thresholds", "value": {
              "mode": "absolute", "steps": [
                { "color": "green", "value": null },
                { "color": "yellow", "value": 0.01 },
                { "color": "red",    "value": 0.05 }
              ]
            }}] }
        ]
      },
      "gridPos": { "h": 10, "w": 24, "x": 0, "y": 51 },
      "id": 60,
      "options": { "sortBy": [{ "displayName": "Total Cost (USD)", "desc": true }] },
      "title": "Per-Model Summary Table",
      "type": "table",
      "targets": [
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum by (provider, model) (increase(ractogateway_requests_total[$__range]))",
          "legendFormat": "{{provider}} / {{model}}",
          "refId": "A",
          "instant": true
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum by (model) (rate(ractogateway_request_duration_seconds_sum[$__rate_interval])) / sum by (model) (rate(ractogateway_request_duration_seconds_count[$__rate_interval]))",
          "legendFormat": "{{model}}",
          "refId": "B",
          "instant": true
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum by (model) (increase(ractogateway_cost_usd_total[$__range]))",
          "legendFormat": "{{model}}",
          "refId": "C",
          "instant": true
        },
        {
          "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
          "expr": "sum by (model) (increase(ractogateway_requests_total{status=\"error\"}[$__range])) / clamp_min(sum by (model) (increase(ractogateway_requests_total[$__range])), 1)",
          "legendFormat": "{{model}}",
          "refId": "D",
          "instant": true
        }
      ],
      "transformations": [
        { "id": "merge", "options": {} },
        { "id": "organize", "options": {
          "renameByName": {
            "Value #A": "Total Requests",
            "Value #B": "Avg Latency (s)",
            "Value #C": "Total Cost (USD)",
            "Value #D": "Error Rate"
          }
        }}
      ]
    }

  ],

  "refresh": "30s",
  "schemaVersion": 38,
  "style": "dark",
  "tags": ["ractogateway", "llm", "ai", "observability"],
  "templating": {
    "list": [
      {
        "current": {},
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "definition": "label_values(ractogateway_requests_total, provider)",
        "hide": 0,
        "includeAll": true,
        "label": "Provider",
        "multi": true,
        "name": "provider",
        "options": [],
        "query": { "query": "label_values(ractogateway_requests_total, provider)", "refId": "StandardVariableQuery" },
        "refresh": 2,
        "type": "query"
      },
      {
        "current": {},
        "datasource": { "type": "prometheus", "uid": "${DS_PROMETHEUS}" },
        "definition": "label_values(ractogateway_requests_total{provider=~\"$provider\"}, model)",
        "hide": 0,
        "includeAll": true,
        "label": "Model",
        "multi": true,
        "name": "model",
        "options": [],
        "query": { "query": "label_values(ractogateway_requests_total{provider=~\"$provider\"}, model)", "refId": "StandardVariableQuery" },
        "refresh": 2,
        "type": "query"
      }
    ]
  },
  "time": { "from": "now-1h", "to": "now" },
  "timepicker": {},
  "timezone": "browser",
  "title": "RactoGateway — LLM Observability",
  "uid": "ractogateway-observability",
  "version": 1
}
