Agent Wealth HubFree public resources
中文 EN العربية

Back to catalog / Agent operations

Set a USD budget cap for an agent run

Use max_budget_usd to cap a run’s cost and detect the budget-exceeded terminal status.

Goal

Set a cost guardrail for an agent that can call tools and perform multiple turns.

Steps

  1. Set an approved USD limit with ClaudeAgentOptions(max_budget_usd=0.10). [budget]
  2. Iterate the result stream and read ResultMessage.total_cost_usd and subtype. [budget]
  3. Treat subtype == "error_max_budget_usd" as a budget stop and report partial output with actual cost. [budget]

How to verify

A low-budget test identifies the budget status and records final cost instead of treating it as ordinary success.

Troubleshooting

If final cost slightly exceeds the cap, the README explains checks occur after each API call and may overshoot by one call; leave headroom.

Permissions

Prerequisites

Permissions

Limitations

The budget cap is not a strict per-token hard stop, and budget termination does not roll back tool side effects.

Pinned upstream source

https://github.com/anthropics/claude-agent-sdk-python/blob/b6e9d12fe1cc98dde988ab7b7713c1feeee50c6c/examples/max_budget_usd.py

Repository
anthropics/claude-agent-sdk-python
File path
examples/max_budget_usd.py
Pinned commit
b6e9d12fe1cc98dde988ab7b7713c1feeee50c6c
Source checked
2026-10-14 · Reviewed against the pinned upstream source

Repository metrics

These values describe the upstream repository, not popularity or likes for an individual skill or playbook.

Stars
Not collected
Forks
Not collected
Observed at
Not collected

No metrics snapshot is available yet.

License: MIT

Main topic

Take a coding Agent change from context to verification

Related resources