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 and how to handle 429s with backoff.
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 first.
Key level: how do you limit the damage of a leaked key?
An API key is spending authority: whoever holds it can use your balance. Limit each key’s blast radius:
- 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.
- Lock keys to your servers. Add your egress IPs or CIDR ranges to the IP allow-list, up to 32 entries. Requests from other addresses get a 403.
- Revoke fast. Revocations and allow-list changes apply to new requests within five seconds.
- Keep portal credentials off servers. They can create keys and make purchases. Inference keys only call models.
Lock API keys with 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.
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:
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 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).
From those logs, alert on:
- daily cost per key above the expected figure from your cost estimate;
- 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.
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.
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 gives the break-even for your token mix.
Per-token rates can also rise under you. 52 archived LLM API price rises show how often that happened, at which providers and by how much.
A short checklist
- Prepaid balance sized to your monthly limit.
- One named key per app, with an IP allow-list.
max_tokenson every request.- Step cap, retry cap and repeated-call detection on every agent.
- Running cost per job, checked before each call.
- Token logging per request, with alerts on your thresholds.
- 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.
