[Feature] Spend and token budget cap for runs and sessions
Description
Add a spend and token cap for a run or session (a --max-budget style limit) that stops the agent cleanly when reached.
Use Case
Unattended runs (nanocoder run, daemon skill triggers, verify) and long interactive sessions against paid APIs can run up unbounded cost when a model loops or a task is larger than expected. nanocoder.headless.maxTurns limits turns, but turns are a poor proxy for money. The usage module already prices provider-reported tokens through models.dev, so the number to compare against exists.
Proposed Solution
Add --max-budget-usd <n> and --max-tokens-total <n> flags for run, and nanocoder.budget settings (perSessionUsd, perRunUsd) for the daemon and interactive mode. After each response, compare the running total from source/usage/ with the limit. At 80 percent show a warning; at 100 percent stop before the next request, print the total spent, and exit with a distinct non-zero code in headless mode. In interactive mode ask whether to continue and raise the limit.
Alternatives Considered
A provider-side spending cap (not available on every provider, and none for local models measured in time or tokens). Watching /usage by hand.
Additional Context
- [x] I have searched existing issues to ensure this is not a duplicate
- [x] This feature aligns with the project's goals (local-first AI assistance)
Implementation Notes (optional)
Models without a price in the database should fall back to the token limit only, and the UI should say the cost is unknown instead of treating it as zero.
Hi! I'd like to implement this — adding --max-budget-usd / --max-tokens-total flags for run, nanocoder.budget settings, the 80% warning and clean stop at 100% with a distinct exit code, using the existing usage/pricing from source/usage/. Opening a PR shortly.
Hi @iamcipherdev, I’m assigning this one to you. I’ll review your PR this time. Going forward, please comment on the issue first and wait until you’re assigned before you start working on it. That way we avoid duplicate work and keep things fair for everyone. Thanks for contributing!