| | | 1 | | using System.Globalization; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using OpenAI.Chat; |
| | | 5 | | |
| | | 6 | | namespace OpenAiIntegration; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Service for tracking token usage and costs across multiple API calls |
| | | 10 | | /// </summary> |
| | | 11 | | public class TokenUsageTracker : ITokenUsageTracker |
| | | 12 | | { |
| | | 13 | | private readonly ILogger<TokenUsageTracker> _logger; |
| | | 14 | | private readonly ICostCalculationService _costCalculationService; |
| | 1 | 15 | | private readonly object _lock = new(); |
| | | 16 | | |
| | | 17 | | private int _totalUncachedInputTokens = 0; |
| | | 18 | | private int _totalCachedInputTokens = 0; |
| | | 19 | | private int _totalOutputReasoningTokens = 0; |
| | | 20 | | private int _totalOutputTokens = 0; |
| | | 21 | | private decimal _totalCost = 0m; |
| | | 22 | | |
| | | 23 | | // Track last usage for individual match reporting |
| | | 24 | | private int _lastUncachedInputTokens = 0; |
| | | 25 | | private int _lastCachedInputTokens = 0; |
| | | 26 | | private int _lastOutputReasoningTokens = 0; |
| | | 27 | | private int _lastOutputTokens = 0; |
| | | 28 | | private decimal _lastCost = 0m; |
| | | 29 | | private ChatTokenUsage? _lastUsage = null; |
| | | 30 | | |
| | 1 | 31 | | public TokenUsageTracker(ILogger<TokenUsageTracker> logger, ICostCalculationService costCalculationService) |
| | | 32 | | { |
| | 1 | 33 | | _logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
| | 1 | 34 | | _costCalculationService = costCalculationService ?? throw new ArgumentNullException(nameof(costCalculationServic |
| | 1 | 35 | | } |
| | | 36 | | |
| | | 37 | | public void AddUsage(string model, ChatTokenUsage usage) |
| | | 38 | | { |
| | 1 | 39 | | lock (_lock) |
| | | 40 | | { |
| | | 41 | | // Store last usage for individual reporting |
| | 1 | 42 | | _lastUsage = usage; |
| | | 43 | | |
| | | 44 | | // Get token counts |
| | 1 | 45 | | var cachedInputTokens = usage.InputTokenDetails?.CachedTokenCount ?? 0; |
| | 1 | 46 | | var uncachedInputTokens = usage.InputTokenCount - cachedInputTokens; |
| | 1 | 47 | | var outputReasoningTokens = usage.OutputTokenDetails?.ReasoningTokenCount ?? 0; |
| | 1 | 48 | | var regularOutputTokens = usage.OutputTokenCount - outputReasoningTokens; |
| | | 49 | | |
| | | 50 | | // Store last usage for individual reporting |
| | 1 | 51 | | _lastUncachedInputTokens = uncachedInputTokens; |
| | 1 | 52 | | _lastCachedInputTokens = cachedInputTokens; |
| | 1 | 53 | | _lastOutputReasoningTokens = outputReasoningTokens; |
| | 1 | 54 | | _lastOutputTokens = regularOutputTokens; |
| | | 55 | | |
| | | 56 | | // Add to totals |
| | 1 | 57 | | _totalUncachedInputTokens += uncachedInputTokens; |
| | 1 | 58 | | _totalCachedInputTokens += cachedInputTokens; |
| | 1 | 59 | | _totalOutputReasoningTokens += outputReasoningTokens; |
| | 1 | 60 | | _totalOutputTokens += regularOutputTokens; |
| | | 61 | | |
| | | 62 | | // Calculate cost for this usage |
| | 1 | 63 | | var costForThisUsage = _costCalculationService.CalculateCost(model, usage) ?? 0m; |
| | 1 | 64 | | _lastCost = costForThisUsage; |
| | 1 | 65 | | _totalCost += costForThisUsage; |
| | | 66 | | |
| | 1 | 67 | | _logger.LogDebug("Added usage for model {Model}: {UncachedInput} uncached + {CachedInput} cached + {OutputRe |
| | 1 | 68 | | model, uncachedInputTokens, cachedInputTokens, outputReasoningTokens, regularOutputTokens, costForThisUs |
| | 1 | 69 | | } |
| | 1 | 70 | | } |
| | | 71 | | |
| | | 72 | | public string GetCompactSummary() |
| | | 73 | | { |
| | 1 | 74 | | lock (_lock) |
| | | 75 | | { |
| | 1 | 76 | | return $"{_totalUncachedInputTokens.ToString("N0", CultureInfo.InvariantCulture)} / {_totalCachedInputTokens |
| | | 77 | | } |
| | 1 | 78 | | } |
| | | 79 | | |
| | | 80 | | public string GetCompactSummaryWithEstimatedCosts(string estimatedCostsModel) |
| | | 81 | | { |
| | 1 | 82 | | lock (_lock) |
| | | 83 | | { |
| | 1 | 84 | | var baseSummary = $"{_totalUncachedInputTokens.ToString("N0", CultureInfo.InvariantCulture)} / {_totalCached |
| | | 85 | | |
| | | 86 | | // Calculate estimated costs for the alternative model |
| | 1 | 87 | | decimal totalEstimatedCost = CalculateTotalEstimatedCost(estimatedCostsModel); |
| | | 88 | | |
| | 1 | 89 | | return $"{baseSummary} (est {estimatedCostsModel}: ${totalEstimatedCost.ToString("F4", CultureInfo.Invariant |
| | | 90 | | } |
| | 1 | 91 | | } |
| | | 92 | | |
| | | 93 | | private decimal CalculateTotalEstimatedCost(string estimatedCostsModel) |
| | | 94 | | { |
| | | 95 | | // Manually calculate estimated cost based on our tracked totals |
| | 1 | 96 | | if (!ModelPricingData.Pricing.TryGetValue(estimatedCostsModel, out var pricing)) |
| | | 97 | | { |
| | 1 | 98 | | return 0m; // Can't calculate if we don't have pricing info |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | // Calculate costs for each component |
| | 1 | 102 | | var uncachedInputCost = (_totalUncachedInputTokens / 1_000_000m) * pricing.InputPrice; |
| | 1 | 103 | | var cachedInputCost = pricing.CachedInputPrice.HasValue |
| | 1 | 104 | | ? (_totalCachedInputTokens / 1_000_000m) * pricing.CachedInputPrice.Value |
| | 1 | 105 | | : 0m; |
| | 1 | 106 | | var totalOutputTokenCount = _totalOutputReasoningTokens + _totalOutputTokens; |
| | 1 | 107 | | var outputCost = (totalOutputTokenCount / 1_000_000m) * pricing.OutputPrice; |
| | | 108 | | |
| | 1 | 109 | | return uncachedInputCost + cachedInputCost + outputCost; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | public string GetLastUsageCompactSummary() |
| | | 113 | | { |
| | 1 | 114 | | lock (_lock) |
| | | 115 | | { |
| | 1 | 116 | | return $"{_lastUncachedInputTokens.ToString("N0", CultureInfo.InvariantCulture)} / {_lastCachedInputTokens.T |
| | | 117 | | } |
| | 1 | 118 | | } |
| | | 119 | | |
| | | 120 | | public string GetLastUsageCompactSummaryWithEstimatedCosts(string estimatedCostsModel) |
| | | 121 | | { |
| | 1 | 122 | | lock (_lock) |
| | | 123 | | { |
| | 1 | 124 | | var baseSummary = $"{_lastUncachedInputTokens.ToString("N0", CultureInfo.InvariantCulture)} / {_lastCachedIn |
| | | 125 | | |
| | | 126 | | // Calculate estimated cost for last usage |
| | 1 | 127 | | if (_lastUsage != null) |
| | | 128 | | { |
| | 1 | 129 | | var estimatedCost = _costCalculationService.CalculateCost(estimatedCostsModel, _lastUsage) ?? 0m; |
| | 1 | 130 | | return $"{baseSummary} (est {estimatedCostsModel}: ${estimatedCost.ToString("F4", CultureInfo.InvariantC |
| | | 131 | | } |
| | | 132 | | |
| | 1 | 133 | | return baseSummary; |
| | | 134 | | } |
| | 1 | 135 | | } |
| | | 136 | | |
| | | 137 | | public string? GetLastUsageJson() |
| | | 138 | | { |
| | 1 | 139 | | lock (_lock) |
| | | 140 | | { |
| | 1 | 141 | | if (_lastUsage == null) |
| | 1 | 142 | | return null; |
| | | 143 | | |
| | 1 | 144 | | var usageData = new |
| | 1 | 145 | | { |
| | 1 | 146 | | InputTokenCount = _lastUsage.InputTokenCount, |
| | 1 | 147 | | OutputTokenCount = _lastUsage.OutputTokenCount, |
| | 1 | 148 | | InputTokenDetails = _lastUsage.InputTokenDetails != null ? new |
| | 1 | 149 | | { |
| | 1 | 150 | | CachedTokenCount = _lastUsage.InputTokenDetails.CachedTokenCount, |
| | 1 | 151 | | AudioTokenCount = _lastUsage.InputTokenDetails.AudioTokenCount |
| | 1 | 152 | | } : null, |
| | 1 | 153 | | OutputTokenDetails = _lastUsage.OutputTokenDetails != null ? new |
| | 1 | 154 | | { |
| | 1 | 155 | | ReasoningTokenCount = _lastUsage.OutputTokenDetails.ReasoningTokenCount, |
| | 1 | 156 | | AudioTokenCount = _lastUsage.OutputTokenDetails.AudioTokenCount |
| | 1 | 157 | | } : null |
| | 1 | 158 | | }; |
| | | 159 | | |
| | 1 | 160 | | return JsonSerializer.Serialize(usageData, new JsonSerializerOptions { WriteIndented = false }); |
| | | 161 | | } |
| | 1 | 162 | | } |
| | | 163 | | |
| | | 164 | | public decimal GetTotalCost() |
| | | 165 | | { |
| | 1 | 166 | | lock (_lock) |
| | | 167 | | { |
| | 1 | 168 | | return _totalCost; |
| | | 169 | | } |
| | 1 | 170 | | } |
| | | 171 | | |
| | | 172 | | public decimal GetLastCost() |
| | | 173 | | { |
| | 1 | 174 | | lock (_lock) |
| | | 175 | | { |
| | 1 | 176 | | return _lastCost; |
| | | 177 | | } |
| | 1 | 178 | | } |
| | | 179 | | |
| | | 180 | | public void Reset() |
| | | 181 | | { |
| | 1 | 182 | | lock (_lock) |
| | | 183 | | { |
| | 1 | 184 | | _totalUncachedInputTokens = 0; |
| | 1 | 185 | | _totalCachedInputTokens = 0; |
| | 1 | 186 | | _totalOutputReasoningTokens = 0; |
| | 1 | 187 | | _totalOutputTokens = 0; |
| | 1 | 188 | | _totalCost = 0m; |
| | | 189 | | |
| | 1 | 190 | | _lastUncachedInputTokens = 0; |
| | 1 | 191 | | _lastCachedInputTokens = 0; |
| | 1 | 192 | | _lastOutputReasoningTokens = 0; |
| | 1 | 193 | | _lastOutputTokens = 0; |
| | 1 | 194 | | _lastCost = 0m; |
| | 1 | 195 | | _lastUsage = null; |
| | | 196 | | |
| | 1 | 197 | | _logger.LogDebug("Token usage tracker reset"); |
| | 1 | 198 | | } |
| | 1 | 199 | | } |
| | | 200 | | } |