Skip to main content
AgentRearrange runs agents in the order a flow string gives. An arrow -> separates steps that run one after another. A comma , puts several agents in the same step, where they run at the same time. Every agent reads one shared conversation: the task, then each earlier agent’s answer under its name. When the flow ends, run() returns that conversation in the format output_type names. Reference: AgentRearrange.

1. A sequential pipeline

Name each agent, then refer to it by agent_name in the flow. Here a researcher, a writer and an editor run one after another, and each one builds on the answers before it.
The default output_type="all" returns the whole conversation as text, one timestamped line per message:
Each run() starts a fresh conversation, so reusing the same AgentRearrange for a new task does not carry the old transcript.

2. Run agents in parallel with a comma

Agents in the same step run at the same time and see the same conversation. The next step sees all of their answers. Call explain() to print the plan without calling any model.
explain() prints:
With output_type="final", run() returns only the Judge’s answer as a string.
Steps run by name, so two different agents with the same agent_name raise ValueError when you build the AgentRearrange. The same agent can still appear in more than one step.

3. Read the output

output_type decides what run() returns. With "list", you get one dict per message, with role and content, in the order the messages were added:
The values you will use most: Any other value raises ValueError when you build the AgentRearrange. After a run, review.conversation still holds the full conversation as a Conversation.

4. Tell agents where they sit in the flow

team_awareness=True adds the flow structure to the start of the conversation as a system message, and tells each agent in a single-agent step which agents come before and after it. collab_prompt is a system message given to every agent but kept out of the shared conversation. This block reuses the agents from section 1:
The two helper methods show what the agents are told:

5. Run many tasks with batch_run

batch_run() sends each task through the whole flow and returns the results in the same order as the tasks. Each task runs on its own copy of the swarm, with copied agents and a fresh conversation, so tasks never share history. batch_size caps how many tasks run at once.
concurrent_run(tasks, max_workers=None) does the same with a thread limit you choose; by default every task gets its own thread.

6. Change the flow between runs

set_custom_flow() replaces the flow and checks it right away. A flow that names an unknown agent raises ValueError, and the old flow stays in place. steps shows the parsed flow, one list of names per step.
run() also checks the flow before any agent runs, so a bad flow fails fast instead of after the first steps have spent tokens.
run() accepts a custom_tasks keyword that is meant to splice an extra step in front of an agent. On the current release it only works for the first agent in a flow written without spaces around ->, and its value is read as an agent name, not as task text. Use set_custom_flow() to change which agents run.

7. Stream tokens as agents write

run_stream() yields output while each agent is still writing. With with_events=True it yields dicts: agent_start, then one token event per chunk, then agent_end with the agent’s whole output. Agents in a parallel step stream at the same time, so their tokens interleave.
Without with_events, each item is an (agent_name, token) tuple. Streaming runs the flow once, whatever max_loops is. Inside async code, use async for event in streaming.arun_stream(...).

Tips

  • Match names exactly. The flow refers to agents by agent_name, and spaces around -> and , are ignored.
  • Check before you spend. explain() and validate_flow() check the flow without calling a model.
  • Use max_loops for rounds. max_loops=2 runs the whole flow twice on the same conversation, so the second pass sees the first.
  • Nest whole swarms. A ConcurrentWorkflow, SequentialWorkflow or another AgentRearrange with a name can be a step of the flow. See Nested Swarms.

Next steps