-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Publish live Grok tokens and xAI spend into Usage & Spend #3085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
steipete
merged 13 commits into
steipete:main
from
Chipagosfinest:agent/grok-xai-live-spend
Aug 20, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f868195
Publish live Grok tokens and xAI spend into Usage & Spend
Chipagosfinest 5f869e7
docs: add #3085 to changelog
Chipagosfinest 0ef3b36
Fix lint on Grok and xAI spend messages
Chipagosfinest 12260d3
Fix Grok/xAI spend publication, Today, and coverage
Chipagosfinest eeba582
Fix Grok and xAI spend edge cases
Chipagosfinest c48d4a2
Fix OpenRouter localization test after #3086
Chipagosfinest 85dedfd
Merge branch 'main' into agent/grok-xai-live-spend
steipete a4f8067
Align OpenRouter localization test with main
Chipagosfinest f00f229
Merge origin/main
steipete 1ba4389
Merge origin/main
steipete be953ac
Merge remote-tracking branch 'origin/main' into pr-3085
steipete bda138a
test: reconcile gatekeeper anchors and fingerprints with Grok/xAI spe…
steipete a45b485
test: include Grok and xAI in the cost-capable dashboard source contract
steipete File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
Sources/CodexBarCore/Providers/XAI/XAICostUsageMapping.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import Foundation | ||
|
|
||
| public enum XAICostUsageMapping { | ||
| public static let historyDays = 30 | ||
| private static let dayPattern = #"^\d{4}-\d{2}-\d{2}$"# | ||
|
|
||
| /// True when prepaid balance arrived but `/usage` history did not. | ||
| public static func isAnalyticsUnavailable(_ snapshot: UsageSnapshot) -> Bool { | ||
| !snapshot.details.contains { $0.chart != nil } | ||
| } | ||
|
|
||
| /// Maps the Management API daily-spend chart onto the shared spend catalog. | ||
| /// Prepaid ledger balance is remaining credit, not spend, so it is never used here. | ||
| public static func tokenSnapshot(from snapshot: UsageSnapshot, historyDays _: Int) -> CostUsageTokenSnapshot? { | ||
| guard let chart = snapshot.details.compactMap(\.chart).first else { return nil } | ||
| let entries = chart.points | ||
| .compactMap { point -> CostUsageDailyReport.Entry? in | ||
| guard point.label.range(of: self.dayPattern, options: .regularExpression) != nil, | ||
| point.value.isFinite, | ||
| point.value >= 0 | ||
| else { return nil } | ||
| return CostUsageDailyReport.Entry( | ||
| date: point.label, | ||
| inputTokens: nil, | ||
| outputTokens: nil, | ||
| totalTokens: nil, | ||
| costUSD: point.value, | ||
| modelsUsed: nil, | ||
| modelBreakdowns: nil) | ||
| } | ||
| .sorted { $0.date < $1.date } | ||
| let total = entries.compactMap(\.costUSD).reduce(0, +) | ||
| guard total.isFinite else { return nil } | ||
| let today = self.utcDayKey(snapshot.updatedAt) | ||
| return CostUsageTokenSnapshot( | ||
| sessionTokens: nil, | ||
| sessionCostUSD: entries.first { $0.date == today }?.costUSD, | ||
| last30DaysTokens: nil, | ||
| last30DaysCostUSD: total, | ||
| historyDays: Self.historyDays, | ||
| historyCoverageIsEstablished: snapshot.dataConfidence != .estimated, | ||
| historyLabel: snapshot.dataConfidence == .estimated ? "Last 30 days (partial)" : nil, | ||
| meteredCostUSD: total, | ||
| costProvenance: .vendorMetered, | ||
| daily: entries, | ||
| updatedAt: snapshot.updatedAt) | ||
| } | ||
|
|
||
| static func utcDayKey(_ date: Date) -> String { | ||
| var calendar = Calendar(identifier: .gregorian) | ||
| calendar.timeZone = TimeZone(secondsFromGMT: 0) ?? .gmt | ||
| let components = calendar.dateComponents([.year, .month, .day], from: date) | ||
| return String( | ||
| format: "%04d-%02d-%02d", | ||
| components.year ?? 0, | ||
| components.month ?? 0, | ||
| components.day ?? 0) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.