Skip to content

Conversation

@Steve-Dusty
Copy link
Contributor

@Steve-Dusty Steve-Dusty commented Dec 22, 2025

Fix: IRE Agent Infinite Iteration and Performance Issues

Problem

The Iterative Reflective Expansion (IRE) agent had critical issues causing it to run indefinitely or excessively, especially with complex prompts or higher max_iterations values:

  1. No early termination - Always ran full iterations even when excellent solutions were found
  2. Score parsing failures - LLM responses with markdown formatting (**Score**: 0.9) failed to parse, defaulting to 0.0, triggering unnecessary revisions
  3. Exponential path growth - Paths multiplied uncontrollably (5 → 15 → 45 → ...)
  4. No path limits - Could accumulate hundreds of paths per iteration
  5. Excessive LLM calls - Up to 150+ calls for simple tasks
  6. Poor visibility - No progress logging to understand what was happening

Solution

Implemented comprehensive fixes to control iteration behavior while maintaining reasoning quality:

1. Robust Score Extraction (_extract_score_robust)

  • Handles markdown formatting in LLM responses
  • Multiple fallback strategies: regex patterns, sentiment analysis
  • Default score of 0.5 (instead of 0.0) prevents unnecessary revisions
  • Clamps scores to valid 0.0-1.0 range

2. Hard Path Limits

  • MAX_PATHS_PER_ITERATION = 5 enforced throughout
  • Initial hypotheses limited to 5 paths
  • Revisions per path capped at 3
  • Path selection enforces hard limits with fallback

3. Early Termination

  • Score ≥ 0.9: Immediate termination
  • Score ≥ 0.85: Marked as high-quality
  • 2+ high-quality paths after iteration 1: Stop iterating
  • Prevents wasted computation on already-good solutions

4. Comprehensive Progress Logging

  • Clear iteration headers and summaries
  • Per-path simulation status
  • Score tracking (iteration best & overall best)
  • High-quality path counts
  • Helps debug and understand reasoning progress

5. Improved Path Selection

  • Truncates long paths for LLM display (prevents context overflow)
  • Falls back to first N paths if LLM selection fails
  • Always returns exactly MAX_PATHS_PER_ITERATION paths

6. Better Extraction Parsing

  • Handles both plain text and markdown formatting
  • Case-insensitive matching
  • Robust error handling

Changes

Modified Files:

  • swarms/agents/i_agent.py - Core IRE agent implementation

Key Changes:

  • Added configuration constants (lines 40-44)
  • New _extract_score_robust() method (lines 87-142)
  • Updated simulate_path() with better parsing (lines 170-216)
  • Improved select_promising_paths() with hard limits (lines 263-310)
  • Rewrote run() method with termination logic (lines 339-450)

Configuration

Tunable constants in swarms/agents/i_agent.py:

MAX_PATHS_PER_ITERATION = 5      # Max paths to keep per iteration
SCORE_THRESHOLD = 0.7             # Revise paths below this score
EARLY_TERMINATION_SCORE = 0.85   # High-quality threshold
DEFAULT_SCORE = 0.5               # Fallback when parsing fails

Testing

Before Fix:

- Simple prompt with max_iterations=5: Could run indefinitely or take minutes
- Complex prompts: Never terminated, exponential path growth

After Fix:

from swarms.agents.reasoning_agents import ReasoningAgentRouter

# Test 1: Simple prompt (triggers early termination)
router = ReasoningAgentRouter(swarm_type="ire", model_name="gpt-4o-mini", num_samples=3)
result = router.run("Explain photosynthesis in one sentence.")
# Expected: Terminates after 1 iteration with score 0.9+

# Test 2: Complex prompt (runs full iterations with control)
complex_prompt = """
Design a DDoS prevention algorithm for cloud infrastructure handling 1M requests/sec...
"""
result = router.run(complex_prompt)
# Expected: Runs 3 iterations, max 5 paths each, completes in reasonable time

Run: python ire.py

Performance Impact

Before:
- Simple tasks: 50-150+ LLM calls
- Runtime: Minutes or indefinite
- Path count: Uncontrolled growth

After:
- Simple tasks: 1-5 LLM calls (early termination)
- Complex tasks: 15-45 LLM calls (3 iterations × 5 paths)
- Runtime: Seconds to complete
- Path count: Hard-limited to 5 per iteration

Breaking Changes

None. All changes are internal improvements. The API remains unchanged.

Related Issues

Fixes issues where IRE agent:
- Never terminates with max_iterations > 1
- Runs excessively with complex prompts
- Provides no visibility into reasoning progress

<!-- readthedocs-preview swarms start -->
----
📚 Documentation preview 📚: https://swarms--1265.org.readthedocs.build/en/1265/

<!-- readthedocs-preview swarms end -->

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant