Skip to main content
Autonomy budgets cap how much money and how many tokens an autonomous run can spend, pausing or stopping the loop when a cap is hit.
The user caps the spend on an autonomous task; the loop checks cumulative cost after each iteration and pauses (or stops) the moment a cap is reached.

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 returns budget_exhausted the moment a cap is crossed.
StepWhat happens
1. BaselineSpend and tokens are snapshotted at loop start
2. ExecuteThe agent runs one iteration
3. CheckCumulative spend (current − baseline) is compared to the caps
4. DecideOver 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

OptionTypeDefaultDescription
max_budget_usdOptional[float]NoneCumulative USD spend cap for the run. None = unlimited.
max_tokensOptional[int]NoneCumulative token cap (input + output). None = unlimited.
budget_actionstr"pause""pause" (recoverable) or "stop" (terminal) when a cap is hit.
Unset caps default to unlimited — adding these fields to an existing config changes nothing until you set a value.

Completion outcome

On budget_exhausted, AutonomyResult returns the partial output and a populated metadata dict.
metadata keyMeaning
spend_usdUSD spent during this run
tokensTokens used during this run
max_budget_usdThe configured USD cap (or None)
max_tokensThe 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

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.
max_budget_usd is your billing safety net; max_tokens is your latency safety net. Setting both stops on whichever triggers first.
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.
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.

Autonomy Loop

Iterative autonomous loops and completion reasons

Agent Max Budget

The ExecutionConfig-based budget for any run