Fix an OpenRouter 402 Error and Negative Balance

Read limit_source to tell an empty balance from the new in-flight budget 402, when Retry-After is safe, and how to fix a negative balance.

Published Checked By RouterPlex9 min read

Key facts

Checked October 20, 2026

  • Read error.metadata.limit_source: openrouter_credits, openrouter_key_limit or openrouter_in_flight_budget names the check that failed.
  • Only retry openrouter_in_flight_budget, after the Retry-After seconds. That 402 can fire while your balance is positive.
  • A negative balance blocks free models too. Free variants cap at 20 requests per minute and 50 or 1,000 per day.
  • Concurrency can cross zero: $0.08 left and four requests costing $0.04 each ends at -$0.08.
  • RouterPlex balances stop at $0 and never go negative, and each key can carry a hard lifetime spend budget.

An OpenRouter 402 means a credit or budget check refused the request. Most 402s mean the balance or a key limit is used up, and retrying will not help. One newer 402 is different. OpenRouter can return it while your balance is still positive, because running and recent requests have filled your in-flight budget. That one clears on its own, and the response tells you how long to wait.

Read error.metadata.limit_source first. It names the check that failed. If the dashboard also shows a negative balance, stop the client before you top up. Automatic retries will keep spending against a balance that is already below zero.

Recovery from an empty balance is three steps: stop the workload, add enough credit to get above zero, then send one small manual request before restarting the agent. The rest of this page explains why a prepaid-looking balance can cross zero, and how to keep the next incident inside a $5 or $10 key budget.

OpenRouter 402: check limit_source first

Every OpenRouter 402 that comes from a budget check carries error.metadata.limit_source. Some also carry a reason and a remedy_hint (a one-line suggested fix). OpenRouter's docs say to branch on limit_source, not on the message text.

Checked 20 October 2026: OpenRouter API limits and OpenRouter errors and debugging. Field names and limits can change. Read the current docs before you hard-code them.

limit_sourcereasonWhat it meansRetry?Fix
openrouter_in_flight_budgetin_flight_budget_exhaustedRunning and recently finished requests fill your in-flight budget. Your balance can still be positive.Yes, after Retry-AfterWait the number of seconds in the Retry-After header, then send it again. Lower concurrency if it repeats.
openrouter_creditsweight_exceeds_budgetThis one request's estimated cost is larger than your whole in-flight budget.NoLower max_tokens or the prompt size, or add credit.
openrouter_creditsnoneThe balance cannot cover the request.NoAdd credit. Stop the client first if the balance is negative.
openrouter_key_limitnoneThe credit limit set on this API key is used up.NoRaise the key limit or use another key.

The docs also list openrouter_guardrail_budget and openrouter_workspace_budget for budgets set by an org admin. Treat those like a key limit. A 402 without a Retry-After header is not a wait-and-retry case.

This snippet calls OpenRouter with the OpenAI Python SDK. It retries only the in-flight case.

python
import os, time
from openai import OpenAI, APIStatusError
 
client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"])
 
def ask(model, messages):
for attempt in range(3):
try:
return client.chat.completions.create(model=model, messages=messages, max_tokens=500)
except APIStatusError as error:
body = error.body if isinstance(error.body, dict) else {}
body = body.get("error", body)
source = (body.get("metadata") or {}).get("limit_source")
wait = error.response.headers.get("retry-after")
if error.status_code == 402 and source == "openrouter_in_flight_budget" and wait:
time.sleep(float(wait))
continue
raise
raise RuntimeError("in-flight budget still full after 3 tries")

Which 402 message did you get?

OpenRouter returns a few different 402 messages. Each one points at a different fix.

"This request requires more credits, or fewer max_tokens"

The full message reads like: *"This request requires more credits, or fewer max_tokens. You requested up to 64000 tokens, but can only afford 1600."* OpenRouter checks the most the request could cost, using its max_tokens, against your remaining credit. Coding agents often ask for 32K or 64K output tokens by default, so a small balance fails even for a short answer.

  • Lower max_tokens for the request. In Claude Code, set CLAUDE_CODE_MAX_OUTPUT_TOKENS to a smaller value, such as 8000.
  • Switch to a cheaper model for the same task.
  • Or add credit so the balance covers the reserved output.

"Insufficient credits. This account never purchased credits"

