ChatGPTNextWeb/NextChat · error · Error

Failed to query usage from openai

Error message

Failed to query usage from openai

What it means

Thrown at openai.ts:464 when either the 'used' or 'subs' fetch to OpenAI's billing endpoints returns a non-2xx status (not .ok), after the 401 check has already passed. The two endpoints are dashboard/billing/usage and dashboard/billing/subscription, both of which OpenAI deprecated and removed from api.openai.com, so against the real API this almost always fires on 404/410 regardless of a valid key.

Source

Thrown at app/client/platforms/openai.ts:464

          `${OpenaiPath.UsagePath}?start_date=${startDate}&end_date=${endDate}`,
        ),
        {
          method: "GET",
          headers: getHeaders(),
        },
      ),
      fetch(this.path(OpenaiPath.SubsPath), {
        method: "GET",
        headers: getHeaders(),
      }),
    ]);

    if (used.status === 401) {
      throw new Error(Locale.Error.Unauthorized);
    }

    if (!used.ok || !subs.ok) {
      throw new Error("Failed to query usage from openai");
    }

    const response = (await used.json()) as {
      total_usage?: number;
      error?: {
        type: string;
        message: string;
      };
    };

    const total = (await subs.json()) as {
      hard_limit_usd?: number;
    };

    if (response.error && response.error.type) {
      throw Error(response.error.message);
    }

View on GitHub (pinned to defdcdb55d)

Solutions

  1. If querying the real api.openai.com, stop relying on dashboard/billing/* — they are removed; guard with a 404 check and return an 'unavailable' usage object instead of throwing.
  2. If behind a proxy, confirm the proxy implements the billing/usage and billing/subscription routes or disable the usage panel.
  3. Add per-response status logging (used.status, subs.status) so the actual failure code is visible instead of a generic message.
  4. Handle 429 with backoff/retry and surface a 'rate limited' message rather than the generic failure.
  5. Wrap the whole usage() call in the caller so a failure degrades gracefully (hide the usage widget) instead of blocking the UI.

Example fix

// before
if (!used.ok || !subs.ok) {
  throw new Error("Failed to query usage from openai");
}

// after
if (!used.ok || !subs.ok) {
  const code = !used.ok ? used.status : subs.status;
  if (code === 404) {
    // billing endpoints not supported by this provider/proxy
    return { used: undefined, total: undefined } as LLMUsage;
  }
  throw new Error(`Failed to query usage from openai (status ${code})`);
}
Defensive patterns

Strategy: fallback

Validate before calling

const isBillingSupported = (baseUrl: string): boolean => {
  // official api.openai.com removed dashboard/billing/*; only proxies that implement them qualify
  return !/api\.openai\.com/.test(baseUrl);
};

if (isBillingSupported(baseUrl)) {
  const usage = await openai.usage();
} else {
  return { used: undefined, total: undefined };
}

Type guard

function isUsageEndpointRemoved(res: Response): boolean {
  return res.status === 404 || res.status === 410;
}

Try / catch

try {
  return await openai.usage();
} catch (e) {
  if (e instanceof Error && e.message.startsWith("Failed to query usage")) {
    // degrade: hide the usage widget instead of erroring
    return { used: undefined, total: undefined };
  }
  throw e;
}

Prevention

When it happens

Trigger: Any non-401 failure status from GET dashboard/billing/usage or GET dashboard/billing/subscription: 404 (endpoints removed from api.openai.com), 429 rate limit, 500/502/503 server error, a proxy that returns 4xx for unknown paths, or a network-layer failure that still resolves to a Response with ok=false.

Common situations: Hitting the official api.openai.com — the dashboard/billing/* routes were retired, so .ok is always false; a third-party OpenAI-compatible proxy that does not implement the billing routes; rate-limited account; transient 5xx during an OpenAI incident; CORS or proxy returning an HTML error page with status 200 but body that fails JSON.parse later.

Related errors


AI-assisted analysis of ChatGPTNextWeb/NextChat@defdcdb55d (2026-08-12). Data as JSON: /api/errors/8a33d7d5e9248251. Report an issue: GitHub.