CrewAI Agent Stuck in Loop

A CrewAI infinite loop occurs when agents get stuck in a delegation cycle, passing tasks between each other indefinitely. This happens when multiple agents have allow_delegation=True and repeatedly delegate to each other, or when a single agent retries a task without making progress.

CRITICAL Severity

CrewAI Delegation Loop

Vulnerable
python
# Circular delegation between agents
analyst = Agent(role="Analyst", allow_delegation=True)
writer = Agent(role="Writer", allow_delegation=True)
reviewer = Agent(role="Reviewer", allow_delegation=True)
# Analyst -> Writer -> Reviewer -> Analyst (loop!)
Secure
python
# Unidirectional delegation chain
analyst = Agent(role="Analyst", allow_delegation=True, max_iter=5)
writer = Agent(role="Writer", allow_delegation=True, max_iter=5)
reviewer = Agent(role="Reviewer", allow_delegation=False)  # End of chain

Frequently Asked Questions

Why is my CrewAI agent stuck in a loop?

Common causes: (1) Two agents with allow_delegation=True delegating to each other, (2) An agent retrying a task that consistently fails, (3) Missing max_iter parameter (defaults to 25 but may not be enough), (4) Tasks that produce output the next agent can't parse, causing retry cycles.

How do I fix a CrewAI delegation loop?

Set allow_delegation=False on agents that should not delegate. If delegation is needed, use max_iter to bound it: Agent(role="Writer", allow_delegation=True, max_iter=5). Also ensure at least one agent in the chain has delegation disabled to prevent cycles.

What is max_iter in CrewAI?

max_iter controls the maximum number of iterations an agent can perform for a single task. Default is 25. Setting it lower (e.g., 5-10) prevents runaway loops while still giving the agent enough attempts to complete tasks.

How Inkog Detects This

Inkog detects CrewAI delegation cycles by building an agent delegation graph. It identifies mutual delegation patterns (A delegates to B, B delegates back to A), missing max_iter settings, and unbounded retry patterns.

bash
npx -y @inkog-io/cli scan .

Scan CrewAI for Loops

Scan your AI agents for vulnerabilities. Free for developers.

Start Free Scan