How to Fix 'agent infinite loop when scaling' in AutoGen (Python)
When AutoGen starts repeating the same tool calls, the same assistant messages, or the same handoff loop until your process gets killed, you’re usually dealing with a termination problem, not a “random” runtime bug. In practice, this shows up when you scale from one happy-path chat to multi-agent workflows with tools, retries, and longer context windows.
The error often appears as agent infinite loop when scaling, but the real symptoms are usually things like Maximum number of consecutive auto-replies reached, repeated assistant -> tool -> assistant cycles, or a GroupChatManager that never reaches a stopping condition.
The Most Common Cause
The #1 cause is a missing or incorrect termination condition in your agent loop.
In AutoGen, agents will keep responding if nothing tells them to stop. When you scale up, that becomes visible because more messages, more tool outputs, and more agents make it easier to miss the final exit signal.
Broken vs fixed pattern
| Broken pattern | Fixed pattern |
|---|---|
| No explicit stop condition | Terminate on a clear message or max turn limit |
| Tool returns data but never finalizes | Return a final answer after tool execution |
human_input_mode="NEVER" with no guardrails | Add is_termination_msg or bounded turns |
# BROKEN
from autogen import AssistantAgent, UserProxyAgent
assistant = AssistantAgent(
name="assistant",
llm_config={"config_list": [{"model": "gpt-4o-mini"}]},
)
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
code_execution_config={"work_dir": "tmp"},
)
user_proxy.initiate_chat(
assistant,
message="Check the policy document and summarize risks."
)
# FIXED
from autogen import AssistantAgent, UserProxyAgent
assistant = AssistantAgent(
name="assistant",
llm_config={"config_list": [{"model": "gpt-4o-mini"}]},
is_termination_msg=lambda msg: "TERMINATE" in msg.get("content", ""),
)
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
code_execution_config={"work_dir": "tmp"},
)
user_proxy.initiate_chat(
assistant,
message="Check the policy document and summarize risks. End with TERMINATE."
)
If you’re using GroupChat, do the same thing at the group level. A missing speaker transition rule or no terminating speaker is enough to create an endless cycle.
Other Possible Causes
1) Tool output keeps triggering the same tool call
If your agent sees its own tool output and interprets it as a new instruction, it can call the same function forever.
# BAD: tool output looks like an instruction
def lookup_claim(claim_id: str):
return f"Call lookup_claim({claim_id}) again if needed"
# GOOD: return neutral structured data
def lookup_claim(claim_id: str):
return {"claim_id": claim_id, "status": "approved", "next_step": "finalize"}
2) max_consecutive_auto_reply is too high or unset
AutoGen will keep auto-replying until it hits the limit. In scaled flows, that limit can be so high that it looks infinite.
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=1000,
)
Use something sane while debugging:
user_proxy = UserProxyAgent(
name="user_proxy",
human_input_mode="NEVER",
max_consecutive_auto_reply=5,
)
3) Group chat speaker selection is bouncing between agents
With GroupChatManager, two agents can keep handing off to each other if your speaker selection logic is too permissive.
from autogen import GroupChat, GroupChatManager
groupchat = GroupChat(
agents=[planner, executor],
messages=[],
max_round=50,
)
manager = GroupChatManager(groupchat=groupchat)
Fix by constraining transitions:
groupchat = GroupChat(
agents=[planner, executor],
messages=[],
max_round=10,
speaker_selection_method="round_robin",
)
If you need custom routing, make sure it can return None or a terminal agent when work is done.
4) The model keeps rephrasing instead of concluding
This happens when your system prompt asks for “analysis” but never asks for a final answer format. The model stays in planning mode forever.
system_message = """
You are an assistant that analyzes claims.
"""
Make the output contract explicit:
system_message = """
You are an assistant that analyzes claims.
Return:
- findings
- decision
- TERMINATE at the end
"""
How to Debug It
- •
Print every message in the conversation
- •Look for repeated content patterns.
- •If you see identical assistant/tool exchanges three times in a row, you have a loop trigger.
- •
Lower turn limits immediately
- •Set
max_consecutive_auto_reply=3andmax_round=6. - •This turns an infinite loop into a short reproducible trace.
- •Set
- •
Inspect termination logic
- •Check
is_termination_msg. - •Check whether your prompts ever produce the stop token.
- •For
GroupChat, verify there is a valid end state.
- •Check
- •
Disable tools one by one
- •Start with plain chat.
- •Then add retrieval.
- •Then add code execution.
- •Then add external APIs.
The first component that reintroduces repetition is usually the culprit.
Here’s a minimal logging wrapper that helps:
def log_message(msg):
print(f"[{msg.get('role')}] {msg.get('name', '')}: {msg.get('content', '')[:300]}")
# Use this around your chat loop or callback hooks if you have them enabled.
Prevention
- •
Always define a termination contract:
- •explicit stop token like
TERMINATE - •or strict max rounds / auto-reply caps
- •explicit stop token like
- •
Keep tool outputs machine-friendly:
- •JSON objects
- •no instructions inside returned text
- •no echoing user prompts back into the next turn
- •
Test with small limits before scaling:
- •one agent pair first
- •then add tools
- •then add group chat routing
If you’re seeing Maximum number of consecutive auto-replies reached in AutoGen Python, treat it as a signal that your conversation graph has no clean exit. Fix the termination path first; most “infinite loop when scaling” bugs disappear once the agent knows exactly when to stop.
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
By Cyprian Aarons, AI Consultant at Topiax.
Want the complete 8-step roadmap?
Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.
Get the Starter Kit