MajorityVoting sends one task to a panel of agents at the same time. Each agent answers on its own. A consensus agent, which the swarm builds for you, then reads every answer, compares them, ranks them and writes the final answer. With more than one loop, the voters see the last consensus and can change their minds before the consensus agent decides again.
The swarm does not count votes in code. The consensus agent reads the answers and decides, so you control how it judges through its prompt.
Reference: MajorityVoting.
1. Run a vote
Give each voter its ownAgent and a unique agent_name. Votes are stored by name, so the constructor raises ValueError if two voters share one. It also raises ValueError for an empty agents list or a max_loops below 1.
The consensus agent runs on gpt-5.4 unless you set consensus_agent_model_name.
2. Read the output
run returns the vote’s conversation, formatted by output_type. The default is "dict", a list of {"role", "content"} messages in this order:
With more loops, the voter and consensus messages repeat once per loop. The role of the consensus message is
consensus_agent_name, which defaults to Consensus-Agent.
Other useful values:
"final"returns only the last consensus answer, as a string."str"returns the whole conversation as one string, onerole: contentblock per message."list"and"json"give the same messages as a list or a JSON string.
run starts a fresh conversation, so reusing a MajorityVoting never mixes two tasks.
3. Let the panel revise its answers
Setmax_loops above 1 to run more rounds. In each later round, every voter sees the task, its own earlier answers, the other voters’ answers and the last consensus, each labelled with the speaker’s name. The consensus agent then decides again. Here output_type="final" returns only the last decision.
max_loops * (len(agents) + 1) agent calls: every voter plus the consensus agent, once per loop.
4. Change how the consensus is reached
The default consensus prompt asks for an evaluation of each answer, a comparison, a ranked list and a final recommendation. For a strict tally, give the consensus agent your own prompt. The constructor also takesconsensus_agent_name, consensus_agent_description and additional_consensus_agent_kwargs, which go to the consensus agent’s Agent constructor.
Pass streaming_callback to run to stream the consensus agent’s answer as it is written. The callback receives (agent_name, chunk, is_final). Only the consensus agent streams; the voters do not, and is_final is always False.
Chunks that are only whitespace, such as a lone space or line break, are not passed to the callback, so a printed stream can miss some spaces and line breaks. The answer that
run returns is complete.5. Vote on an image
run takes img, which is sent to every voter with the task. Use a model that accepts images. The consensus agent reads only the voters’ answers, not the image.
6. Many tasks, async runs and token usage
batch_run(tasks) runs one vote per task, one after another, and returns a list of results in task order. arun is the async form of run: it awaits each voter’s arun on the event loop, and max_concurrency caps how many voters call the model at once. It takes no streaming_callback or img.
usage sums the provider token counts of every voter and the consensus agent, across every run.
usage is a dict with input_tokens, output_tokens, cached_tokens, reasoning_tokens and total_tokens. See Token usage.
Tips
- Short, structured answers vote best. Ask voters to start with a label such as
YES,NOor a category, and tell the consensus agent to pick the most common one. - Mix models or prompts. Voters with the same model and prompt tend to give the same answer. Different system prompts or models make the vote worth more.
- Run one task at a time per panel. Voters keep each run’s turns in their own memory, so don’t run two tasks on the same agents at once. For parallel tasks, build one
MajorityVotingwith its own agents per task. - Save the transcript. With
autosave=True, each run writes the conversation to a workspace folder, andvote.swarm_workspace_dirholds its path.
Next steps
- MajorityVoting reference
- CouncilAsAJudge guide, to grade one answer on several dimensions
- LLMCouncil guide, where members rank each other’s answers
- Runnable scripts