The `RoundRobinSwarm` class is designed to manage and execute tasks among multiple agents in a round-robin fashion. This approach ensures that each agent in a swarm receives an equal opportunity to execute tasks, which promotes fairness and efficiency in distributed systems. It is particularly useful in environments where collaborative, sequential task execution is needed among various agents.
This swarm implements an AutoGen-style communication pattern where agents are shuffled randomly each loop for varied interaction patterns. Each agent receives the full conversation context to build upon others' responses.
## What is Round-Robin?
Round-robin is a scheduling technique commonly used in computing for managing processes in shared systems. It involves assigning a fixed time slot to each process and cycling through all processes in a circular order without prioritization. In the context of swarms of agents, this method ensures equitable distribution of tasks and resource usage among all agents.
@ -10,12 +12,33 @@ Round-robin is a scheduling technique commonly used in computing for managing pr
In swarms, `RoundRobinSwarm` utilizes the round-robin scheduling to manage tasks among agents like software components, autonomous robots, or virtual entities. This strategy is beneficial where tasks are interdependent or require sequential processing.
## Architecture
```mermaid
graph LR
User[Task] --> A1[Agent 1]
A1 --> A2[Agent 2]
A2 --> A3[Agent 3]
A3 --> A1
A3 --> Output[Result]
```
Each agent receives the task with full conversation history, responds, then passes context to the next agent. This cycle repeats for `max_loops` iterations.
## Class Attributes
- `agents (List[Agent])`: List of agents participating in the swarm.
- `verbose (bool)`: Enables or disables detailed logging of swarm operations.
- `max_loops (int)`: Limits the number of times the swarm cycles through all agents.
- `index (int)`: Maintains the current position in the agent list to ensure round-robin execution.
| Attribute | Type | Description |
|-----------|------|-------------|
| `name` | `str` | Name of the swarm. |
| `description` | `str` | Description of the swarm's purpose. |
| `agents` | `List[Agent]` | List of agents participating in the swarm. |
| `verbose` | `bool` | Enables or disables detailed logging of swarm operations. |
| `max_loops` | `int` | Limits the number of times the swarm cycles through all agents. |
| `callback` | `callable` | Callback function executed after each loop. |
| `index` | `int` | Maintains the current position in the agent list to ensure round-robin execution. |
| `max_retries` | `int` | Maximum number of retries for agent execution. |
| `output_type` | `OutputType` | Type of output format (e.g., "final", "all", "json"). |
| `conversation` | `Conversation` | Conversation history for the swarm. |
## Methods
@ -24,30 +47,92 @@ In swarms, `RoundRobinSwarm` utilizes the round-robin scheduling to manage tasks
Initializes the swarm with the provided list of agents, verbosity setting, and operational parameters.
| agents | List[Agent], optional | List of agents in the swarm. |
| verbose | bool | Boolean flag for detailed logging. |
| max_loops | int | Maximum number of execution cycles. |
| callback | Callable, optional | Function called after each loop. |
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `name` | `str` | `"RoundRobinSwarm"` | Name of the swarm. |
| `description` | `str` | `"A swarm implementation..."` | Description of the swarm's purpose. |
| `agents` | `List[Agent]` | **Required** | List of agents in the swarm. |
| `verbose` | `bool` | `False` | Boolean flag for detailed logging. |
| `max_loops` | `int` | `1` | Maximum number of execution cycles. |
| `callback` | `callable` | `None` | Function called after each loop with `(loop_index, result)` arguments. |
| `max_retries` | `int` | `3` | Maximum number of retries for agent execution. |
| `output_type` | `OutputType` | `"final"` | Type of output format. |
**Raises:**
- `ValueError`: If no agents are provided during initialization.
---
### `run`
Executes a specified task across all agents in a round-robin manner, cycling through each agent repeatedly for the number of specified loops.
Executes a specified task across all agents in a randomized round-robin manner, cycling through each agent repeatedly for the number of specified loops.
| `tasks` | `List[str]` | A list of task strings to be executed. |
**Returns:**
| Type | Description |
|------|-------------|
| `List[Union[str, dict, list]]` | A list of results, one for each task, in the format specified by `output_type`. |
## Examples
In this example, `RoundRobinSwarm` is used to distribute network requests evenly among a group of servers. This is common in scenarios where load balancing is crucial for maintaining system responsiveness and scalability.
### Basic Usage with `run`
In this example, `RoundRobinSwarm` is used to distribute a sales task among a group of specialized agents. Each agent contributes their unique perspective to the collaborative output.
task = "Generate a sales email for an accountant firm executive to sell swarms of agents to automate their accounting processes."
out = sales_swarm.run(task)
print(out)
# Run the task
result = sales_swarm.run(task)
print(result)
```
### Batch Processing with `run_batch`
Use `run_batch` when you need to process multiple independent tasks through the swarm. Each task is executed separately with full round-robin collaboration.
```python
from swarms import Agent, RoundRobinSwarm
# Define research agents
researcher1 = Agent(
agent_name="Technical Researcher",
system_prompt="You are a technical researcher who analyzes topics from a technical perspective.",
model_name="gpt-4.1",
max_loops=1,
)
researcher2 = Agent(
agent_name="Market Researcher",
system_prompt="You are a market researcher who analyzes topics from a business and market perspective.",
model_name="gpt-4.1",
max_loops=1,
)
# Initialize the swarm
research_swarm = RoundRobinSwarm(
name="ResearchSwarm",
agents=[researcher1, researcher2],
verbose=True,
max_loops=1,
output_type="json",
)
# Define multiple research tasks
tasks = [
"Analyze the current state of AI in healthcare.",
"Research the impact of automation on manufacturing.",
"Evaluate emerging trends in renewable energy.",
]
# Run all tasks and get results
results = research_swarm.run_batch(tasks)
# Process each result
for i, result in enumerate(results):
print(f"Task {i + 1} Result:")
print(result)
print("-" * 50)
```
### Using Callbacks
You can use callbacks to monitor or process intermediate results after each loop:
```python
def my_callback(loop_index: int, result: str):
"""Called after each loop completes."""
print(f"Loop {loop_index + 1} completed")
print(f"Latest result: {result[:100]}...") # Print first 100 chars
swarm = RoundRobinSwarm(
agents=[agent1, agent2, agent3],
max_loops=3,
callback=my_callback,
)
result = swarm.run("Analyze this complex topic from multiple perspectives.")