Skip to content

feat(xai): surface Grok usage on quota dashboard - #5806

Merged
diegosouzapw merged 1 commit into
release/v3.8.44from
feat/port-pr-2150-xai-usage
Jul 3, 2026
Merged

diegosouzapw merged 1 commit into
release/v3.8.44from
feat/port-pr-2150-xai-usage

Conversation

@diegosouzapw

Copy link
Copy Markdown
Owner

Summary

xAI (Grok) had no usage-dashboard support: xai was absent from USAGE_FETCHER_PROVIDERS and had no case in getUsageForProvider, so the quota dashboard showed nothing for xAI connections. xAI also has no public per-account quota API — the billing console at console.x.ai requires a session cookie, not an API key.

This adds local, self-tracked usage for xAI, mirroring the existing Xiaomi MiMo self-track pattern: OmniRoute sums the tokens it itself routed to the connection (from usage_history) and surfaces them on the quota dashboard. Unlike Xiaomi MiMo, xAI has no fixed monthly cap, so the aggregate is reported as unlimited: true with remaining: 100 — this renders the dashboard's green "100%" badge instead of a meaningless progress bar against a zero total.

Changes

  • open-sse/services/usage.ts
    • Add getXaiUsage(connectionId) — aggregates tokens via getMonthlyProviderTokensForConnection("xai", connectionId) (from src/lib/usage/usageStats.ts) and returns a cumulative { unlimited: true, remaining: 100 } quota, provider + connection-scoped.
    • Register "xai" in USAGE_FETCHER_PROVIDERS.
    • Add a case "xai" to getUsageForProvider delegating to getXaiUsage.
    • Export getXaiUsage from __testing.
  • CHANGELOG.md — new-features entry.
  • tests/unit/xai-usage.test.ts — new regression test (real temp SQLite DB).

The usage route handler (src/app/api/usage/[connectionId]/route.ts) is a thin delegator and is intentionally left untouched.

Attribution

Thanks to @DevEstacion for the original implementation.

Testing

New tests/unit/xai-usage.test.ts (node:test, real temp DATA_DIR SQLite DB, with resetDbInstance() + temp cleanup in test.after):

  • inserts usage_history rows for a xai connection at now / out-of-window / a different connection / a different provider;
  • asserts getXaiUsage / getUsageForProvider("xai", ...) aggregate only in-window rows for the target connection (no provider/connection bleed);
  • asserts the quota is unlimited: true with remaining: 100;
  • asserts "xai" is registered in USAGE_FETCHER_PROVIDERS.

TDD: failed first (function/registration absent), passes after the implementation — 7/7 green.

node --import tsx/esm --test tests/unit/xai-usage.test.ts
# tests 7 | pass 7 | fail 0

npm run typecheck:core clean; eslint clean on changed files. The existing tests/unit/xiaomi-mimo-selftrack-usage.test.ts stays green (no regression).

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements self-tracked cumulative usage tracking for xAI (Grok) by querying locally routed tokens, as xAI lacks a public quota API. It also adds comprehensive unit tests for this new provider. The review feedback suggests calculating and providing the actual monthly reset timestamp for the resetAt field instead of hardcoding it to null, ensuring the dashboard accurately displays when the usage count resets.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +333 to +345
return {
plan: "xAI / Grok (OmniRoute-tracked)",
quotas: {
monthly: {
used,
total: 0,
remaining: 100,
remainingPercentage: 100,
resetAt: null,
unlimited: true,
} as UsageQuota,
},
};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since xAI usage is tracked on a monthly basis using getMonthlyProviderTokensForConnection, the usage will reset at the start of each calendar month. Hardcoding resetAt to null is misleading for the user as the dashboard won't display when the usage resets, and the count will suddenly drop to 0 on the 1st of the next month. We should calculate and provide the actual reset timestamp (the start of the next UTC month), mirroring the pattern used in getXiaomiMimoUsage.

    const now = new Date();
    const resetAt = new Date(
      Date.UTC(now.getUTCFullYear(), now.getUTCMonth() + 1, 1)
    ).toISOString();
    return {
      plan: "xAI / Grok (OmniRoute-tracked)",
      quotas: {
        monthly: {
          used,
          total: 0,
          remaining: 100,
          remainingPercentage: 100,
          resetAt,
          unlimited: true,
        } as UsageQuota,
      },
    };

@diegosouzapw
diegosouzapw changed the base branch from release/v3.8.43 to release/v3.8.44 July 2, 2026 16:18
…ry aggregation

xAI has no public per-account quota API (the billing console requires a
session cookie, not an API key). Add getXaiUsage(connectionId), mirroring
the existing Xiaomi MiMo self-track pattern: sum tokens routed to the
connection from usage_history via getMonthlyProviderTokensForConnection
and surface them as a cumulative, uncapped quota (unlimited: true,
remaining: 100 — xAI has no fixed monthly cap). Register 'xai' in
USAGE_FETCHER_PROVIDERS and wire a switch case in getUsageForProvider.

Co-authored-by: ron <devestacion@gmail.com>
Inspired-by: decolua/9router#2150
@diegosouzapw
diegosouzapw force-pushed the feat/port-pr-2150-xai-usage branch from 51fb769 to 57d2893 Compare July 3, 2026 11:50
@diegosouzapw
diegosouzapw merged commit b7160e9 into release/v3.8.44 Jul 3, 2026
1 of 3 checks passed
@diegosouzapw
diegosouzapw deleted the feat/port-pr-2150-xai-usage branch July 3, 2026 12:12
@diegosouzapw diegosouzapw mentioned this pull request Jul 4, 2026
tkgo11 pushed a commit to tkgo11/OmniRoute that referenced this pull request Sep 23, 2026
…ry aggregation (diegosouzapw#5806)

xAI has no public per-account quota API (the billing console requires a
session cookie, not an API key). Add getXaiUsage(connectionId), mirroring
the existing Xiaomi MiMo self-track pattern: sum tokens routed to the
connection from usage_history via getMonthlyProviderTokensForConnection
and surface them as a cumulative, uncapped quota (unlimited: true,
remaining: 100 — xAI has no fixed monthly cap). Register 'xai' in
USAGE_FETCHER_PROVIDERS and wire a switch case in getUsageForProvider.


Inspired-by: decolua/9router#2150

Co-authored-by: ron <devestacion@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant