Max Iterations / Turn Limit — a safety cap on how many loop cycles an agent may run before stopping, unconditionally.
A research assistant with a deliberately “flaky” search tool, capped at MAX_ITERATIONS = 5 loop cycles. Type exit to quit.
../interrupts_breakpoints/interrupts_breakpoints.py: that template’s breakpoints only fire when a specific condition is met, and in principle could let a task run forever if none of them ever trigger. run_turn here has no such gap: iteration increments every pass and the loop stops at max_iterations regardless of what’s happening in the task.if iteration > max_iterations: return ... runs at the top of the loop, before that iteration’s API call — the (N+1)th call genuinely never happens, not just “the loop looks like it stopped.”From the repo root:
pip install -r requirements.txt
export ANTHROPIC_API_KEY=your-key-here
python3 Execution_Loops/max_iterations/max_iterations.py
Try a query the mock tool won’t resolve, to watch the cap trigger:
You: Look up the shipping policy for international orders.
[iteration 1/5]
[tool] flaky_search({'query': 'international shipping policy'})
[result] No useful results found for that query. Try rephrasing.
[iteration 2/5]
...
[iteration 5/5]
[tool] flaky_search({'query': '...'})
[result] No useful results found for that query. Try rephrasing.
=== stopped: reached MAX_ITERATIONS (5) without a final answer ===
MODEL, MAX_TOKENS, EFFORT, SYSTEM_PROMPT — see ../../Core_Architecture/basics/README.mdMAX_ITERATIONS — the cap (default: 5, deliberately low so it’s reachable in a demo session — a real agent would set this much higher, sized to the task)KNOWN_QUERIES — the one query flaky_search will actually resolve, for testing the “completes before the cap” path../interrupts_breakpoints/README.md — the more targeted, condition-based alternative this template’s unconditional cap backstops../human_in_the_loop/README.md — pausing on specific actions rather than a total iteration count