diff --git a/packages/ai/src/usage/xai-oauth.ts b/packages/ai/src/usage/xai-oauth.ts index 161533d1f..b15984491 100644 --- a/packages/ai/src/usage/xai-oauth.ts +++ b/packages/ai/src/usage/xai-oauth.ts @@ -202,9 +202,12 @@ function parseMonthlyBillingConfig(raw: Record): XaiMonthlyBill onDemandUsed: parseOnDemandAmount(raw.onDemandUsed), }; } -function hasPositiveMonthlyLimit(raw: Record): boolean { + +function confirmsNoMonthlyQuota(raw: Record): boolean { const limit = parseOnDemandAmount(raw.monthlyLimit); - return limit !== undefined && limit > 0; + if (limit !== undefined) return limit === 0; + // Some weekly accounts return the credits shape from the default endpoint too. + return parseWeeklyBillingConfig(raw)?.inferredPercent === true; } function buildOnDemandLimit( @@ -393,7 +396,7 @@ export const xaiOauthUsageProvider: UsageProvider = { monthlyPayload && isRecord(monthlyPayload) && isRecord(monthlyPayload.config) ? monthlyPayload.config : null; - if (!monthlyConfig || hasPositiveMonthlyLimit(monthlyConfig)) { + if (!monthlyConfig || !confirmsNoMonthlyQuota(monthlyConfig)) { effectiveWeekly = null; } } diff --git a/packages/ai/test/xai-oauth-usage.test.ts b/packages/ai/test/xai-oauth-usage.test.ts index 09674d4ff..e2556ef80 100644 --- a/packages/ai/test/xai-oauth-usage.test.ts +++ b/packages/ai/test/xai-oauth-usage.test.ts @@ -336,6 +336,31 @@ describe("xai-oauth usage provider", () => { expect(report).toBeNull(); }); + it("rejects inferred unified weekly usage when monthly quota evidence is absent", async () => { + const periodEnd = new Date(Date.now() + 6 * 24 * 60 * 60 * 1000).toISOString(); + const periodStart = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString(); + const report = await xaiOauthUsageProvider.fetchUsage( + { provider: "xai-oauth", credential: makeCredential() }, + { + fetch: dualBillingFetch( + { + config: { + currentPeriod: { + end: periodEnd, + start: periodStart, + type: "USAGE_PERIOD_TYPE_WEEKLY", + }, + isUnifiedBillingUser: true, + }, + }, + { config: { isUnifiedBillingUser: true } }, + ).fetch, + }, + ); + + expect(report).toBeNull(); + }); + it("falls back to monthly included quota when credits has no percent fields", async () => { const { fetch, calls } = dualBillingFetch(makeUnifiedCreditsPayload(), makeUnifiedMonthlyPayload()); const report = await xaiOauthUsageProvider.fetchUsage(