Scenario-Based AI Engineering Questions

Course Content

Scenario-Based AI Engineering Questions

26 sections · 146 lessons

Scenario – 3: Infinite Reasoning Loops


What you need to know

The scenario: an agent keeps calling tools and "thinking" without finishing, until it hits a limit or runs up a large bill.

Why agents loop

An agent works in a loop: think, call a tool, read the result, decide whether it is done. It finishes when it believes it can produce the expected output. Two things stop that from ever happening:

  • Unhelpful tool results. An empty list or a stack trace gives no new information, so the agent tries the same call again.
  • An impossible task. If expected_output demands a number that exists in no available source, the agent cannot succeed, and nothing tells it that failure is allowed.

Bound it

SettingWhat it limits
max_iterReasoning iterations per task before the agent must give its best answer
max_execution_timeWall-clock seconds per task
max_rpmModel requests per minute, which caps spend rate
max_retry_limitRetries after errors

Set these explicitly; defaults vary between versions.

Make tools helpful, and detect repeats

Python
from crewai.tools import tool@tool("search_filings")def search_filings(query: str) -> str:    """Search company filings. Returns JSON with results or a hint."""    try:        hits = filings_index.search(query, k=5)    except TimeoutError:        return '{"results": [], "error": "search timed out; try again once, then continue without it"}'    if not hits:        return ('{"results": [], "hint": "no filings match. Try the legal company name, '                'or report that the figure is not available."}')    return json.dumps({"results": [h.summary for h in hits]})

For repeat detection, record (tool name, arguments) in a step callback, and stop the run when the same pair appears twice. Progress, not iteration count, is the real signal.

Allow honest failure

Write expected_output so that "Not found, with what was searched and why" is an acceptable result. An agent that is allowed to fail finishes; one that is not, loops.

Watch it

Track p95 iterations per task, the tool-repeat rate and the timeout rate. With tracing on, a loop shows up as the same tool span repeating.

A real-life example

Scenario, numbers made up. A due-diligence crew asks an agent for "the company's FY2025 R&D spend" for private companies that do not publish it. The search tool returns an empty list, and the agent tries 20 variations before the iteration cap. Some runs cost 15 times the normal amount.

The team changes the tool to return a hint, adds "state 'not disclosed' with the sources checked" to expected_output, sets max_iter=8 and max_execution_time=120, and adds repeat detection. p95 iterations fall from 19 to 5, and cost per run drops by 60%. Reports now say "R&D spend not disclosed; checked annual report, MCA filings and press releases", which analysts find more useful than a guess.

Follow-up questions to expect

  • "Isn't max_iter enough?" — It turns an endless loop into an expensive failed run. Repeat detection and helpful tool results stop it early and produce an answer.
  • "What should happen when the cap is hit?" — Return the best partial result with a clear note, not an error page.
  • "How do you find the looping agent?" — Traces per task: iteration count and repeated tool spans point straight to it.