Install
consolepip install runbudget
Use
pythonfrom runbudget import Budget, BudgetExceeded, RunBudget
budget = RunBudget(
budget=Budget(max_cost=2.00, max_tokens=500_000, max_steps=40),
prices={"my-model": (0.50, 1.50)}, # dollars per million tokens
warn_at=0.8,
)
while True:
status = budget.status()
if status.stopped:
report_to_user(status.reason) # "cost budget reached: $2.0000 of $2.0000"
break
if status.action == "warn":
log(status.reason) # "cost budget at 84%: $1.6900 of $2.0000"
response = model(...)
budget.add("my-model", response.input_tokens, response.output_tokens)
Or raise instead of returning a status:
pythontry:
budget.check() # also spelled budget.enforce()
except BudgetExceeded as error:
error.reason # "steps budget reached: 40 of 40"
At the end of a run:
pythonbudget.spent, budget.tokens, budget.steps
budget.remaining # Budget(max_cost=…, max_tokens=…, max_steps=…)
budget.breakdown() # {"gpt-4o": {"steps": 3, "input_tokens": …, "cost": …}}
Prices
DEFAULT_PRICES holds a small table (per million tokens, in dollars) so a first run works without configuration: gpt-4o, gpt-4o-mini, gpt-4.1, gpt-4.1-mini, claude-opus, claude-sonnet, claude-haiku, gemini-2.5-pro, gemini-2.5-flash, llama (free).
- override any entry with
prices={"gpt-4o": (2.00, 8.00)} - a model name is matched exactly first, then by prefix, longest first, so
gpt-4o-2026-01-01 uses the gpt-4o price
- an unknown model raises
KeyErrorinstead of quietly costing zero — the failure mode
that makes a budget report say "you spent nothing"
Charges that are not model calls (a paid search API, a sandbox minute) go in with budget.add_cost(0.02, model="web_search").
Rules
- All three limits are optional;
Nonemeans unlimited. warn_atis a fraction of any limit; the status becomeswarnat that point.- The status is
ok→warn→stop, and aStatusis truthy until it isstop. - Token counts are what the provider reported. Estimating input tokens before the call is
a different problem; this library settles up after each response.
- Money is rounded to six decimals at each step, so cent-level totals stay readable.
Limits
- The price table is a convenience, not a source of truth. Check it against your
provider's page before trusting a monthly number.
- One currency (US dollars), one price per model — no caching discounts, no batch rates.
- It tracks a single run; for a daily or per-tenant cap, keep a long-lived object and
call reset() when a run ends.
Development
consolepython -m venv .venv
.venv/bin/pip install -e ".[test]"
.venv/bin/pytest
The suite covers every line of the package (pytest --cov --cov-branch), and the examples in the docstrings run as tests through --doctest-modules.
License
MIT.
Other libraries here
One problem each, standard library only, no runtime dependencies.
- argrepair — turn a bad tool call into a message the model can act on
- backoff-headers — turn a rate-limit header into the exact time to wait
- columncheck — find the CSV defect before the pipeline does
- dir-digest — one hash for a directory tree, identical on every OS
- docanchor — catch every broken heading anchor in your docs
- envfile-diff — diff two .env files without printing the secrets
- fsync-replace — replace a file so a crash cannot leave it half-written
- header-date — HTTP dates parsed strictly, formatted correctly
- junit-merge — merge JUnit XML from every job into one honest report
- npm-semver — npm semver ranges, parsed the way npm parses them
- outputcap — keep a huge tool result out of the context window
- parse-size — byte sizes in and out, MB and MiB never confused
- path-containment — keep untrusted paths inside their directory
- redactscope — stop secrets in tool results before they reach the model
- testflakes — rank your flaky tests from the history you already have
- tool-loop-guard — catch an agent repeating itself while it is still cheap
- toolcall-ledger — a hash-chained record of every tool call
- toolcircuit — stop retrying a tool that is down
- toolquota — client-side rate limits your agent loop can wait on
- wheeldeps — read a wheel's dependencies offline, markers included
- zip-deterministic — byte-identical archives, build after build