Quick Start
1
USD cap
Cap spend in dollars — the run pauses once cumulative cost passes the cap.
2
Token cap
Cap total tokens (input + output) instead of — or alongside — dollars.
3
Pause and resume
With
budget_action="pause" (the default), inspect the spend, raise the cap, and re-invoke to continue.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.
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
Unset caps default to unlimited — adding these fields to an existing config changes nothing until you set a value.
Completion outcome
Onbudget_exhausted, AutonomyResult returns the partial output and a populated metadata dict.
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
