Skip to content
Billfuse

How to set a spending limit on the OpenRouter API

Last updated

OpenRouter runs on credits: you buy them, and requests spend them. On top of your credit balance, it has three spending limits you can set: a credit limit on each API key, a budget on a workspace, and a budget in a guardrail. This page shows where to set them, what happens when one is reached, and what they don't cover.

Everything here was checked against OpenRouter's docs on November 13, 2026. The sources are linked under the sections they back up.

The short version

  1. In OpenRouter, open Settings → API Keys and click the key.
  2. Under Key limit, enter a Credit limit in dollars.
  3. Choose when it resets: daily, weekly or monthly. With no reset, the limit covers the key's whole lifetime.

When the key has spent its limit, requests with that key fail until the limit resets or you raise it. The limit applies to that key only.

Your balance, and three limits you set

The limits stack. OpenRouter checks every one that applies on each paid request, and the first one that is spent blocks the request. Daily limits reset at 00:00 UTC, weekly ones on Monday, monthly ones on the 1st.

Sources: OpenRouter, Limits, Spend Controls

Set a credit limit on an API key

  1. Open Settings → API Keys and click the key.
  2. Under Key limit, enter a Credit limit in dollars.
  3. Choose the reset interval: daily, weekly, monthly, or none.

Sources: OpenRouter, Capping Shared Agent Keys, Management API Keys, Limits, Notifications

Set a workspace budget

Your keys already live in a Default workspace, and it can have budgets too. Use a workspace budget as the cap for everything in the workspace, whichever key is used.

  1. Open the workspace's Settings page, at openrouter.ai/workspaces/<slug>/settings.
  2. Scroll to the Budgets section.
  3. Click Add budget, choose an interval, and enter a dollar amount.
  4. Click Save.

Source: OpenRouter, Workspace Budgets, Workspaces

Guardrail budgets, for teams

A guardrail can carry a budget limit along with its other rules. You create one on the workspace's Guardrails page, then assign it to members or API keys. In an organization, that takes an organization admin.

Sources: OpenRouter, Guardrails, Spend Controls, Guardrail Policies

What happens when you reach a limit

When a spending limit blocks a request, the error can carry a field that names the limit: error.metadata.limit_source. Read that, not only the status code.

A budget or a key limit is spent: error 403

The request fails with error 403 before it reaches a provider. limit_source is openrouter_guardrail_budget, openrouter_workspace_budget or openrouter_key_limit. The error's metadata also has limit_usd, spend_usd and resets_at, the time the limit resets (null for a limit that never resets). OpenRouter says retrying before then fails the same way, and there is no Retry-After header.

Error 403, as reported
Key limit exceeded (daily limit). Manage it using https://openrouter.ai/workspaces/default/keys/... $2.03 of $2.00 used; resets 2026-10-28T00:00:00.000Z.

This one is a key's own limit: raise the key's credit limit, or wait for the reset time at the end of the message. The text is from a GitHub issue dated October 27, 2026; we cut the key's ID from the link. OpenRouter's docs give only the words Key limit exceeded, and say the message ends with the amount used, the limit and the reset time.

One thing to know about key limits: OpenRouter's docs list a spent key limit under both 403 and 402 errors. Checking limit_source works either way.

Your credits can't cover the request: error 402

limit_source is openrouter_credits. Add credits, or make the request smaller. Retrying won't help.

Error 402, as reported
This request requires more credits, or fewer max_tokens. You requested up to 131072 tokens, but can only afford 35063. To increase, visit https://openrouter.ai/settings/credits and add more credits

The message says the request could use more tokens than you can afford. OpenRouter's advice for a 402: add credits, or make the request smaller (fewer input tokens or a lower max_tokens) so its estimated cost fits. If you still have credits, see the next section. The checks below don't retry this error.

OpenRouter's docs don't show this message, and the report above (GitHub, November 8, 2026) doesn't show its limit_source. Other reports have a link to an API key instead of the credits page (November 12, 2026), or limit_source set to openrouter_key_limit (November 3, 2026), which OpenRouter says means the key's credit limit is exhausted. So check limit_source to see which limit you hit.

For why this happens while you still have credits, and how to fix each case, see our guide to the “requires more credits” error.

A 402 while you still have credits

OpenRouter charges a request when it finishes, so it holds each paid request's estimated cost while it runs. On some prepaid accounts, those with a balance below a threshold and, for now, newer accounts, the total it will hold is limited. Two things can then happen while your balance is still positive:

