Why does my AI agent keep repeating the same step?
Common causes of repetition
An agent often repeats a step when a tool returns an empty or confusing response. The model reads that as a failure and tries the same action again. A search tool that returns nothing without an explanation is a typical example.
A vague goal is another cause. If the prompt says to keep improving the answer, the agent may never decide it is finished. Loops can also start when two tools undo each other's work, such as one that saves a file while another deletes it.
How to fix the loop
Make every tool return a clear status, such as success, empty, or error, along with a short message. Then tell the agent what to do for each status. A rule like 'if a search returns nothing, try one new query and then stop' prevents endless retries.
Add a step counter and a check that compares the current action with the last few actions. If the same call repeats three times, force the agent to stop or switch to a fallback path. Empty results, vague stop rules, and tools that undo each other are the three patterns to check first, because they cause most loops.
Checking the logs
Read the logs for the first few repeated steps and look at exactly what the model received. The problem is often in the input rather than in the model's reasoning. Once you see the pattern, a small change to the tool output usually resolves it.
Common mistakes
- Assuming the model is broken when the real cause is an empty or unclear tool response.
- Adding more instructions to the prompt without also adding a hard step limit in the code.

Related questions
- How should I design the architecture of an AI agent?
- What is the ReAct pattern and why use it?
- How do I add memory to an AI agent?
- What is the difference between a single-agent and multi-agent system?
- How do I handle tool use and function calling in AI agents?
- What are common pitfalls in AI agent design?