Prompt
My AI agent keeps looping on the same step, how do I debug it?
Latest observation
A good way to debug a looping AI agent is to treat it like a state machine or control-flow bug, not just a “model issue.”
1) First, identify the loop pattern
Figure out which kind of loop it is:
- Same tool call repeated
Example: it keeps callingsearch()with the same query. - Same reasoning step repeated
Example: it keeps saying “I need more information” but never changes strategy. - Same output/action repeated
Example: it writes the same response or asks the same question. - Multi-step cycle
Example: A → B → C → A
If you can, log:
- input
- agent state
- tool calls
- tool outputs
- final assistant message
- any routing decisions
2) Add step-by-step traces
Enable verbose logging for:
- prompt sent to the model
- model response
- intermediate tool results
- retry/repair logic
- stop conditions
You want to see:
- whether the model is actually changing its plan
- whether your orchestration code is re-feeding the same state
- whether a tool is returning the same output repeatedly
3) Check for missing stop conditions
Common bug: the agent has no clear “done” criterion.
Add explicit termination rules like:
- max iterations
- max repeated tool calls
- stop if the same action occurs N times
- stop if confidence/goal completion threshold is met
- stop if no new information is gained
Example:
- if the agent calls the same tool with identical args 3 times, break and escalate
- if the agent produces the same plan twice, force a different approach
4) Compare input vs. output state
Look for whether your code is updating state correctly.
Common issues:
- tool result isn’t being appended to memory
- memory is overwritten instead of accumulated
- the agent never sees the new tool output
- conversation history is truncated too aggressively
- a “next step” variable is never updated
A very common loop is:
- model asks for data
- tool returns data
- orchestration forgets to pass that data back
- model asks again
5) Inspect tool outputs
Sometimes the tool itself is the cause:
- it returns empty/identical results
- it fails silently
- it returns malformed JSON
- it always says “not found”
- it has nondeterministic formatting that confuses the parser
Check:
- are tool outputs deterministic?
- are they actually different across calls?
- do they contain enough information for the next step?
6) Watch for parser / schema problems
If the model output is structured, a schema mismatch can cause retries that look like loops.
Examples:
- JSON parse fails, so your system asks the model to retry forever
- function-call arguments are invalid and the agent keeps regenerating them
- the agent outputs a tool call but your executor ignores it
Add logging for:
- validation errors
- retry count
- exact parsed vs raw output
7) Make the agent explain its decision
Temporarily prompt it to include:
- current goal
- what it tried
- what it learned
- why it chose the next step
- why it believes the task is not yet complete
This can reveal whether the model is stuck because:
- it lacks information
- it thinks it already tried something
- it’s misreading the instructions
8) Reduce the problem to a minimal case
Strip the system down:
- one prompt
- one tool
- one loop
- one simple task
Then test:
- does it loop with no tools?
- does it loop only with one specific tool?
- does it loop only on certain inputs?
This helps isolate whether the issue is:
- prompt design
- tool behavior
- orchestration logic
- memory management
9) Add loop detection and recovery
Practical safeguards:
- track last N actions
- detect repeated tool calls or repeated text
- if repeated, inject a “reset” instruction
- ask the model to choose a different strategy
- escalate to a fallback path or human review
Example recovery prompt:
You have repeated the same action without progress. Summarize what changed, what hasn’t changed, and pick a different next step.
10) Look at incentives in the prompt
Sometimes the agent loops because the prompt rewards “being thorough” over “being decisive.”
Fix by adding:
- explicit completion criteria
- “do not repeat the same action”
- “if no progress after 2 attempts, stop and explain”
- “prefer advancing the task over exhaustive checking”
Quick debugging checklist
- Are tool results actually changing?
- Is state passed back into the agent correctly?
- Is there a max-iteration limit?
- Are repeated actions detected?
- Are parse/validation errors causing hidden retries?
- Does the prompt define “done” clearly?
- Can the agent take a different action after failure?
If you want, I can help you debug this more concretely if you paste:
- the agent loop code,
- a short trace of the repeated steps, and
- the prompt/tool definitions.