为什么预算控制很重要
在循环中调用付费 API 的 AI 代理可能迅速累积大量费用。预算控制让您能够:- 限制每个会话的总消费上限
- 设置按域名的限额(例如,在
api.weather.com上每次会话最多 100 sats) - 在每笔支付前接收回调
- 随时获取完整的消费报告
全局预算
- TypeScript
- Python
priceSats 字段且该金额将超过剩余预算时,客户端会在付款之前抛出 BudgetExceededError —— 不会花费任何 satoshis。
按域名预算
- TypeScript
- Python
回调
- TypeScript
- Python
onBudgetExceeded / on_budget_exceeded 会在抛出 BudgetExceededError 之前被调用 —— 适用于日志记录或发送警报。
消费报告
- TypeScript
- Python
spendingReport() 返回 null / None。
处理 BudgetExceededError
- TypeScript
- Python