agent-usage-tracker 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. agent_usage_tracker/__init__.py +8 -0
  2. agent_usage_tracker/backup.py +225 -0
  3. agent_usage_tracker/cli.py +479 -0
  4. agent_usage_tracker/collectors/__init__.py +115 -0
  5. agent_usage_tracker/collectors/_util.py +310 -0
  6. agent_usage_tracker/collectors/activity_common.py +294 -0
  7. agent_usage_tracker/collectors/activitywatch.py +394 -0
  8. agent_usage_tracker/collectors/claude_code.py +484 -0
  9. agent_usage_tracker/collectors/claude_code_activity.py +317 -0
  10. agent_usage_tracker/collectors/codex.py +612 -0
  11. agent_usage_tracker/collectors/commits.py +154 -0
  12. agent_usage_tracker/collectors/cursor.py +193 -0
  13. agent_usage_tracker/collectors/cursor_api.py +247 -0
  14. agent_usage_tracker/collectors/cursor_cli.py +592 -0
  15. agent_usage_tracker/collectors/cursor_sdk.py +95 -0
  16. agent_usage_tracker/collectors/generic_sdk.py +100 -0
  17. agent_usage_tracker/collectors/pi.py +397 -0
  18. agent_usage_tracker/collectors/session_digest.py +379 -0
  19. agent_usage_tracker/collectors/t3.py +490 -0
  20. agent_usage_tracker/config.py +392 -0
  21. agent_usage_tracker/coverage.py +615 -0
  22. agent_usage_tracker/coverage_catalog.json +588 -0
  23. agent_usage_tracker/cursor_run.py +62 -0
  24. agent_usage_tracker/diagnostics.py +74 -0
  25. agent_usage_tracker/episode_store.py +78 -0
  26. agent_usage_tracker/event.py +132 -0
  27. agent_usage_tracker/git_day_cache.py +128 -0
  28. agent_usage_tracker/git_days.py +499 -0
  29. agent_usage_tracker/git_safe.py +58 -0
  30. agent_usage_tracker/harness_inventory.py +208 -0
  31. agent_usage_tracker/history_steers.py +88 -0
  32. agent_usage_tracker/improve/__init__.py +1 -0
  33. agent_usage_tracker/improve/case_view.py +112 -0
  34. agent_usage_tracker/improve/cli.py +138 -0
  35. agent_usage_tracker/improve/desktop.py +347 -0
  36. agent_usage_tracker/improve/evidence.py +322 -0
  37. agent_usage_tracker/improve/judge.py +671 -0
  38. agent_usage_tracker/improve/proposals.py +72 -0
  39. agent_usage_tracker/improve/rubric.py +360 -0
  40. agent_usage_tracker/improve/rubrics/avoidable_recovery.md +20 -0
  41. agent_usage_tracker/improve/rubrics/code_quality.md +22 -0
  42. agent_usage_tracker/improve/rubrics/completion_supported.md +20 -0
  43. agent_usage_tracker/improve/rubrics/redundant_tests.md +21 -0
  44. agent_usage_tracker/improve/rubrics/verbosity.md +21 -0
  45. agent_usage_tracker/improve/runtime.py +117 -0
  46. agent_usage_tracker/improve/schedule.py +291 -0
  47. agent_usage_tracker/improve/search_check.py +563 -0
  48. agent_usage_tracker/improve/store.py +244 -0
  49. agent_usage_tracker/improve/studies.py +285 -0
  50. agent_usage_tracker/improve/study_cli.py +46 -0
  51. agent_usage_tracker/improve/study_decision.py +121 -0
  52. agent_usage_tracker/improve/study_followup.py +174 -0
  53. agent_usage_tracker/improve/t3.py +269 -0
  54. agent_usage_tracker/improve/trial.py +318 -0
  55. agent_usage_tracker/improve/validation.py +148 -0
  56. agent_usage_tracker/inspector.py +350 -0
  57. agent_usage_tracker/label/__init__.py +22 -0
  58. agent_usage_tracker/label/backends.py +742 -0
  59. agent_usage_tracker/label/bakeoff.py +661 -0
  60. agent_usage_tracker/label/baseline.py +173 -0
  61. agent_usage_tracker/label/clef.py +258 -0
  62. agent_usage_tracker/label/context.py +382 -0
  63. agent_usage_tracker/label/core.py +355 -0
  64. agent_usage_tracker/label/covered_run.py +92 -0
  65. agent_usage_tracker/label/evalset.py +154 -0
  66. agent_usage_tracker/label/gold.py +209 -0
  67. agent_usage_tracker/label/local.py +326 -0
  68. agent_usage_tracker/label/local_parallel.py +232 -0
  69. agent_usage_tracker/label/merge.py +57 -0
  70. agent_usage_tracker/label/outcome_coverage.py +57 -0
  71. agent_usage_tracker/label/replay.py +194 -0
  72. agent_usage_tracker/label/run.py +339 -0
  73. agent_usage_tracker/label/tasks.py +579 -0
  74. agent_usage_tracker/label/teach.py +145 -0
  75. agent_usage_tracker/local_page.py +128 -0
  76. agent_usage_tracker/manual_time.py +69 -0
  77. agent_usage_tracker/net.py +23 -0
  78. agent_usage_tracker/observation_cli.py +65 -0
  79. agent_usage_tracker/observation_report.py +116 -0
  80. agent_usage_tracker/observation_server.py +110 -0
  81. agent_usage_tracker/observations.py +365 -0
  82. agent_usage_tracker/outcome_git.py +383 -0
  83. agent_usage_tracker/outcomes.py +464 -0
  84. agent_usage_tracker/pricing.py +248 -0
  85. agent_usage_tracker/push.py +530 -0
  86. agent_usage_tracker/redact.py +45 -0
  87. agent_usage_tracker/repo_keys.py +165 -0
  88. agent_usage_tracker/review.py +252 -0
  89. agent_usage_tracker/scan.py +472 -0
  90. agent_usage_tracker/scheduled.py +164 -0
  91. agent_usage_tracker/sdk.py +79 -0
  92. agent_usage_tracker/sink.py +82 -0
  93. agent_usage_tracker/sink_stack/README.md +71 -0
  94. agent_usage_tracker/sink_stack/apply.sh +63 -0
  95. agent_usage_tracker/sink_stack/compose.yaml +94 -0
  96. agent_usage_tracker/sink_stack/env.example +20 -0
  97. agent_usage_tracker/sink_stack/grafana/dashboards/agents-effort.json +634 -0
  98. agent_usage_tracker/sink_stack/grafana/dashboards/agents-usage.json +1279 -0
  99. agent_usage_tracker/sink_stack/grafana/provisioning/dashboards/aut.yaml +9 -0
  100. agent_usage_tracker/sink_stack/grafana/provisioning/datasources/agents.yaml +16 -0
  101. agent_usage_tracker/sink_stack/sql/01_schema.sql +1186 -0
  102. agent_usage_tracker/sink_stack/sql/02_price.sql +60 -0
  103. agent_usage_tracker/sink_stack/sql/03_effort.sql +1180 -0
  104. agent_usage_tracker/sink_stack/sql/04_grants.sql +149 -0
  105. agent_usage_tracker/sink_stack/sql/refresh.sql +66 -0
  106. agent_usage_tracker/sources.py +180 -0
  107. agent_usage_tracker/state.py +191 -0
  108. agent_usage_tracker/telemetry.py +165 -0
  109. agent_usage_tracker-0.1.0.dist-info/METADATA +366 -0
  110. agent_usage_tracker-0.1.0.dist-info/RECORD +113 -0
  111. agent_usage_tracker-0.1.0.dist-info/WHEEL +4 -0
  112. agent_usage_tracker-0.1.0.dist-info/entry_points.txt +3 -0
  113. agent_usage_tracker-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,1279 @@
