Study FAQ
How should I set turn and spend limits for a Claude Agent SDK run?
Also asked as 2
- What should I budget for when setting max turns and max budget in the Claude Agent SDK?
- How does the Claude Agent SDK report when a turn or budget cap is reached?
Answer
Set max_turns in Python or maxTurns in TypeScript to bound the number of model turns. Count each pass through the loop, including the final text-only turn. For example, an investigation that needs three tool-calling turns and one final answer needs at least four turns. Set max_budget_usd or maxBudgetUsd to cap the run against estimated spend. The check happens after responses arrive, so provide headroom for spend above the threshold.
Handle error_max_turns and error_max_budget_usd as incomplete outcomes. Keep any verified evidence, record what remains, and decide whether to ask for input or run a narrower task. In a single-shot query the reported error result can be followed by an iterator exception. With Claude Code v2.1.217 or later providing the SDK runtime, the documented budget enforcement includes subagent spend and stops remaining background subagents at the cap.
Use a separate elapsed-time limit if the caller has a deadline, restrict tools independently, and verify the outcome against the task’s acceptance criteria. An SDK limit ends work on its own terms. The host decides what to do with any partial work.
Full explanation
Choosing Single-Shot, Workflow, and Agentic Claude Architectures
Study guide · CCAR-F
Read the full explanation