Next.js · server log

OpenAI 429 in a chat app: insufficient_quota or rate limit reached

verified on · Next.js 16.4.5 · React 19.3

Check yours: npm ls next react · On a different version? Paste your package.json into the free Doctor and it checks your versions against these fixes.

what it looks like
AI_RetryError: Failed after 3 attempts. Last error: You exceeded your current quota, please check your plan and billing details.
429  insufficient_quota

Rate limit reached for gpt-4o-mini in organization org-... on tokens per min (TPM): Limit 200000, Used 199500, Requested 1200. Please try again in 210ms.

What it means

OpenAI uses the same 429 status for two different problems, and the message tells you which one you have. "You exceeded your current quota" (type insufficient_quota, or a newer code like credit_balance_exhausted or project_spend_limit_exceeded) is about billing: the API account has no prepaid credits left or hit a spend limit. Waiting and retrying never fixes that one. "Rate limit reached for ... per min" means you sent too many requests or tokens for your usage tier in a short window, and it clears on its own. The AI SDK treats every 429 as retryable and retries twice by default, so a quota error still takes a few seconds and then lands in your log as AI_RetryError: Failed after 3 attempts. In the browser, useChat only gets "An error occurred." because the AI SDK hides server errors from the client unless you pass onError.

Common causes

  • No prepaid credits on the API account. A new account, or credits that ran out, fails on the very first call
  • A ChatGPT Plus or Pro subscription but no API credits. ChatGPT and the API are billed separately
  • A monthly spend limit you set on the organization or project was reached (organization_spend_limit_exceeded or project_spend_limit_exceeded)
  • The API key belongs to a different organization or project than the one you added credits to
  • Too many requests or tokens per minute for your usage tier, often from a loop, parallel calls, or one key shared by several apps
  • Your own retry loop on top of the SDK's two retries, so one failed call turns into many

How to fix it

  1. Read the message before changing code. Quota or billing wording means money, rate limit wording means speed
  2. Quota: add credits in the API billing settings on platform.openai.com, for the same organization and project your key belongs to. A ChatGPT subscription does not cover API calls
  3. Spend limit: raise or remove it in your organization or project limits, or wait for the monthly reset
  4. Rate limit: check your tier in Settings, Organization, Limits, send fewer or smaller requests, and wait for the Retry-After header. The AI SDK already waits for Retry-After when it is under 60 seconds
  5. Don't wrap billing errors in retry loops. If you retry yourself, set maxRetries: 0 on that call so the retries don't stack
  6. Pass onError to toUIMessageStreamResponse so users see a real message instead of An error occurred, and unwrap RetryError to read the status and body
  7. Cap usage per user on the server, a daily message or token limit checked before the model call, so one user can't drain your credits
src/app/api/chat/route.ts
import { APICallError, RetryError, streamText } from "ai";

// A message the user can act on instead of "An error occurred."
function chatError(error: unknown): string {
  const e = RetryError.isInstance(error) ? error.lastError : error;
  console.error("[chat]", e);
  if (APICallError.isInstance(e) && e.statusCode === 429) {
    const text = `${e.message} ${e.responseBody ?? ""}`;
    if (/quota|credit|spend_limit|usage_limit|billing/i.test(text))
      return "The AI service is out of credits right now. Please try again later.";
    return "Lots of people are chatting right now. Try again in a few seconds.";
  }
  return "Something went wrong. Please try again.";
}

// In POST, keep your streamText(...) call and return:
//   return result.toUIMessageStreamResponse({ onError: chatError });

works with Next.js 16.4.5 · React 19.3last verified

Prompt for Cursor or Claude Code

My Next.js chat app calls OpenAI through the AI SDK and fails with a 429. From the server log, tell me if it's a billing error (You exceeded your current quota, insufficient_quota, credit_balance_exhausted or a spend limit) or a rate limit (Rate limit reached for ... per min), and what I need to change in my OpenAI account for each. Don't add retry loops for billing errors. In the chat route, pass an onError to toUIMessageStreamResponse that unwraps RetryError, logs the full error on the server and returns a short message the user can act on. Then list every place that calls the model in a loop or in parallel, and tell me whether a per user daily limit is checked on the server before the model call.

Not quite your error? Paste yours into the free VibeBoot Doctor or browse the Next.js error fixer.

Check your whole repo from the terminal: npx github:vibeboot/vibeboot-doctor (free, offline).

Upgrading from Next.js 15? The Next.js 16 upgrade checklist covers every breaking change, the error it causes and the fix.

next: skip the next one

Skip the setup. The VibeBoot SaaS Starter gives you auth, Stripe and a database already wired on Next.js 16, with AGENTS.md and .cursor/rules that keep your agent on the Next.js 16 conventions. $49, one-time.

14-day money-back guarantee · lifetime updates

Building an AI chat app? The AI Chatbot Starter ships streaming chat, saved conversations, Free/Pro usage limits enforced on the server and Stripe, with AGENTS.md and .cursor/rules. Get the AI Chatbot Starter, $59

$ fixed it? get the next one first

New fixes for what AI agents break, by email.

When we add a fix or a Cursor rule for a new Next.js, Auth.js, Prisma or Stripe break, it goes out in the VibeBoot newsletter. Free, occasional, unsubscribe in one click.

Subscribe free