Cost Budget Auto-Stop
TheCostTracker checks budgets after every tool roundtrip, not just before the loop starts. This prevents a multi-roundtrip agent from blowing through its budget during tool-calling loops.
How It Works
When acostTracker is configured on an agent, the onRoundtripComplete loop hook automatically:
- Calls
checkInProgressBudget()with cumulative token usage (without persisting an entry — no double-counting) - Checks if any budget limit is exceeded
- If exceeded, gracefully stops the LLM loop and returns the last assistant message
agent.run(), ensuring accurate totals.
Configuration
CostTracker Methods
Behavior
| Scenario | Before | After |
|---|---|---|
| 10-roundtrip loop, budget hit at roundtrip 3 | Budget only checked before loop | Loop stops at roundtrip 3 |
| Cost tracking | Only after full loop completes | Budget checked each roundtrip, entry persisted once at end |
| Multi-roundtrip tool calls | Could double-count intermediate roundtrips | checkInProgressBudget checks without persisting |
Works Across All Agent Types
Cost auto-stop works with:- Agent — Text agents with tool calling
- VoiceAgent — Tracks realtime API token usage
- BrowserAgent — Tracks vision model tokens across browser steps