Skip to main content
Doom loop detection is built into the Agent class to prevent agents from getting stuck in infinite loops, repeated failures, or no-progress states.

Overview

When you enable autonomy on an Agent, doom loop detection is automatically included. The agent monitors action history for patterns that indicate stuck states:
  • Repeated identical actions
  • Repeated similar actions
  • Consecutive failures
  • No meaningful progress

Quick Start

from praisonaiagents import Agent

# Create agent with autonomy (includes doom loop detection)
agent = Agent(
    instructions="You are a coding assistant.",
    autonomy=True  # Doom loop detection enabled by default
)

# Or with custom threshold
agent = Agent(
    instructions="You are a coding assistant.",
    autonomy={"doom_loop_threshold": 5}  # Trigger after 5 repeated actions
)

# The agent automatically tracks actions during execution
# If a doom loop is detected, the agent will:
# - Break the loop
# - Report the issue
# - Suggest a different approach

How It Works

When autonomy is enabled, the agent tracks every action it takes. If the same action is repeated too many times (default: 3), the agent detects a doom loop and takes corrective action.
from praisonaiagents import Agent

agent = Agent(
    instructions="You are a coding assistant.",
    autonomy={"doom_loop_threshold": 3}
)

# Simulate repeated actions (this happens automatically during agent execution)
for i in range(4):
    agent._record_action("read_file", {"path": "config.py"}, "content", True)
    
    if agent._is_doom_loop():
        print(f"Doom loop detected after {i+1} actions!")
        agent._reset_doom_loop()  # Reset for next task
        break

Configuration Options

Configure doom loop detection through the autonomy parameter:
from praisonaiagents import Agent

agent = Agent(
    instructions="You are a coding assistant.",
    autonomy={
        "doom_loop_threshold": 3,    # Actions before loop detection (default: 3)
        "max_iterations": 20,        # Max total iterations (default: 20)
        "auto_escalate": True        # Auto-escalate on complexity (default: True)
    }
)

Manual Checking

You can manually check for doom loops during custom execution flows:
from praisonaiagents import Agent

agent = Agent(instructions="Assistant", autonomy=True)

# During your custom execution loop
while not done:
    # Record each action
    agent._record_action(action_type, args, result, success)
    
    # Check for doom loop
    if agent._is_doom_loop():
        print("Doom loop detected! Breaking out...")
        break
    
    # Continue execution...

# Reset for next task
agent._reset_doom_loop()

Best Practices

  1. Use Agent(autonomy=True) - Doom loop detection is automatic
  2. Adjust threshold - Set doom_loop_threshold based on your use case
  3. Reset between tasks - Call _reset_doom_loop() when starting new tasks
  4. Let the agent handle it - The agent automatically breaks loops during normal execution