Overview
TheMixtureOfAgents class manages and executes multiple agents in parallel layers, aggregating their responses through a specialized aggregator agent. This architecture enables sophisticated multi-perspective analysis by running agents concurrently and synthesizing their outputs.
The aggregator reads every worker answer from every layer as a separate, labelled chat turn. With more than one layer, each label carries the layer, for example Analyst (layer 2/3), so the aggregator can tell a refinement from a first pass.
Class Definition
Parameters
Unique identifier for the mixture of agents instance. Auto-generated via
generate_id("mixture-of-agents") if not provided, producing mixture-of-agents-<32 hex chars>.A list of reference agents to be used in the mixture. These agents will be executed in parallel layers. An empty list raises
ValueErrorThe aggregator agent to be used for synthesizing responses from all agents. If None, a default aggregator agent will be created
The system prompt for the aggregator agent that guides how responses are synthesized
The number of processing layers to execute. Each layer runs all agents and passes context forward
Stored for compatibility with other swarm classes. It does not change how many times the workers or the aggregator run; use
layers for more worker roundsOutput format type.
"final" returns the aggregator’s answer; "all", "list", "dict" and the other OutputType values return the whole conversation in that formatCap on concurrent worker agents per layer. When omitted, the pool uses 95% of
the available CPU cores.
A comms channel the mixture posts to: the task as
User, each worker’s answer per layer under a name like Analyst (layer 1/3), then the aggregator’s answer. Any channel set on a member agent is detached with a warning, so each message is posted once. See Comms Channels.Methods
reliability_check()
ValueError: If no agents are providedValueError: If no aggregator system prompt is providedValueError: If no layers are specified
step()
run() records the results.
Parameters:
Returns:
Dictionary mapping agent names to their final answers. Each value is the agent’s last message, not its whole transcript
run()
The conversation formatted by
output_type. With the default "final" this is the aggregator’s answer as a string- Whatever the aggregator raised. A failed aggregation is not returned as an answer, so wrap the call in a
try/exceptif you need to recover. A failing worker does not raise: its exception is recorded as that worker’s answer and the run continues
- Starts a fresh conversation and resets every worker agent’s memory, so a reused instance does not carry the previous task into the next one, then adds the task
- For each layer:
- Runs all agents concurrently. The first layer receives the task; later layers receive the original task plus the previous layer’s answers, each prefixed with the agent’s name, not the full transcript
- Adds each agent’s answer to the conversation. When
layers > 1the speaker is labelled with the layer, for exampleResearch-Agent (layer 1/3)
- Runs the aggregator on the conversation, passed as chat turns: the task, then every labelled worker answer. The team roster is left out
- Adds the aggregator’s answer to the conversation and returns the conversation formatted by
output_type
run_batched()
run_concurrently()
arun()
run() on the event loop, awaiting each agent’s arun. Layers run in order, since each needs the previous layer’s answers.
Parameters:
Most workers running at once in a layer. Defaults to
max_workers, or every worker when that is unset. A value below 1 raises ValueErrorA failing worker’s error is recorded as that worker’s answer, in the form
"Error: {e}", and the other workers carry on. A failing aggregator raises.Agent.arun still calls Agent.run through asyncio.to_thread, so each running worker holds a thread from asyncio’s default executor.Usage Example
Architecture
The MixtureOfAgents architecture works as follows:- Layer Processing: Each layer runs all agents concurrently. The first layer sees only the task; each later layer sees the task plus the previous layer’s answers
- Context Accumulation: Agent answers are added to the conversation after each layer, labelled with the agent and, when
layers > 1, the layer - Iterative Refinement: Subsequent layers build upon previous outputs, enabling deeper analysis
- Final Aggregation: The aggregator reads every labelled answer from every layer as chat turns and synthesizes them into one final output