The key belongs to an account or organization with no credit purchase on record. This is common when a key was created under a personal account but credits were bought for an organization, or the other way round. Check which account or org owns the key. If it is the right one, buy credits there. Free-model allowances do not cover paid models.

402 with a positive balance (in-flight budget)

OpenRouter places a hold on each paid request while it runs. The hold is the estimated cost of the request. When the request ends, the actual cost replaces the hold for a short settling window. The total that can be held at once is your in-flight budget. The docs describe it as a fraction of your current balance, up to a fixed ceiling. They do not publish the fraction or the ceiling.

When a new request does not fit beside the running and recent ones, OpenRouter rejects it with 402 before it reaches a provider. This can happen with a positive balance. The docs say it can even happen with no request still running, while recent costs are settling. Look for limit_source set to openrouter_in_flight_budget and a Retry-After header. Wait, then retry. If one request is too big for the whole budget, reason is weight_exceeds_budget and waiting will not help.

"Insufficient credits. Add more using https://openrouter.ai/settings/credits"

The available balance is at or below zero. If the dashboard shows a negative balance, usage posted after the balance check (see below). Stop the client before topping up, or queued retries will spend the new credit straight away.

402 on a free model

A negative balance blocks free models too. OpenRouter's limits page says you "may see 402 errors, including for free models" until the balance is above zero again. Free variants (model ids ending in :free) also have their own caps. With fewer than 10 credits purchased, the limit is 20 requests per minute and 50 per day. With at least 10 credits purchased, it is 20 per minute and 1,000 per day. The page adds that the higher daily ceiling starts one credit below that threshold. To see your daily counter and credits left on a key, send GET https://openrouter.ai/api/v1/key. It does not report the per-minute limit. (Checked 20 October 2026 on the OpenRouter limits page.)

OpenRouter documents its credit system and credit monitoring in the billing section of its FAQ. Error behavior and provider responses can change, so use the current activity page as the source of truth for your account.

If you are deciding whether the failure mode justifies switching services, separate the incident from the purchase model. The OpenRouter credits and fee guide covers funding cost and expiry terms, while the OpenRouter alternatives comparison compares hosted wallets including LLM Gateway and Vercel AI Gateway.

Why a balance can cross zero

LLM billing is finalized after tokens are generated. At request start, a gateway knows the current balance and an estimate of what the request might cost. It does not yet know the final output length.

Several normal API behaviors can create a small accounting gap:

  • Streaming: the response starts before the final output-token count is known.
  • Concurrency: multiple requests pass the same pre-request balance check at nearly the same time.
  • Retries: an SDK, proxy, or agent may retry a request without making the retry obvious in the UI.
  • Long outputs: a missing or high max_tokens value lets a response cost much more than expected.
  • Agent loops: tools can call the model repeatedly until a task ends, multiplying a small per-call cost.
  • Delayed usage posting: provider usage can arrive after the response completes.

Imagine an account with $0.08 left and four concurrent requests. Each request sees a positive balance at the start. If each eventually costs $0.04, the final combined cost is $0.16 and the resulting balance is -$0.08.

How to recover from a 402

  1. Stop the process that is sending requests. Do not let an automatic retry loop keep firing.
  2. Open the activity or usage log and filter to the affected API key and time window.
  3. Check whether requests were concurrent, retried, or unexpectedly long.
  4. Rotate the key if the traffic is not yours.
  5. Add enough credit to restore a positive available balance.
  6. Send one small request manually before restarting the full workload.

For an automated service, treat a 402 as a billing-state error unless limit_source is openrouter_in_flight_budget. Blind exponential retries cannot create credit, and they can hide the actual failure from an operator.

python
import os
from openai import OpenAI, APIStatusError
 
client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=os.environ["OPENROUTER_API_KEY"])
 
try:
client.chat.completions.create(
model="your-model-id",
messages=[{"role": "user", "content": "Summarize this file"}],
max_tokens=500,
)
except APIStatusError as error:
if error.status_code == 402:
# Pause the queue and alert an operator. Retry only the in-flight case shown above.
raise RuntimeError("LLM credit or budget exhausted") from error
raise

Four controls that matter more than a low-balance alert

An alert is useful, but it fires after spend has already happened. Stronger controls stop or constrain the request path.

1. One key per workload

