# LLM API spending limits and failover: stop surprise bills and outages

The two things that hurt most with an LLM API are a bill you didn't expect and a request that never comes back. Here's how to set LLM API spending limits that actually hold, and how failover decides what happens when a machine drops mid-request.

Published 2026-11-24 · https://inpherio.co.uk/blog/llm-api-spending-limits-and-failover

Key points:
- Prepaid credit means you can never be put into debt: a reply the balance can't cover is capped, not billed later.
- Per-key budgets, daily and monthly caps and a maximum price per request each refuse with HTTP 429 once reached.
- Two request headers set a price ceiling and a minimum host health for any single call.
- If a host fails before any reply reaches you, the request moves to the next-best host and you pay once.
- A stream that breaks part-way ends with node_failed_mid_stream and isn't charged, never silently replayed.

Every team that ships an LLM feature eventually has one of two bad days: the loop that called the API ten thousand times overnight, or the provider outage that left users staring at a spinner. Both are preventable with the right controls. Here's how to set LLM API spending limits that hold, and how failover works when a machine drops.

## Layer 1: prepaid, and never into debt

The simplest spending limit is a balance. Credit is prepaid (top-ups from £10 to £1,000), and the platform holds the most a request can cost before it runs. If your balance can't pay for a whole reply, the reply is **capped** to what it can pay for and stops, saying so: `finish_reason: "length"`, the header `X-Inpherio-Stopped: balance`, and a closing note in plain-text replies. If it can't cover even the prompt, the request is refused with HTTP 402 and nothing runs. You're never billed afterwards for more than you had.

Optional auto top-up from a saved card is rate-limited too: at most once every 10 minutes and five times a day, so a runaway script can't drain your card either.

## Layer 2: limits on keys and accounts

| Control | What it does |
|---|---|
| Per-key budget | A key stops once it has spent its budget |
| Daily spend cap | The account stops for the day once reached |
| Monthly spend cap | The account stops for the month once reached |
| Maximum price per request | Refuses any single call that could cost more |

Each one refuses further requests with HTTP 429 and a `Retry-After` header once reached. Good practice: one key per app or environment, each with its own budget, so a bug in staging can't spend production's money. The [developer guide](/developers) shows where each is set.

## Layer 3: per-request routing controls

Two optional headers steer a single call:

- `X-Inpherio-Budget-Per-1M-Tokens`: a price ceiling per million output tokens, in pence (40 means £0.40). Hosts above it aren't used.
- `X-Inpherio-Min-Reliability`: a minimum host health score from 0 to 100.

Every response tells you what happened: response headers give the charge and billed tokens on non-streamed replies, which node answered, and whether failover was used.

(Live on Inpherio: https://inpherio.co.uk/models/qwen3:8b)

## How routing chooses a machine

Every host serving the requested model is scored for each request: currently **35%** on whether the model is already loaded, **25%** on the host's health score, **20%** on price and **20%** on measured speed, within any ceiling or minimum health you set. The health score is recomputed every 5 minutes from uptime over 7 and 30 days, requests completed without dropping, benchmark speed and how often models stay loaded. The [how it works page](/how-it-works) walks through it.

## How failover works

- **Before any reply reaches you:** if the chosen host fails, the request goes to the next-best host. You pay once, for the reply you get.
- **Part-way through a stream:** the stream ends with an error, `node_failed_mid_stream`, and that request isn't charged. It is deliberately not replayed silently, because a replayed answer could contradict what you've already shown the user.
- **No host available:** HTTP 404 if nobody serves the model right now, or 502/503 if every candidate failed or is at capacity, which are safe to retry.

Failed requests always cost £0.

## A retry pattern that works

```python
import time
from openai import OpenAI, APIStatusError
client = OpenAI(base_url="https://inpherio.co.uk/v1", api_key="YOUR_KEY")

def ask(messages, tries=3):
    for i in range(tries):
        try:
            return client.chat.completions.create(model="qwen3:8b", messages=messages)
        except APIStatusError as e:
            if e.status_code in (502, 503) and i < tries - 1:
                time.sleep(2 ** i)
                continue
            raise   # 402 balance, 429 cap or limit: don't hammer
```

Retry 502 and 503 with backoff; respect `Retry-After` on 429; never retry 402 in a loop. If you also run models at home, a fallback to your own machine through [self-serve](/self-serve) costs nothing.

## Streaming and failover in a chat UI

Streaming changes what failover can do. Once the first words of a reply are on screen, the platform won't quietly restart the answer on another machine, because the new answer might contradict what the user has already read. Instead the stream ends with `node_failed_mid_stream` and nothing is charged. In your UI, catch that error, keep what was shown, and offer a "Regenerate" button. Most users accept that far better than a reply that silently changes halfway through.

For non-streamed calls the platform can fail over freely, because nothing has been shown yet. If your feature doesn't need streaming (tagging, extraction, background summaries), turning it off gives you failover for free.

## Budgeting by feature

A useful pattern is to give every feature its own key with its own budget: one for search, one for summaries, one for the chat assistant. Monthly caps then become a cost forecast you can read at a glance, and a runaway feature only stops itself. When a cap trips, the 429 response has a `Retry-After` header; surface it in logs and alerts rather than retrying in a tight loop.

## Alerts before limits

Caps stop spending; alerts tell you before you get there. A simple approach is to log the charge for every request with the feature name, sum it per hour in your monitoring, and alert at half of each key's budget. Combined with prepaid credit and caps, that turns a surprise bill into a message you see early, while there's still time to find the bug.

## Checklist

1. One key per app and environment, each with a budget.
2. Daily and monthly caps on the account.
3. A maximum price per request.
4. Retry 502/503 with backoff; handle `node_failed_mid_stream` in streaming UIs.
5. Log the charge headers so cost per feature is visible.

New to switching providers? Our guide to [an OpenAI API alternative for open models](/blog/openai-api-alternative-open-models) covers the one-line change, and the [pricing page](/pricing) every fee.

## FAQ

**How do I stop my LLM API bill getting out of control?** Use prepaid credit, put a budget on each API key, add daily and monthly spend caps, and set a maximum price per request. Each limit refuses further requests with HTTP 429 once it's reached.
**What happens when my balance runs low mid-reply?** The reply is capped to what the balance can pay for and stops, with finish_reason length and an X-Inpherio-Stopped balance header. You pay for what you received and are never put into debt.
**What is LLM failover?** When the machine serving a request fails, the request is sent to another machine serving the same model. Here it happens automatically if no part of the reply has reached you yet, and you pay once.
**Are failed requests charged?** No. Failed requests cost £0, including a stream that breaks part-way through.
