Why budget control matters
An AI agent calling paid APIs in a loop can rack up costs fast. Budget control lets you:- Cap total spend per session
- Set per-domain limits (e.g., max 100 sats/session on
api.weather.com) - Receive a callback before each payment
- Get a full spending report at any time
Global budget
- TypeScript
- Python
priceSats field and it would exceed the remaining budget, the client throws BudgetExceededError before paying — no satoshis are spent.
Per-domain budget
- TypeScript
- Python
Callbacks
- TypeScript
- Python
onBudgetExceeded / on_budget_exceeded is called just before BudgetExceededError is thrown — useful for logging or alerts.
Spending report
- TypeScript
- Python
spendingReport() returns null / None when no budget is configured.
Handling BudgetExceededError
- TypeScript
- Python
Concurrency notes
Full options reference
| Option | TypeScript | Python | Default | Description |
|---|---|---|---|---|
| Global budget | budgetSats | budget_sats | unlimited | Max sats for the session |
| Per-domain | budgetPerDomain | budget_per_domain | {} | Map of domain → max sats |
| Spend hook | onSpend | on_spend | — | Called after each payment |
| Exceeded hook | onBudgetExceeded | on_budget_exceeded | — | Called before throwing |