---
title: "LLM API spend control - budgets, limits, loop guards"
description: "Stop an LLM API bill from running away: prepaid balances, one key per app, IP allow-lists, usage logging, in-code budgets, loop caps and flat-price keys."
url: https://broadrouter.com/blog/llm-api-spend-control/
lang: en
updated: 2026-11-14
publisher: BroadRouter (a BroadNet site); API by BroadNet Inference, https://inference.broadnet.ai
---

# LLM API spend control: budgets, key limits and loop protection

By the BroadNet Inference team · 5 min read · Published 2026-11-14 · Updated 2026-11-16

Control LLM API spend in layers: a prepaid balance as the account ceiling, one key per app locked to your server IPs, max\_tokens and step caps on every request, and an in-code budget that stops a job before it overspends. Log tokens per key and alert on your own numbers. Flat plans and hourly keys cap the worst case.

## Key takeaways

-   Provider alerts arrive after the money is spent. Put a hard budget check in your own code, before each request.
-   One key per app or agent, locked to your server IPs, limits the damage of a leak. BroadNet applies revocations and allow-list changes within five seconds.
-   Runaway loops come from missing step caps and unbounded retries. Cap both.
-   Log prompt, cached and completion tokens per request with stream\_options include\_usage, and turn them into cost per key and per feature.
-   Flat-price options cap spend by design: $60 a month per plan session, $0.50 per hourly key.

**On this page**

1.  Why are alerts not enough?
2.  What are the layers of LLM spend control?
3.  Account level: how does a prepaid balance cap spend?
4.  Key level: how do you limit the damage of a leaked key?
5.  Request level: how do you bound every reply?
6.  Job level: how do you stop a runaway agent loop?
7.  Reporting: what should you log and alert on?
8.  Pricing level: when does a flat price beat a budget?
9.  A short checklist
10.  Where BroadNet Inference fits

Most LLM bill surprises have the same cause: something kept calling the API with nobody watching. A retry loop, an agent that could not finish, a leaked key on a public repository. Budget alerts tell you about it after the fact. This post builds spend control in layers, from the account down to the single request, and shows which parts BroadNet Inference handles and which you add in code.

## Why are alerts not enough?

An alert compares spend with a threshold after usage is recorded. An agent loop can make dozens of requests a minute. By the time an email arrives, the money is gone. Use alerts as a backstop, and put the hard stop where the request is made.

## What are the layers of LLM spend control?

| Layer | Control | Stops | Where you set it |
| --- | --- | --- | --- |
| Account | Prepaid balance | Spend beyond what you funded | Provider account |
| Key | One key per app or agent | One leak taking everything | Provider portal |
| Key | IP allow-list | A stolen key used elsewhere | Provider portal |
| Request | `max_tokens`, `reasoning_effort` | One huge reply | Your code |
| Job | Step cap, retry cap, loop detection | Runaway agents | Your code |
| Job | Running budget per task and per day | Slow leaks across many calls | Your code or proxy |
| Reporting | Token logging and alerts | Drift you would not notice | Your logs |
| Pricing | Flat plan or hourly key | The token meter itself | Plan choice |

## Account level: how does a prepaid balance cap spend?

