agentic_ai_basics

max_iterations

Max Iterations / Turn Limit — a safety cap on how many loop cycles an agent may run before stopping, unconditionally.

max_iterations.py

A research assistant with a deliberately “flaky” search tool, capped at MAX_ITERATIONS = 5 loop cycles. Type exit to quit.

Concepts covered

Run

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 ===

Configuration

See also