-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdb.py
More file actions
1678 lines (1534 loc) · 79.6 KB
/
Copy pathdb.py
File metadata and controls
1678 lines (1534 loc) · 79.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import os
import sqlite3
from datetime import datetime, timezone
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "tracker.db")
def _connect(path=None):
"""Open tracker.db with WAL + a bounded busy-timeout.
WAL removes the rollback-journal lock-upgrade deadlock that caused
`database is locked`; busy_timeout makes a blocked writer wait (off the
request path) instead of failing instantly. DB_PATH is resolved at call
time so tests that monkeypatch db.DB_PATH are honored.
"""
con = sqlite3.connect(path or DB_PATH, timeout=3.0)
con.execute("PRAGMA journal_mode=WAL")
con.execute("PRAGMA busy_timeout=3000")
con.execute("PRAGMA synchronous=NORMAL")
return con
PRICING = { # all prices in $/million tokens
# ── Anthropic Claude 4 ─────────────────────────────────────────────────────
"claude-fable-5": {"input": 10.0, "output": 50.0},
"claude-opus-4-8": {"input": 5.0, "output": 25.0},
"claude-opus-4-7": {"input": 5.0, "output": 25.0},
"claude-opus-4-6": {"input": 5.0, "output": 25.0},
"claude-opus-4-5": {"input": 5.0, "output": 25.0},
"claude-opus-4-1": {"input": 15.0, "output": 75.0},
"claude-opus-4-20250514": {"input": 15.0, "output": 75.0},
"claude-sonnet-4-6": {"input": 3.0, "output": 15.0},
"claude-sonnet-4-5": {"input": 3.0, "output": 15.0},
"claude-sonnet-4-20250514": {"input": 3.0, "output": 15.0},
"claude-haiku-4-5": {"input": 1.0, "output": 5.0},
"claude-haiku-4-5-20251001": {"input": 1.0, "output": 5.0},
# ── Anthropic Claude 3.x ───────────────────────────────────────────────────
"claude-3-5-sonnet-20241022": {"input": 3.0, "output": 15.0},
"claude-3-5-sonnet-20240620": {"input": 3.0, "output": 15.0},
"claude-3-5-haiku-20241022": {"input": 0.8, "output": 4.0},
"claude-3-opus-20240229": {"input": 15.0, "output": 75.0},
"claude-3-sonnet-20240229": {"input": 3.0, "output": 15.0},
"claude-3-haiku-20240307": {"input": 0.25, "output": 1.25},
# ── OpenAI GPT-4.1 ────────────────────────────────────────────────────────
"gpt-4.1": {"input": 2.0, "output": 8.0},
"gpt-4.1-mini": {"input": 0.4, "output": 1.6},
"gpt-4.1-nano": {"input": 0.1, "output": 0.4},
# ── OpenAI GPT-4o ─────────────────────────────────────────────────────────
"gpt-5": {"input": 15.0, "output": 60.0},
"gpt-5-mini": {"input": 1.25, "output": 5.0},
"gpt-4o": {"input": 2.5, "output": 10.0},
"gpt-4o-2024-11-20": {"input": 2.5, "output": 10.0},
"gpt-4o-2024-08-06": {"input": 2.5, "output": 10.0},
"gpt-4o-2024-05-13": {"input": 5.0, "output": 15.0},
"gpt-4o-mini": {"input": 0.15, "output": 0.60},
"gpt-4o-mini-2024-07-18": {"input": 0.15, "output": 0.60},
"chatgpt-4o-latest": {"input": 5.0, "output": 15.0},
# ── OpenAI o-series (reasoning) ───────────────────────────────────────────
"o1": {"input": 15.0, "output": 60.0},
"o1-2024-12-17": {"input": 15.0, "output": 60.0},
"o1-pro": {"input": 150.0, "output": 600.0},
"o1-mini": {"input": 1.1, "output": 4.4},
"o1-mini-2024-09-12": {"input": 1.1, "output": 4.4},
"o1-preview": {"input": 15.0, "output": 60.0},
"o3": {"input": 10.0, "output": 40.0},
"o3-mini": {"input": 1.1, "output": 4.4},
"o3-mini-2025-01-31": {"input": 1.1, "output": 4.4},
"o4-mini": {"input": 1.1, "output": 4.4},
"o4-mini-2025-04-16": {"input": 1.1, "output": 4.4},
# ── OpenAI GPT-4 / GPT-3.5 ────────────────────────────────────────────────
"gpt-4-turbo": {"input": 10.0, "output": 30.0},
"gpt-4-turbo-2024-04-09": {"input": 10.0, "output": 30.0},
"gpt-4": {"input": 30.0, "output": 60.0},
"gpt-4-32k": {"input": 60.0, "output": 120.0},
"gpt-3.5-turbo": {"input": 0.5, "output": 1.5},
"gpt-3.5-turbo-0125": {"input": 0.5, "output": 1.5},
# ── Azure OpenAI ──────────────────────────────────────────────────────────
"azure_ai/gpt-5.4": {"input": 2.5, "output": 15.0},
"azure_ai/gpt-5.4-mini": {"input": 0.75, "output": 4.5},
"azure_ai/gpt-5.4-nano": {"input": 0.20, "output": 1.25},
"azure_ai/gpt-5.4-pro": {"input": 30.0, "output": 180.0},
# ── Google Gemini ──────────────────────────────────────────────────────────
"gemini/gemini-2.5-pro": {"input": 1.25, "output": 10.0},
"gemini/gemini-2.5-pro-preview": {"input": 1.25, "output": 10.0},
"gemini/gemini-2.5-flash": {"input": 0.075, "output": 0.30},
"gemini/gemini-2.5-flash-8b": {"input": 0.0375, "output": 0.15},
"gemini/gemini-2.0-flash": {"input": 0.075, "output": 0.30},
"gemini/gemini-2.0-flash-lite": {"input": 0.0375, "output": 0.15},
"gemini/gemini-2.0-flash-thinking-exp": {"input": 0.075, "output": 0.35},
"gemini/gemini-1.5-pro": {"input": 1.25, "output": 5.0},
"gemini/gemini-1.5-flash": {"input": 0.075, "output": 0.30},
"gemini/gemini-1.5-flash-8b": {"input": 0.0375, "output": 0.15},
"gemini/gemini-exp-1206": {"input": 0.0, "output": 0.0},
"gemini-2.5-pro": {"input": 1.25, "output": 10.0},
"gemini-2.5-flash": {"input": 0.075, "output": 0.30},
"gemini-2.0-flash": {"input": 0.075, "output": 0.30},
"gemini-2.0-flash-lite": {"input": 0.0375, "output": 0.15},
"gemini-1.5-pro": {"input": 1.25, "output": 5.0},
"gemini-1.5-flash": {"input": 0.075, "output": 0.30},
"gemini-1.0-pro": {"input": 0.5, "output": 1.5},
# ── Google Vertex AI (vertex_ai/ prefix) ──────────────────────────────────
"vertex_ai/gemini-2.5-pro": {"input": 1.25, "output": 10.0},
"vertex_ai/gemini-2.0-flash-001": {"input": 0.075, "output": 0.30},
"vertex_ai/gemini-1.5-pro": {"input": 1.25, "output": 5.0},
"vertex_ai/gemini-1.5-flash": {"input": 0.075, "output": 0.30},
"vertex_ai/claude-sonnet-4-6": {"input": 3.0, "output": 15.0},
"vertex_ai/claude-opus-4-7": {"input": 5.0, "output": 25.0},
# ── Groq ──────────────────────────────────────────────────────────────────
"groq/llama-3.3-70b-versatile": {"input": 0.59, "output": 0.79},
"groq/llama-3.1-70b-versatile": {"input": 0.59, "output": 0.79},
"groq/llama-3.1-8b-instant": {"input": 0.05, "output": 0.08},
"groq/llama3-70b-8192": {"input": 0.59, "output": 0.79},
"groq/llama3-8b-8192": {"input": 0.05, "output": 0.08},
"groq/llama-3.2-90b-text-preview": {"input": 0.90, "output": 0.90},
"groq/llama-3.2-11b-text-preview": {"input": 0.18, "output": 0.18},
"groq/llama-3.2-3b-preview": {"input": 0.06, "output": 0.06},
"groq/llama-3.2-1b-preview": {"input": 0.04, "output": 0.04},
"groq/mixtral-8x7b-32768": {"input": 0.24, "output": 0.24},
"groq/gemma2-9b-it": {"input": 0.20, "output": 0.20},
"groq/gemma-7b-it": {"input": 0.07, "output": 0.07},
"groq/qwen-qwq-32b": {"input": 0.29, "output": 0.39},
"groq/deepseek-r1-distill-llama-70b": {"input": 0.75, "output": 0.99},
"groq/llama-4-scout-17b-16e-instruct": {"input": 0.11, "output": 0.34},
"groq/llama-4-maverick-17b-128e-instruct": {"input": 0.50, "output": 0.77},
"llama-3.3-70b-versatile": {"input": 0.59, "output": 0.79},
"llama-3.1-70b-versatile": {"input": 0.59, "output": 0.79},
"llama-3.1-8b-instant": {"input": 0.05, "output": 0.08},
"mixtral-8x7b-32768": {"input": 0.24, "output": 0.24},
# ── Mistral ───────────────────────────────────────────────────────────────
"mistral/mistral-large-latest": {"input": 2.0, "output": 6.0},
"mistral/mistral-large-2411": {"input": 2.0, "output": 6.0},
"mistral/mistral-medium-latest": {"input": 0.4, "output": 2.0},
"mistral/mistral-small-latest": {"input": 0.1, "output": 0.3},
"mistral/mistral-small-2409": {"input": 0.1, "output": 0.3},
"mistral/open-mistral-7b": {"input": 0.25, "output": 0.25},
"mistral/open-mixtral-8x7b": {"input": 0.7, "output": 0.7},
"mistral/open-mixtral-8x22b": {"input": 2.0, "output": 6.0},
"mistral/mistral-nemo": {"input": 0.15, "output": 0.15},
"mistral/open-mistral-nemo": {"input": 0.15, "output": 0.15},
"mistral/codestral-latest": {"input": 0.3, "output": 0.9},
"mistral/codestral-2501": {"input": 0.3, "output": 0.9},
"mistral/pixtral-large-latest": {"input": 2.0, "output": 6.0},
"mistral/pixtral-12b-2409": {"input": 0.15, "output": 0.15},
"mistral-large-latest": {"input": 2.0, "output": 6.0},
"mistral-small-latest": {"input": 0.1, "output": 0.3},
"codestral-latest": {"input": 0.3, "output": 0.9},
"open-mistral-nemo": {"input": 0.15, "output": 0.15},
# ── DeepSeek ──────────────────────────────────────────────────────────────
"deepseek/deepseek-chat": {"input": 0.14, "output": 0.28},
"deepseek/deepseek-v3": {"input": 0.14, "output": 0.28},
"deepseek/deepseek-v3-0324": {"input": 0.27, "output": 1.10},
"deepseek/deepseek-reasoner": {"input": 0.55, "output": 2.19},
"deepseek/deepseek-r1": {"input": 0.55, "output": 2.19},
"deepseek/deepseek-r1-zero": {"input": 0.55, "output": 2.19},
"deepseek-chat": {"input": 0.14, "output": 0.28},
"deepseek-reasoner": {"input": 0.55, "output": 2.19},
"deepseek-v3": {"input": 0.14, "output": 0.28},
# ── xAI Grok ──────────────────────────────────────────────────────────────
"xai/grok-3": {"input": 3.0, "output": 15.0},
"xai/grok-3-beta": {"input": 3.0, "output": 15.0},
"xai/grok-3-mini": {"input": 0.3, "output": 0.5},
"xai/grok-3-mini-beta": {"input": 0.3, "output": 0.5},
"xai/grok-3-fast": {"input": 5.0, "output": 25.0},
"xai/grok-2-1212": {"input": 2.0, "output": 10.0},
"xai/grok-2": {"input": 2.0, "output": 10.0},
"xai/grok-beta": {"input": 5.0, "output": 15.0},
"xai/grok-vision-beta": {"input": 5.0, "output": 15.0},
"grok-3": {"input": 3.0, "output": 15.0},
"grok-3-mini": {"input": 0.3, "output": 0.5},
"grok-2-1212": {"input": 2.0, "output": 10.0},
"grok-beta": {"input": 5.0, "output": 15.0},
# ── Perplexity ────────────────────────────────────────────────────────────
"perplexity/sonar-pro": {"input": 3.0, "output": 15.0},
"perplexity/sonar": {"input": 1.0, "output": 1.0},
"perplexity/sonar-reasoning-pro": {"input": 2.0, "output": 8.0},
"perplexity/sonar-reasoning": {"input": 1.0, "output": 5.0},
"perplexity/llama-3.1-sonar-huge-128k-online": {"input": 5.0, "output": 5.0},
"sonar-pro": {"input": 3.0, "output": 15.0},
"sonar": {"input": 1.0, "output": 1.0},
# ── Cohere ────────────────────────────────────────────────────────────────
"cohere/command-r-plus": {"input": 2.5, "output": 10.0},
"cohere/command-r-plus-08-2024": {"input": 2.5, "output": 10.0},
"cohere/command-r": {"input": 0.15, "output": 0.60},
"cohere/command-r-08-2024": {"input": 0.15, "output": 0.60},
"cohere/command-a-03-2025": {"input": 2.5, "output": 10.0},
"cohere/command-light": {"input": 0.3, "output": 0.6},
"command-r-plus": {"input": 2.5, "output": 10.0},
"command-r": {"input": 0.15, "output": 0.60},
"command-a-03-2025": {"input": 2.5, "output": 10.0},
# ── Cerebras ──────────────────────────────────────────────────────────────
"cerebras/llama-3.3-70b": {"input": 0.85, "output": 1.20},
"cerebras/llama3.1-70b": {"input": 0.85, "output": 1.20},
"cerebras/llama3.1-8b": {"input": 0.10, "output": 0.10},
"cerebras/llama3.1-405b": {"input": 6.0, "output": 6.0},
"cerebras/qwen-3-32b": {"input": 0.40, "output": 0.80},
# ── Together AI ───────────────────────────────────────────────────────────
"together_ai/meta-llama/Llama-3-70b-chat-hf": {"input": 0.9, "output": 0.9},
"together_ai/meta-llama/Llama-3-8b-chat-hf": {"input": 0.2, "output": 0.2},
"together_ai/meta-llama/Llama-3.1-405B-Instruct-Turbo": {"input": 5.0, "output": 5.0},
"together_ai/meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo": {"input": 0.88, "output": 0.88},
"together_ai/meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo": {"input": 0.18, "output": 0.18},
"together_ai/mistralai/Mixtral-8x22B-Instruct-v0.1": {"input": 1.2, "output": 1.2},
"together_ai/Qwen/Qwen2-72B-Instruct": {"input": 0.9, "output": 0.9},
"together_ai/deepseek-ai/DeepSeek-V3": {"input": 1.25, "output": 1.25},
"together_ai/deepseek-ai/DeepSeek-R1": {"input": 7.0, "output": 7.0},
"together_ai/google/gemma-2-27b-it": {"input": 0.8, "output": 0.8},
# ── Fireworks AI ──────────────────────────────────────────────────────────
"fireworks_ai/accounts/fireworks/models/llama-v3p3-70b-instruct": {"input": 0.9, "output": 0.9},
"fireworks_ai/accounts/fireworks/models/llama-v3p1-70b-instruct": {"input": 0.9, "output": 0.9},
"fireworks_ai/accounts/fireworks/models/llama-v3p1-8b-instruct": {"input": 0.2, "output": 0.2},
"fireworks_ai/accounts/fireworks/models/qwen2p5-72b-instruct": {"input": 0.9, "output": 0.9},
"fireworks_ai/accounts/fireworks/models/deepseek-v3": {"input": 1.2, "output": 1.2},
"fireworks_ai/accounts/fireworks/models/deepseek-r1": {"input": 8.0, "output": 8.0},
# ── Amazon Bedrock ────────────────────────────────────────────────────────
"bedrock/amazon.nova-pro-v1:0": {"input": 0.8, "output": 3.2},
"bedrock/amazon.nova-lite-v1:0": {"input": 0.06, "output": 0.24},
"bedrock/amazon.nova-micro-v1:0": {"input": 0.035, "output": 0.14},
"bedrock/meta.llama3-70b-instruct-v1:0": {"input": 2.65, "output": 3.5},
"bedrock/meta.llama3-8b-instruct-v1:0": {"input": 0.3, "output": 0.6},
"bedrock/meta.llama3-1-70b-instruct-v1:0": {"input": 2.65, "output": 3.5},
"bedrock/meta.llama3-2-90b-instruct-v1:0": {"input": 2.0, "output": 2.0},
"bedrock/mistral.mistral-large-2402-v1:0": {"input": 4.0, "output": 12.0},
"bedrock/mistral.mixtral-8x7b-instruct-v0:1":{"input": 0.45, "output": 0.7},
"bedrock/cohere.command-r-plus-v1:0": {"input": 3.0, "output": 15.0},
"bedrock/cohere.command-r-v1:0": {"input": 0.5, "output": 1.5},
"bedrock/ai21.jamba-1-5-large-v1:0": {"input": 2.0, "output": 8.0},
"bedrock/ai21.jamba-1-5-mini-v1:0": {"input": 0.2, "output": 0.4},
# ── Ollama (local — free) ──────────────────────────────────────────────────
"ollama/llama3": {"input": 0.0, "output": 0.0},
"ollama/llama3.1": {"input": 0.0, "output": 0.0},
"ollama/llama3.2": {"input": 0.0, "output": 0.0},
"ollama/llama3.3": {"input": 0.0, "output": 0.0},
"ollama/mistral": {"input": 0.0, "output": 0.0},
"ollama/mistral-nemo": {"input": 0.0, "output": 0.0},
"ollama/phi3": {"input": 0.0, "output": 0.0},
"ollama/phi4": {"input": 0.0, "output": 0.0},
"ollama/qwen2.5": {"input": 0.0, "output": 0.0},
"ollama/qwen2.5-coder": {"input": 0.0, "output": 0.0},
"ollama/gemma2": {"input": 0.0, "output": 0.0},
"ollama/deepseek-r1": {"input": 0.0, "output": 0.0},
"ollama/codellama": {"input": 0.0, "output": 0.0},
# ── HuggingFace Inference API (most free / very cheap) ────────────────────
"huggingface/meta-llama/Llama-3.1-70B-Instruct": {"input": 0.59, "output": 0.79},
"huggingface/mistralai/Mixtral-8x7B-Instruct-v0.1": {"input": 0.5, "output": 0.5},
"huggingface/google/gemma-2-27b-it": {"input": 0.6, "output": 0.6},
# ── Replicate ─────────────────────────────────────────────────────────────
"replicate/meta/llama-3.1-405b-instruct": {"input": 9.5, "output": 9.5},
"replicate/meta/llama-3.1-70b-instruct": {"input": 0.65, "output": 0.65},
"replicate/meta/llama-3.1-8b-instruct": {"input": 0.05, "output": 0.25},
"replicate/mistralai/mixtral-8x7b-instruct-v0.1": {"input": 0.3, "output": 1.0},
# ── Anyscale ──────────────────────────────────────────────────────────────
"anyscale/meta-llama/Llama-3-70B-Instruct": {"input": 1.0, "output": 1.0},
"anyscale/meta-llama/Llama-3-8B-Instruct": {"input": 0.15,"output": 0.15},
"anyscale/mistralai/Mixtral-8x22B-Instruct-v0.1": {"input": 0.9, "output": 0.9},
# ── OpenRouter ────────────────────────────────────────────────────────────
"openrouter/openai/gpt-4o": {"input": 2.5, "output": 10.0},
"openrouter/openai/gpt-4o-mini": {"input": 0.15, "output": 0.60},
"openrouter/anthropic/claude-sonnet-4-6": {"input": 3.0, "output": 15.0},
"openrouter/anthropic/claude-opus-4-7": {"input": 5.0, "output": 25.0},
"openrouter/google/gemini-2.0-flash": {"input": 0.075, "output": 0.30},
"openrouter/google/gemini-2.5-pro": {"input": 1.25, "output": 10.0},
"openrouter/meta-llama/llama-3.3-70b-instruct":{"input": 0.12, "output": 0.4},
"openrouter/deepseek/deepseek-chat": {"input": 0.14, "output": 0.28},
"openrouter/x-ai/grok-3-mini-beta": {"input": 0.3, "output": 0.5},
"openrouter/mistralai/mistral-large": {"input": 2.0, "output": 6.0},
"openrouter/qwen/qwq-32b": {"input": 0.15, "output": 0.6},
"default": {"input": 3.0, "output": 15.0},
}
HAIKU_PRICING = {"input": 1.0, "output": 5.0} # claude-haiku-4-5 baseline
def calc_cost(model, input_tok, output_tok, cache_read=0, cache_creation=0, cache_creation_1h=0):
# Try exact match, then provider/model prefix variants
p = PRICING.get(model)
if not p and "/" in model:
# e.g. "groq/llama-3.3-70b-versatile" → try bare name too
p = PRICING.get(model.split("/", 1)[1])
p = p or PRICING["default"]
# cache_creation is the TOTAL cache-write tokens; cache_creation_1h is the
# 1-hour-TTL portion. 1h writes cost 2x base input, 5-minute writes 1.25x.
# Old rows have cache_creation_1h=0 → all priced at 1.25x, unchanged.
cache_5m = max(cache_creation - cache_creation_1h, 0)
return (
input_tok * p["input"] +
output_tok * p["output"] +
cache_read * p["input"] * 0.10 +
cache_5m * p["input"] * 1.25 +
cache_creation_1h * p["input"] * 2.00
) / 1_000_000
def _naive_dt(s: str) -> datetime:
dt = datetime.fromisoformat(s)
return dt.replace(tzinfo=None) if dt.tzinfo else dt
def _period_clause(period):
if period == "today": return "AND date(ts, 'localtime') = date('now', 'localtime')"
if period == "7d": return "AND ts >= datetime('now', '-7 days')"
if period == "30d": return "AND ts >= datetime('now', '-30 days')"
return ""
def _fmt_ms(ms):
if not ms:
return "0s"
ms = int(ms)
if ms < 1000:
return f"{ms}ms"
s = ms // 1000
if s < 60:
return f"{s}s"
m, s = divmod(s, 60)
return f"{m}m {s}s" if s else f"{m}m"
def init_db():
con = _connect()
con.execute("""
CREATE TABLE IF NOT EXISTS requests (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ts TEXT,
source TEXT,
model TEXT,
input_tokens INTEGER,
output_tokens INTEGER,
cache_read_tokens INTEGER DEFAULT 0,
cache_creation_tokens INTEGER DEFAULT 0,
cost_usd REAL,
duration_ms INTEGER,
status INTEGER,
user_agent TEXT,
stop_reason TEXT,
tool_call_count INTEGER DEFAULT 0,
tools_json TEXT
)
""")
con.execute("CREATE TABLE IF NOT EXISTS settings (key TEXT PRIMARY KEY, value TEXT)")
existing = {row[1] for row in con.execute("PRAGMA table_info(requests)")}
for col, defn in [
("cache_read_tokens", "INTEGER DEFAULT 0"),
("cache_creation_tokens", "INTEGER DEFAULT 0"),
("cache_creation_1h_tokens", "INTEGER DEFAULT 0"),
("user_agent", "TEXT"),
("stop_reason", "TEXT"),
("tool_call_count", "INTEGER DEFAULT 0"),
("tools_json", "TEXT"),
("effort", "TEXT DEFAULT 'standard'"),
("prompt_preview", "TEXT DEFAULT ''"),
("msg_uuid", "TEXT"),
("auto_thinking", "INTEGER DEFAULT 0"),
("optimizations_json", "TEXT"),
("optimizer_savings_usd", "REAL DEFAULT 0"),
]:
if col not in existing:
con.execute(f"ALTER TABLE requests ADD COLUMN {col} {defn}")
con.execute(
"CREATE UNIQUE INDEX IF NOT EXISTS idx_msg_uuid "
"ON requests(msg_uuid) WHERE msg_uuid IS NOT NULL"
)
con.commit()
con.close()
def save_request(source, model, input_tok, output_tok, cache_read, cache_creation,
cost, duration_ms, status, user_agent="", stop_reason=None,
tool_call_count=0, tools_json=None, effort="standard",
prompt_preview="", msg_uuid=None, auto_thinking=False,
optimizations_json=None, optimizer_savings_usd=0, cache_creation_1h=0,
ts=None):
con = _connect()
con.execute(
"""INSERT OR IGNORE INTO requests
(ts,source,model,input_tokens,output_tokens,cache_read_tokens,cache_creation_tokens,
cost_usd,duration_ms,status,user_agent,stop_reason,tool_call_count,tools_json,
effort,prompt_preview,msg_uuid,auto_thinking,optimizations_json,optimizer_savings_usd,
cache_creation_1h_tokens)
VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)""",
(ts or datetime.now(timezone.utc).isoformat(), source, model,
input_tok, output_tok, cache_read, cache_creation,
cost, duration_ms, status, user_agent, stop_reason, tool_call_count, tools_json,
effort or "standard", prompt_preview or "", msg_uuid, 1 if auto_thinking else 0,
optimizations_json, optimizer_savings_usd, cache_creation_1h),
)
con.commit()
con.close()
def get_raw_logs(limit: int = 500):
con = _connect()
rows = con.execute(
"""SELECT id, ts, source, model, input_tokens, output_tokens,
cache_read_tokens, cache_creation_tokens, cost_usd,
duration_ms, status, stop_reason, tool_call_count,
effort, prompt_preview, tools_json, auto_thinking
FROM requests
ORDER BY id DESC
LIMIT ?""",
(min(limit, 500),)
).fetchall()
con.close()
cols = ["id","ts","source","model","input_tokens","output_tokens",
"cache_read_tokens","cache_creation_tokens","cost_usd",
"duration_ms","status","stop_reason","tool_call_count",
"effort","prompt_preview","tools_json","auto_thinking"]
result = []
for r in rows:
d = dict(zip(cols, r))
# Parse tools_json string → list for the frontend
tj = d.pop("tools_json", None)
if tj:
try:
import json as _json
d["tools"] = _json.loads(tj)
except Exception:
d["tools"] = []
else:
d["tools"] = []
result.append(d)
return result
def get_optimizer_stats(period="7d"):
"""
Get optimizer performance stats: all optimizations with savings.
Returns {
"total_saved": float,
"actual_spent": float,
"roi_percent": float,
"event_count": int,
"by_type": {
"routing": {...},
"cache": {...},
...
},
"daily": [{date, total_saved, breakdown}, ...],
"recent_events": [{id, ts, source, model, type, saved_usd, details}, ...]
}
"""
clause = _period_clause(period)
con = _connect()
# Get all requests with optimizations (include token/effort data for routing table)
rows = con.execute(
f"""SELECT id, ts, source, model, optimizations_json, optimizer_savings_usd,
input_tokens, output_tokens, effort
FROM requests
WHERE optimizations_json IS NOT NULL AND optimizer_savings_usd > 0.0001 {clause}
ORDER BY ts DESC
LIMIT 500"""
).fetchall()
# Routing groups: aggregate directly from DB for full accuracy (not limited to 100 events)
routing_rows = con.execute(
f"""SELECT
json_extract(opt.value, '$.from') AS from_model,
json_extract(opt.value, '$.to') AS to_model,
COUNT(*) AS cnt,
SUM(json_extract(opt.value, '$.saved_usd')) AS saved,
MAX(r.ts) AS ts_last,
AVG(r.input_tokens) AS avg_in,
AVG(r.output_tokens) AS avg_out,
r.effort
FROM requests r,
json_each(r.optimizations_json) AS opt
WHERE json_extract(opt.value, '$.type') = 'routing'
AND r.optimizations_json IS NOT NULL
{clause}
GROUP BY from_model, to_model, r.effort
ORDER BY saved DESC"""
).fetchall()
# Get total actual cost (all requests in period, not just optimized ones)
total_cost_row = con.execute(
f"SELECT SUM(cost_usd) FROM requests WHERE 1=1 {clause}"
).fetchone()
actual_spent = total_cost_row[0] or 0
con.close()
if not rows:
return {
"total_saved": 0,
"actual_spent": actual_spent,
"roi_percent": 0,
"event_count": 0,
"by_type": {},
"daily": [],
"recent_events": []
}
import json as _json
from collections import defaultdict
from datetime import datetime as dt
total_saved = 0
by_type = defaultdict(lambda: {"count": 0, "saved": 0})
daily = defaultdict(lambda: {"saved": 0, "breakdown": defaultdict(float)})
recent_events = []
for row in rows:
rid, ts_str, source, model, opt_json_str, saved, inp_tok, out_tok, eff = row
total_saved += saved or 0
# Parse optimizations
try:
opts = _json.loads(opt_json_str) if opt_json_str else []
except:
opts = []
# Extract date for daily rollup
ts_dt = dt.fromisoformat(ts_str)
date_key = ts_dt.strftime("%Y-%m-%d")
for opt in opts:
opt_type = opt.get("type", "unknown")
opt_saved = opt.get("saved_usd", 0)
by_type[opt_type]["count"] += 1
by_type[opt_type]["saved"] += opt_saved
daily[date_key]["saved"] += opt_saved
daily[date_key]["breakdown"][opt_type] += opt_saved
# Recent events (last 100)
for opt in opts:
recent_events.append({
"id": rid,
"ts": ts_str,
"source": source,
"model": model,
"type": opt.get("type"),
"saved_usd": opt.get("saved_usd", 0),
"input_tokens": inp_tok,
"output_tokens": out_tok,
"effort": eff,
"details": {k: v for k, v in opt.items() if k not in ("type", "saved_usd")}
})
# Sort events by recency
recent_events = recent_events[:100]
# Convert daily to sorted list
daily_list = []
for date_key in sorted(daily.keys()):
daily_list.append({
"date": date_key,
"total_saved": daily[date_key]["saved"],
"breakdown": dict(daily[date_key]["breakdown"])
})
# Calculate ROI
if total_saved + actual_spent > 0:
roi = (total_saved / (total_saved + actual_spent)) * 100
else:
roi = 0
# Build routing_groups from DB aggregates
routing_groups = []
for rr in routing_rows:
from_m, to_m, cnt, saved_sum, ts_last, avg_in, avg_out, eff = rr
routing_groups.append({
"from": from_m or "?",
"to": to_m or "?",
"count": cnt,
"saved": round(saved_sum or 0, 4),
"ts_last": ts_last,
"avg_input_tokens": round(avg_in or 0),
"avg_output_tokens": round(avg_out or 0),
"effort": eff or "standard",
})
return {
"total_saved": round(total_saved, 4),
"actual_spent": round(actual_spent, 4),
"roi_percent": round(roi, 1),
"event_count": len(recent_events),
"by_type": {k: {"count": v["count"], "saved": round(v["saved"], 4)} for k, v in by_type.items()},
"daily": daily_list,
"recent_events": recent_events,
"routing_groups": routing_groups,
}
def get_sessions(period="7d", limit=50):
clause = _period_clause(period)
con = _connect()
rows = con.execute(
f"SELECT ts, source, cost_usd, duration_ms, tools_json, model, "
f"input_tokens, output_tokens, cache_read_tokens, cache_creation_tokens "
f"FROM requests WHERE 1=1 {clause} ORDER BY ts"
).fetchall()
con.close()
if not rows:
return []
SESSION_GAP = 30 * 60
sessions, current = [], []
for row in rows:
ts = row[0]
if current:
prev_dt = _naive_dt(current[-1][0])
curr_dt = _naive_dt(ts)
if (curr_dt - prev_dt).total_seconds() > SESSION_GAP:
sessions.append(current)
current = []
current.append(row)
if current:
sessions.append(current)
result = []
for session in reversed(sessions[-limit:]):
start_dt = _naive_dt(session[0][0])
end_dt = _naive_dt(session[-1][0])
wall_ms = int((end_dt - start_dt).total_seconds() * 1000)
api_ms = sum(r[3] or 0 for r in session)
efficiency = round(api_ms / wall_ms * 100, 1) if wall_ms > 1000 else 100.0
sources = list({r[1] for r in session})
costs = [r[2] or 0.0 for r in session]
total_input = sum(r[6] or 0 for r in session)
total_output = sum(r[7] or 0 for r in session)
total_cache_write = sum(r[9] or 0 for r in session)
input_cost = output_cost = 0.0
for r in session:
model = r[5] or "default"
p = PRICING.get(model) or PRICING["default"]
input_cost += (r[6] or 0) * p["input"] / 1e6
input_cost += (r[8] or 0) * p["input"] * 0.10 / 1e6 # cache_read
input_cost += (r[9] or 0) * p["input"] * 1.25 / 1e6 # cache_creation
output_cost += (r[7] or 0) * p["output"] / 1e6
tool_counts: dict = {}
for r in session:
tj = r[4]
if tj:
try:
for t in json.loads(tj):
tool_counts[t] = tool_counts.get(t, 0) + 1
except Exception:
pass
top_tools = sorted(tool_counts.items(), key=lambda x: x[1], reverse=True)[:5]
result.append({
"start": session[0][0],
"end": session[-1][0],
"wall_ms": wall_ms,
"api_ms": api_ms,
"wall_fmt": _fmt_ms(wall_ms),
"api_fmt": _fmt_ms(api_ms),
"efficiency": efficiency,
"req_count": len(session),
"sources": sources,
"total_cost": round(sum(costs), 5),
"max_cost": round(max(costs), 5),
"top_tools": [{"name": k, "count": v} for k, v in top_tools],
"total_input": total_input,
"total_output": total_output,
"total_cache_write": total_cache_write,
"input_cost": round(input_cost, 5),
"output_cost": round(output_cost, 5),
})
return result
def _tool_breakdown(period):
clause = _period_clause(period)
con = _connect()
rows = con.execute(
f"SELECT tools_json FROM requests WHERE 1=1 {clause} AND tools_json IS NOT NULL"
).fetchall()
con.close()
counts: dict = {}
for (tj,) in rows:
try:
for t in json.loads(tj):
key = t[0].upper() + t[1:] if t else t
counts[key] = counts.get(key, 0) + 1
except Exception:
pass
return sorted([{"name": k, "count": v} for k, v in counts.items()],
key=lambda x: x["count"], reverse=True)
def _hourly_heatmap():
con = _connect()
rows = con.execute("""
SELECT
date(ts, 'localtime') as day,
CAST(strftime('%H', ts, 'localtime') AS INTEGER) as hour,
ROUND(SUM(cost_usd), 6) as total_cost,
COUNT(*) as reqs
FROM requests
WHERE date(ts, 'localtime') >= date('now', '-7 days', 'localtime')
GROUP BY day, hour
ORDER BY day, hour
""").fetchall()
con.close()
return [{"day": r[0], "hour": r[1], "total_cost": r[2], "reqs": r[3]} for r in rows]
def _daily_trend(period):
con = _connect()
if period == "today":
rows = con.execute("""
SELECT strftime('%H', ts, 'localtime') as lbl,
ROUND(SUM(cost_usd), 5) as cost, COUNT(*) as reqs
FROM requests WHERE date(ts, 'localtime') = date('now', 'localtime')
GROUP BY lbl ORDER BY lbl
""").fetchall()
elif period in ("7d", "30d"):
days = "7" if period == "7d" else "30"
rows = con.execute(f"""
SELECT date(ts, 'localtime') as lbl,
ROUND(SUM(cost_usd), 5) as cost, COUNT(*) as reqs
FROM requests WHERE ts >= datetime('now', '-{days} days')
GROUP BY lbl ORDER BY lbl
""").fetchall()
else:
rows = con.execute("""
SELECT strftime('%Y-%m', ts, 'localtime') as lbl,
ROUND(SUM(cost_usd), 5) as cost, COUNT(*) as reqs
FROM requests GROUP BY lbl ORDER BY lbl
""").fetchall()
con.close()
return [{"label": r[0], "cost": r[1] or 0, "reqs": r[2]} for r in rows]
def _projection(period="7d"):
"""Monthly projection consistent with the selected period."""
con = _connect()
if period == "today":
row = con.execute(
"SELECT ROUND(SUM(cost_usd),4) FROM requests "
"WHERE date(ts,'localtime')=date('now','localtime')"
).fetchone()
daily = row[0] or 0
elif period == "30d":
row = con.execute(
"SELECT ROUND(SUM(cost_usd),4) FROM requests "
"WHERE ts >= datetime('now','-30 days')"
).fetchone()
daily = (row[0] or 0) / 30
else: # 7d and all — use stable 7-day rolling average
row = con.execute(
"SELECT ROUND(SUM(cost_usd),4) FROM requests "
"WHERE ts >= datetime('now','-7 days')"
).fetchone()
daily = (row[0] or 0) / 7
con.close()
return round(daily * 30, 2)
def _cost_breakdown(period):
clause = _period_clause(period)
con = _connect()
rows = con.execute(f"""
SELECT model,
SUM(input_tokens) as inp, SUM(output_tokens) as out,
SUM(cache_read_tokens) as cr, SUM(cache_creation_tokens) as cw
FROM requests WHERE 1=1 {clause} GROUP BY model
""").fetchall()
con.close()
bd = {"input": 0.0, "output": 0.0, "cache_read": 0.0, "cache_creation": 0.0}
for model, inp, out, cr, cw in rows:
p = PRICING.get(model) or PRICING["default"]
bd["input"] += (inp or 0) * p["input"] / 1e6
bd["output"] += (out or 0) * p["output"] / 1e6
bd["cache_read"] += (cr or 0) * p["input"] * 0.10 / 1e6
bd["cache_creation"] += (cw or 0) * p["input"] * 1.25 / 1e6
return {k: round(v, 5) for k, v in bd.items()}
def _cache_by_tool(period):
clause = _period_clause(period)
con = _connect()
rows = con.execute(f"""
SELECT REPLACE(source,'-history','') as source, model,
COUNT(*) as reqs,
SUM(input_tokens) as inp,
SUM(cache_read_tokens) as cr,
SUM(cache_creation_tokens) as cw,
ROUND(SUM(cost_usd), 5) as cost
FROM requests WHERE 1=1 {clause}
GROUP BY REPLACE(source,'-history',''), model
""").fetchall()
con.close()
tools: dict = {}
for source, model, reqs, inp, cr, cw, cost in rows:
p = PRICING.get(model) or PRICING["default"]
saved = (cr or 0) * p["input"] * 0.9 / 1e6
if source not in tools:
tools[source] = {"reqs": 0, "inp": 0, "cr": 0, "cw": 0, "cost": 0.0, "saved": 0.0}
t = tools[source]
t["reqs"] += reqs; t["inp"] += (inp or 0); t["cr"] += (cr or 0)
t["cw"] += (cw or 0); t["cost"] += (cost or 0); t["saved"] += saved
result = []
for source, t in sorted(tools.items(), key=lambda x: x[1]["cost"], reverse=True):
total_toks = t["inp"] + t["cr"] + t["cw"]
hit_rate = round(t["cr"] / total_toks * 100, 1) if total_toks else 0
result.append({
"source": source,
"reqs": t["reqs"],
"hit_rate": hit_rate,
"cache_read": t["cr"],
"saved": round(t["saved"], 4),
"cost": round(t["cost"], 4),
"avg_cost": round(t["cost"] / t["reqs"], 5) if t["reqs"] else 0,
})
return result
def _haiku_savings(period):
clause = _period_clause(period)
con = _connect()
rows = con.execute(f"""
SELECT model,
SUM(input_tokens) as inp, SUM(output_tokens) as out,
SUM(cache_read_tokens) as cr, SUM(cache_creation_tokens) as cw,
ROUND(SUM(cost_usd), 5) as actual, COUNT(*) as reqs,
AVG(input_tokens) as avg_inp, AVG(output_tokens) as avg_out
FROM requests WHERE 1=1 {clause} AND model NOT LIKE '%haiku%'
GROUP BY model
""").fetchall()
effort_rows = con.execute(f"""
SELECT effort, COUNT(*) as cnt
FROM requests WHERE 1=1 {clause} AND model NOT LIKE '%haiku%'
GROUP BY effort
""").fetchall()
routing_rows = con.execute(f"""
SELECT DISTINCT optimizations_json FROM requests
WHERE optimizations_json IS NOT NULL {clause}
""").fetchall()
con.close()
import json as _json
hp = HAIKU_PRICING
total_actual = total_haiku = total_reqs = 0.0
total_inp = total_out = 0.0
for _, inp, out, cr, cw, actual, reqs, _, _ in rows:
total_haiku += ((inp or 0)*hp["input"] + (out or 0)*hp["output"] +
(cr or 0)*hp["input"]*0.10 + (cw or 0)*hp["input"]*1.25) / 1e6
total_actual += (actual or 0)
total_reqs += reqs
total_inp += (inp or 0)
total_out += (out or 0)
effort_counts = {row[0]: row[1] for row in effort_rows}
avg_input = round(total_inp / total_reqs) if total_reqs > 0 else 0
avg_output = round(total_out / total_reqs) if total_reqs > 0 else 0
# Extract unique original models from routing optimizations_json
seen = set()
original_models = []
for (opt_str,) in routing_rows:
try:
for opt in _json.loads(opt_str):
if opt.get("type") == "routing" and opt.get("from"):
m = opt["from"].split("[")[0].strip()
if m and m not in seen:
seen.add(m)
original_models.append(m)
except Exception:
pass
return {
"actual": round(total_actual, 4), "haiku_equivalent": round(total_haiku, 4),
"savings": round(total_actual - total_haiku, 4), "requests": int(total_reqs),
"avg_input_tokens": avg_input, "avg_output_tokens": avg_output,
"effort_counts": effort_counts,
"original_models": original_models,
}
def _cache_savings(period):
clause = _period_clause(period)
con = _connect()
rows = con.execute(f"""
SELECT model, SUM(cache_read_tokens) as cr
FROM requests WHERE 1=1 {clause} GROUP BY model
""").fetchall()
con.close()
saved = 0.0
for model, cr in rows:
if cr:
p = PRICING.get(model) or PRICING["default"]
saved += cr * p["input"] * 0.9 / 1_000_000
return round(saved, 4)
def _pause_analysis(period):
"""Analyze inter-request pauses to recommend 5min vs 1h cache TTL."""
clause = _period_clause(period)
period_days = {"today": 1, "7d": 7, "30d": 30}.get(period, 7)
con = _connect()
rows = con.execute(
f"SELECT ts, cache_creation_tokens, cache_creation_1h_tokens "
f"FROM requests WHERE 1=1 {clause} ORDER BY ts"
).fetchall()
con.close()
if len(rows) < 3:
return None
SESSION_GAP = 30 * 60
TTL_5M = 5 * 60
TTL_1H = 60 * 60
timestamps = [_naive_dt(r[0]) for r in rows]
cw_all = [r[1] or 0 for r in rows]
cw_1h_all = [r[2] or 0 for r in rows]
within_gaps = [] # intra-session gap durations (seconds)
sessions = 1
for i in range(1, len(timestamps)):
gap = (timestamps[i] - timestamps[i-1]).total_seconds()
if gap >= SESSION_GAP:
sessions += 1
else:
within_gaps.append(gap)
if not within_gaps:
return None
n = len(within_gaps)
hot = sum(1 for g in within_gaps if g < TTL_5M) # cache warm
mid = sum(1 for g in within_gaps if TTL_5M <= g < TTL_1H) # 5m expires; 1h survives
long_ = sum(1 for g in within_gaps if g >= TTL_1H) # expires regardless
avg_cw = sum(cw_all) / max(len(cw_all), 1)
ip = (PRICING.get("claude-sonnet-4-6") or PRICING["default"])["input"]
# Each "mid" gap avoided by 1h TTL: saves (1.25→0.10) per re-write token
# Extra cost of 1h vs 5m write: (2.00-1.25) per original write token
saved_period = mid * avg_cw * ip * (1.25 - 0.10) / 1e6
extra_period = sessions * avg_cw * ip * (2.00 - 1.25) / 1e6
net_period = saved_period - extra_period
avg_gap_min = round(sum(within_gaps) / n / 60, 1)
# Observed write TTL from the actual cache-write breakdown. Old rows (pre
# split-tracking) report 1h=0, so a period of only old data reads as "5 min"
# and the recommendation behaves exactly as before. Threshold: predominantly
# 1h (>=80% of write tokens) → "1h"; predominantly 5m (<=20%) → "5 min".
sum_cw = sum(cw_all)
sum_1h = sum(cw_1h_all)
pct_1h = round(sum_1h / sum_cw * 100) if sum_cw else 0
observed_ttl = "1h" if pct_1h >= 80 else ("5 min" if pct_1h <= 20 else "mixed")
return {
"sessions": sessions,
"within_gaps": n,
"hot_count": hot,
"mid_count": mid,
"long_count": long_,
"hot_pct": round(hot / n * 100),
"mid_pct": round(mid / n * 100),
"long_pct": round(long_ / n * 100),
"avg_gap_min": avg_gap_min,
"net_period": round(net_period, 4),
"net_monthly": round(net_period / period_days * 30, 2),
"recommendation": "1h" if mid > 0 and net_period > 0 else "5min",
"mid_per_day": round(mid / period_days, 1),
"cache_1h_pct": pct_1h,
"observed_ttl": observed_ttl,
}
def _health_grade(summary, period):
clause = _period_clause(period)
total_reqs = summary.get("total_requests") or 0
if total_reqs < 5:
return None
total_inp = summary.get("total_input") or 0
total_cr = summary.get("total_cache_read") or 0
total_cw = summary.get("total_cache_creation") or 0
avg_inp = total_inp / max(total_reqs, 1)
con = _connect()
max_tok_count = con.execute(
f"SELECT COUNT(*) FROM requests WHERE stop_reason='max_tokens' AND 1=1 {clause}"
).fetchone()[0]
con.close()
score = 0
details = []
# 1. Cache hit rate (35 pts)
cacheable = total_cr + total_inp
hit_rate = (total_cr / cacheable * 100) if cacheable > 0 else 0
if hit_rate >= 60: cache_pts = 35
elif hit_rate >= 40: cache_pts = 25
elif hit_rate >= 20: cache_pts = 15
elif hit_rate >= 5: cache_pts = 5
else: cache_pts = 0
score += cache_pts
if cache_pts < 25:
details.append(f"Cache hit {hit_rate:.0f}% — keep sessions open longer")