Do not share the same key across production, local development, CI, and coding agents. Separate keys make attribution and revocation straightforward.

2. A hard key budget

A server-side key budget is better than a dashboard target. Set the maximum amount that a key is allowed to consume. If a local experiment should cost at most $10, its credential should not be able to reach the other $490 in the account.

3. Output and concurrency limits

Set max_tokens, bound the number of parallel requests, and cap agent steps. These limits reduce both expected cost and the uncertainty between request start and final accounting.

4. Fail closed when balance data is unavailable

If your own service cannot retrieve the current gateway balance, do not display a made-up zero or assume the full lifetime top-up remains available. Mark the balance unavailable and pause high-risk jobs until billing state is trustworthy.

The prepaid design alternative

RouterPlex uses a prepaid account balance and lets every API key carry a hard lifetime spend budget. Requests stop when the balance reaches $0 instead of turning into a negative amount that must be repaid. When credit runs out, the API returns a 402 that says the balance is used up. The message links straight to the top-up page, so a coding agent does not sit in a retry loop.

New accounts can connect Claude Code, Cursor, Codex or another OpenAI-compatible tool in guided setup and test it with $1 of credit before paying. Top-ups start at $5 and the credit does not expire.

The safest setup still uses defense in depth: a $10 key budget, explicit output limits, bounded concurrency, and a low-balance alert. No single billing check can replace application-level controls for an autonomous agent.

For the exact setup, read API keys and budgets. You can also compare RouterPlex and OpenRouter directly or read how Vercel AI Gateway compares with OpenRouter. To see the failure behavior yourself, create a $5 prepaid account and test it with a small key budget.

Frequently asked questions

What does HTTP 402 mean on OpenRouter?
A 402 means a credit or budget check refused the request. Read error.metadata.limit_source to see which one. openrouter_credits means the balance cannot cover the request. openrouter_key_limit means the credit limit on that API key is used up. openrouter_in_flight_budget means running and recently finished requests fill your in-flight budget, which can happen while your balance is still positive. Only that last case is temporary. It comes with a Retry-After header that says how many seconds to wait. The guardrail and workspace budget sources are limits an org admin set. Checked 20 October 2026 on OpenRouter's limits and errors docs.
What does "This request requires more credits, or fewer max_tokens" mean?
OpenRouter compares the most the request could cost, based on its max_tokens, with your remaining credit. "You requested up to 64000 tokens, but can only afford 1600" means the balance covers only 1,600 output tokens at that model's price. Lower max_tokens (in Claude Code, set CLAUDE_CODE_MAX_OUTPUT_TOKENS), pick a cheaper model, or add credit.
What does "This account never purchased credits" mean on OpenRouter?
The key belongs to an account or organization with no credit purchase on record. Check that the key was created under the account or org you paid with. If it was, buy credits for that account. Free-model limits do not cover paid models.
What does OpenRouter negative credits or a minus balance mean?
Negative credits are a below-zero account balance. Final cost is known only after tokens are generated. Streaming, concurrent requests, retries, long outputs, and delayed usage posting can let several requests pass the balance check before their costs land. The ledger then ends below zero. OpenRouter returns 402 until the balance is back above zero, and its limits page says this can include free models. Stop retries, add credit, then put a hard limit on the next key. RouterPlex works differently. A prepaid balance stops at $0 and never goes negative.
Should I retry an OpenRouter 402 error?
Only in one case. Retry when error.metadata.limit_source is openrouter_in_flight_budget and the reason is in_flight_budget_exhausted. Wait the number of seconds in the Retry-After header first, then send the request again. Do not retry when limit_source is openrouter_credits or openrouter_key_limit, or when the reason is weight_exceeds_budget. Those mean the balance, the key limit, or the size of that one request is the problem, and waiting will not fix it. OpenRouter's docs say a 402 without a Retry-After header is not a wait-and-retry case. Checked 20 October 2026.
How do I prevent a coding agent from draining my balance?
Use a dedicated key with a hard server-side budget, bound concurrency, set max_tokens, and alert on spend before the account-wide balance is exhausted. Lower concurrency also makes in-flight budget 402s less likely.

Run the smallest paid test.

Add $5, cap the key, and verify the result with your own workload. No subscription, and credit never expires. A first top-up of $25+ is matched with $25 extra.

Weighing the switch? RouterPlex vs OpenRouter, line by line