runbudget
runbudget

how runbudget fits in

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).

gpt-4o-2026-01-01 uses the gpt-4o price

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

a different problem; this library settles up after each response.

Limits

provider's page before trusting a monthly number.

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.