Budget Enforcement
Fuze enforces hard budget ceilings per-step and per-run. A call that would exceed the ceiling is blocked before execution.
How it works
- Before each guarded call, Fuze estimates the cost based on the model and expected token usage
- If
estimated_cost + accumulated_cost > ceiling, the call is blocked before execution - After each call, the actual cost is recorded and accumulated
const search = guard(searchFn, {
maxCost: 0.50, // Per-call override, or set via registerTools() defaults
})Configuration
Budget types
| Type | Scope | What it limits |
|---|---|---|
max_cost_per_step | Single function call | Prevents expensive individual operations |
max_cost_per_run | Entire agent run | Total spend ceiling across all steps |
max_tokens | Entire agent run | Total token usage |
timeout | Single function call | Wall-clock time |
Provider price registry
Fuze maintains a price registry for major LLM providers:
- OpenAI (GPT-4o, GPT-4, GPT-3.5)
- Anthropic (Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku)
- Google (Gemini Pro, Gemini Flash)
Override pricing for enterprise discounts:
Timer cleanup
When a timeout is configured, Fuze sets up an internal timer for each guarded call. On successful completion, the timer is properly cleaned up, there are no leaked timers. If the timeout fires before the call completes, Fuze throws a GuardTimeout error.
Error handling
Fuze throws specific error classes for budget violations so you can catch and inspect them programmatically.
import { BudgetExceeded, GuardTimeout } from 'fuze-ai'
try {
await guardedSearch('query')
} catch (err) {
if (err instanceof BudgetExceeded) {
console.log(err.accumulated) // Total spent so far
console.log(err.ceiling) // The ceiling that was hit
console.log(err.estimated) // Estimated cost of blocked call
}
if (err instanceof GuardTimeout) {
// The guarded call exceeded its configured timeout
console.log(err.message)
}
}| Error class | When thrown |
|---|---|
BudgetExceeded | Estimated cost would push accumulated spend past the ceiling |
GuardTimeout | A guarded call exceeded its configured timeout duration |