Overview
RoundRobinSwarm visits agents in their declared insertion order, cycling through the full roster once per loop. The schedule is deterministic and identical on every loop:
max_loops turns. Before each turn the swarm injects a role header telling the agent its position, the current loop, the previous/next speaker, and the other participants, then asks it to build on the prior contribution.
Each run starts from an empty conversation, so a reused swarm never carries an earlier task’s transcript.
Earlier versions of
RoundRobinSwarm shuffled agents randomly each loop and supported callback / max_retries parameters. Those are removed — the order is now strictly deterministic and those parameters no longer exist.Installation
Import
Constructor
str
default:"RoundRobinSwarm"
Name of the swarm. Also used to name the internal conversation.
str
Description of the swarm’s purpose.
List[Agent]
required
Agents that take turns in declared order. Required — constructing the swarm without agents raises
ValueError.bool
default:"False"
Enable verbose logging of each turn and loop.
int
default:"1"
Number of full passes over the roster. Each agent speaks exactly once per loop, so with
N agents and max_loops loops the swarm runs N * max_loops turns total.OutputType
default:"final"
Output format applied to the conversation history. Common values:
"final" (last message only), "list", "dict", "str", "json". The conversation records timestamps and message IDs, so "str" lines carry a timestamp and "dict" entries carry extra fields.Methods
run(task, *args, **kwargs)
Execute the task across the agents in deterministic round-robin order. Returns the conversation in the format specified by output_type.
str
required
The task to execute. The conversation is cleared, then the task is posted as the opening
User message that the first agent responds to.*args / **kwargs are forwarded to each underlying agent.run() call. The swarm passes messages= itself, so do not pass that keyword.
Returns: the task result formatted per output_type.
Raises: re-raises any exception thrown by an agent during execution.
run_batch(tasks)
Execute multiple tasks sequentially. Each task runs through its own full round-robin cycle, starting from an empty conversation.
List[str]
required
Tasks to execute, one full round-robin run per task.
tasks.
Usage Examples
Basic round-robin execution
Multiple loops
Different output types
Batch processing
How It Works
- Opening message — the conversation is cleared and the user task is added as the first
Usermessage. - Deterministic schedule — for
max_loopsloops, the swarm iteratesagentsin insertion order: turntgoes toagents[t % N]. - Full context as typed turns — before each turn, the whole conversation is passed to the agent as chat messages: the task as a
userturn, the agent’s own earlier replies asassistantturns, and every other agent’s replies asuserturns prefixed with the speaker’s name. - Role header — a per-turn header (see below) tells the agent its position, loop, previous/next speaker, and peers.
- Collaborative reply — the agent is asked to build on the prior speaker’s contribution (or address the task directly if it opens). Its final answer, not its whole transcript, is appended to the conversation under its name.
- Formatted output — after all loops complete, the conversation is returned per
output_type.
Per-turn prompt
Each agent receives a generated header and a standing instruction, produced bybuild_turn_header and build_collaborative_task. The running transcript is not concatenated into this prompt string — run() calls build_collaborative_task(conversation_context="", turn_header=turn_header), so the prior turns are delivered separately as typed chat messages via the messages argument of agent.run(), not as literal text in the task prompt:
Features
- Deterministic order — agents always speak in declared insertion order, identically every loop.
- Full context — each agent sees the complete conversation so far, with its own replies kept distinct from its peers’.
- Collaborative prompting — agents are told their position and neighbors and asked to build on prior turns.
- Flexible output — choose
"final","list","dict","str", or"json". - Batch processing —
run_batchruns many tasks, one full cycle each. - Serializable — inherits
SerializableMixinfor config serialization.
Best Practices
- Order matters — place agents in the sequence you want them to speak; the opener sets the framing for everyone after.
- Agent diversity — use complementary roles so each turn adds a distinct perspective.
- Loop count — start with
max_loops=1; increase only when deeper back-and-forth is needed (cost scales withN * max_loops). - Output type — use
"final"for a single answer,"list"/"dict"to inspect the whole collaboration. - Verbose mode — enable
verbose=Truewhile debugging to trace each turn.