Turns and spend both default to unlimited

The Claude Agent SDK's execution loop has Claude call tools, read the results, and decide again, repeating until it returns a text-only response. The official docs state that both stop options, max_turns and max_budget_usd, default to "no limit." Leave either one unset and a single open-ended prompt like "improve this codebase" can run turns and spend indefinitely. Once you do set a cap, the loop reports why it stopped through the ResultMessage subtype field: error_max_turns for the turn cap, error_max_budget_usd for the budget cap, and a separate error_max_structured_output_retries when structured-output validation exhausts its configured retries.

You cross the cap before the check catches it

The budget cap is compared only after a model response arrives, because that response's cost is computed after the fact from the token usage the API returns. So the response that trips the cap is already complete and already counted in total_cost_usd. Actual spend can therefore exceed max_budget_usd by the cost of that one response plus whatever subagents were still running at that moment — which is why the docs recommend leaving headroom above your real target. Subagent spend counts toward the same budget: once total spend reaches the cap, spawning a new subagent fails with a "Budget limit reached" error and subagents already running are stopped. That enforcement only applies on Claude Code v2.1.217 or later.

From design to operations: a turn-and-budget cap gate checklist

Set the cap from the distribution of your target tasks, not an average turn count. The official example uses max_turns=30 for a well-scoped task like "find and fix the failing tests." For a production service, take the p95 turn count from the last one to two weeks of sessions as your turn cap, and set the budget cap at 1.3 to 1.5 times the average token cost for that many turns to leave headroom.

The first failure pattern: when the turn cap trips, the result field is empty, so the run doesn't look finished — it looks stuck. session_id is returned on every subtype, so an error_max_turns response should trigger a resume with the same session_id and a higher turn limit. In streaming-input sessions, a message still queued when the turn limit hits carries over to a new turn and the turn count restarts for it, so resume logic that doesn't account for this boundary ends up double-counting turns.

The second failure pattern: a tool exception gets swallowed silently. The tool runner catches whatever exception a tool function throws and converts it into a tool_result with is_error: true, carrying only the exception's message — in Python, its type and message — with the full stack trace dropped. If you need to trace a tool failure from production logs, log inside the tool function before the exception reaches Claude; skip that and you can't reconstruct the root cause afterward.

Three items belong on the pre-deploy checklist: are both max_turns and max_budget_usd explicitly set, does a PreToolUse hook gate risky calls such as payment or deploy commands separately from the cap, and is the running Claude Code binary v2.1.217 or later so the cap actually propagates to subagents. Skip that third check and a cap you declared in code can be silently ignored in production.

Weekly, track the share of sessions that end in error_max_turns or error_max_budget_usd alongside the extra turns and spend each resumed session needed to finish. If resumed sessions repeatedly need more than half the original cap again, that's a signal the cap was set too low for the task's real difficulty — log a cap increase and a prompt-scope reduction as separate changes so the next adjustment doesn't have to re-guess the cause.

What to apply right now

If you're running headless agents on the Claude Agent SDK, leaving max_turns and max_budget_usd unset is the first thing to fix. Caps are only checked after a response completes, so declare them with headroom for subagent spend, and wire up per-subtype resume logic and tool-exception logging — only then does the cap function as a real operational gate.

References

How the agent loop works — Claude Agent SDK Docs

Tool runner (SDK) — Claude Platform Docs

Ask AI about this article

The assistant has read this article. Ask anything — it answers from the text and says so when something isn't in it.

Loading the chat…