Budget Enforcement
Fuze enforces hard budget ceilings at two levels: per-step and per-run. The step doesn't execute if it would exceed the ceiling.
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-step ceiling
})Configuration
[defaults]
max_cost_per_step = 1.00 # USD per individual call
max_cost_per_run = 10.00 # USD for the entire run
max_tokens = 100000 # Token ceiling per run
timeout = "30s" # Time ceiling per callBudget 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:
[providers]
"openai/gpt-4o" = { input = 0.0020, output = 0.008 }
"anthropic/claude-3-5-sonnet" = { input = 0.003, output = 0.015 }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 |