Budget Alerts¶
tokenspy can monitor cost per invocation and either warn or raise when a function exceeds its budget.
Warning (default)¶
Set budget_usd on any decorated function:
When a single invocation costs more than $0.10, Python emits a UserWarning:
Execution continues normally — the warning is non-blocking.
Raising an exception¶
Use on_exceeded="raise" to stop execution:
This raises tokenspy.BudgetExceededError:
try:
strict_agent(query)
except tokenspy.BudgetExceededError as e:
print(f"Aborted: spent ${e.spent:.4f}, limit was ${e.budget:.4f}")
# Handle gracefully — log, fallback, alert, etc.
BudgetExceededError¶
| Attribute | Type | Description |
|---|---|---|
spent |
float |
Actual cost of the invocation in USD |
budget |
float |
Budget limit set on the decorator |
Why BaseException?
BudgetExceededError inherits from BaseException (not Exception) so it propagates correctly through provider try/except guards that catch broad exceptions. Standard except Exception blocks in your code won't accidentally swallow it.
Per-function budgets¶
Different budgets for different functions:
@tokenspy.profile(budget_usd=0.01) # cheap function — tight budget
def classify(text: str) -> str:
...
@tokenspy.profile(budget_usd=1.00) # expensive agent — loose budget
def deep_research(topic: str) -> str:
...
Production use¶
Budget alerts are useful for:
- Catching prompt injection — an adversarial prompt that causes runaway token usage will trip the budget
- Cost-safe multi-tenant apps — per-request budget caps
- Development guards — fail fast if a refactor accidentally inflates costs