Sources: OpenRouter, Limits, Errors and Debugging, Capping Shared Agent Keys

Stop your code from retrying a limit

A spent limit keeps failing until it resets or someone raises it, so a loop that retries every error can run for hours without doing anything. These checks use the OpenAI SDK, which OpenRouter says you can point at its API. Check before you retry:

Python
import openai

# limit_source values that mean a spending limit was reached
SPEND_LIMITS = {
    "openrouter_key_limit",
    "openrouter_workspace_budget",
    "openrouter_guardrail_budget",
    "openrouter_credits",
}
# OpenRouter may send a Retry-After header with these
RETRY = {429, 503}
MAX_ATTEMPTS = 5


def limit_source(error: Exception) -> str | None:
    """OpenRouter's error.metadata.limit_source, if the error has one."""
    if not isinstance(error, openai.APIStatusError):
        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 None


def spend_limit_reached(error: Exception) -> bool:
    """A named spending limit, or any other 402 except the in-flight one."""
    source = limit_source(error)
    if source in SPEND_LIMITS:
        return True
    return (
        isinstance(error, openai.APIStatusError)
        and error.status_code == 402
        and source != "openrouter_in_flight_budget"
    )


def should_retry(error: Exception, attempts: int) -> bool:
    """attempts: how many times this request has been sent so far."""
    if not isinstance(error, openai.APIStatusError) or attempts >= MAX_ATTEMPTS:
        return False
    if spend_limit_reached(error):
        return False
    # The one 402 to retry, and only when OpenRouter says how long to wait
    if error.status_code == 402:
        return error.response.headers.get("retry-after") is not None
    # Everything else, such as 400, 401 and a 403 from a content filter
    return error.status_code in RETRY
TypeScript
import OpenAI from "openai";

// limit_source values that mean a spending limit was reached
const SPEND_LIMITS = new Set([
  "openrouter_key_limit",
  "openrouter_workspace_budget",
  "openrouter_guardrail_budget",
  "openrouter_credits",
]);
// OpenRouter may send a Retry-After header with these
const RETRY = new Set([429, 503]);
const MAX_ATTEMPTS = 5;

// OpenRouter's error.metadata.limit_source, if the error has one
function limitSource(error: unknown): string | undefined {
  if (!(error instanceof OpenAI.APIError)) return undefined;
  const body = error.error as { metadata?: { limit_source?: unknown } };
  const source = body?.metadata?.limit_source;
  return typeof source === "string" ? source : undefined;
}

// A named spending limit, or any other 402 except the in-flight one
function spendLimitReached(error: unknown): boolean {
  const source = limitSource(error);
  if (source && SPEND_LIMITS.has(source)) return true;
  return (
    error instanceof OpenAI.APIError &&
    error.status === 402 &&
    source !== "openrouter_in_flight_budget"
  );
}

// The SDK keeps headers in a Headers object; older versions use a plain object
function hasRetryAfter(error: { headers?: unknown }): boolean {
  const headers = error.headers as
    | Headers
    | Record<string, string | null | undefined>
    | undefined;
  const value =
    headers instanceof Headers
      ? headers.get("retry-after")
      : headers?.["retry-after"];
  return Boolean(value);
}

// attempts: how many times this request has been sent so far.
function shouldRetry(error: unknown, attempts: number): boolean {
  if (!(error instanceof OpenAI.APIError) || attempts >= MAX_ATTEMPTS) {
    return false;
  }
  if (spendLimitReached(error)) return false;
  // The one 402 to retry, and only when OpenRouter says how long to wait
  if (error.status === 402) return hasRetryAfter(error);
  // Everything else, such as 400, 401 and a 403 from a content filter
  return RETRY.has(error.status ?? 0);
}

Call the retry check after each failed attempt. When it says yes, wait for the Retry-After header if there is one, then try again. When the spend limit check says yes, stop and tell someone. That check also says yes for a 402 that names no limit, since OpenRouter describes 402 as insufficient credits.

We ran the checks with the OpenAI Python SDK (openai 3.50.0 and 1.40.0) and the OpenAI TypeScript SDK (openai 7.56.0 and 4.52.0), against a local test server that sends the errors described above.

Sources: OpenRouter, Errors and Debugging, Quickstart

Alerts before a limit

Sources: OpenRouter, Notifications, FAQ, Authentication

What the limits don't cover

Where Billfuse fits

Billfuse is the tool we built for these gaps. It doesn't replace OpenRouter's limits. Keep them on.

More guides

It's free while Billfuse is in early access. You can try it with sample data first, no sign-up needed.