Skip to main content

Fail-closed budget enforcement

Since v1.90.0

Budget checks read current spend from a counter in Redis that every worker and replica shares, and the database catches up in the background. When Redis restarts from an older snapshot or becomes unreachable, that counter can read lower than what was really spent. By default LiteLLM still admits the request, falling back to the spend each worker holds in memory, which keeps traffic flowing but can let a budget run over. general_settings.fail_closed_budget_enforcement: true makes every budgeted request check its spend against the database as well, and rejects the request with a 503 when its spend can be verified against neither Redis nor the database.

LiteLLM also reserves each request's estimated cost against its budgets before it calls the provider, so a burst of concurrent requests cannot all pass a budget that only some of them fit. Reservation is on by default, with or without the setting. The setting makes it strict, so a request whose estimated cost does not fit the remaining budget is rejected up front Since v1.101.0, and a reservation that cannot be written rejects the request with a 503 Since v1.95.0.

How it works​

With the setting off, LiteLLM reads the database only when the counter is missing, when it reads below the spend LiteLLM already has cached for that budget, or for a budget_limits window, so healthy traffic stays off the database. Spend that only this worker's memory holds is trusted, a reservation that cannot be written is skipped, and a request whose estimate is larger than what remains is admitted with its reservation cut down to the remaining budget. That last request can still land a little over the cap, since its actual cost can exceed the part that was reserved.

When to use it​

Turn it on when a budget is a hard spending ceiling, such as a customer's prepaid allowance, a cap that finance holds a team to, or a budget you bill against. It also fits deployments where Redis restarts or fails over without persistence, since a counter that comes back empty or from an old snapshot would otherwise under-report spend until the database catches up. In both cases a short burst of 503s during an outage is cheaper than spend you cannot recover.

Leave it off when staying up matters more than an exact cap. The default mode already re-checks the database whenever the counter reads below the spend LiteLLM knows about, which catches the common stale-counter case without a database read on every request. To cap throughput rather than spend during a Redis outage, use fail_closed_rate_limit_enforcement instead.

Supported scopes​

Every scope marked Yes is also reserved before the call, except AI tools and a Customer Management max_budget; see Budget reservation.

ScopeSupportedHow to set it
Whole proxyNoProxy-wide spend is read from its own database row on a separate path that the setting does not change.
OrganizationYesgeneral_settings.fail_closed_budget_enforcement: true.
TeamYesgeneral_settings.fail_closed_budget_enforcement: true. Covers max_budget and budget_limits windows.
Team memberYesgeneral_settings.fail_closed_budget_enforcement: true. A member who has a team member budget but no membership row is rejected with a 403 until a team admin adds them with /team/member_add.
ProjectYesgeneral_settings.fail_closed_budget_enforcement: true.
Virtual keyYesgeneral_settings.fail_closed_budget_enforcement: true. Covers max_budget and budget_limits windows.
Internal userYesgeneral_settings.fail_closed_budget_enforcement: true. Covers max_budget and budget_limits windows.
CustomerYesgeneral_settings.fail_closed_budget_enforcement: true. Legacy end-user budgets are checked against the customer's database row. Customer Management budget windows reject with a 503 whenever their spend cannot be read, with or without the setting.
TagYesgeneral_settings.fail_closed_budget_enforcement: true. Spend is checked against the tag's recorded spend, which is read along with the tag itself, so only a failed reservation returns a 503.
Model access groupYesgeneral_settings.fail_closed_budget_enforcement: true. Same as tags.
AI toolYesgeneral_settings.fail_closed_budget_enforcement: true. Spend is checked against the AI tool budget row, but nothing is reserved, so concurrent requests can still overshoot.
Agent sessionNoSession spend lives in its own counter that the setting does not read.

How to use it​

Turn it on​

config.yaml
general_settings:
fail_closed_budget_enforcement: true

Keep budget reservation on, which is the default. general_settings.disable_budget_reservation: true turns reservation off, and then concurrent requests can pass a budget that only some of them fit even with fail-closed enforcement on, since each request is checked only against spend already recorded.

What callers get​

A request whose spend cannot be verified, or whose reservation cannot be written, returns HTTP 503 with Budget enforcement unavailable in its error.message. A Customer Management budget window whose spend cannot be read returns a 503 with Customer budget window spend is unavailable. instead. Treat either like any other 503 and retry with backoff once Redis or the database is back. Requests that carry no budget are never rejected this way.

A request whose estimated cost does not fit the remaining budget returns the regular budget_exceeded error, 422 by default, and the message adds the estimate:

Budget has been exceeded! Team=team-research Current cost: 9.5, Estimated request cost: 1.0, Max budget: 10.0

The estimate prices the request's input plus its max_tokens or max_completion_tokens, capped at the model's output limit. A request that sets neither is estimated at 16,384 output tokens, or the model's output limit when that is lower. Set max_tokens on requests from a key that runs close to its cap, so the estimate reflects what the request can really cost.

Performance​

When the counter reads below the cap, the setting adds up to one database read per budget the request falls under. LiteLLM caches each read in the worker for 5 seconds, so the extra load is at most one read per budget per worker every 5 seconds, not one per request. A request that is already over its cap is rejected without the read. Without Redis, every worker relies on these database reads, and while the database is unreachable every budgeted request under its cap returns 503.

Limits​

The proxy-wide litellm_settings.max_budget, per-model budgets, and agent session budgets track spend on their own paths, and the setting does not change how they are read.

Some requests are never reserved. Routes the model's pricing cannot estimate, such as some image and audio routes, are checked only against recorded spend. A POST /batches request names an input file rather than its prompts, so LiteLLM cannot price the whole batch at submission and records its cost when the batch completes. See Batch requests.

See Budget exceeded errors for the error a refused request receives, Budget reservation for how reservation estimates cost, and Budgets and spend limits for every scope a request is checked against.

LiteLLM Enterprise
SSO/SAML, audit logs, spend tracking, multi-team management, and guardrails, built for production.
Learn more →