Phase 2b: xAI Management API — usage analytics integration #2
Labels
No labels
amy
autonomous
ba
bug
deep
drop
face
feature
hannibal
infrastructure
murdock
needs-design
needs-ludo
priority
quick
vigilio
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference: trentuna/token-monitor#2
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What this is
xAI has a Management API (
https://management-api.x.ai) that provides programmatic access to billing and usage data — the same data visible inconsole.x.ai/usage. This is the missing piece for understanding our xAI spend.Status — 2026-04-06
Management key is live.
XAI_MANAGEMENT_KEYandXAI_TEAM_IDare exported in~/.secrets/keys.env.First real query results (Apr 4–6 spend, grouped by API key):
Root cause identified: B.A. was sharing
xai-vigiliokey with a 2M context window. Fixed:xai-bakeygrok-code-fast-1(256K context, 10-15x cheaper than grok-4-1-fast-reasoning)trentuna/a-team#103comment for full pricing comparisonContext caching finding: No API flag to disable prompt caching — it's automatic server-side. Context size is the only lever. Current windows: Face/Amy/Murdock 131K, Vigilio 131K, B.A. 256K. We're not setting
x-grok-conv-id(pi doesn't inject it), so we're not actively maximising cache hits — it just fires on repeated prefixes naturally.Rate limits: Currently NONE set on any key (verified via management API). All keys are wide open.
What to build
1.
providers/xai-billing.jsNew module wrapping the management API billing endpoints:
Env vars required:
XAI_MANAGEMENT_KEY,XAI_TEAM_ID(already in keys.env).Resolves key IDs → names by calling
/auth/teams/{id}/api-keysonce and caching the mapping.2. Rate limit management
The management API can set
qps,qpm,tpmper key. We should define sensible defaults and either:configure-key-limits.jsscript for reproducibilitySuggested limits per key:
3. Integration into
analyze.js(from #1)Add xAI billing section to the wake report:
Only rendered if
XAI_MANAGEMENT_KEYis set.4. Key ID → name resolution
The billing API returns key UUIDs, not names. Cache the mapping at startup:
The key endpoint
POST /v1/billing/teams/{team_id}/usageonhttps://management-api.x.aiSupports: time range, time unit, groupBy (
api_key_id,unit_type,description,model), aggregations, filters.Docs:
xai-docs page "developers/rest-api-reference/management/billing"Dependencies
analyze.js@vigilio, is this a component or added layer to the broader token-monitor?
Can you prepare a non spoofed mission brief for the a-team with added broader scope view on token monitor and then assign the mission to the Hannibal to run the whole team on it?
End product should be data that you can programatically interpret on how to manage our budgets, where and what to watch out for how, and another is the human TUI view.
A cool use cases would be to track a-team cost per mission for examples.
Session 91 — xAI billing module shipped
Commit:
2371e02—providers/xai-billing.js+analyze.jsintegrationWhat was built
providers/xai-billing.js— three public exports:fetchXaiUsage({ managementKey, teamId, startDate, endDate })— usage by API key + unit type viaPOST /v1/billing/teams/{id}/usagefetchXaiInvoicePreview({ managementKey, teamId })— current period total, prepaid credits, balance viaGET /v1/billing/teams/{id}/postpaid/invoice/previewrenderXaiSection(...)— formatted text section for the analyze.js reportanalyze.jschanges:--xaiflag: standalone xAI spend viewnode analyze.js) now appends xAI section whenXAI_MANAGEMENT_KEYis setLive output (April 1–6 billing period)
API finding (for future reference)
The key discovery:
analyticsRequest.timeRangerequires format"YYYY-MM-DD HH:MM:SS"withtimezone: "Etc/GMT". Using ISO8601 orTIME_UNIT_NONEwithout exact endTime gives a 3/Invalid argument error. ThegroupLabelsfield on each series already includes human-readable key names — no separate key-listing call needed.Remaining from this issue
--jsonflag still returns Anthropic Teams data only. If Ludo wants xAI in the JSON output, easy addition.The core ask (billing visibility) is delivered. Marking ready for Ludo's review.
Session 92 — starting rate limit enforcement work.
All 5 keys confirmed wide open (no qps/qpm/tpm set). Building
configure-key-limits.js— a reproducible script to apply per-key limits viaPUT /auth/api-keys/{id}.Planned limits:
Rate limit script built — blocked on one permission grant.
configure-key-limits.jsis written and tested (dry-run works). Live apply hits 403:The
trentuna-token-monitormanagement key hasBillingReadand a full read-only ACL set, but noUpdateApiKey.Ludo action needed: Add
team-token:endpoint:UpdateApiKeyto thetrentuna-token-monitormanagement key atconsole.x.ai → Settings → Management keys.Once granted, run:
That will set:
The script is idempotent — safe to re-run. Closing out the billing integration portion of this issue; rate limit application becomes operational once the ACL is in place.
Session 97 — delivered the broader mission brief Ludo requested.
Created token-monitor#3 — "Mission: Budget Intelligence & TUI" — and assigned to Hannibal. That issue covers:
--budget-jsonflag with USD estimates, provider recommendations, alerts--mission bookmarko#1to attribute utilization delta to a specific Forgejo issue windownode tui.js, ANSI only, 60s auto-refresh, no external depsThis issue (#2) has delivered the xAI billing module. Rate limit enforcement remains blocked on the ACL grant (
team-token:endpoint:UpdateApiKey) — see comment #1733 for the exact Ludo action needed. Closing this issue; the broader scope lives in #3.This was closed but:
@vigilio wrote in https://trentuna.exe.xyz/git/trentuna/token-monitor/issues/2#issuecomment-1733:
done
done
what are
qpsandqpm?:P
Also,
Session update — rate limits + clarifications
Rate limits: Re-ran
configure-key-limits.js— applied successfully to all 5 keys. No errors. All keys now have qps/qpm guards.qps / qpm: Queries per second and queries per minute — the maximum request rate the xAI API will accept before returning a 429 (rate limit hit). A query here means one API call, regardless of token count. We set them conservatively (e.g. xai-ba: 2/sec, 30/min) so that even if B.A. fires rapid sequential requests during a build, we don't blow the daily budget accidentally.
On
tui.jsnot found: That's correct — it doesn't exist yet. The TUI was spec'd in token-monitor#3 (Hannibal's mission brief, session 97) as the B.A. deliverable, but that mission hasn't been run yet. Hannibal hasn't started it.The issue brief exists, the code does not. Apologies for the confusion — I created #3 as a future mission spec, not a completion notice. The TUI is the thing that needs to be built.
On "notification / email for what was built and is available": That's a real gap. I build things in issue comments and commits, but there's no mechanism pushing a summary to you. You have to read the issues to find out. That's backwards.
Opening a new issue for this — a build digest emailer. It scans recent commits and closed issues, formats a summary, and sends it on demand (or on schedule). You should be able to run one command and get "here's what changed this week" in your inbox.
Will track as a new issue.
Session 114 — Phase 2b complete
Status: all deliverables shipped. Closing this issue.
What's done
providers/xai-billing.js— fetchXaiUsage, fetchXaiInvoicePreview, aggregateByKey, renderXaiSectionanalyze.js(--xaiflag + full report section)configure-key-limits.js— qps/qpm per key roleRunning
node analyze.js --xainow returns:⚠️ Budget alert for Ludo
xAI prepaid credit is nearly exhausted: $3.19 remaining of $31.00.
The $27.88 from
xai-vigiliois the primary driver — a combination of xai_search calls, grok reasoning queries, and image generation. This will exhaust within a session or two of normal use. The billing cycle is 2026-04 so it resets at end of April.Recommended action: Top up the prepaid credit in console.x.ai, or suspend xai_search usage until the cycle resets.
TUI (token-monitor#3) is still unbuilt — that's Hannibal's mission brief and hasn't been executed yet.
Closing this issue. Phase 2b is done.