1
+ {
2
+ "uid": "agents-usage",
3
+ "title": "Coding Agent Token Usage",
4
+ "tags": [
5
+ "agents",
6
+ "tokens",
7
+ "llm"
8
+ ],
9
+ "timezone": "browser",
10
+ "schemaVersion": 39,
11
+ "version": 2,
12
+ "editable": true,
13
+ "graphTooltip": 1,
14
+ "refresh": "",
15
+ "time": {
16
+ "from": "now-90d",
17
+ "to": "now"
18
+ },
19
+ "timepicker": {
20
+ "refresh_intervals": [
21
+ "1h",
22
+ "6h",
23
+ "12h",
24
+ "24h"
25
+ ],
26
+ "quick_ranges": [
27
+ {
28
+ "display": "Last 24 hours",
29
+ "from": "now-24h",
30
+ "to": "now"
31
+ },
32
+ {
33
+ "display": "Last 7 days",
34
+ "from": "now-7d",
35
+ "to": "now"
36
+ },
37
+ {
38
+ "display": "Last 30 days",
39
+ "from": "now-30d",
40
+ "to": "now"
41
+ },
42
+ {
43
+ "display": "Last 90 days",
44
+ "from": "now-90d",
45
+ "to": "now"
46
+ },
47
+ {
48
+ "display": "Last 6 months",
49
+ "from": "now-6M",
50
+ "to": "now"
51
+ },
52
+ {
53
+ "display": "Last 1 year",
54
+ "from": "now-1y",
55
+ "to": "now"
56
+ },
57
+ {
58
+ "display": "This year",
59
+ "from": "now/y",
60
+ "to": "now/y"
61
+ }
62
+ ]
63
+ },
64
+ "templating": {
65
+ "list": [
66
+ {
67
+ "name": "machine",
68
+ "type": "query",
69
+ "datasource": {
70
+ "type": "grafana-postgresql-datasource",
71
+ "uid": "agents-timescaledb"
72
+ },
73
+ "label": "Machine",
74
+ "multi": true,
75
+ "includeAll": true,
76
+ "current": {
77
+ "text": "All",
78
+ "value": [
79
+ "$__all"
80
+ ]
81
+ },
82
+ "refresh": 2,
83
+ "sort": 1,
84
+ "query": "SELECT DISTINCT machine AS __text, machine AS __value FROM agents.usage_daily WHERE machine IS NOT NULL ORDER BY 1",
85
+ "options": [],
86
+ "definition": "SELECT DISTINCT machine AS __text, machine AS __value FROM agents.usage_daily WHERE machine IS NOT NULL ORDER BY 1"
87
+ },
88
+ {
89
+ "name": "agent",
90
+ "type": "query",
91
+ "datasource": {
92
+ "type": "grafana-postgresql-datasource",
93
+ "uid": "agents-timescaledb"
94
+ },
95
+ "label": "Agent",
96
+ "multi": true,
97
+ "includeAll": true,
98
+ "current": {
99
+ "text": "All",
100
+ "value": [
101
+ "$__all"
102
+ ]
103
+ },
104
+ "refresh": 2,
105
+ "sort": 1,
106
+ "query": "SELECT DISTINCT agent AS __text, agent AS __value FROM agents.usage_daily WHERE agent IS NOT NULL ORDER BY 1",
107
+ "options": [],
108
+ "definition": "SELECT DISTINCT agent AS __text, agent AS __value FROM agents.usage_daily WHERE agent IS NOT NULL ORDER BY 1"
109
+ },
110
+ {
111
+ "name": "model",
112
+ "type": "query",
113
+ "datasource": {
114
+ "type": "grafana-postgresql-datasource",
115
+ "uid": "agents-timescaledb"
116
+ },
117
+ "label": "Model",
118
+ "multi": true,
119
+ "includeAll": true,
120
+ "current": {
121
+ "text": "All",
122
+ "value": [
123
+ "$__all"
124
+ ]
125
+ },
126
+ "refresh": 2,
127
+ "sort": 1,
128
+ "query": "SELECT DISTINCT coalesce(model,'unknown') AS __text, coalesce(model,'unknown') AS __value FROM agents.usage_daily ORDER BY 1",
129
+ "options": [],
130
+ "definition": "SELECT DISTINCT coalesce(model,'unknown') AS __text, coalesce(model,'unknown') AS __value FROM agents.usage_daily ORDER BY 1"
131
+ }
132
+ ]
133
+ },
134
+ "annotations": {
135
+ "list": []
136
+ },
137
+ "panels": [
138
+ {
139
+ "id": 1,
140
+ "title": "Overview",
141
+ "type": "row",
142
+ "collapsed": false,
143
+ "gridPos": {
144
+ "h": 1,
145
+ "w": 24,
146
+ "x": 0,
147
+ "y": 0
148
+ },
149
+ "panels": []
150
+ },
151
+ {
152
+ "id": 2,
153
+ "title": "Total tokens",
154
+ "type": "stat",
155
+ "datasource": {
156
+ "type": "grafana-postgresql-datasource",
157
+ "uid": "agents-timescaledb"
158
+ },
159
+ "gridPos": {
160
+ "h": 5,
161
+ "w": 4,
162
+ "x": 0,
163
+ "y": 1
164
+ },
165
+ "targets": [
166
+ {
167
+ "refId": "A",
168
+ "datasource": {
169
+ "type": "grafana-postgresql-datasource",
170
+ "uid": "agents-timescaledb"
171
+ },
172
+ "format": "table",
173
+ "rawSql": "SELECT sum(total_tokens) AS v FROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]);",
174
+ "editorMode": "code"
175
+ }
176
+ ],
177
+ "fieldConfig": {
178
+ "defaults": {
179
+ "unit": "short",
180
+ "color": {
181
+ "mode": "fixed",
182
+ "fixedColor": "#5794F2"
183
+ }
184
+ },
185
+ "overrides": []
186
+ },
187
+ "options": {
188
+ "reduceOptions": {
189
+ "calcs": [
190
+ "lastNotNull"
191
+ ],
192
+ "fields": "",
193
+ "values": false
194
+ },
195
+ "colorMode": "value",
196
+ "graphMode": "none",
197
+ "textMode": "value",
198
+ "justifyMode": "center"
199
+ },
200
+ "description": "Total tokens in the selected period. Respects $machine/$agent/$model."
201
+ },
202
+ {
203
+ "id": 3,
204
+ "title": "Output tokens",
205
+ "type": "stat",
206
+ "datasource": {
207
+ "type": "grafana-postgresql-datasource",
208
+ "uid": "agents-timescaledb"
209
+ },
210
+ "gridPos": {
211
+ "h": 5,
212
+ "w": 4,
213
+ "x": 4,
214
+ "y": 1
215
+ },
216
+ "targets": [
217
+ {
218
+ "refId": "A",
219
+ "datasource": {
220
+ "type": "grafana-postgresql-datasource",
221
+ "uid": "agents-timescaledb"
222
+ },
223
+ "format": "table",
224
+ "rawSql": "SELECT sum(output_tokens) AS v FROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]);",
225
+ "editorMode": "code"
226
+ }
227
+ ],
228
+ "fieldConfig": {
229
+ "defaults": {
230
+ "unit": "short",
231
+ "color": {
232
+ "mode": "fixed",
233
+ "fixedColor": "#B877D9"
234
+ }
235
+ },
236
+ "overrides": []
237
+ },
238
+ "options": {
239
+ "reduceOptions": {
240
+ "calcs": [
241
+ "lastNotNull"
242
+ ],
243
+ "fields": "",
244
+ "values": false
245
+ },
246
+ "colorMode": "value",
247
+ "graphMode": "none",
248
+ "textMode": "value",
249
+ "justifyMode": "center"
250
+ },
251
+ "description": "Generated output tokens (the part you 'write')."
252
+ },
253
+ {
254
+ "id": 4,
255
+ "title": "Agent turns",
256
+ "type": "stat",
257
+ "datasource": {
258
+ "type": "grafana-postgresql-datasource",
259
+ "uid": "agents-timescaledb"
260
+ },
261
+ "gridPos": {
262
+ "h": 5,
263
+ "w": 4,
264
+ "x": 8,
265
+ "y": 1
266
+ },
267
+ "targets": [
268
+ {
269
+ "refId": "A",
270
+ "datasource": {
271
+ "type": "grafana-postgresql-datasource",
272
+ "uid": "agents-timescaledb"
273
+ },
274
+ "format": "table",
275
+ "rawSql": "SELECT count(*) FILTER (WHERE kind = 'agent') AS v FROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]);",
276
+ "editorMode": "code"
277
+ }
278
+ ],
279
+ "fieldConfig": {
280
+ "defaults": {
281
+ "unit": "short",
282
+ "color": {
283
+ "mode": "fixed",
284
+ "fixedColor": "#73BF69"
285
+ }
286
+ },
287
+ "overrides": []
288
+ },
289
+ "options": {
290
+ "reduceOptions": {
291
+ "calcs": [
292
+ "lastNotNull"
293
+ ],
294
+ "fields": "",
295
+ "values": false
296
+ },
297
+ "colorMode": "value",
298
+ "graphMode": "none",
299
+ "textMode": "value",
300
+ "justifyMode": "center"
301
+ },
302
+ "description": "How many times the agent replied (assistant turns)."
303
+ },
304
+ {
305
+ "id": 5,
306
+ "title": "Human turns",
307
+ "type": "stat",
308
+ "datasource": {
309
+ "type": "grafana-postgresql-datasource",
310
+ "uid": "agents-timescaledb"
311
+ },
312
+ "gridPos": {
313
+ "h": 5,
314
+ "w": 4,
315
+ "x": 12,
316
+ "y": 1
317
+ },
318
+ "targets": [
319
+ {
320
+ "refId": "A",
321
+ "datasource": {
322
+ "type": "grafana-postgresql-datasource",
323
+ "uid": "agents-timescaledb"
324
+ },
325
+ "format": "table",
326
+ "rawSql": "SELECT count(*) FILTER (WHERE kind = 'human') AS v FROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[]);",
327
+ "editorMode": "code"
328
+ }
329
+ ],
330
+ "fieldConfig": {
331
+ "defaults": {
332
+ "unit": "short",
333
+ "color": {
334
+ "mode": "fixed",
335
+ "fixedColor": "#E0B400"
336
+ }
337
+ },
338
+ "overrides": []
339
+ },
340
+ "options": {
341
+ "reduceOptions": {
342
+ "calcs": [
343
+ "lastNotNull"
344
+ ],
345
+ "fields": "",
346
+ "values": false
347
+ },
348
+ "colorMode": "value",
349
+ "graphMode": "none",
350
+ "textMode": "value",
351
+ "justifyMode": "center"
352
+ },
353
+ "description": "How many prompts YOU sent interactively. Collectors already leave out subagents, `codex exec` and scheduled automation."
354
+ },
355
+ {
356
+ "id": 6,
357
+ "title": "Active days",
358
+ "type": "stat",
359
+ "datasource": {
360
+ "type": "grafana-postgresql-datasource",
361
+ "uid": "agents-timescaledb"
362
+ },
363
+ "gridPos": {
364
+ "h": 5,
365
+ "w": 4,
366
+ "x": 16,
367
+ "y": 1
368
+ },
369
+ "targets": [
370
+ {
371
+ "refId": "A",
372
+ "datasource": {
373
+ "type": "grafana-postgresql-datasource",
374
+ "uid": "agents-timescaledb"
375
+ },
376
+ "format": "table",
377
+ "rawSql": "SELECT count(DISTINCT time_bucket('1 day', ts, current_setting('TimeZone'))) AS v FROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]);",
378
+ "editorMode": "code"
379
+ }
380
+ ],
381
+ "fieldConfig": {
382
+ "defaults": {
383
+ "unit": "short",
384
+ "color": {
385
+ "mode": "fixed",
386
+ "fixedColor": "#37A2A2"
387
+ }
388
+ },
389
+ "overrides": []
390
+ },
391
+ "options": {
392
+ "reduceOptions": {
393
+ "calcs": [
394
+ "lastNotNull"
395
+ ],
396
+ "fields": "",
397
+ "values": false
398
+ },
399
+ "colorMode": "value",
400
+ "graphMode": "none",
401
+ "textMode": "value",
402
+ "justifyMode": "center"
403
+ }
404
+ },
405
+ {
406
+ "id": 7,
407
+ "title": "Cache-read share",
408
+ "type": "gauge",
409
+ "datasource": {
410
+ "type": "grafana-postgresql-datasource",
411
+ "uid": "agents-timescaledb"
412
+ },
413
+ "gridPos": {
414
+ "h": 5,
415
+ "w": 4,
416
+ "x": 20,
417
+ "y": 1
418
+ },
419
+ "targets": [
420
+ {
421
+ "refId": "A",
422
+ "datasource": {
423
+ "type": "grafana-postgresql-datasource",
424
+ "uid": "agents-timescaledb"
425
+ },
426
+ "format": "table",
427
+ "rawSql": "SELECT 100.0 * sum(cache_read_tokens) / nullif(sum(cache_read_tokens + input_tokens), 0) AS \"Cache-read share\"\nFROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]);",
428
+ "editorMode": "code"
429
+ }
430
+ ],
431
+ "fieldConfig": {
432
+ "defaults": {
433
+ "unit": "percent",
434
+ "min": 0,
435
+ "max": 100,
436
+ "thresholds": {
437
+ "mode": "absolute",
438
+ "steps": [
439
+ {
440
+ "color": "red",
441
+ "value": null
442
+ },
443
+ {
444
+ "color": "yellow",
445
+ "value": 50
446
+ },
447
+ {
448
+ "color": "green",
449
+ "value": 80
450
+ }
451
+ ]
452
+ }
453
+ },
454
+ "overrides": []
455
+ },
456
+ "options": {
457
+ "reduceOptions": {
458
+ "calcs": [
459
+ "lastNotNull"
460
+ ],
461
+ "fields": "",
462
+ "values": false
463
+ },
464
+ "showThresholdLabels": false
465
+ },
466
+ "description": "Share of prompt tokens served from cache. Here the color IS meaningful (a low share is genuinely worse), so the ratio keeps its red/yellow/green scale."
467
+ },
468
+ {
469
+ "id": 8,
470
+ "title": "Trends",
471
+ "type": "row",
472
+ "collapsed": false,
473
+ "gridPos": {
474
+ "h": 1,
475
+ "w": 24,
476
+ "x": 0,
477
+ "y": 6
478
+ },
479
+ "panels": []
480
+ },
481
+ {
482
+ "id": 9,
483
+ "title": "Tokens/day by agent",
484
+ "type": "timeseries",
485
+ "datasource": {
486
+ "type": "grafana-postgresql-datasource",
487
+ "uid": "agents-timescaledb"
488
+ },
489
+ "gridPos": {
490
+ "h": 8,
491
+ "w": 12,
492
+ "x": 0,
493
+ "y": 7
494
+ },
495
+ "targets": [
496
+ {
497
+ "refId": "A",
498
+ "datasource": {
499
+ "type": "grafana-postgresql-datasource",
500
+ "uid": "agents-timescaledb"
501
+ },
502
+ "format": "time_series",
503
+ "rawSql": "SELECT time_bucket('1 day', ts, current_setting('TimeZone')) AS time, agent AS metric, sum(total_tokens) AS tokens\nFROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])\nGROUP BY 1, agent ORDER BY time ASC, metric ASC;",
504
+ "editorMode": "code"
505
+ }
506
+ ],
507
+ "fieldConfig": {
508
+ "defaults": {
509
+ "unit": "short",
510
+ "custom": {
511
+ "drawStyle": "line",
512
+ "fillOpacity": 25,
513
+ "stacking": {
514
+ "mode": "normal"
515
+ },
516
+ "lineWidth": 1,
517
+ "lineInterpolation": "smooth",
518
+ "showPoints": "never"
519
+ }
520
+ },
521
+ "overrides": []
522
+ },
523
+ "options": {}
524
+ },
525
+ {
526
+ "id": 10,
527
+ "title": "Tokens/day by model",
528
+ "type": "timeseries",
529
+ "datasource": {
530
+ "type": "grafana-postgresql-datasource",
531
+ "uid": "agents-timescaledb"
532
+ },
533
+ "gridPos": {
534
+ "h": 8,
535
+ "w": 12,
536
+ "x": 12,
537
+ "y": 7
538
+ },
539
+ "targets": [
540
+ {
541
+ "refId": "A",
542
+ "datasource": {
543
+ "type": "grafana-postgresql-datasource",
544
+ "uid": "agents-timescaledb"
545
+ },
546
+ "format": "time_series",
547
+ "rawSql": "SELECT time_bucket('1 day', ts, current_setting('TimeZone')) AS time, coalesce(model,'unknown') AS metric, sum(total_tokens) AS tokens\nFROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])\nGROUP BY 1, coalesce(model,'unknown') ORDER BY time ASC, metric ASC;",
548
+ "editorMode": "code"
549
+ }
550
+ ],
551
+ "fieldConfig": {
552
+ "defaults": {
553
+ "unit": "short",
554
+ "custom": {
555
+ "drawStyle": "line",
556
+ "fillOpacity": 25,
557
+ "stacking": {
558
+ "mode": "normal"
559
+ },
560
+ "lineWidth": 1,
561
+ "lineInterpolation": "smooth",
562
+ "showPoints": "never"
563
+ }
564
+ },
565
+ "overrides": []
566
+ },
567
+ "options": {}
568
+ },
569
+ {
570
+ "id": 11,
571
+ "title": "Token-type split /day (raw)",
572
+ "type": "timeseries",
573
+ "datasource": {
574
+ "type": "grafana-postgresql-datasource",
575
+ "uid": "agents-timescaledb"
576
+ },
577
+ "gridPos": {
578
+ "h": 8,
579
+ "w": 12,
580
+ "x": 0,
581
+ "y": 15
582
+ },
583
+ "targets": [
584
+ {
585
+ "refId": "A",
586
+ "datasource": {
587
+ "type": "grafana-postgresql-datasource",
588
+ "uid": "agents-timescaledb"
589
+ },
590
+ "format": "time_series",
591
+ "rawSql": "SELECT time_bucket('1 day', ts, current_setting('TimeZone')) AS time, t.kind AS metric, sum(t.val) AS tokens\nFROM agents.usage_event e\nCROSS JOIN LATERAL (VALUES\n ('input', e.input_tokens), ('output_excl_reasoning', e.output_tokens - coalesce(e.reasoning_tokens, 0)),\n ('cache_read', e.cache_read_tokens), ('cache_write', e.cache_write_tokens),\n ('reasoning', coalesce(e.reasoning_tokens, 0))) AS t(kind, val)\nWHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])\nGROUP BY 1, t.kind ORDER BY time ASC, metric ASC;",
592
+ "editorMode": "code"
593
+ }
594
+ ],
595
+ "fieldConfig": {
596
+ "defaults": {
597
+ "unit": "short",
598
+ "custom": {
599
+ "drawStyle": "line",
600
+ "fillOpacity": 25,
601
+ "stacking": {
602
+ "mode": "normal"
603
+ },
604
+ "lineWidth": 1,
605
+ "lineInterpolation": "smooth",
606
+ "showPoints": "never"
607
+ }
608
+ },
609
+ "overrides": []
610
+ },
611
+ "options": {},
612
+ "description": "Raw usage_event. Reasoning is a subset of output, so output is shown excluding reasoning and the stacked slices sum to the true total. Follows the time picker (raw rows are kept for good)."
613
+ },
614
+ {
615
+ "id": 12,
616
+ "title": "Turns/day by agent",
617
+ "type": "timeseries",
618
+ "datasource": {
619
+ "type": "grafana-postgresql-datasource",
620
+ "uid": "agents-timescaledb"
621
+ },
622
+ "gridPos": {
623
+ "h": 8,
624
+ "w": 12,
625
+ "x": 12,
626
+ "y": 15
627
+ },
628
+ "targets": [
629
+ {
630
+ "refId": "A",
631
+ "datasource": {
632
+ "type": "grafana-postgresql-datasource",
633
+ "uid": "agents-timescaledb"
634
+ },
635
+ "format": "time_series",
636
+ "rawSql": "SELECT time_bucket('1 day', ts, current_setting('TimeZone')) AS time, agent AS metric, count(*) AS turns\nFROM agents.usage_event WHERE $__timeFilter(ts) AND kind = 'agent' AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])\nGROUP BY 1, agent ORDER BY time ASC, metric ASC;",
637
+ "editorMode": "code"
638
+ }
639
+ ],
640
+ "fieldConfig": {
641
+ "defaults": {
642
+ "unit": "short",
643
+ "custom": {
644
+ "drawStyle": "line",
645
+ "fillOpacity": 0,
646
+ "stacking": {
647
+ "mode": "none"
648
+ },
649
+ "lineWidth": 2,
650
+ "lineInterpolation": "smooth",
651
+ "showPoints": "never"
652
+ }
653
+ },
654
+ "overrides": []
655
+ },
656
+ "options": {},
657
+ "description": "Assistant turns (activity volume) per tool."
658
+ },
659
+ {
660
+ "id": 13,
661
+ "title": "Breakdowns",
662
+ "type": "row",
663
+ "collapsed": false,
664
+ "gridPos": {
665
+ "h": 1,
666
+ "w": 24,
667
+ "x": 0,
668
+ "y": 23
669
+ },
670
+ "panels": []
671
+ },
672
+ {
673
+ "id": 14,
674
+ "title": "Top-10 models",
675
+ "type": "bargauge",
676
+ "datasource": {
677
+ "type": "grafana-postgresql-datasource",
678
+ "uid": "agents-timescaledb"
679
+ },
680
+ "gridPos": {
681
+ "h": 8,
682
+ "w": 8,
683
+ "x": 0,
684
+ "y": 24
685
+ },
686
+ "targets": [
687
+ {
688
+ "refId": "A",
689
+ "datasource": {
690
+ "type": "grafana-postgresql-datasource",
691
+ "uid": "agents-timescaledb"
692
+ },
693
+ "format": "table",
694
+ "rawSql": "SELECT coalesce(model,'unknown') AS metric, sum(total_tokens) AS tokens\nFROM agents.usage_event WHERE $__timeFilter(ts) AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])\nGROUP BY 1 ORDER BY tokens DESC LIMIT 10;",
695
+ "editorMode": "code"
696
+ }
697
+ ],
698
+ "fieldConfig": {
699
+ "defaults": {
700
+ "unit": "short"
701
+ },
702
+ "overrides": []
703
+ },
704
+ "options": {
705
+ "reduceOptions": {
706
+ "calcs": [
707
+ "lastNotNull"
708
+ ],
709
+ "fields": "",
710
+ "values": true
711
+ },
712
+ "displayMode": "gradient",
713
+ "orientation": "horizontal"
714
+ }
715
+ },
716
+ {
717
+ "id": 15,
718
+ "title": "Lifetime tokens by machine",
719
+ "type": "table",
720
+ "datasource": {
721
+ "type": "grafana-postgresql-datasource",
722
+ "uid": "agents-timescaledb"
723
+ },
724
+ "gridPos": {
725
+ "h": 8,
726
+ "w": 8,
727
+ "x": 8,
728
+ "y": 24
729
+ },
730
+ "targets": [
731
+ {
732
+ "refId": "A",
733
+ "datasource": {
734
+ "type": "grafana-postgresql-datasource",
735
+ "uid": "agents-timescaledb"
736
+ },
737
+ "format": "table",
738
+ "rawSql": "SELECT machine AS \"Machine\", sum(total_tokens) AS \"Tokens\", sum(turns) AS \"Turns\",\n round(100.0 * sum(cache_read_tokens) / nullif(sum(cache_read_tokens + input_tokens), 0), 1) AS \"Cache %\",\n round(100.0 * sum(total_tokens) / sum(sum(total_tokens)) OVER (), 1) AS \"Share %\"\nFROM agents.usage_daily WHERE kind = 'agent' AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]) GROUP BY machine ORDER BY \"Tokens\" DESC;",
739
+ "editorMode": "code"
740
+ }
741
+ ],
742
+ "fieldConfig": {
743
+ "defaults": {},
744
+ "overrides": []
745
+ },
746
+ "options": {},
747
+ "description": "Lifetime (ignores time picker); respects $machine/$agent/$model."
748
+ },
749
+ {
750
+ "id": 16,
751
+ "title": "Lifetime tokens by agent + model",
752
+ "type": "table",
753
+ "datasource": {
754
+ "type": "grafana-postgresql-datasource",
755
+ "uid": "agents-timescaledb"
756
+ },
757
+ "gridPos": {
758
+ "h": 8,
759
+ "w": 8,
760
+ "x": 16,
761
+ "y": 24
762
+ },
763
+ "targets": [
764
+ {
765
+ "refId": "A",
766
+ "datasource": {
767
+ "type": "grafana-postgresql-datasource",
768
+ "uid": "agents-timescaledb"
769
+ },
770
+ "format": "table",
771
+ "rawSql": "SELECT agent AS \"Agent\", coalesce(model,'unknown') AS \"Model\", sum(total_tokens) AS \"Tokens\",\n sum(turns) AS \"Turns\",\n round(100.0 * sum(cache_read_tokens) / nullif(sum(cache_read_tokens + input_tokens), 0), 1) AS \"Cache %\"\nFROM agents.usage_daily WHERE kind = 'agent' AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]) GROUP BY 1, 2 ORDER BY \"Tokens\" DESC;",
772
+ "editorMode": "code"
773
+ }
774
+ ],
775
+ "fieldConfig": {
776
+ "defaults": {},
777
+ "overrides": []
778
+ },
779
+ "options": {},
780
+ "description": "Lifetime (ignores time picker); respects filters."
781
+ },
782
+ {
783
+ "id": 17,
784
+ "title": "Cadence (raw usage_event)",
785
+ "type": "row",
786
+ "collapsed": false,
787
+ "gridPos": {
788
+ "h": 1,
789
+ "w": 24,
790
+ "x": 0,
791
+ "y": 32
792
+ },
793
+ "panels": []
794
+ },
795
+ {
796
+ "id": 18,
797
+ "title": "Agent activity (weekday x hour)",
798
+ "type": "heatmap",
799
+ "datasource": {
800
+ "type": "grafana-postgresql-datasource",
801
+ "uid": "agents-timescaledb"
802
+ },
803
+ "gridPos": {
804
+ "h": 9,
805
+ "w": 12,
806
+ "x": 0,
807
+ "y": 33
808
+ },
809
+ "targets": [
810
+ {
811
+ "refId": "A",
812
+ "datasource": {
813
+ "type": "grafana-postgresql-datasource",
814
+ "uid": "agents-timescaledb"
815
+ },
816
+ "format": "table",
817
+ "rawSql": "WITH e AS (\n SELECT extract(hour FROM ts)::int AS hour, extract(isodow FROM ts)::int AS dow, total_tokens AS tokens\n FROM agents.usage_event WHERE $__timeFilter(ts) AND kind = 'agent' AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]))\nSELECT g.hour AS \"Hour\",\n coalesce(sum(tokens) FILTER (WHERE dow=1),0) AS \"Mon\",\n coalesce(sum(tokens) FILTER (WHERE dow=2),0) AS \"Tue\",\n coalesce(sum(tokens) FILTER (WHERE dow=3),0) AS \"Wed\",\n coalesce(sum(tokens) FILTER (WHERE dow=4),0) AS \"Thu\",\n coalesce(sum(tokens) FILTER (WHERE dow=5),0) AS \"Fri\",\n coalesce(sum(tokens) FILTER (WHERE dow=6),0) AS \"Sat\",\n coalesce(sum(tokens) FILTER (WHERE dow=7),0) AS \"Sun\"\nFROM generate_series(0,23) AS g(hour) LEFT JOIN e ON e.hour = g.hour\nGROUP BY g.hour ORDER BY g.hour;",
818
+ "editorMode": "code"
819
+ }
820
+ ],
821
+ "fieldConfig": {
822
+ "defaults": {},
823
+ "overrides": []
824
+ },
825
+ "options": {
826
+ "calculate": false,
827
+ "cellGap": 2,
828
+ "rowsFrame": {
829
+ "layout": "auto"
830
+ },
831
+ "yAxis": {
832
+ "axisLabel": "Weekday"
833
+ },
834
+ "color": {
835
+ "mode": "scheme",
836
+ "scheme": "Greens",
837
+ "steps": 64,
838
+ "reverse": true,
839
+ "scale": "exponential",
840
+ "exponent": 0.4
841
+ }
842
+ },
843
+ "description": "Agent token volume by hour-of-day (x) and weekday (y). Darker = more (exponential colour scale - the data is very skewed, so a linear scale would look near-binary). Typical-week rhythm: the whole range is folded into a fixed 7x24 grid, so the axes stay put - only intensities move. Raw usage_event; follows the picker."
844
+ },
845
+ {
846
+ "id": 19,
847
+ "title": "When I prompt (weekday x hour)",
848
+ "type": "heatmap",
849
+ "datasource": {
850
+ "type": "grafana-postgresql-datasource",
851
+ "uid": "agents-timescaledb"
852
+ },
853
+ "gridPos": {
854
+ "h": 9,
855
+ "w": 12,
856
+ "x": 12,
857
+ "y": 33
858
+ },
859
+ "targets": [
860
+ {
861
+ "refId": "A",
862
+ "datasource": {
863
+ "type": "grafana-postgresql-datasource",
864
+ "uid": "agents-timescaledb"
865
+ },
866
+ "format": "table",
867
+ "rawSql": "WITH e AS (\n SELECT extract(hour FROM ts)::int AS hour, extract(isodow FROM ts)::int AS dow\n FROM agents.usage_event WHERE $__timeFilter(ts) AND kind = 'human' AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[]))\nSELECT g.hour AS \"Hour\",\n coalesce(count(e.hour) FILTER (WHERE dow=1),0) AS \"Mon\",\n coalesce(count(e.hour) FILTER (WHERE dow=2),0) AS \"Tue\",\n coalesce(count(e.hour) FILTER (WHERE dow=3),0) AS \"Wed\",\n coalesce(count(e.hour) FILTER (WHERE dow=4),0) AS \"Thu\",\n coalesce(count(e.hour) FILTER (WHERE dow=5),0) AS \"Fri\",\n coalesce(count(e.hour) FILTER (WHERE dow=6),0) AS \"Sat\",\n coalesce(count(e.hour) FILTER (WHERE dow=7),0) AS \"Sun\"\nFROM generate_series(0,23) AS g(hour) LEFT JOIN e ON e.hour = g.hour\nGROUP BY g.hour ORDER BY g.hour;",
868
+ "editorMode": "code"
869
+ }
870
+ ],
871
+ "fieldConfig": {
872
+ "defaults": {},
873
+ "overrides": []
874
+ },
875
+ "options": {
876
+ "calculate": false,
877
+ "cellGap": 2,
878
+ "rowsFrame": {
879
+ "layout": "auto"
880
+ },
881
+ "yAxis": {
882
+ "axisLabel": "Weekday"
883
+ },
884
+ "color": {
885
+ "mode": "scheme",
886
+ "scheme": "Blues",
887
+ "steps": 64,
888
+ "reverse": true,
889
+ "scale": "exponential",
890
+ "exponent": 0.4
891
+ }
892
+ },
893
+ "description": "Your INTERACTIVE prompts by hour-of-day (x) and weekday (y). Darker = more. Hours and days are in the sink time zone (AUT_TZ). Tooltip: top number = hour, Value = prompt count, Name = weekday."
894
+ },
895
+ {
896
+ "id": 20,
897
+ "title": "Projects (raw usage_event)",
898
+ "type": "row",
899
+ "collapsed": false,
900
+ "gridPos": {
901
+ "h": 1,
902
+ "w": 24,
903
+ "x": 0,
904
+ "y": 42
905
+ },
906
+ "panels": []
907
+ },
908
+ {
909
+ "id": 21,
910
+ "title": "Top projects",
911
+ "type": "table",
912
+ "datasource": {
913
+ "type": "grafana-postgresql-datasource",
914
+ "uid": "agents-timescaledb"
915
+ },
916
+ "gridPos": {
917
+ "h": 10,
918
+ "w": 24,
919
+ "x": 0,
920
+ "y": 43
921
+ },
922
+ "targets": [
923
+ {
924
+ "refId": "A",
925
+ "datasource": {
926
+ "type": "grafana-postgresql-datasource",
927
+ "uid": "agents-timescaledb"
928
+ },
929
+ "format": "table",
930
+ "rawSql": "WITH n AS (\n SELECT nullif(regexp_replace(coalesce(project,'(none)'), '^-+|-+$', '', 'g'), '') AS proj,\n agent, total_tokens, cache_read_tokens, input_tokens, ts\n FROM agents.usage_event WHERE $__timeFilter(ts) AND kind = 'agent' AND machine = ANY(ARRAY[${machine:sqlstring}]::text[])\nAND agent = ANY(ARRAY[${agent:sqlstring}]::text[])\nAND coalesce(model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]))\nSELECT coalesce(proj,'(none)') AS \"Project\",\n string_agg(DISTINCT agent, ', ' ORDER BY agent) AS \"Agents\",\n sum(total_tokens) AS \"Tokens\",\n count(*) AS \"Turns\",\n round(100.0 * sum(cache_read_tokens) / nullif(sum(cache_read_tokens + input_tokens), 0), 1) AS \"Cache %\",\n max(ts)::date AS \"Last active\"\nFROM n GROUP BY 1 ORDER BY \"Tokens\" DESC LIMIT 20;",
931
+ "editorMode": "code"
932
+ }
933
+ ],
934
+ "fieldConfig": {
935
+ "defaults": {},
936
+ "overrides": []
937
+ },
938
+ "options": {},
939
+ "description": "Raw usage_event, follows the time picker. 'Agents' = which tools worked this project."
940
+ },
941
+ {
942
+ "id": 22,
943
+ "title": "Retention & accuracy note",
944
+ "type": "text",
945
+ "datasource": {
946
+ "type": "grafana-postgresql-datasource",
947
+ "uid": "agents-timescaledb"
948
+ },
949
+ "gridPos": {
950
+ "h": 3,
951
+ "w": 24,
952
+ "x": 0,
953
+ "y": 76
954
+ },
955
+ "targets": [],
956
+ "fieldConfig": {
957
+ "defaults": {},
958
+ "overrides": []
959
+ },
960
+ "options": {
961
+ "mode": "markdown",
962
+ "content": "**Range-following** panels (KPIs, tokens/turns by agent/model, Top-10 models, Token-type split, Weekday x hour, Top projects) read raw `usage_event` (kept for good) so any window - including 'Last 24 hours' - is exact and always current. The **Lifetime** tables read the `usage_daily` rollup (all-time, ignores the picker). **$ (API-equivalent)** is shown in the **Cost** section above, priced from `agents.model_price` (rates from a price sync job; aut does not fill this table yet) - notional, since subscriptions are flat-rate; models absent from the price table drop out, never guessed. `cursor` (IDE) has no per-message model (`unknown`) and no cache attribution -- a lower bound; the accurate Cursor path is its usage API (opt-in)."
963
+ }
964
+ },
965
+ {
966
+ "id": 23,
967
+ "type": "row",
968
+ "title": "💲 Cost (API-equivalent $)",
969
+ "collapsed": false,
970
+ "panels": [],
971
+ "gridPos": {
972
+ "x": 0,
973
+ "y": 53,
974
+ "w": 24,
975
+ "h": 1
976
+ }
977
+ },
978
+ {
979
+ "id": 24,
980
+ "type": "stat",
981
+ "title": "Total API-equivalent $",
982
+ "description": "Sum over the selected range. Notional / API-equivalent.",
983
+ "datasource": {
984
+ "type": "grafana-postgresql-datasource",
985
+ "uid": "agents-timescaledb"
986
+ },
987
+ "gridPos": {
988
+ "x": 0,
989
+ "y": 54,
990
+ "w": 8,
991
+ "h": 5
992
+ },
993
+ "targets": [
994
+ {
995
+ "refId": "A",
996
+ "datasource": {
997
+ "type": "grafana-postgresql-datasource",
998
+ "uid": "agents-timescaledb"
999
+ },
1000
+ "format": "table",
1001
+ "rawQuery": true,
1002
+ "rawSql": "SELECT sum((u.input_tokens*p.input_cost + u.output_tokens*p.output_cost + u.cache_read_tokens*coalesce(p.cache_read_cost,p.input_cost) + u.cache_write_tokens*coalesce(p.cache_write_cost,p.input_cost))) AS \"$\" FROM agents.usage_event u JOIN agents.model_price p ON lower(u.model)=p.model_key WHERE u.kind='agent' AND $__timeFilter(ts) AND u.machine = ANY(ARRAY[${machine:sqlstring}]::text[]) AND u.agent = ANY(ARRAY[${agent:sqlstring}]::text[]) AND coalesce(u.model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])"
1003
+ }
1004
+ ],
1005
+ "fieldConfig": {
1006
+ "defaults": {
1007
+ "unit": "currencyUSD",
1008
+ "decimals": 0
1009
+ },
1010
+ "overrides": []
1011
+ },
1012
+ "options": {
1013
+ "reduceOptions": {
1014
+ "calcs": [
1015
+ "lastNotNull"
1016
+ ],
1017
+ "fields": "",
1018
+ "values": false
1019
+ },
1020
+ "colorMode": "value",
1021
+ "graphMode": "none",
1022
+ "textMode": "value"
1023
+ }
1024
+ },
1025
+ {
1026
+ "id": 25,
1027
+ "type": "stat",
1028
+ "title": "Caching saved (x)",
1029
+ "description": "Naive cost (cache reads at full input rate) / actual cost.",
1030
+ "datasource": {
1031
+ "type": "grafana-postgresql-datasource",
1032
+ "uid": "agents-timescaledb"
1033
+ },
1034
+ "gridPos": {
1035
+ "x": 8,
1036
+ "y": 54,
1037
+ "w": 8,
1038
+ "h": 5
1039
+ },
1040
+ "targets": [
1041
+ {
1042
+ "refId": "A",
1043
+ "datasource": {
1044
+ "type": "grafana-postgresql-datasource",
1045
+ "uid": "agents-timescaledb"
1046
+ },
1047
+ "format": "table",
1048
+ "rawQuery": true,
1049
+ "rawSql": "SELECT sum((u.input_tokens*p.input_cost + u.output_tokens*p.output_cost + u.cache_read_tokens*p.input_cost + u.cache_write_tokens*coalesce(p.cache_write_cost,p.input_cost)))/nullif(sum((u.input_tokens*p.input_cost + u.output_tokens*p.output_cost + u.cache_read_tokens*coalesce(p.cache_read_cost,p.input_cost) + u.cache_write_tokens*coalesce(p.cache_write_cost,p.input_cost))),0) AS \"saved (x)\" FROM agents.usage_event u JOIN agents.model_price p ON lower(u.model)=p.model_key WHERE u.kind='agent' AND $__timeFilter(ts) AND u.machine = ANY(ARRAY[${machine:sqlstring}]::text[]) AND u.agent = ANY(ARRAY[${agent:sqlstring}]::text[]) AND coalesce(u.model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])"
1050
+ }
1051
+ ],
1052
+ "fieldConfig": {
1053
+ "defaults": {
1054
+ "unit": "none",
1055
+ "decimals": 2
1056
+ },
1057
+ "overrides": []
1058
+ },
1059
+ "options": {
1060
+ "reduceOptions": {
1061
+ "calcs": [
1062
+ "lastNotNull"
1063
+ ],
1064
+ "fields": "",
1065
+ "values": false
1066
+ },
1067
+ "colorMode": "value",
1068
+ "graphMode": "none",
1069
+ "textMode": "value"
1070
+ }
1071
+ },
1072
+ {
1073
+ "id": 26,
1074
+ "type": "stat",
1075
+ "title": "$ per active day",
1076
+ "description": "Cost / distinct active days.",
1077
+ "datasource": {
1078
+ "type": "grafana-postgresql-datasource",
1079
+ "uid": "agents-timescaledb"
1080
+ },
1081
+ "gridPos": {
1082
+ "x": 16,
1083
+ "y": 54,
1084
+ "w": 8,
1085
+ "h": 5
1086
+ },
1087
+ "targets": [
1088
+ {
1089
+ "refId": "A",
1090
+ "datasource": {
1091
+ "type": "grafana-postgresql-datasource",
1092
+ "uid": "agents-timescaledb"
1093
+ },
1094
+ "format": "table",
1095
+ "rawQuery": true,
1096
+ "rawSql": "SELECT sum((u.input_tokens*p.input_cost + u.output_tokens*p.output_cost + u.cache_read_tokens*coalesce(p.cache_read_cost,p.input_cost) + u.cache_write_tokens*coalesce(p.cache_write_cost,p.input_cost)))/nullif(count(DISTINCT ts::date),0) AS \"$/day\" FROM agents.usage_event u JOIN agents.model_price p ON lower(u.model)=p.model_key WHERE u.kind='agent' AND $__timeFilter(ts) AND u.machine = ANY(ARRAY[${machine:sqlstring}]::text[]) AND u.agent = ANY(ARRAY[${agent:sqlstring}]::text[]) AND coalesce(u.model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[])"
1097
+ }
1098
+ ],
1099
+ "fieldConfig": {
1100
+ "defaults": {
1101
+ "unit": "currencyUSD",
1102
+ "decimals": 2
1103
+ },
1104
+ "overrides": []
1105
+ },
1106
+ "options": {
1107
+ "reduceOptions": {
1108
+ "calcs": [
1109
+ "lastNotNull"
1110
+ ],
1111
+ "fields": "",
1112
+ "values": false
1113
+ },
1114
+ "colorMode": "value",
1115
+ "graphMode": "none",
1116
+ "textMode": "value"
1117
+ }
1118
+ },
1119
+ {
1120
+ "id": 27,
1121
+ "type": "timeseries",
1122
+ "title": "Cost per month (by agent)",
1123
+ "datasource": {
1124
+ "type": "grafana-postgresql-datasource",
1125
+ "uid": "agents-timescaledb"
1126
+ },
1127
+ "gridPos": {
1128
+ "x": 0,
1129
+ "y": 59,
1130
+ "w": 24,
1131
+ "h": 8
1132
+ },
1133
+ "targets": [
1134
+ {
1135
+ "refId": "A",
1136
+ "datasource": {
1137
+ "type": "grafana-postgresql-datasource",
1138
+ "uid": "agents-timescaledb"
1139
+ },
1140
+ "format": "time_series",
1141
+ "rawQuery": true,
1142
+ "rawSql": "SELECT date_trunc('month',ts) AS \"time\", u.agent AS metric, sum((u.input_tokens*p.input_cost + u.output_tokens*p.output_cost + u.cache_read_tokens*coalesce(p.cache_read_cost,p.input_cost) + u.cache_write_tokens*coalesce(p.cache_write_cost,p.input_cost))) AS \"$\" FROM agents.usage_event u JOIN agents.model_price p ON lower(u.model)=p.model_key WHERE u.kind='agent' AND $__timeFilter(ts) AND u.machine = ANY(ARRAY[${machine:sqlstring}]::text[]) AND u.agent = ANY(ARRAY[${agent:sqlstring}]::text[]) AND coalesce(u.model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]) GROUP BY 1,u.agent ORDER BY 1"
1143
+ }
1144
+ ],
1145
+ "fieldConfig": {
1146
+ "defaults": {
1147
+ "unit": "currencyUSD",
1148
+ "custom": {
1149
+ "drawStyle": "bars",
1150
+ "stacking": {
1151
+ "mode": "normal"
1152
+ },
1153
+ "fillOpacity": 70,
1154
+ "lineWidth": 1
1155
+ }
1156
+ },
1157
+ "overrides": []
1158
+ },
1159
+ "options": {
1160
+ "legend": {
1161
+ "displayMode": "table",
1162
+ "placement": "right",
1163
+ "calcs": [
1164
+ "sum"
1165
+ ]
1166
+ },
1167
+ "tooltip": {
1168
+ "mode": "multi"
1169
+ }
1170
+ }
1171
+ },
1172
+ {
1173
+ "id": 28,
1174
+ "type": "table",
1175
+ "title": "Cost by model (top 15)",
1176
+ "datasource": {
1177
+ "type": "grafana-postgresql-datasource",
1178
+ "uid": "agents-timescaledb"
1179
+ },
1180
+ "gridPos": {
1181
+ "x": 0,
1182
+ "y": 67,
1183
+ "w": 12,
1184
+ "h": 9
1185
+ },
1186
+ "targets": [
1187
+ {
1188
+ "refId": "A",
1189
+ "datasource": {
1190
+ "type": "grafana-postgresql-datasource",
1191
+ "uid": "agents-timescaledb"
1192
+ },
1193
+ "format": "table",
1194
+ "rawQuery": true,
1195
+ "rawSql": "SELECT u.model AS \"model\", count(*) AS \"turns\", sum((u.input_tokens*p.input_cost + u.output_tokens*p.output_cost + u.cache_read_tokens*coalesce(p.cache_read_cost,p.input_cost) + u.cache_write_tokens*coalesce(p.cache_write_cost,p.input_cost)))::numeric(14,2) AS \"$\" FROM agents.usage_event u JOIN agents.model_price p ON lower(u.model)=p.model_key WHERE u.kind='agent' AND $__timeFilter(ts) AND u.machine = ANY(ARRAY[${machine:sqlstring}]::text[]) AND u.agent = ANY(ARRAY[${agent:sqlstring}]::text[]) AND coalesce(u.model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]) GROUP BY 1 ORDER BY 3 DESC NULLS LAST LIMIT 15"
1196
+ }
1197
+ ],
1198
+ "fieldConfig": {
1199
+ "defaults": {},
1200
+ "overrides": [
1201
+ {
1202
+ "matcher": {
1203
+ "id": "byName",
1204
+ "options": "$"
1205
+ },
1206
+ "properties": [
1207
+ {
1208
+ "id": "unit",
1209
+ "value": "currencyUSD"
1210
+ },
1211
+ {
1212
+ "id": "custom.cellOptions",
1213
+ "value": {
1214
+ "type": "color-background"
1215
+ }
1216
+ }
1217
+ ]
1218
+ }
1219
+ ]
1220
+ },
1221
+ "options": {
1222
+ "showHeader": true
1223
+ }
1224
+ },
1225
+ {
1226
+ "id": 29,
1227
+ "type": "table",
1228
+ "title": "Cost by agent",
1229
+ "datasource": {
1230
+ "type": "grafana-postgresql-datasource",
1231
+ "uid": "agents-timescaledb"
1232
+ },
1233
+ "gridPos": {
1234
+ "x": 12,
1235
+ "y": 67,
1236
+ "w": 12,
1237
+ "h": 9
1238
+ },
1239
+ "targets": [
1240
+ {
1241
+ "refId": "A",
1242
+ "datasource": {
1243
+ "type": "grafana-postgresql-datasource",
1244
+ "uid": "agents-timescaledb"
1245
+ },
1246
+ "format": "table",
1247
+ "rawQuery": true,
1248
+ "rawSql": "SELECT u.agent AS \"agent\", count(*) AS \"turns\", sum((u.input_tokens*p.input_cost + u.output_tokens*p.output_cost + u.cache_read_tokens*coalesce(p.cache_read_cost,p.input_cost) + u.cache_write_tokens*coalesce(p.cache_write_cost,p.input_cost)))::numeric(14,2) AS \"$\" FROM agents.usage_event u JOIN agents.model_price p ON lower(u.model)=p.model_key WHERE u.kind='agent' AND $__timeFilter(ts) AND u.machine = ANY(ARRAY[${machine:sqlstring}]::text[]) AND u.agent = ANY(ARRAY[${agent:sqlstring}]::text[]) AND coalesce(u.model,'unknown') = ANY(ARRAY[${model:sqlstring}]::text[]) GROUP BY 1 ORDER BY 3 DESC NULLS LAST"
1249
+ }
1250
+ ],
1251
+ "fieldConfig": {
1252
+ "defaults": {},
1253
+ "overrides": [
1254
+ {
1255
+ "matcher": {
1256
+ "id": "byName",
1257
+ "options": "$"
1258
+ },
1259
+ "properties": [
1260
+ {
1261
+ "id": "unit",
1262
+ "value": "currencyUSD"
1263
+ },
1264
+ {
1265
+ "id": "custom.cellOptions",
1266
+ "value": {
1267
+ "type": "color-background"
1268
+ }
1269
+ }
1270
+ ]
1271
+ }
1272
+ ]
1273
+ },
1274
+ "options": {
1275
+ "showHeader": true
1276
+ }
1277
+ }
1278
+ ]
1279
+ }