feat: add xAI platform billing provider - #2472
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a85baf3e19
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| providerCost: ProviderCostSnapshot( | ||
| used: self.balanceUSD, | ||
| limit: 0, |
There was a problem hiding this comment.
Render xAI prepaid credits as a balance in CLI text
When an xAI fetch succeeds, this creates a snapshot with no rate-limit windows and a providerCost whose used value is actually the prepaid balance and whose limit is 0. In CLI text mode, CLIRenderer.appendPrimaryLines falls back for any no-window providerCost to Cost: <used> / <limit>, so codexbar --provider xai will show a misleading Cost: $balance / 0.0-style line instead of the prepaid balance; xAI needs a CLI-specific balance line or should avoid the generic cost fallback shape.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch — fixed in 3c0bdc0: xAI's balance-shaped providerCost is excluded from the generic no-window cost fallback, and the CLI text/card renderers now emit a dedicated Balance: $X.XX line plus the 30-day spend line (same pattern as the Devin/MiMo balance lines), covered by a new renderer test.
The generic no-window providerCost fallback printed the prepaid balance as "Cost: 7.4 / 0.0", presenting money remaining as spend against a zero budget. Exclude xAI's balance-shaped providerCost from that fallback and add a dedicated Balance + spend-window line to the CLI text, card body, and card extras renderers, matching the Devin/MiMo balance-line pattern. Flagged by Codex review on steipete#2472.
|
Codex review: needs maintainer review before merge. Reviewed July 29, 2026, 5:23 PM ET / 21:23 UTC. ClawSweeper reviewWhat this changesThis PR adds an optional xAI Management API provider that reads a team’s prepaid balance and 30-day daily platform spend, then exposes it through CodexBar settings, CLI output, menus, dashboards, diagnostics, tests, and documentation. Merge readinessKeep this PR open for maintainer acceptance rather than cleanup closure. It has credible live-account proof, focused provider coverage, and no supported correctness finding, but it deliberately adds a new optional credential-backed billing surface that needs an explicit product and support-boundary decision. Priority: P2 Review scores
Verification
How this fits togetherCodexBar collects usage data from configured AI providers and turns each provider snapshot into menu-bar, CLI, dashboard, and diagnostic output. The new xAI path accepts a Management API key and team ID, requests billing data from xAI, and renders prepaid-credit and spend-history information separately from the existing Grok consumer-subscription provider. flowchart LR
A[xAI Management key and team ID] --> B[Provider configuration]
B --> C[xAI Management API]
C --> D[Balance and daily spend snapshot]
D --> E[CLI and menu output]
D --> F[Cost-history dashboard]
D --> G[Diagnostics and docs]
Decision needed
Why: The implementation and live proof support the proposed behavior, but only maintainers can choose whether this new credential-backed provider and its ongoing API/support obligation belong in the product. Before merge
Agent review detailsSecurityNone. Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Accept the provider only if maintainers want CodexBar to support xAI developer-platform billing as a distinct optional surface, retaining its separate credentials, partial-history labeling, posted-ledger documentation, and focused regression coverage. Do we have a high-confidence way to reproduce the issue? Not applicable: this PR adds a new optional provider rather than repairing an established failing behavior. Its intended live behavior is nevertheless demonstrated by a redacted real-account CLI capture and endpoint-contract confirmation. Is this the best way to solve the issue? Yes, conditionally: a distinct xAI Management API provider is the narrowest maintainable design because it avoids coupling platform billing credentials and identity with the existing Grok consumer-quota provider. Maintainer approval is still required for the new product and credential surface. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against ab31038ed8cf. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rank-up movesOptional improvements that raise the rating; they are not merge blockers.
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (5 earlier review cycles)
|
|
Verified against the Management API with a management key (raw responses withheld). The prepaid-balance response represents remaining prepaid credit as a negative The usage response has @clawsweeper re-review |
|
Thanks @mitchins — that closes the live-contract question: inverted-ledger balance (negative cent-string @clawsweeper re-review |
|
🦞👀 Command router queued. I will update this comment with the next step. |
|
Status for the maintainer: the API contract is now confirmed on live data (thanks again @mitchins), and patch quality shows no findings across both review passes. The remaining bot gate is an end-to-end capture — this PR's build rendering a real account — which by nature needs an account holder: gh repo clone SilentKnight87/CodexBar -- --branch feat/xai-platform-provider --depth 1
cd CodexBar
swift build -c release --product CodexBarCLI
XAI_MANAGEMENT_API_KEY="…" XAI_TEAM_ID="…" ./.build/release/CodexBarCLI usage --provider xaiThe only account-derived lines in that output are |
Route xAI through cleanPlanName so the CLI plan badge shows "Management API" instead of the .capitalized mangling "Management Api" seen in live verification on steipete#2472. Document that the balance is the posted prepaid ledger: spend posts at billing-cycle close, so mid-cycle the ledger balance exceeds the Console's live remaining credit by the cycle's unposted spend, as confirmed on a real account.
|
Huge thanks @mitchins — that's the full end-to-end proof, and your balance observation was a genuinely useful catch. It matches the ledger schema: entries are keyed by billing period ( |
589a419 to
8c57b07
Compare
The generic no-window providerCost fallback printed the prepaid balance as "Cost: 7.4 / 0.0", presenting money remaining as spend against a zero budget. Exclude xAI's balance-shaped providerCost from that fallback and add a dedicated Balance + spend-window line to the CLI text, card body, and card extras renderers, matching the Devin/MiMo balance-line pattern. Flagged by Codex review on steipete#2472.
Route xAI through cleanPlanName so the CLI plan badge shows "Management API" instead of the .capitalized mangling "Management Api" seen in live verification on steipete#2472. Document that the balance is the posted prepaid ledger: spend posts at billing-cycle close, so mid-cycle the ledger balance exceeds the Console's live remaining credit by the cycle's unposted spend, as confirmed on a real account.
|
Rebased onto current |
8c57b07 to
dc21474
Compare
The generic no-window providerCost fallback printed the prepaid balance as "Cost: 7.4 / 0.0", presenting money remaining as spend against a zero budget. Exclude xAI's balance-shaped providerCost from that fallback and add a dedicated Balance + spend-window line to the CLI text, card body, and card extras renderers, matching the Devin/MiMo balance-line pattern. Flagged by Codex review on steipete#2472.
Route xAI through cleanPlanName so the CLI plan badge shows "Management API" instead of the .capitalized mangling "Management Api" seen in live verification on steipete#2472. Document that the balance is the posted prepaid ledger: spend posts at billing-cycle close, so mid-cycle the ledger balance exceeds the Console's live remaining credit by the cycle's unposted spend, as confirmed on a real account.
|
Rebased again onto current |
Add a separate xai provider for xAI developer-platform billing from the documented Management API (management key + team ID): prepaid credit balance from /prepaid/balance (inverted ledger in string USD cents; a 200 without a parseable total is an error, never $0.00) and a 30-day daily USD spend dashboard from /usage via the shared cost-history seam. History is best-effort enrichment - its failure keeps the balance while auth failures and cancellation escalate - and limitReached marks the window "(partial)" with estimated confidence instead of exact. No coupling to the Grok subscription provider. Covers descriptor, fetcher, settings reader/store, registries, config projection, widget and CLI switches, template icon with a Grok-distinctness test, docs, and fixture-based tests on stubbed transports. Moves ProviderIdentitySnapshot to its own file to hold the UsageFetcher length limit. (steipete#1712)
The generic no-window providerCost fallback printed the prepaid balance as "Cost: 7.4 / 0.0", presenting money remaining as spend against a zero budget. Exclude xAI's balance-shaped providerCost from that fallback and add a dedicated Balance + spend-window line to the CLI text, card body, and card extras renderers, matching the Devin/MiMo balance-line pattern. Flagged by Codex review on steipete#2472.
Route xAI through cleanPlanName so the CLI plan badge shows "Management API" instead of the .capitalized mangling "Management Api" seen in live verification on steipete#2472. Document that the balance is the posted prepaid ledger: spend posts at billing-cycle close, so mid-cycle the ledger balance exceeds the Console's live remaining credit by the cycle's unposted spend, as confirmed on a real account.
dc21474 to
c4313ca
Compare
|
Merged after full verification — owner-approved (Peter) as a first-class provider. xAI platform billing now surfaces prepaid ledger balance and 30-day spend from the xAI Management API, with credentials siloed from the Grok provider (verified during review that billing credentials can never be read from or attributed to Grok), sound team validation, error taxonomy, and honest partial-data semantics rather than a fabricated zero balance. Maintainer changes: removed the contributor CHANGELOG edit (maintainer adds changelog on merge), rebased through the recent provider landings, and regenerated |

Summary
Adds a separate
xaiprovider for xAI developer-platform billing, closing the gap described in #1712: prepaid credit balance plus a 30-day daily USD spend dashboard, sourced exclusively from xAI's documented Management API (management-api.x.ai) with an explicit Management key + team ID. No Grok coupling: the existing Grok provider keeps consumer Grok/SuperGrok quota, and identity/credentials are never shared between the two surfaces.ProviderConfig.apiKey/workspaceIDmap toXAI_MANAGEMENT_API_KEY/XAI_TEAM_ID. Inference API keys are rejected by the Management API and the docs/error copy say so.GET /v1/billing/teams/{team_id}/prepaid/balance. The documented example records a $10 top-up astotal.val = "-1000"(string USD cents, inverted ledger), so remaining balance =-cents/100. A 200 body without a parseable total is a hard parse error, never $0.00. Rendered as "Balance: $X.XX" via the same menu-card seam ZenMux/Neuralwatt use.POST /v1/billing/teams/{team_id}/usagewith the documentedanalyticsRequest(last 30 days UTC,TIME_UNIT_DAY,usd/AGGREGATION_SUM). Daily buckets render through the shared provider-cost-history inline dashboard (same path as Groq/OpenAI). History is best-effort: its failure keeps the balance (auth failures and cancellation still escalate).limitReachedmarks the snapshot estimated and labels the window "Last 30 days (partial)" instead of presenting truncated totals as exact.ProviderIdentitySnapshot/UsageDataConfidenceinto their own file to keepUsageFetcher.swiftunder the file-length lint limit.Commands run
swift buildswift test --filter XAIProviderTests(27 tests: request shape incl. exact analytics body, sign/unit mapping, malformed-200 guards, team-ID path-encoding + traversal rejection, error taxonomy 401/403/404/429/5xx, partial-success, limitReached, projections, Codable round-trip, registry, config projection, menu-card render)make test— the only failing suites (SpendDashboardControllerTests,SpendDashboardTokenProvenanceTests, 13 issues) reproduce identically on a pristinemaincheckout atcc8da27cin the same environment, so they are pre-existing and unrelated;CodexAccountScopedRefreshTestsis load-sensitive locally and passes clean (~3s) uncontended on both trees.make check(SwiftFormat + SwiftLint strict — 0 violations)node scripts/check-documentation-links.mjs,make docs-list./Scripts/regenerate-codex-parser-hash.sh write(CostUsageScanner case list touched)Live behavior proof
End-to-end on a real account: @mitchins built this branch and ran
CodexBarCLI usage --provider xaiagainst their live account (capture with screenshot) — output rendersBalance: $13.92/Last 30 days: $6.67exactly as implemented. They also verified both raw endpoints earlier (comment): the prepaid balance is a negativetotal.valnumeric string in minor USD units (the inverted-ledger mapping implemented here), and the usage response matches the implemented contract (timeSeries,group/groupLabels, dailydataPointswith numeric USDvalues, booleanlimitReached).One semantic learned from live data and now documented in
docs/xai.md: the balance endpoint reports the posted ledger — spend posts at billing-cycle close, so mid-cycle the ledger balance exceeds the Console's live remaining credit by the cycle's unposted spend. The live capture also surfaced a cosmetic plan-badge mangling ("Management Api"), fixed in a follow-up commit.Closes #1712.