{
	"__inputs": [
		{
			"name": "DS_LOKI",
			"label": "Loki",
			"type": "datasource",
			"pluginId": "loki",
			"pluginName": "Loki"
		}
	],
	"title": "Codex - Agent Performance",
	"description": "Tool results and reported response token counts from Loki for service_name=codex_exec.",
	"uid": "codex-tool-calls",
	"schemaVersion": 41,
	"tags": [
		"codex",
		"observability"
	],
	"time": {
		"from": "now-1h",
		"to": "now"
	},
	"panels": [
		{
			"id": 1,
			"title": "Tool Calls",
			"type": "stat",
			"gridPos": {
				"x": 0,
				"y": 0,
				"w": 8,
				"h": 4
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "sum(count_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.tool_result\" [$__range]\n)) or vector(0)",
					"queryType": "instant",
					"refId": "A"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0,
					"color": {
						"mode": "fixed",
						"fixedColor": "blue"
					}
				},
				"overrides": []
			},
			"options": {
				"colorMode": "value",
				"graphMode": "none",
				"justifyMode": "auto",
				"orientation": "auto",
				"percentChangeColorMode": "standard",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": false
				},
				"showPercentChange": false,
				"textMode": "value",
				"wideLayout": true
			}
		},
		{
			"id": 2,
			"title": "Average Call Duration",
			"type": "stat",
			"gridPos": {
				"x": 8,
				"y": 0,
				"w": 8,
				"h": 4
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "avg_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.tool_result\"\n  | duration_ms!=\"\" | unwrap duration_ms | __error__=\"\" [$__range]\n) by (service_name)",
					"queryType": "instant",
					"refId": "A"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "ms",
					"decimals": 1,
					"min": 0,
					"color": {
						"mode": "fixed",
						"fixedColor": "blue"
					}
				},
				"overrides": []
			},
			"options": {
				"colorMode": "value",
				"graphMode": "none",
				"justifyMode": "auto",
				"orientation": "auto",
				"percentChangeColorMode": "standard",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": false
				},
				"showPercentChange": false,
				"textMode": "value",
				"wideLayout": true
			}
		},
		{
			"id": 3,
			"title": "Failure Rate",
			"type": "stat",
			"gridPos": {
				"x": 16,
				"y": 0,
				"w": 8,
				"h": 4
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "(100 * (sum(count_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.tool_result\"\n  | success=\"false\" [$__range]\n)) or vector(0))\n/ sum(count_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.tool_result\" [$__range]\n))) or vector(0)",
					"queryType": "instant",
					"refId": "A"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "percent",
					"decimals": 1,
					"min": 0,
					"color": {
						"mode": "thresholds"
					},
					"max": 100,
					"thresholds": {
						"mode": "absolute",
						"steps": [
							{
								"color": "green",
								"value": null
							},
							{
								"color": "orange",
								"value": 5
							},
							{
								"color": "red",
								"value": 15
							}
						]
					}
				},
				"overrides": []
			},
			"options": {
				"colorMode": "value",
				"graphMode": "none",
				"justifyMode": "auto",
				"orientation": "auto",
				"percentChangeColorMode": "standard",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": false
				},
				"showPercentChange": false,
				"textMode": "value",
				"wideLayout": true
			}
		},
		{
			"id": 4,
			"title": "Distribution of Calls by Tool",
			"type": "piechart",
			"gridPos": {
				"x": 0,
				"y": 4,
				"w": 9,
				"h": 7
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "sort_desc(sum by (tool_name) (count_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.tool_result\"\n  | tool_name!=\"\" [$__range]\n)))",
					"queryType": "instant",
					"refId": "A",
					"legendFormat": "{{tool_name}}"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0,
					"color": {
						"mode": "palette-classic"
					}
				},
				"overrides": []
			},
			"options": {
				"displayLabels": [
					"value"
				],
				"legend": {
					"displayMode": "list",
					"placement": "bottom",
					"showLegend": true,
					"values": [
						"value",
						"percent"
					]
				},
				"pieType": "pie",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": true
				},
				"sort": "desc",
				"tooltip": {
					"hideZeros": false,
					"mode": "single",
					"sort": "none"
				}
			}
		},
		{
			"id": 5,
			"title": "Tool Calls Over Time",
			"type": "timeseries",
			"gridPos": {
				"x": 9,
				"y": 4,
				"w": 15,
				"h": 7
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "sum(count_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.tool_result\" [$__interval]\n))",
					"queryType": "range",
					"refId": "A",
					"legendFormat": "Tool calls"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0,
					"custom": {
						"drawStyle": "bars",
						"fillOpacity": 65,
						"showPoints": "never"
					}
				},
				"overrides": []
			},
			"options": {
				"legend": {
					"calcs": [],
					"displayMode": "list",
					"placement": "bottom",
					"showLegend": true
				},
				"tooltip": {
					"hideZeros": false,
					"mode": "single",
					"sort": "none"
				}
			},
			"interval": "1m"
		},
		{
			"id": 6,
			"title": "Input Tokens",
			"type": "stat",
			"gridPos": {
				"x": 0,
				"y": 11,
				"w": 6,
				"h": 4
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "sum(sum_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.sse_event\"\n  | event_kind=\"response.completed\"\n  | unwrap input_token_count | __error__=\"\" [$__range]\n)) or vector(0)",
					"queryType": "instant",
					"refId": "A"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0,
					"color": {
						"mode": "fixed",
						"fixedColor": "blue"
					}
				},
				"overrides": []
			},
			"options": {
				"colorMode": "value",
				"graphMode": "none",
				"justifyMode": "auto",
				"orientation": "auto",
				"percentChangeColorMode": "standard",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": false
				},
				"showPercentChange": false,
				"textMode": "value",
				"wideLayout": true
			}
		},
		{
			"id": 7,
			"title": "Output Tokens",
			"type": "stat",
			"gridPos": {
				"x": 6,
				"y": 11,
				"w": 6,
				"h": 4
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "sum(sum_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.sse_event\"\n  | event_kind=\"response.completed\"\n  | unwrap output_token_count | __error__=\"\" [$__range]\n)) or vector(0)",
					"queryType": "instant",
					"refId": "A"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0,
					"color": {
						"mode": "fixed",
						"fixedColor": "blue"
					}
				},
				"overrides": []
			},
			"options": {
				"colorMode": "value",
				"graphMode": "none",
				"justifyMode": "auto",
				"orientation": "auto",
				"percentChangeColorMode": "standard",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": false
				},
				"showPercentChange": false,
				"textMode": "value",
				"wideLayout": true
			}
		},
		{
			"id": 8,
			"title": "Cached Input Tokens",
			"type": "stat",
			"gridPos": {
				"x": 12,
				"y": 11,
				"w": 6,
				"h": 4
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "sum(sum_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.sse_event\"\n  | event_kind=\"response.completed\"\n  | unwrap cached_token_count | __error__=\"\" [$__range]\n)) or vector(0)",
					"queryType": "instant",
					"refId": "A"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0,
					"color": {
						"mode": "fixed",
						"fixedColor": "blue"
					}
				},
				"overrides": []
			},
			"options": {
				"colorMode": "value",
				"graphMode": "none",
				"justifyMode": "auto",
				"orientation": "auto",
				"percentChangeColorMode": "standard",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": false
				},
				"showPercentChange": false,
				"textMode": "value",
				"wideLayout": true
			}
		},
		{
			"id": 9,
			"title": "Total Tokens",
			"type": "stat",
			"gridPos": {
				"x": 18,
				"y": 11,
				"w": 6,
				"h": 4
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "(sum(sum_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.sse_event\"\n  | event_kind=\"response.completed\"\n  | unwrap input_token_count | __error__=\"\" [$__range]\n)) or vector(0))\n+ (sum(sum_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.sse_event\"\n  | event_kind=\"response.completed\"\n  | unwrap output_token_count | __error__=\"\" [$__range]\n)) or vector(0))",
					"queryType": "instant",
					"refId": "A"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0,
					"color": {
						"mode": "fixed",
						"fixedColor": "blue"
					}
				},
				"overrides": []
			},
			"options": {
				"colorMode": "value",
				"graphMode": "none",
				"justifyMode": "auto",
				"orientation": "auto",
				"percentChangeColorMode": "standard",
				"reduceOptions": {
					"calcs": [
						"lastNotNull"
					],
					"fields": "",
					"values": false
				},
				"showPercentChange": false,
				"textMode": "value",
				"wideLayout": true
			}
		},
		{
			"id": 10,
			"title": "Input Tokens by Model",
			"type": "barchart",
			"gridPos": {
				"x": 0,
				"y": 15,
				"w": 24,
				"h": 7
			},
			"datasource": {
				"type": "loki",
				"uid": "${DS_LOKI}"
			},
			"targets": [
				{
					"datasource": {
						"type": "loki",
						"uid": "${DS_LOKI}"
					},
					"editorMode": "code",
					"expr": "sum by (model)(sum_over_time(\n  {service_name=\"codex_exec\"} | event_name=\"codex.sse_event\"\n  | event_kind=\"response.completed\"\n  | model!=\"\"\n  | unwrap input_token_count | __error__=\"\" [$__range]\n))",
					"queryType": "instant",
					"refId": "A",
					"legendFormat": "{{model}}"
				}
			],
			"fieldConfig": {
				"defaults": {
					"unit": "none",
					"decimals": 0,
					"min": 0
				},
				"overrides": []
			},
			"options": {
				"barRadius": 0,
				"barWidth": 0.9,
				"fullHighlight": false,
				"groupWidth": 0.8,
				"legend": {
					"calcs": [],
					"displayMode": "list",
					"placement": "bottom",
					"showLegend": false
				},
				"orientation": "horizontal",
				"showValue": "always",
				"stacking": "none",
				"tooltip": {
					"hideZeros": false,
					"mode": "single",
					"sort": "none"
				},
				"xField": "model",
				"xTickLabelRotation": 0,
				"xTickLabelSpacing": 100
			},
			"transformations": [
				{
					"id": "labelsToFields",
					"options": {
						"mode": "columns",
						"keepLabels": [
							"model"
						]
					}
				},
				{
					"id": "merge",
					"options": {}
				}
			]
		}
	]
}
