Quick Start
How It Works
The loop reads per-run spend after each iteration, subtracts the spend recorded at loop start (so a reused agent’s prior lifetime cost isn’t counted), and returnsbudget_exhausted the moment a cap is crossed.
| Step | What happens |
|---|---|
| 1. Baseline | Spend and tokens are snapshotted at loop start |
| 2. Execute | The agent runs one iteration |
| 3. Check | Cumulative spend (current − baseline) is compared to the caps |
| 4. Decide | Over a cap → budget_exhausted; budget_action sets pause vs stop |
The baseline subtraction means caps are per run, not per agent lifetime. Reusing the same
Agent across runs starts each run’s budget from zero.Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
max_budget_usd | Optional[float] | None | Cumulative USD spend cap for the run. None = unlimited. |
max_tokens | Optional[int] | None | Cumulative token cap (input + output). None = unlimited. |
budget_action | str | "pause" | "pause" (recoverable) or "stop" (terminal) when a cap is hit. |
Completion outcome
Onbudget_exhausted, AutonomyResult returns the partial output and a populated metadata dict.
metadata key | Meaning |
|---|---|
spend_usd | USD spent during this run |
tokens | Tokens used during this run |
max_budget_usd | The configured USD cap (or None) |
max_tokens | The configured token cap (or None) |
status | "paused" (resumable) or "stopped" (terminal) |
TerminationReason and RunStatus
completion_reason is typed by the public TerminationReason enum — string values are unchanged, so existing string comparisons keep working.
termination_to_run_status(...) maps a termination reason onto the shared RunStatus vocabulary. budget_exhausted maps to failure, so it’s caught by any generic failure handling.
Common Patterns
Pause and resume with a raised cap
Hard stop for CI-only runs
Combined USD + token caps
Async parity
Best Practices
Pick pause for interactive flows, stop for CI
Pick pause for interactive flows, stop for CI
Use
budget_action="pause" when a human can raise the cap and resume. Use "stop" as a hard kill-switch in CI or unattended jobs where no resume is expected.Set both caps
Set both caps
max_budget_usd is your billing safety net; max_tokens is your latency safety net. Setting both stops on whichever triggers first.Reused agents are capped per run
Reused agents are capped per run
The loop subtracts a baseline taken at start, so caps apply to the current run only — prior lifetime spend on a reused
Agent isn’t counted.Validate cost accounting for local models
Validate cost accounting for local models
USD accounting depends on the LLM returning cost data. For local or self-hosted models that report no cost, lean on
max_tokens instead of max_budget_usd.Related
Autonomy Loop
Iterative autonomous loops and completion reasons
Agent Max Budget
The
ExecutionConfig-based budget for any run
