AgentRearrange Example¶
Overview
Learn how to create flexible multi-agent workflows using AgentRearrange. Define custom flow patterns with sequential execution (->) and concurrent execution (,) to orchestrate agents in sophisticated workflows.
Prerequisites¶
Before You Begin
Make sure you have: - Python 3.7+ installed - A valid API key for your model provider - The Swarms package installed
Installation¶
Environment Setup¶
Code Implementation¶
Import Required Modules¶
Configure Agents¶
Agent Configuration
Here's how to set up your specialized agents:
# Research Agent
researcher = Agent(
agent_name="Researcher",
system_prompt="You are a research specialist. Gather information, analyze data, and provide comprehensive findings.",
model_name="gpt-4o-mini",
max_loops=1,
)
# Writer Agent
writer = Agent(
agent_name="Writer",
system_prompt="You are a professional writer. Create clear and engaging content based on research findings.",
model_name="gpt-4o-mini",
max_loops=1,
)
# Editor Agent
editor = Agent(
agent_name="Editor",
system_prompt="You are an expert editor. Review content for clarity, accuracy, and style.",
model_name="gpt-4o-mini",
max_loops=1,
)
Initialize AgentRearrange¶
Workflow Setup
Configure AgentRearrange with your agents and flow pattern:
Run the Workflow¶
Execute the Workflow
Start the workflow:
Complete Example¶
Full Implementation
Here's the complete code combined:
from swarms import Agent, AgentRearrange
# Create agents
researcher = Agent(
agent_name="Researcher",
system_prompt="You are a research specialist. Gather information, analyze data, and provide comprehensive findings.",
model_name="gpt-4o-mini",
max_loops=1,
)
writer = Agent(
agent_name="Writer",
system_prompt="You are a professional writer. Create clear and engaging content based on research findings.",
model_name="gpt-4o-mini",
max_loops=1,
)
editor = Agent(
agent_name="Editor",
system_prompt="You are an expert editor. Review content for clarity, accuracy, and style.",
model_name="gpt-4o-mini",
max_loops=1,
)
# Define flow pattern
flow = "Researcher -> Writer -> Editor"
# Create workflow
workflow = AgentRearrange(
name="content-creation-workflow",
agents=[researcher, writer, editor],
flow=flow,
max_loops=1,
)
# Execute workflow
result = workflow.run(
"Research and write a comprehensive article about the impact of AI on healthcare"
)
print(result)
Flow Pattern Examples¶
Flow Pattern Syntax
- Sequential:
"Agent1 -> Agent2 -> Agent3"- Agents run one after another - Parallel:
"Agent1, Agent2 -> Agent3"- Agent1 and Agent2 run simultaneously, then Agent3 - Mixed:
"Agent1 -> Agent2, Agent3 -> Agent4"- Combine sequential and parallel execution
Configuration Options¶
Key Parameters
| Parameter | Description | Default |
|---|---|---|
agents |
List of Agent objects | Required |
flow |
Flow pattern string defining execution order | Required |
max_loops |
Maximum number of execution loops | 1 |
team_awareness |
Enable sequential awareness for agents | False |
Next Steps¶
What to Try Next
- Experiment with parallel execution:
"Agent1, Agent2 -> Agent3" - Enable
team_awareness=Truefor better agent coordination - Try more complex flows combining sequential and parallel patterns
- Use SwarmRouter with
swarm_type="AgentRearrange"for unified interface
Troubleshooting¶
Common Issues
- Ensure agent names in flow match
agent_nameexactly - Check for typos in agent names
- Verify all agents in flow are included in agents list
- Enable verbose mode for debugging:
verbose=True