← All articles

Your LangChain Agent Is Stuck in a Loop. Here Is What It Is Actually Doing

TL;DR

A LangChain or LangGraph agent loops when a tool keeps returning a result the model cannot act on, most often a page that did not change because a login, banner or CAPTCHA blocked the action. Detect the loop by hashing tool outputs and counting repeats, cap iterations low, and on the third identical result stop and hand the task to a person rather than spending another twenty calls discovering nothing.

The agent is not confused. It is doing the sensible thing given what it sees: the action did not work, so try it again. What it cannot see is why the action did not work, and no number of retries will show it.

Why loops happen

Almost every browser-agent loop is the same shape. The model issues a click, the tool returns the page, the page is unchanged because something blocked the click, the model reasons that it should click again. A login wall, a consent overlay, a disabled button and a CAPTCHA all produce this. The model's transcript will describe progress that is not happening.

Detect it cheaply

import hashlib

seen: dict[str, int] = {}

def guard(tool_output: str) -> bool:
    key = hashlib.sha256(tool_output.encode()).hexdigest()
    seen[key] = seen.get(key, 0) + 1
    return seen[key] >= 3      # the same world three times: stop, this is a wall

# In LangGraph, put this in a node that runs after each tool call and routes to a
# handoff node when it returns True, instead of back to the model.

Then do the right thing with it

A hard iteration cap stops the bill, but it leaves the task undone and the person who finds out is whoever reads the logs tomorrow. Better: the third identical result routes to a handoff. A person opens the live browser, sees the banner or the login the model could not, clears it in ten seconds, and the run resumes. Record that as a procedure and the loop never forms on that wall again, because the first tool call the next time asks whether a person already solved it.

Set the cap low, around eight. An agent that has not made progress in eight steps is not about to.

Questions people ask

Why does my LangChain agent repeat the same action?

A tool keeps returning an unchanged result, usually because a login, overlay or CAPTCHA blocked the action, and the model reasonably tries again. It cannot see the cause, so it cannot stop.

How do I stop an agent from looping?

Hash each tool output and count repeats. On the third identical result, stop and route to a human handoff rather than back to the model. Keep the overall iteration cap low.

What should happen when the loop is detected?

Hand the live session to a person who can see what the model cannot, let them clear it, resume the run, and record the fix so the same wall is answered automatically next time.

Does a max_iterations cap solve this?

It stops the spending, not the failure. The task is still undone and nobody is told why. Pair the cap with a handoff so the run can actually finish.

DoubleOh is the reliability layer for AI agents. When one gets stuck, a person fixes it once in a live browser, and the fix becomes a skill the whole fleet follows from then on.

Start free