OpenRouter 402: "This request requires more credits, or fewer max_tokens"
Last updated
If OpenRouter answers with error 402 and “This request requires more credits, or fewer max_tokens. You requested up to … tokens, but can only afford …”, the message itself says why: the request could cost more than you can afford. OpenRouter says a 402 without a Retry-After header isn't one to wait and retry, so sending the same request again won't help. Make it smaller, add credits, or raise your API key's limit. error.metadata.limit_source tells you which.
What this page says about OpenRouter was checked against OpenRouter's docs on November 14, 2026. The error bodies are from developers' reports, with dates. Where the advice is ours, the page says so.
The short version
- Read
error.metadata.limit_sourcein the response. - If it says
openrouter_credits, set a lowermax_tokensor send a shorter prompt, or add credits on OpenRouter's Credits page. - If it says
openrouter_key_limit, the API key's own limit is the problem: raise it on the Keys page, or, if the limit resets, wait for the reset.
Why it can happen while you still have credits
OpenRouter's docs don't explain this message, and don't say how the two numbers in it are counted. We searched the full docs on November 14, 2026: “requires more credits” and “can only afford” appear 0 times.
What the docs do say, in the Limits page's section on the in-flight spending budget: OpenRouter charges a request when it finishes, so it estimates each paid request's token cost up front and holds it while the request runs. The estimate is your input tokens plus the output tokens max_tokens allows, up to a fixed cap per request, at that endpoint's prices. Per-request fees, plugin charges and image pricing aren't part of it.
- Without
max_tokens, OpenRouter uses that fixed cap for the output tokens. Its docs don't say how large the cap is. - The estimate counts every output token
max_tokensallows (up to that cap), not the tokens the request will actually use. A request that would have produced a short answer can still be turned down. - On some prepaid accounts the total it holds is smaller. OpenRouter limits the total to a fraction of your balance on accounts with a balance below a threshold and, for now, on newer accounts. When one request's estimate is larger than that whole total, OpenRouter says retrying will not help: lower
max_tokensor the prompt size, or add credits.
Source: OpenRouter, Limits
Which limit did you hit?
We found the message with two values of limit_source. The link at the end of the message differs too.
Your credits: openrouter_credits
{
"message": "This request requires more credits, or fewer max_tokens. You requested up to 32000 tokens, but can only afford 15989. To increase, visit https://openrouter.ai/settings/credits and upgrade to a paid account",
"code": 402,
"metadata": {
"limit_source": "openrouter_credits",
"remedy_hint": "Add credits at https://openrouter.ai/settings/credits, or lower max_tokens / prompt size to fit your remaining balance.",
"provider_name": null,
"previous_errors": […]
}
}From a GitHub issue dated October 18, 2026. We formatted it and shortened previous_errors, which the report had already cut. A report from October 3, 2026 has the same limit_source and ends “and add more credits”.
OpenRouter says openrouter_credits means your balance can't cover the request, or the single request is too expensive for your in-flight budget. The remedy_hint in the body says the same thing as a fix: add credits, or lower max_tokens or the prompt size.
The API key's limit: openrouter_key_limit
{
"message": "This request requires more credits, or fewer max_tokens. You requested up to 917141 tokens, but can only afford 374710. To increase, visit https://openrouter.ai/workspaces/default/keys/... and adjust the key's daily limit",
"code": 402,
"metadata": {
"limit_source": "openrouter_key_limit",
"remedy_hint": "Raise or remove this API key's usage limit at https://openrouter.ai/settings/keys, or use a key without a limit.",
"provider_name": null,
"previous_errors": […]
}
}From a comment on a GitHub issue dated November 4, 2026. We formatted it, shortened previous_errors and cut the key's ID from the link.
OpenRouter says openrouter_key_limit means the API key's credit limit is exhausted. A key can have its own limit, and that limit can reset daily, weekly or monthly. The message offers “fewer max_tokens”; the remedy_hint says to raise or remove the key's limit, or use a key without one.
- Both bodies have no outer
errorobject. The developers' tools appear to have printed what OpenRouter puts inside it. OpenRouter's own examples wrap it in{"error": {…}}. - In the reports,
previous_errorslists more 402s with the same message. In the key-limit one, their “can only afford” figures differ. OpenRouter's docs don't mention this field. - A key limit can also show up as a 403 with “Key limit exceeded”. Our OpenRouter spending limit guide covers that one.
Sources: OpenRouter, Limits, Errors and Debugging, Capping Shared Agent Keys
Fix it
Ask for fewer tokens
Our advice: set max_tokens on the request to the longest answer you actually need. OpenRouter says it caps how many tokens the model can generate, and the estimate above counts the output tokens it allows. A shorter prompt, such as less chat history, means fewer input tokens in the estimate.
OpenRouter also accepts max_completion_tokens, which it says works the same way. Its description of the estimate only names max_tokens.
- Reasoning models: OpenRouter says that on most providers,
max_tokenscovers the model's reasoning and its visible answer together. Set it too low and the reasoning can use all of it: the response ends withfinish_reason: "length"and empty content. - Don't go too low. OpenRouter's Python SDK reference notes that some providers enforce a minimum of 16.
Add credits
Add credits on the Credits page, the link in the message. OpenRouter says adding credits also raises the in-flight total on accounts that have one, up to a fixed ceiling.
Raise the key's limit
For openrouter_key_limit, raise the limit on that key on the Keys page, or, if the limit resets, wait for the reset. OpenRouter's hint also offers removing the limit or using a key without one; that also removes the cap the limit gave you. Our OpenRouter spending limit guide has the steps. To see what the key has left, ask OpenRouter:
curl https://openrouter.ai/api/v1/key \
-H "Authorization: Bearer $OPENROUTER_API_KEY"In the response, limit_remaining is what the key can still spend (null if it has no limit), and limit_reset says how often the limit resets (daily, weekly or monthly), or null if it never does. For the whole account, GET /api/v1/credits returns the total credits purchased and used, and needs a management key.
Sources: OpenRouter, Parameters, Python SDK reference, Limits, Get remaining credits
Tell this 402 apart in code
OpenRouter's docs say the OpenAI SDK, which works with OpenRouter, doesn't retry a 402 on its own. So your code sees the error once. Read limit_source to decide what to do:
import openai
def why_402(error: Exception) -> str | None:
"""For an OpenRouter 402: error.metadata.limit_source, or "unknown"."""
if not (isinstance(error, openai.APIStatusError) and error.status_code == 402):
return None
body = error.body if isinstance(error.body, dict) else {}
metadata = body.get("metadata")
source = metadata.get("limit_source") if isinstance(metadata, dict) else None
return source if isinstance(source, str) else "unknown"import OpenAI from "openai";
// For an OpenRouter 402: error.metadata.limit_source, or "unknown".
function why402(error: unknown): string | undefined {
if (!(error instanceof OpenAI.APIError) || error.status !== 402) {
return undefined;
}
const body = error.error as { metadata?: { limit_source?: unknown } };
const source = body?.metadata?.limit_source;
return typeof source === "string" ? source : "unknown";
}openrouter_credits: don't send it again unchanged. Lowermax_tokens, shorten the prompt, or tell someone to add credits.openrouter_key_limit: tell someone who can raise the key's limit, or, if the limit resets, wait for the reset.openrouter_in_flight_budget: OpenRouter's example for it has a different message. If the response has aRetry-Afterheader, wait that long and send it again. OpenRouter says a 402 without the header isn't one to wait and retry."unknown": nolimit_source. OpenRouter's Errors page describes a 402 as insufficient credits on the account or the API key.
We ran both checks with the Python SDK (openai 3.50.0) and the TypeScript SDK (openai 7.56.0), against a local test server. It sent the two errors above (with the outer error object added and previous_errors left out), OpenRouter's own in-flight example, a made-up 402 with no metadata, and a 429. The default client sent each 402 once. For a full retry check that covers every OpenRouter limit, see our OpenRouter spending limit guide.
Sources: OpenRouter, Errors and Debugging, Quickstart
Where Billfuse fits
Billfuse is the tool we built to watch AI API spend. It can't prevent this error. It doesn't read your OpenRouter balance or your key limits, it can't pause OpenRouter, and OpenRouter spend reaches it about a day late, because OpenRouter reports each UTC day once it's over.
What Billfuse does: one monthly budget across OpenAI, Anthropic, Gemini and OpenRouter, with emails at 50%, 80% and 100% of it. It also alerts you when one provider's day reaches 3 times a normal day, and at least $10 more. Against this error, use OpenRouter's own tools: a sensible max_tokens, and an eye on the key's limit_remaining.
More guides
- How to put a hard spending cap on OpenAI, Anthropic, Gemini and OpenRouter
- How to set a hard spending limit on the OpenAI API
- How to set a spending limit on the Anthropic API
- How to set a spending limit on the Gemini API
- How to set a spending limit on the OpenRouter API
- Billing error or rate limit? AI API errors from OpenAI, Anthropic, Gemini and OpenRouter
- Leaked AI API key: what to do first
- Fix "Your credit balance is too low to access the Anthropic API"
- Gemini API error 402: "Your prepayment credits are depleted"
- OpenAI error 429 insufficient_quota: "You exceeded your current quota"
It's free while Billfuse is in early access. You can try it with sample data first, no sign-up needed.