BroadNet Inference pay-as-you-go keys draw on a prepaid account balance; new accounts start with $5 of credit and no card. When the pay-as-you-go allowance is used up, requests return `401`, and a purchase the balance can’t cover (such as an hourly key) returns `402 insufficient_balance`. Treat both as a stop sign, not something to retry. BroadNet’s error reference also lists a spending limit among the causes of a 429, so read the error body before you retry any 429. [All error codes](https://broadrouter.com/guides/errors-and-retries/) and [how to handle 429s with backoff](https://broadrouter.com/blog/handle-llm-api-429-rate-limits/).

The balance is your outer ceiling. Fund it for what you are willing to lose this month, not for a year. To size that number, run your expected traffic through the [LLM API cost calculator](https://broadrouter.com/tools/llm-api-cost-calculator/) first.

## Key level: how do you limit the damage of a leaked key?

An [API key](https://broadrouter.com/glossary/api-key/) is spending authority: whoever holds it can use your balance. Limit each key’s blast radius:

1.  **One key per app, agent or environment.** BroadNet allows up to 10 live keys per account, each valid for up to a year. Name them so the logs tell you which one spent.
2.  **Lock keys to your servers.** Add your egress IPs or CIDR ranges to the [IP allow-list](https://broadrouter.com/glossary/ip-allow-list/), up to 32 entries. Requests from other addresses get a 403.
3.  **Revoke fast.** Revocations and allow-list changes apply to new requests within five seconds.
4.  **Keep portal credentials off servers.** They can create keys and make purchases. Inference keys only call models.

[Lock API keys with IP allow-lists](https://broadrouter.com/guides/api-keys-ip-allow-lists/).

## Request level: how do you bound every reply?

Set `max_tokens` on every call. On reasoning models (DeepSeek V4.1 Flash, GLM 5.3, Qwen3.8 27B) keep it at 1,000 or more, because thinking shares the budget and too low a value returns an empty reply you still pay for. Lower `reasoning_effort` on simple tasks. [How reasoning tokens are billed](https://broadrouter.com/blog/reasoning-tokens-cost/).

## Job level: how do you stop a runaway agent loop?

Runaway loops have recognisable signs: the same tool call repeating, context growing every turn, and errors retried without limit. Put a guard around the client:

```python
import json, os, urllib.request
from openai import OpenAI

# Per-1M prices from BroadNet's machine-readable rate card
card = json.load(urllib.request.urlopen("https://inference.broadnet.ai/pricing.json"))
m = next(x for x in card["models"] if x["id"] == "broadnet/deepseek-v4.1-flash")
PRICE = {"in": m["input_per_m"], "cached": m["cached_input_per_m"], "out": m["output_per_m"]}
client = OpenAI(base_url="https://inference.broadnet.ai/v1", api_key=os.environ["BROADNET_API_KEY"], max_retries=3)

class BudgetExceeded(Exception): pass

class Guard:
    def __init__(self, budget_usd, max_steps=40):
        self.budget, self.max_steps, self.spent, self.steps, self.last = budget_usd, max_steps, 0.0, 0, []

    def call(self, **kw):
        self.steps += 1
        if self.steps > self.max_steps: raise BudgetExceeded("step cap")
        if self.spent >= self.budget: raise BudgetExceeded("budget")
        r = client.chat.completions.create(max_tokens=kw.pop("max_tokens", 4000), **kw)
        u = r.usage
        cached = (u.prompt_tokens_details.cached_tokens or 0) if u.prompt_tokens_details else 0
        self.spent += ((u.prompt_tokens - cached) * PRICE["in"] + cached * PRICE["cached"]
                       + u.completion_tokens * PRICE["out"]) / 1e6
        calls = [(t.function.name, t.function.arguments) for t in (r.choices[0].message.tool_calls or [])]
        self.last = (self.last + [calls])[-3:]
        if calls and len(self.last) == 3 and all(c == calls for c in self.last):
            raise BudgetExceeded("same tool call three times")
        return r
```

The guard reads prices from the rate card rather than hard-coding them, so it stays correct when prices change. Pass the same model to `call()` that you priced. Keep `max_retries` small: the OpenAI SDKs already retry 429 and 5xx, and each retry is billed again.

For a team, run the same check in a shared proxy so every service draws on one daily budget. Retries also run into rate limits: the [rate limit calculator](https://broadrouter.com/tools/llm-rate-limit-calculator/) shows how many calls a minute your key really allows.

## Reporting: what should you log and alert on?

Log, for every request: key alias, feature or agent name, model, `prompt_tokens`, `prompt_tokens_details.cached_tokens`, `completion_tokens`, status and `X-Request-ID`. When streaming, set `stream_options: {"include_usage": true}` or the usage never arrives ([streaming guide](https://broadrouter.com/guides/streaming-chat-completions/)).

From those logs, alert on:

-   daily cost per key above the expected figure from your [cost estimate](https://broadrouter.com/blog/estimate-llm-api-cost-before-launch/);
-   tokens per task more than three times the median;
-   a falling cache share, which raises cost without more traffic;
-   a rising rate of 429 or 5xx responses, which means retries.

BroadNet keeps content-free records such as timings, token counts and cost, not prompts, so your own logs are where per-feature detail lives. Avoid logging prompt content if your data policy forbids it. [How to build usage logging with cost per user and feature](https://broadrouter.com/blog/llm-api-usage-logging/).

## Pricing level: when does a flat price beat a budget?

The most reliable spend control is a price that doesn’t move:

-   **Plan session**: $60 a month (Full Moon), $15 a week (Orbit) or $1 an hour (Meteor) for unlimited DeepSeek V4.1 Flash, one request at a time. Requests on a plan key never draw on your balance.
-   **Hourly key**: $0.50 for one hour of Gemma 4 VL 26B on one stream. Good for keys that leave your servers. [Hourly keys explained](https://broadrouter.com/blog/hourly-llm-api-key-explained/).

A loop on a plan wastes time, not money. You still want step caps, because a stuck agent blocks the session for other work. Whether a plan is cheaper than your metered bill depends on volume; the [flat-rate vs per-token calculator](https://broadrouter.com/tools/flat-rate-vs-per-token-calculator/) gives the break-even for your token mix.

Per-token rates can also rise under you. [52 archived LLM API price rises](https://broadrouter.com/blog/llm-api-price-increases/) show how often that happened, at which providers and by how much.

## A short checklist

1.  Prepaid balance sized to your monthly limit.
2.  One named key per app, with an IP allow-list.
3.  `max_tokens` on every request.
4.  Step cap, retry cap and repeated-call detection on every agent.
5.  Running cost per job, checked before each call.
6.  Token logging per request, with alerts on your thresholds.
7.  A flat plan or hourly key wherever usage is continuous or the key is exposed.

## Where BroadNet Inference fits

BroadNet gives you the account and key layers: prepaid balance, separate keys, IP allow-lists with revocation in seconds, and flat-price plans and hourly keys. It doesn’t provide the job-level budgets in this post; those belong in your code or proxy. If you need per-team budgets across many providers, a gateway in front of all of them is the right tool. [Read the pricing](https://inference.broadnet.ai/pricing?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-spend-control&utm_content=body).

Free tools for this

-   [LLM API cost calculator](https://broadrouter.com/tools/llm-api-cost-calculator/) Price a workload on every model at once, with cache hits, reasoning and a flat-plan check.
-   [Flat-rate vs per-token LLM cost calculator](https://broadrouter.com/tools/flat-rate-vs-per-token-calculator/) Break-even between an unlimited plan and paying per token, per agent.
-   [Prompt caching savings calculator](https://broadrouter.com/tools/prompt-caching-calculator/) Bill with and without caching, by prefix share and hit rate.
-   [Prompt token diff](https://broadrouter.com/tools/prompt-token-diff/) Two prompt versions side by side: changed lines, exact token delta and the monthly cost difference.

[All free tools](https://broadrouter.com/tools/)

## Terms in this article

[Token](https://broadrouter.com/glossary/token/)

A token is the unit of text a language model reads and writes: a word, part of a word, a punctuation mark or a byte sequence produced by the model's tokenizer. LLM APIs count, limit and usually bill usage in tokens.

[Prepaid credit](https://broadrouter.com/glossary/prepaid-credit/)

Prepaid credit is money added to an API account before use. Each request deducts its cost from the balance, and when the balance runs out, metered requests stop until you top up. It caps spend at what you have paid in.

[API key](https://broadrouter.com/glossary/api-key/)

An API key is a secret string that identifies and authorizes a client calling an API. LLM APIs usually expect it in an Authorization: Bearer header, and anyone who holds the key can make requests that are billed to the account that owns it.

[Tool calling](https://broadrouter.com/glossary/tool-calling/)

Tool calling, also called function calling, is an LLM API feature in which the model returns a structured request to run a function the developer has defined, with JSON arguments. The application runs the function and sends the result back; the model never runs code itself.

[AI agent](https://broadrouter.com/glossary/agent/)

An AI agent is a system in which a language model directs its own steps toward a goal: it decides which tools to call, reads the results and repeats until it produces a final answer or hits a limit. A fixed script that calls a model at set points is usually called a workflow instead.

[Pay-as-you-go](https://broadrouter.com/glossary/pay-as-you-go/)

Pay-as-you-go is a pricing model in which you pay only for what you use, metered per unit such as tokens, requests or audio minutes, with no subscription or minimum commitment. Most LLM APIs bill this way, from a prepaid balance or a periodic invoice.

[Rate limit](https://broadrouter.com/glossary/rate-limit/)

A rate limit is a cap an API places on how much a client can use it in a time window, such as requests per minute or tokens per minute. Requests over the limit are refused, usually with HTTP 429 Too Many Requests, until usage falls back under it.

[Streaming](https://broadrouter.com/glossary/streaming/)

Streaming is a way of returning a language model response in small chunks while it is still being generated, instead of as one reply at the end. OpenAI-compatible APIs stream over server-sent events when the request sets stream to true.

## Questions

### How do I set a spending limit on an LLM API?

Use the provider's prepaid balance or spending cap as the outer limit, then enforce your own per-job and per-day budget in code before each request, because provider limits and alerts can lag behind a fast loop.

### How do I stop an AI agent from running up a huge bill?

Cap steps per task, cap retries, set max\_tokens on every request, detect repeated identical tool calls, and stop the job when its running cost passes a budget.

### Should I retry a 401 or 402 error when my balance runs out?

No. On BroadNet, an exhausted pay-as-you-go allowance returns 401, and 402 insufficient\_balance means the account cannot cover a purchase such as an hourly key. Fund the account first. Retry 429 and 5xx with backoff instead.

### What should I log to track LLM spend?

Per request, log the key alias, feature or agent name, model, prompt\_tokens, cached\_tokens, completion\_tokens, status code and request ID. Multiply by the price file to get cost.

### Is a flat-rate plan a form of spend control?

Yes. A plan session or hourly key costs the same however many tokens it uses, so a loop costs time, not money.

## Keep reading

-   [How to estimate LLM API cost before you launch a feature](https://broadrouter.com/blog/estimate-llm-api-cost-before-launch/)
-   [10 ways to cut coding agent LLM costs, with estimated savings](https://broadrouter.com/blog/cut-coding-agent-llm-costs/)
-   [Hourly LLM API key: a flat price per hour, explained](https://broadrouter.com/blog/hourly-llm-api-key-explained/)
-   [Flat-rate vs pay-per-token LLM API: the break-even math](https://broadrouter.com/blog/flat-rate-vs-pay-per-token-llm-api/)
-   [LLM cost per user: unit economics for a SaaS AI feature](https://broadrouter.com/blog/llm-cost-per-user-saas/)
-   [Cache LLM API responses to cut cost: an exact-match cache in Python](https://broadrouter.com/blog/cache-llm-responses-cut-cost/)
-   [LLM API price changes in October 2026: 3 rises, no cuts](https://broadrouter.com/blog/llm-api-price-changes-october-2026/)
-   [Do LLM API prices only go down? 52 rises and how to budget for them](https://broadrouter.com/blog/llm-api-price-increases/)
-   [Guide: Lock API keys to your servers with IP allow-lists](https://broadrouter.com/guides/api-keys-ip-allow-lists/)
-   [Guide: BroadNet Inference error codes and when to retry](https://broadrouter.com/guides/errors-and-retries/)
-   [Guide: Stream chat completions and handle dropped connections](https://broadrouter.com/guides/streaming-chat-completions/)

Try it on your real workload. New accounts start with $5 of credit for Gemma and transcription. DeepSeek, GLM and Qwen3.8 need a top-up.

[Get started](https://inference.broadnet.ai/account/?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-spend-control&utm_content=page-cta) ·

**Sources**

-   [BroadNet Inference API reference, IP allow-lists and keys (checked 2026-11-16)](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-spend-control&utm_content=sources#ip-allow-lists)
-   [BroadNet Inference API reference, errors and limits (checked 2026-11-16)](https://inference.broadnet.ai/docs?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-spend-control&utm_content=sources#errors)
-   [BroadNet Inference pricing and hourly keys (checked 2026-11-16)](https://inference.broadnet.ai/pricing?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-spend-control&utm_content=sources)
-   [BroadNet Inference plans (checked 2026-11-16)](https://inference.broadnet.ai/plans?utm_source=broadrouter&utm_medium=referral&utm_campaign=blog-llm-api-spend-control&utm_content=sources)
