-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathtoken_usage.go
More file actions
1068 lines (962 loc) · 32.4 KB
/
Copy pathtoken_usage.go
File metadata and controls
1068 lines (962 loc) · 32.4 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
package cli
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"math"
"os"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"time"
"github.com/github/gh-aw/pkg/console"
"github.com/github/gh-aw/pkg/logger"
"github.com/github/gh-aw/pkg/timeutil"
)
var tokenUsageLog = logger.New("cli:token_usage")
// TokenUsageEntry represents a single line from token-usage.jsonl
type TokenUsageEntry struct {
Schema string `json:"_schema,omitempty"` // Self-describing record type, e.g. "token-usage/v0.26.0"
Timestamp string `json:"timestamp"`
RequestID string `json:"request_id"`
Provider string `json:"provider"`
Model string `json:"model"`
Path string `json:"path"`
Status int `json:"status"`
Streaming bool `json:"streaming"`
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
CacheReadTokens int `json:"cache_read_tokens"`
CacheWriteTokens int `json:"cache_write_tokens"`
ReasoningTokens int `json:"reasoning_tokens"`
EffectiveTokens int `json:"effective_tokens"`
DurationMs int `json:"duration_ms"`
ResponseBytes int `json:"response_bytes"`
}
// AmbientContextMetrics captures token footprint for the first LLM invocation.
type AmbientContextMetrics struct {
InputTokens int `json:"input_tokens" console:"header:Ambient Input,format:number"`
CachedTokens int `json:"cached_tokens" console:"header:Ambient Cached,format:number"`
EffectiveTokens int `json:"effective_tokens,omitempty"`
}
// TokenUsageSummary contains aggregated token usage from the firewall proxy
type TokenUsageSummary struct {
TotalInputTokens int `json:"total_input_tokens" console:"header:Input Tokens,format:number"`
TotalOutputTokens int `json:"total_output_tokens" console:"header:Output Tokens,format:number"`
TotalCacheReadTokens int `json:"total_cache_read_tokens" console:"header:Cache Read,format:number"`
TotalCacheWriteTokens int `json:"total_cache_write_tokens" console:"header:Cache Write,format:number"`
TotalRequests int `json:"total_requests" console:"header:Requests"`
TotalSteeringEvents int `json:"total_steering_events,omitempty" console:"header:Steering Events,format:number,omitempty"`
TotalDurationMs int `json:"total_duration_ms"`
TotalResponseBytes int `json:"total_response_bytes"`
CacheEfficiency float64 `json:"cache_efficiency"`
TotalEffectiveTokens int `json:"total_effective_tokens,omitempty"`
TotalAIC float64 `json:"total_aic,omitempty"`
AmbientContext *AmbientContextMetrics `json:"ambient_context,omitempty"`
ByModel map[string]*ModelTokenUsage `json:"by_model"`
SubagentModelRequests []SubagentModelRequest `json:"subagent_model_requests,omitempty"`
SubagentModelActuals []SubagentModelActual `json:"subagent_model_actuals,omitempty"`
MismatchCount int `json:"mismatch_count,omitempty"`
Warnings []string `json:"warnings,omitempty"`
}
// ModelTokenUsage contains per-model token usage statistics
type ModelTokenUsage struct {
Provider string `json:"provider"`
InputTokens int `json:"input_tokens" console:"header:Input,format:number"`
OutputTokens int `json:"output_tokens" console:"header:Output,format:number"`
CacheReadTokens int `json:"cache_read_tokens" console:"header:Cache Read,format:number"`
CacheWriteTokens int `json:"cache_write_tokens" console:"header:Cache Write,format:number"`
ReasoningTokens int `json:"reasoning_tokens,omitempty"`
Requests int `json:"requests" console:"header:Requests"`
DurationMs int `json:"duration_ms"`
ResponseBytes int `json:"response_bytes"`
EffectiveTokens int `json:"effective_tokens,omitempty"`
AIC float64 `json:"aic,omitempty"`
}
// ModelTokenUsageRow is a flattened version for console table rendering
type ModelTokenUsageRow struct {
Model string `json:"model" console:"header:Model"`
Provider string `json:"provider" console:"header:Provider"`
InputTokens int `json:"input_tokens" console:"header:Input,format:number"`
OutputTokens int `json:"output_tokens" console:"header:Output,format:number"`
CacheReadTokens int `json:"cache_read_tokens" console:"header:Cache Read,format:number"`
CacheWriteTokens int `json:"cache_write_tokens" console:"header:Cache Write,format:number"`
EffectiveTokens int `json:"effective_tokens,omitempty"`
AIC float64 `json:"aic,omitempty"`
Requests int `json:"requests" console:"header:Requests"`
AvgDuration string `json:"avg_duration" console:"header:Avg Duration"`
}
// SubagentModelRequest captures requested/effective model attribution for a sub-agent.
type SubagentModelRequest struct {
AgentName string `json:"agent_name"`
RequestedModel string `json:"requested_model"`
InvocationCount int `json:"invocation_count"`
EffectiveModel string `json:"effective_model,omitempty"`
ReasonCode string `json:"reason_code,omitempty"`
}
// SubagentModelActual captures model usage observed in token-usage logs.
type SubagentModelActual struct {
Model string `json:"model"`
Provider string `json:"provider,omitempty"`
Requests int `json:"requests"`
}
// tokenUsageJSONLPath is the relative path within the firewall logs directory
const tokenUsageJSONLPath = "api-proxy-logs/token-usage.jsonl"
const proxyEventsJSONLPath = "api-proxy-logs/events.jsonl"
const agentUsageJSONPath = "agent_usage.json"
const modelMismatchReasonTokenUsageMissing = "TOKEN_USAGE_MISSING"
const modelMismatchReasonModelNotObserved = "REQUESTED_MODEL_NOT_OBSERVED"
const subagentStdioWarning = "partial or incorrect data: sub-agent model requests are inferred from agent-stdio.log; use token_usage.jsonl for reliable token consumption"
const tokenSteeringEventName = "token_steering"
const timeoutSteeringEventName = "timeout_steering"
const awfTokenWarningPrefix = "[AWF TOKEN WARNING]"
const awfTimeWarningPrefix = "[AWF TIME WARNING]"
var subagentDispatchPattern = regexp.MustCompile(`([A-Za-z0-9][A-Za-z0-9._-]*)\(([A-Za-z0-9][A-Za-z0-9._:-]*)\)`)
// parseTokenUsageFile parses a token-usage.jsonl file and returns the aggregated summary.
func parseTokenUsageFile(filePath string) (*TokenUsageSummary, error) {
tokenUsageLog.Printf("Parsing token usage file: %s", filePath)
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open token usage file: %w", err)
}
defer file.Close()
summary := &TokenUsageSummary{
ByModel: make(map[string]*ModelTokenUsage),
}
scanner := bufio.NewScanner(file)
// Increase buffer size for potentially large lines
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
entries := make([]TokenUsageEntry, 0)
lineNum := 0
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
var entry TokenUsageEntry
if err := json.Unmarshal([]byte(line), &entry); err != nil {
tokenUsageLog.Printf("Skipping invalid JSON at line %d: %v", lineNum, err)
continue
}
entries = append(entries, entry)
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading token usage file: %w", err)
}
if len(entries) == 0 {
tokenUsageLog.Print("No token usage entries found")
return nil, nil
}
for _, entry := range entries {
// Aggregate totals
summary.TotalInputTokens += entry.InputTokens
summary.TotalOutputTokens += entry.OutputTokens
summary.TotalCacheReadTokens += entry.CacheReadTokens
summary.TotalCacheWriteTokens += entry.CacheWriteTokens
summary.TotalRequests++
summary.TotalDurationMs += entry.DurationMs
summary.TotalResponseBytes += entry.ResponseBytes
// Aggregate by model
model := entry.Model
if model == "" {
model = "unknown"
}
if _, exists := summary.ByModel[model]; !exists {
summary.ByModel[model] = &ModelTokenUsage{
Provider: entry.Provider,
}
}
m := summary.ByModel[model]
m.InputTokens += entry.InputTokens
m.OutputTokens += entry.OutputTokens
m.CacheReadTokens += entry.CacheReadTokens
m.CacheWriteTokens += entry.CacheWriteTokens
m.ReasoningTokens += entry.ReasoningTokens
m.Requests++
m.DurationMs += entry.DurationMs
m.ResponseBytes += entry.ResponseBytes
}
tokenUsageLog.Printf("Parsed %d entries: %d input, %d output, %d cache_read, %d cache_write, %d requests",
lineNum, summary.TotalInputTokens, summary.TotalOutputTokens,
summary.TotalCacheReadTokens, summary.TotalCacheWriteTokens, summary.TotalRequests)
populateAIC(summary)
summary.AmbientContext = extractAmbientContextMetrics(entries)
return summary, nil
}
func extractAmbientContextMetrics(entries []TokenUsageEntry) *AmbientContextMetrics {
if len(entries) == 0 {
return nil
}
type orderedTokenEntry struct {
entry TokenUsageEntry
timestamp time.Time
hasTimestamp bool
order int
}
ordered := make([]orderedTokenEntry, 0, len(entries))
for i, entry := range entries {
ts, hasTimestamp := parseTokenUsageTimestamp(entry.Timestamp)
ordered = append(ordered, orderedTokenEntry{
entry: entry,
timestamp: ts,
hasTimestamp: hasTimestamp,
order: i,
})
}
slices.SortStableFunc(ordered, func(left, right orderedTokenEntry) int {
if left.hasTimestamp && right.hasTimestamp {
switch {
case left.timestamp.Before(right.timestamp):
return -1
case right.timestamp.Before(left.timestamp):
return 1
default:
return 0
}
}
if left.hasTimestamp != right.hasTimestamp {
if left.hasTimestamp {
return -1
}
return 1
}
if left.order < right.order {
return -1
}
if left.order > right.order {
return 1
}
return 0
})
firstCall := ordered[0].entry
return &AmbientContextMetrics{
InputTokens: firstCall.InputTokens,
CachedTokens: firstCall.CacheReadTokens,
}
}
func parseTokenUsageTimestamp(value string) (time.Time, bool) {
if value == "" {
return time.Time{}, false
}
if ts, err := time.Parse(time.RFC3339Nano, value); err == nil {
return ts, true
}
if ts, err := time.Parse(time.RFC3339, value); err == nil {
return ts, true
}
return time.Time{}, false
}
// findTokenUsageFile searches for token-usage.jsonl in the run directory
func findTokenUsageFile(runDir string) string {
usageArtifactCandidate := filepath.Join(runDir, "usage", "agent", "token_usage.jsonl")
if _, err := os.Stat(usageArtifactCandidate); err == nil {
tokenUsageLog.Printf("Found token usage file in usage artifact: %s", usageArtifactCandidate)
return usageArtifactCandidate
}
// Primary path: sandbox/firewall/logs/api-proxy-logs/token-usage.jsonl
primary := filepath.Join(runDir, "sandbox", "firewall", "logs", tokenUsageJSONLPath)
if _, err := os.Stat(primary); err == nil {
tokenUsageLog.Printf("Found token usage file at primary path: %s", primary)
return primary
}
// Check legacy firewall-audit-logs artifact directory (backward compat for older runs)
entries, err := os.ReadDir(runDir)
if err != nil {
return ""
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
name := entry.Name()
if strings.HasPrefix(name, "firewall-audit-logs") || strings.HasPrefix(name, "firewall-logs") {
candidate := filepath.Join(runDir, name, tokenUsageJSONLPath)
if _, err := os.Stat(candidate); err == nil {
tokenUsageLog.Printf("Found token usage file in %s: %s", name, candidate)
return candidate
}
}
}
// Walk sandbox directory for any token-usage.jsonl
if walkErr := filepath.Walk(runDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
tokenUsageLog.Printf("walk error at %s: %v", path, err)
return nil
}
if info == nil || info.IsDir() {
return nil
}
if info.Name() == "token-usage.jsonl" || info.Name() == "token_usage.jsonl" {
primary = path
return filepath.SkipAll
}
return nil
}); walkErr != nil && !errors.Is(walkErr, filepath.SkipAll) {
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("filesystem error walking %s: %v", runDir, walkErr)))
}
if primary != filepath.Join(runDir, "sandbox", "firewall", "logs", tokenUsageJSONLPath) {
tokenUsageLog.Printf("Found token usage file via walk: %s", primary)
return primary
}
tokenUsageLog.Print("No token usage file found")
return ""
}
// findAgentUsageFile searches for agent_usage.json in the run directory.
func findAgentUsageFile(runDir string) string {
primary := filepath.Join(runDir, agentUsageJSONPath)
if _, err := os.Stat(primary); err == nil {
tokenUsageLog.Printf("Found agent usage file at primary path: %s", primary)
return primary
}
var found string
if walkErr := filepath.Walk(runDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
tokenUsageLog.Printf("walk error at %s: %v", path, err)
return nil
}
if info == nil || info.IsDir() {
return nil
}
if info.Name() == agentUsageJSONPath {
found = path
return filepath.SkipAll
}
return nil
}); walkErr != nil && !errors.Is(walkErr, filepath.SkipAll) {
fmt.Fprintln(os.Stderr, console.FormatWarningMessage(fmt.Sprintf("filesystem error walking %s: %v", runDir, walkErr)))
}
if found != "" {
tokenUsageLog.Printf("Found agent usage file via walk: %s", found)
}
return found
}
func parseAgentUsageFile(filePath string) (*TokenUsageSummary, error) {
cleanPath := filepath.Clean(filePath)
data, err := os.ReadFile(cleanPath)
if err != nil {
return nil, fmt.Errorf("failed to read agent usage file: %w", err)
}
var entry TokenUsageEntry
if err := json.Unmarshal(data, &entry); err != nil {
return nil, fmt.Errorf("failed to parse agent usage file: %w", err)
}
model := strings.TrimSpace(entry.Model)
if model == "" {
model = "unknown"
}
summary := &TokenUsageSummary{
TotalInputTokens: entry.InputTokens,
TotalOutputTokens: entry.OutputTokens,
TotalCacheReadTokens: entry.CacheReadTokens,
TotalCacheWriteTokens: entry.CacheWriteTokens,
ByModel: make(map[string]*ModelTokenUsage),
}
hasRawTokenData := summary.TotalInputTokens > 0 ||
summary.TotalOutputTokens > 0 ||
summary.TotalCacheReadTokens > 0 ||
summary.TotalCacheWriteTokens > 0 ||
entry.ReasoningTokens > 0
hasTokenData := hasRawTokenData
if hasTokenData {
summary.TotalRequests = 1
summary.ByModel[model] = &ModelTokenUsage{
Provider: entry.Provider,
InputTokens: entry.InputTokens,
OutputTokens: entry.OutputTokens,
CacheReadTokens: entry.CacheReadTokens,
CacheWriteTokens: entry.CacheWriteTokens,
ReasoningTokens: entry.ReasoningTokens,
Requests: 1,
}
}
summary.AmbientContext = &AmbientContextMetrics{
InputTokens: entry.InputTokens,
CachedTokens: entry.CacheReadTokens,
}
if hasRawTokenData {
populateAIC(summary)
}
tokenUsageLog.Printf("Parsed agent usage file: input=%d, output=%d, cache_read=%d, cache_write=%d",
summary.TotalInputTokens, summary.TotalOutputTokens, summary.TotalCacheReadTokens, summary.TotalCacheWriteTokens)
return summary, nil
}
// analyzeTokenUsage finds and parses the token-usage.jsonl file from a run directory.
func analyzeTokenUsage(runDir string, verbose bool) (*TokenUsageSummary, error) {
tokenUsageLog.Printf("Analyzing token usage in: %s", runDir)
filePath := findTokenUsageFile(runDir)
if filePath != "" {
fileInfo, _ := os.Stat(filePath)
if fileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
}
summary, err := parseTokenUsageFile(filePath)
if err != nil || summary == nil {
return summary, err
}
summary.TotalSteeringEvents = countAPIProxySteeringEvents(runDir)
augmentSubagentModelAttribution(runDir, summary)
return summary, nil
}
agentUsagePath := findAgentUsageFile(runDir)
if agentUsagePath == "" {
return nil, nil
}
agentFileInfo, _ := os.Stat(agentUsagePath)
if agentFileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found agent usage file: %s (%d bytes)", filepath.Base(agentUsagePath), agentFileInfo.Size()))
}
summary, err := parseAgentUsageFile(agentUsagePath)
if err != nil || summary == nil {
return summary, err
}
summary.TotalSteeringEvents = countAPIProxySteeringEvents(runDir)
augmentSubagentModelAttribution(runDir, summary)
return summary, nil
}
func findUsageJSONLFiles(runDir string) []string {
usageDir := filepath.Join(runDir, "usage")
if _, err := os.Stat(usageDir); err != nil {
return nil
}
var files []string
if walkErr := filepath.Walk(usageDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
tokenUsageLog.Printf("walk error at %s: %v", path, err)
return nil
}
if info == nil || info.IsDir() {
return nil
}
if strings.HasSuffix(strings.ToLower(info.Name()), ".jsonl") {
files = append(files, path)
}
return nil
}); walkErr != nil {
tokenUsageLog.Printf("usage walk error at %s: %v", usageDir, walkErr)
}
sort.Strings(files)
return files
}
func extractUsageRecord(value any) map[string]any {
record, ok := value.(map[string]any)
if !ok {
return nil
}
return record
}
func usageNumericValue(parsed map[string]any, usage map[string]any, keys ...string) float64 {
for _, key := range keys {
for _, candidate := range []any{usage[key], parsed[key]} {
switch v := candidate.(type) {
case float64:
if !isFinite(v) {
continue
}
return v
case json.Number:
if num, err := v.Float64(); err == nil && isFinite(num) {
return num
}
case int:
return float64(v)
case int64:
return float64(v)
case string:
if strings.TrimSpace(v) == "" {
continue
}
num := json.Number(v)
if parsedNum, err := num.Float64(); err == nil && isFinite(parsedNum) {
return parsedNum
}
}
}
}
return 0
}
func usageStringValue(parsed map[string]any, usage map[string]any, keys ...string) string {
for _, key := range keys {
for _, candidate := range []any{usage[key], parsed[key]} {
if value, ok := candidate.(string); ok && strings.TrimSpace(value) != "" {
return value
}
}
}
return ""
}
func isFinite(value float64) bool {
return !math.IsNaN(value) && !math.IsInf(value, 0)
}
func sumAICFromUsageJSONLFiles(filePaths []string) (float64, error) {
var totalAIC float64
found := false
for _, filePath := range filePaths {
file, err := os.Open(filepath.Clean(filePath))
if err != nil {
return 0, fmt.Errorf("failed to open usage JSONL file %s: %w", filePath, err)
}
scanner := bufio.NewScanner(file)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || !strings.HasPrefix(line, "{") {
continue
}
var parsed map[string]any
if err := json.Unmarshal([]byte(line), &parsed); err != nil {
continue
}
usage := extractUsageRecord(parsed["usage"])
explicitAICredits := usageNumericValue(parsed, usage, "ai_credits", "aiCredits")
if explicitAICredits > 0 {
totalAIC += explicitAICredits
found = true
continue
}
explicitAIC := usageNumericValue(parsed, usage, "aic")
if explicitAIC > 0 {
totalAIC += explicitAIC
found = true
continue
}
computedAIC := computeModelInferenceAIC(
usageStringValue(parsed, usage, "provider"),
usageStringValue(parsed, usage, "model"),
int(usageNumericValue(parsed, usage, "input_tokens", "inputTokens")),
int(usageNumericValue(parsed, usage, "output_tokens", "outputTokens")),
int(usageNumericValue(parsed, usage, "cache_read_tokens", "cacheReadTokens")),
int(usageNumericValue(parsed, usage, "cache_write_tokens", "cacheWriteTokens")),
int(usageNumericValue(parsed, usage, "reasoning_tokens", "reasoningTokens")),
)
if computedAIC > 0 {
totalAIC += computedAIC
found = true
}
}
closeErr := file.Close()
if err := scanner.Err(); err != nil {
return 0, fmt.Errorf("error reading usage JSONL file %s: %w", filePath, err)
}
if closeErr != nil {
return 0, fmt.Errorf("failed to close usage JSONL file %s: %w", filePath, closeErr)
}
}
if !found {
return 0, nil
}
return totalAIC, nil
}
// analyzeTokenUsageAICOnly parses token usage inputs and computes only TotalAIC.
// It intentionally skips effective-token computation for callers that only need cost.
func analyzeTokenUsageAICOnly(runDir string, verbose bool) (*TokenUsageSummary, error) {
tokenUsageLog.Printf("Analyzing token usage (AIC only) in: %s", runDir)
usageJSONLFiles := findUsageJSONLFiles(runDir)
if len(usageJSONLFiles) > 0 {
console.LogVerbose(verbose, " Found usage JSONL files: "+strings.Join(usageJSONLFiles, ", "))
totalAIC, err := sumAICFromUsageJSONLFiles(usageJSONLFiles)
if err != nil {
return nil, err
}
return &TokenUsageSummary{TotalAIC: totalAIC}, nil
}
filePath := findTokenUsageFile(runDir)
if filePath != "" {
fileInfo, _ := os.Stat(filePath)
if fileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
}
file, err := os.Open(filePath)
if err != nil {
return nil, fmt.Errorf("failed to open token usage file: %w", err)
}
defer file.Close()
totalAIC := 0.0
found := false
scanner := bufio.NewScanner(file)
scanner.Buffer(make([]byte, 0, 64*1024), 1024*1024)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
continue
}
var entry TokenUsageEntry
if err := json.Unmarshal([]byte(line), &entry); err != nil {
continue
}
model := entry.Model
if model == "" {
model = "unknown"
}
totalAIC += computeModelInferenceAIC(entry.Provider, model, entry.InputTokens, entry.OutputTokens, entry.CacheReadTokens, entry.CacheWriteTokens, entry.ReasoningTokens)
found = true
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading token usage file: %w", err)
}
if !found {
return nil, nil
}
return &TokenUsageSummary{TotalAIC: totalAIC}, nil
}
agentUsagePath := findAgentUsageFile(runDir)
if agentUsagePath == "" {
return nil, nil
}
agentFileInfo, _ := os.Stat(agentUsagePath)
if agentFileInfo != nil {
console.LogVerbose(verbose, fmt.Sprintf(" Found agent usage file: %s (%d bytes)", filepath.Base(agentUsagePath), agentFileInfo.Size()))
}
data, err := os.ReadFile(filepath.Clean(agentUsagePath))
if err != nil {
return nil, fmt.Errorf("failed to read agent usage file: %w", err)
}
var entry TokenUsageEntry
if err := json.Unmarshal(data, &entry); err != nil {
return nil, fmt.Errorf("failed to parse agent usage file: %w", err)
}
model := entry.Model
if model == "" {
model = "unknown"
}
return &TokenUsageSummary{
TotalAIC: computeModelInferenceAIC(entry.Provider, model, entry.InputTokens, entry.OutputTokens, entry.CacheReadTokens, entry.CacheWriteTokens, entry.ReasoningTokens),
}, nil
}
func countAPIProxySteeringEvents(runDir string) int {
eventsPath := findAPIProxyEventsFile(runDir)
if eventsPath == "" {
return 0
}
count, err := parseAPIProxySteeringEvents(eventsPath)
if err != nil {
tokenUsageLog.Printf("Failed to parse API proxy events file %s: %v", eventsPath, err)
return 0
}
return count
}
func findAPIProxyEventsFile(runDir string) string {
primary := filepath.Join(runDir, "sandbox", "firewall", "logs", proxyEventsJSONLPath)
if _, err := os.Stat(primary); err == nil {
return primary
}
entries, err := os.ReadDir(runDir)
if err != nil {
return ""
}
for _, entry := range entries {
if !entry.IsDir() {
continue
}
name := entry.Name()
if strings.HasPrefix(name, "firewall-audit-logs") || strings.HasPrefix(name, "firewall-logs") {
candidate := filepath.Join(runDir, name, proxyEventsJSONLPath)
if _, err := os.Stat(candidate); err == nil {
return candidate
}
}
}
return ""
}
// proxyEventsEntry is a JSONL record from api-proxy-logs/events.jsonl.
// The event name appears under one of four field names depending on the proxy version;
// the message field is present on steering events.
type proxyEventsEntry struct {
// Event name appears under one of these four keys; all are checked.
Event string `json:"event"`
Type string `json:"type"`
EventNameSnake string `json:"event_name"`
EventNameCamel string `json:"eventName"`
// Message text (present on steering events).
Message string `json:"message"`
// Optional RFC3339/RFC3339Nano timestamp (not always present).
Timestamp string `json:"timestamp"`
}
// eventName returns the normalised event name from whichever field is populated.
func (e proxyEventsEntry) eventName() string {
for _, v := range []string{e.Event, e.Type, e.EventNameSnake, e.EventNameCamel} {
if v = strings.TrimSpace(v); v != "" {
return strings.ToLower(v)
}
}
return ""
}
// scanSteeringEntries reads all valid steering proxyEventsEntry records from r.
// Lines that fail the quick-keyword check or JSON decoding are silently skipped.
// The caller is responsible for the lifetime of r.
func scanSteeringEntries(r io.Reader) ([]proxyEventsEntry, error) {
var entries []proxyEventsEntry
scanner := bufio.NewScanner(r)
buf := make([]byte, maxScannerBufferSize)
scanner.Buffer(buf, maxScannerBufferSize)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || !containsSteeringKeyword(line) {
continue
}
var entry proxyEventsEntry
if err := json.Unmarshal([]byte(line), &entry); err != nil {
continue
}
if isSteeringEvent(entry.eventName(), strings.TrimSpace(entry.Message)) {
entries = append(entries, entry)
}
}
return entries, scanner.Err()
}
func parseAPIProxySteeringEvents(filePath string) (int, error) {
file, err := os.Open(filepath.Clean(filePath))
if err != nil {
return 0, err
}
defer file.Close()
entries, err := scanSteeringEntries(file)
return len(entries), err
}
func containsSteeringKeyword(line string) bool {
return strings.Contains(line, "steering") ||
strings.Contains(line, "STEERING") ||
strings.Contains(line, "Steering")
}
// isSteeringEvent matches AWF proxy steering events using both event name and
// message format from the firewall specification.
func isSteeringEvent(eventName, message string) bool {
switch eventName {
case tokenSteeringEventName:
return strings.HasPrefix(message, awfTokenWarningPrefix)
case timeoutSteeringEventName:
return strings.HasPrefix(message, awfTimeWarningPrefix)
default:
return false
}
}
func augmentSubagentModelAttribution(runDir string, summary *TokenUsageSummary) {
if summary == nil {
return
}
requests := extractSubagentModelRequests(runDir)
if len(requests) == 0 {
return
}
addTokenUsageWarning(summary, subagentStdioWarning)
actuals := make([]SubagentModelActual, 0, len(summary.ByModel))
observedModels := make(map[string]string, len(summary.ByModel))
for model, usage := range summary.ByModel {
if usage == nil || model == "" {
continue
}
actuals = append(actuals, SubagentModelActual{
Model: model,
Provider: usage.Provider,
Requests: usage.Requests,
})
observedModels[model] = usage.Provider
}
slices.SortStableFunc(actuals, func(a, b SubagentModelActual) int {
if a.Requests != b.Requests {
if a.Requests > b.Requests {
return -1
}
return 1
}
switch {
case a.Model < b.Model:
return -1
case a.Model > b.Model:
return 1
default:
return 0
}
})
summary.SubagentModelActuals = actuals
var fallbackEffectiveModel string
if len(observedModels) == 1 {
for model := range observedModels {
fallbackEffectiveModel = model
}
}
requestRows := make([]SubagentModelRequest, 0, len(requests))
mismatchCount := 0
for _, row := range requests {
if _, ok := observedModels[row.RequestedModel]; ok {
row.EffectiveModel = row.RequestedModel
} else {
row.EffectiveModel = fallbackEffectiveModel
if len(observedModels) == 0 {
row.ReasonCode = modelMismatchReasonTokenUsageMissing
} else {
row.ReasonCode = modelMismatchReasonModelNotObserved
}
mismatchCount += row.InvocationCount
}
requestRows = append(requestRows, row)
}
summary.SubagentModelRequests = requestRows
summary.MismatchCount = mismatchCount
}
func addTokenUsageWarning(summary *TokenUsageSummary, warning string) {
if summary == nil || warning == "" {
return
}
if slices.Contains(summary.Warnings, warning) {
return
}
summary.Warnings = append(summary.Warnings, warning)
}
func extractSubagentModelRequests(runDir string) []SubagentModelRequest {
agentStdioPath := findAgentStdioFile(runDir)
if agentStdioPath == "" {
return nil
}
file, err := os.Open(agentStdioPath)
if err != nil {
return nil
}
defer file.Close()
type key struct {
agent string
model string
}
counts := make(map[key]int)
reader := bufio.NewReader(file)
for {
line, readErr := reader.ReadString('\n')
line = strings.TrimSpace(line)
if line != "" {
matches := subagentDispatchPattern.FindAllStringSubmatch(line, -1)
for _, m := range matches {
if len(m) < 3 {
continue
}
agentName := strings.TrimSpace(m[1])
requestedModel := strings.TrimSpace(m[2])
if agentName == "" || requestedModel == "" {
continue
}
counts[key{agent: agentName, model: requestedModel}]++
}
}
if readErr == io.EOF {
break
}
if readErr != nil {
return nil
}
}
rows := make([]SubagentModelRequest, 0, len(counts))
for k, n := range counts {
rows = append(rows, SubagentModelRequest{
AgentName: k.agent,
RequestedModel: k.model,
InvocationCount: n,
})
}
slices.SortStableFunc(rows, func(a, b SubagentModelRequest) int {
if a.AgentName != b.AgentName {
if a.AgentName < b.AgentName {
return -1
}
return 1
}
switch {
case a.RequestedModel < b.RequestedModel:
return -1
case a.RequestedModel > b.RequestedModel:
return 1
default:
return 0
}
})
return rows
}
func findAgentStdioFile(runDir string) string {
primary := filepath.Join(runDir, "agent-stdio.log")
if _, err := os.Stat(primary); err == nil {
return primary
}
var found string
if walkErr := filepath.Walk(runDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info == nil || info.IsDir() {
return nil
}
if info.Name() == "agent-stdio.log" {
found = path
return filepath.SkipAll
}
return nil
}); walkErr != nil && !errors.Is(walkErr, filepath.SkipAll) {
tokenUsageLog.Printf("findAgentStdioFile walk error: %v", walkErr)
}
return found
}
func correlateToolCallsWithTokenDelta(toolCalls []MCPToolCall, tokenUsageFile string) []MCPToolCall {