Skip to content

ReasoningAgentRouter Documentation

The ReasoningAgentRouter is a sophisticated agent routing system that enables dynamic selection and execution of different reasoning strategies based on the task requirements. It provides a flexible interface to work with multiple reasoning approaches including Reasoning Duo, Self-Consistency, and Iterative Reflective Expansion (IRE).

Architecture

graph TD
    Task[Task Input] --> Router[ReasoningAgentRouter]
    Router --> SelectSwarm{Select Swarm Type}
    SelectSwarm -->|Reasoning Duo| RD[ReasoningDuo]
    SelectSwarm -->|Self Consistency| SC[SelfConsistencyAgent]
    SelectSwarm -->|IRE| IRE[IterativeReflectiveExpansion]
    RD --> Output[Task Output]
    SC --> Output
    IRE --> Output

Class: ReasoningAgentRouter

Arguments

Argument Type Default Description
agent_name str "reasoning_agent" Name identifier for the agent
description str "A reasoning agent..." Description of the agent's capabilities
model_name str "gpt-4o-mini" The underlying language model to use
system_prompt str "You are a helpful..." System prompt for the agent
max_loops int 1 Maximum number of reasoning loops
swarm_type agent_types "reasoning_duo" Type of reasoning swarm to use
num_samples int 1 Number of samples for self-consistency
output_type OutputType "dict" Format of the output

Methods

Method Description
select_swarm() Selects and initializes the appropriate reasoning swarm based on specified type
run(task: str) Executes the selected swarm's reasoning process on the given task
batched_run(tasks: List[str]) Executes the reasoning process on a batch of tasks

Swarm Types

  1. ReasoningDuo
  2. Uses two agents working together
  3. One for reasoning, one for execution
  4. Best for tasks requiring both analysis and action

  5. SelfConsistencyAgent

  6. Generates multiple samples
  7. Ensures consistency across reasoning paths
  8. Ideal for tasks requiring high reliability

  9. IterativeReflectiveExpansion (IRE)

  10. Uses iterative refinement
  11. Reflects on and improves reasoning paths
  12. Best for complex problem-solving

Usage Examples

Basic Usage

from swarms.agents.reasoning_agents import ReasoningAgentRouter

# Initialize the router
router = ReasoningAgentRouter(
    agent_name="reasoning-agent",
    description="A reasoning agent that can answer questions and help with tasks.",
    model_name="gpt-4o-mini",
    system_prompt="You are a helpful assistant that can answer questions and help with tasks.",
    max_loops=1,
    swarm_type="self-consistency",
    num_samples=1,
    output_type="list"
)

# Run a single task
result = router.run("What is the best approach to solve this problem?")

Batch Processing

# Process multiple tasks
tasks = [
    "What is the optimal solution for X?",
    "How should we approach problem Y?"
]
results = router.batched_run(tasks)

Using Different Swarm Types

ReasoningDuo

router = ReasoningAgentRouter(
    swarm_type="reasoning-duo",
    model_name="gpt-4o-mini"
)

Self-Consistency

router = ReasoningAgentRouter(
    swarm_type="self-consistency",
    num_samples=3,
    model_name="gpt-4o-mini"
)

IRE

router = ReasoningAgentRouter(
    swarm_type="ire",
    max_loops=5,
    model_name="gpt-4o-mini"
)

Best Practices

  1. Swarm Type Selection
  2. Use ReasoningDuo for tasks requiring both analysis and action
  3. Use SelfConsistency for tasks requiring high reliability
  4. Use IRE for complex problem-solving requiring iterative refinement

  5. Performance Optimization

  6. Adjust max_loops based on task complexity
  7. Increase num_samples for higher reliability
  8. Choose appropriate model_name based on task requirements

  9. Output Handling

  10. Use appropriate output_type for your needs
  11. Process batched results appropriately
  12. Handle errors gracefully

Error Handling

The ReasoningAgentRouter includes built-in error handling for: - Invalid swarm types - Model execution failures - Task processing errors

Limitations

  1. Processing time increases with:
  2. Higher num_samples
  3. Larger max_loops
  4. More complex tasks

  5. Model-specific limitations based on:

  6. Token limits
  7. Model capabilities
  8. API rate limits

Contributing

When extending the ReasoningAgentRouter: 1. Follow the existing swarm interface 2. Add comprehensive tests 3. Update documentation 4. Maintain error handling