MCPcopy Create free account
hub / github.com/github/gh-aw / analyzeTokenUsage

Function analyzeTokenUsage

pkg/cli/token_usage.go:488–528  ·  view source on GitHub ↗

analyzeTokenUsage finds and parses the token-usage.jsonl file from a run directory.

(runDir string, verbose bool)

Source from the content-addressed store, hash-verified

486
487// analyzeTokenUsage finds and parses the token-usage.jsonl file from a run directory.
488func analyzeTokenUsage(runDir string, verbose bool) (*TokenUsageSummary, error) {
489 tokenUsageLog.Printf("Analyzing token usage in: %s", runDir)
490
491 filePath := findTokenUsageFile(runDir)
492 if filePath != "" {
493 fileInfo, _ := os.Stat(filePath)
494 if fileInfo != nil {
495 console.LogVerbose(verbose, fmt.Sprintf(" Found token usage file: %s (%d bytes)", filepath.Base(filePath), fileInfo.Size()))
496 }
497
498 summary, err := parseTokenUsageFile(filePath)
499 if err != nil {
500 return summary, err
501 }
502 // When the file exists but contains no entries (e.g. usage artifact has an
503 // empty placeholder token_usage.jsonl), fall through to the agent_usage.json
504 // fallback rather than returning nil immediately.
505 if summary != nil {
506 summary.TotalSteeringEvents = countAPIProxySteeringEvents(runDir)
507 augmentSubagentModelAttribution(runDir, summary)
508 return summary, nil
509 }
510 }
511
512 agentUsagePath := findAgentUsageFile(runDir)
513 if agentUsagePath == "" {
514 return nil, nil
515 }
516 agentFileInfo, _ := os.Stat(agentUsagePath)
517 if agentFileInfo != nil {
518 console.LogVerbose(verbose, fmt.Sprintf(" Found agent usage file: %s (%d bytes)", filepath.Base(agentUsagePath), agentFileInfo.Size()))
519 }
520
521 summary, err := parseAgentUsageFile(agentUsagePath)
522 if err != nil || summary == nil {
523 return summary, err
524 }
525 summary.TotalSteeringEvents = countAPIProxySteeringEvents(runDir)
526 augmentSubagentModelAttribution(runDir, summary)
527 return summary, nil
528}
529
530func findUsageJSONLFiles(runDir string) []string {
531 usageDir := filepath.Join(runDir, "usage")

Callers 3

TestAnalyzeTokenUsageFunction · 0.85

Calls 8

LogVerboseFunction · 0.92
findTokenUsageFileFunction · 0.85
parseTokenUsageFileFunction · 0.85
findAgentUsageFileFunction · 0.85
parseAgentUsageFileFunction · 0.85
PrintfMethod · 0.45

Tested by 1

TestAnalyzeTokenUsageFunction · 0.68