Skip to main content

Cost Budget Auto-Stop

The CostTracker 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 a costTracker is configured on an agent, the onRoundtripComplete loop hook automatically:
  1. Calls checkInProgressBudget() with cumulative token usage (without persisting an entry — no double-counting)
  2. Checks if any budget limit is exceeded
  3. If exceeded, gracefully stops the LLM loop and returns the last assistant message
The actual cost entry is persisted only once at the end of agent.run(), ensuring accurate totals.

Configuration

CostTracker Methods

Behavior

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