Skip to content
AgenticAIWiki
  1. Learn
  2. Part 9
  3. 46. ReAct
  4. 46.3

Lesson 46.3 · 3 of 3 · 5 min read · Reviewed 24 Nov 2026

Failure modes: loops and drift

By the end, you can

  • Recognise loops and drift in ReAct agents.
  • Apply fixes for each.

Loops

search("refund policy")  -> 0 results
search("refund policy")  -> 0 results
search("refund policy")  -> 0 results ...

Causes and fixes:

Cause Fix
Unhelpful tool result (“0 results”) Return guidance: “No match. Try a shorter query, or list_policies().” (chapter 34)
No memory of what was tried Detect identical calls in your loop and add a note: “You already tried this.”
No exit A step limit and a “give up and explain” instruction

Drift

After 30 steps, an agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. asked to “fix the failing test” is refactoring an unrelated module. Causes and fixes:

Cause Fix
Goal buried under long history Restate the goal in a system message, or compact history (chapter 60)
Vague task Give acceptance criteria: “Done when test_x passes and no other tests break.”
Too many tools Offer only the tools this task needs

Detecting trouble in code

  • Count repeated (tool, input) pairs.
  • Track progress signals (new files read, tests passing) and stop if none appear in N steps.
  • Cap tokensToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. and wall-clock time per task.

Look at traces

Most loop and drift bugs are obvious once you read the traceTrace: A record of everything that happened during one request or agent run, as a tree of timed steps. step by step. Log every thought, call and result (chapter 67) and review failed runs regularly (chapter 17).

Key points

  • Loops: the agent repeats the same action because results don't change its view.
  • Drift: the agent wanders off the user's goal after many steps.
  • Fixes include step limits, detecting repeated calls, clearer tool errors, and restating the goal.
  • Log full traces so you can see exactly where a run went wrong.
Hands-on labRuns in the Playground

Catch a looping agent in code

Detect repeated identical calls in the loop and break the loop with new information.

You will have: A loop with repeat detection that turns an endless search into an answer.

Steps

  1. Run the starter: the agent searches the same query until the step limit.
  2. Track each (action, input) pair and spot an identical repeat.
  3. On a repeat, skip the call and add a note saying what was tried and what to try instead.
  4. Run it and follow how the note changes the agent's next action.
  5. Lower MAX_STEPS to 3 and confirm the step limit still ends a stuck run.

Done when

  • The starter hits the step limit with no answer.
  • The solution answers in 5 steps with 1 repeat caught.
  • The repeated search is never run twice.

Open in the Playground No API key needed.

Starter code (Python)
# Catch a looping agent in your own loop code, and give it something new to reason about.
def search_kb(q):
    return "0 results" if "EU" in q else "Refunds are accepted within 30 days, including EU orders."


def list_topics():
    return "topics: refunds, shipping, returns"


def fake_agent(history):
    """Simulated model. It repeats itself unless it is told it already tried something."""
    obs = [h[1] for h in history if h[0] == "obs"]
    told = any(h[0] == "note" for h in history)
    if obs and obs[-1].startswith("Refunds"):
        return ("answer", obs[-1])
    if obs and obs[-1].startswith("topics"):
        return ("search_kb", "refunds")
    if told:
        return ("list_topics", None)
    return ("search_kb", "refund policy for EU orders")


MAX_STEPS = 8
history, seen, repeats = [], set(), 0
for step in range(1, MAX_STEPS + 1):
    action, arg = fake_agent(history)
    if action == "answer":
        print(f"step {step}: answer -> {arg}")
        print(f"answered in {step} steps, repeats caught: {repeats}")
        break
    # TODO: if (action, arg) is in seen, do not run it again. Count it in repeats and add
    #       ("note", "You already tried <action>(<arg>). Try list_topics() or a shorter query.") to history
    seen.add((action, arg))
    result = search_kb(arg) if action == "search_kb" else list_topics()
    print(f"step {step}: {action}({arg!r}) -> {result}")
    history.append(("obs", result))
else:
    print("Stopped: step limit reached without an answer")
Show a solution
# Catch a looping agent in your own loop code, and give it something new to reason about.
def search_kb(q):
    return "0 results" if "EU" in q else "Refunds are accepted within 30 days, including EU orders."


def list_topics():
    return "topics: refunds, shipping, returns"


def fake_agent(history):
    """Simulated model. It repeats itself unless it is told it already tried something."""
    obs = [h[1] for h in history if h[0] == "obs"]
    told = any(h[0] == "note" for h in history)
    if obs and obs[-1].startswith("Refunds"):
        return ("answer", obs[-1])
    if obs and obs[-1].startswith("topics"):
        return ("search_kb", "refunds")
    if told:
        return ("list_topics", None)
    return ("search_kb", "refund policy for EU orders")


MAX_STEPS = 8
history, seen, repeats = [], set(), 0
for step in range(1, MAX_STEPS + 1):
    action, arg = fake_agent(history)
    if action == "answer":
        print(f"step {step}: answer -> {arg}")
        print(f"answered in {step} steps, repeats caught: {repeats}")
        break
    if (action, arg) in seen:
        repeats += 1
        note = f"You already tried {action}({arg!r}). Try list_topics() or a shorter query."
        print(f"step {step}: repeat caught -> {note}")
        history.append(("note", note))
        continue
    seen.add((action, arg))
    result = search_kb(arg) if action == "search_kb" else list_topics()
    print(f"step {step}: {action}({arg!r}) -> {result}")
    history.append(("obs", result))
else:
    print("Stopped: step limit reached without an answer")
Quick checkAn agent calls search('refund policy') six times in a row with identical results. What's the best fix?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.