Overview
TheMajorityVoting module provides a sophisticated multi-loop consensus building system for agents. Unlike simple majority voting, this system enables iterative consensus building where agents can refine their responses across multiple loops, with each subsequent loop considering the previous consensus. This approach leads to more robust and well-reasoned final decisions by leveraging the collective intelligence of multiple specialized agents.
Installation
Architecture
Key Concepts
- Multi-Loop Consensus Building: An iterative process where agents can refine their responses across multiple loops, with each loop building upon the previous consensus.
- Agents: Specialized entities (e.g., models, algorithms) that provide expert responses to tasks or queries.
- Consensus Agent: An automatically created agent that analyzes and synthesizes responses from all agents to determine the final consensus.
- Conversation History: A comprehensive record of all agent interactions, responses, and consensus building across all loops.
- Concurrent Execution: Agents run simultaneously for improved performance and efficiency.
Attributes
Optional[str]
default:"None"
Unique identifier for the majority voting system. When omitted, one is generated as
majority-voting-<32 hex characters>.str
default:"MajorityVoting"
Name of the majority voting system.
str
default:"A multi-loop majority voting system for agents"
Description of the system.
List[Agent]
required
A list of agents to be used in the majority voting system.
bool
default:"False"
When
True, each run writes the conversation to conversation_history.json in a workspace directory, {WORKSPACE_DIR}/swarms/MajorityVoting/{name}-{timestamp}/. The directory path is available as swarm_workspace_dir.bool
default:"False"
Whether to enable verbose logging.
int
default:"1"
Maximum number of consensus building loops. Must be greater than
0.OutputType
default:"dict"
Output format passed to
history_output_formatter, for example "dict", "list", "str", "json", or "final". "dict" returns the message list, including the task.str
default:"CONSENSUS_AGENT_PROMPT"
System prompt for the consensus agent.
str
default:"Consensus-Agent"
Name for the automatically created consensus agent.
str
Description for the consensus agent.
str
default:"gpt-5.4"
Model name for the consensus agent.
dict
default:"{}"
Additional keyword arguments passed to the consensus agent’s
Agent constructor.The constructor validates the configuration and raises
ValueError if agents is empty or None, or if max_loops is 0 or less.Methods
run()
Executes the multi-loop majority voting system for a single task and returns the consensus result.task(str): The task or question to be analyzed by the agent panelstreaming_callback(Optional[Callable[[str, str, bool], None]]): Optional callback invoked as(agent_name, chunk, is_final)while the consensus agent streams its response. Only the consensus agent’s output is streamed, andis_finalis alwaysFalse.*args,**kwargs: Accepted for compatibility and ignored. They are not forwarded to the agents.
output_type
Process Flow:
- Starts a fresh conversation and adds the task to it, so a reused instance never carries an earlier task’s votes
- For each loop (up to
max_loops):- Runs all voters concurrently. Each voter receives the shared conversation as typed chat turns: the task as a
userturn, its own earlier answers asassistantturns, and peers’ answers and earlier consensus asuserturns labelled with the speaker’s name - Records each voter’s final answer (not its whole transcript) under the voter’s name. A voter that raises is recorded with the exception in place of an answer
- Runs the consensus agent on the same typed history and records its answer
- Runs all voters concurrently. Each voter receives the shared conversation as typed chat turns: the task as a
- Saves the conversation to the workspace when
autosave=True - Returns the conversation in the specified output format
batch_run()
Executes the majority voting system for multiple tasks sequentially. Each task starts from a fresh conversation.tasks(List[str]): List of tasks or questions to be processed*args(Any): Variable length argument list passed to each task execution**kwargs(Any): Arbitrary keyword arguments passed to each task execution
run_concurrently()
Executes the majority voting system for multiple tasks concurrently in a thread pool. Each task runs on its own shallow copy of the instance, so the tasks never share a conversation. The copies share the same voter and consensus agents.tasks(List[str]): List of tasks or questions to be processedmax_workers(int, keyword): Number of threads. Defaults toos.cpu_count()*args,**kwargs: Forwarded to each task’sruncall
tasks[i]
reliability_check()
Performs validation checks on the majority voting system configuration.ValueError: If agents list is empty or NoneValueError: Ifmax_loopsis less than or equal to0
Consensus Agent
The MajorityVoting system automatically creates a specialized consensus agent that analyzes and synthesizes responses from all participating agents. This consensus agent:- Comprehensively evaluates each agent’s response across accuracy, depth of analysis, relevance, clarity, and unique perspectives
- Performs comparative analysis by identifying overlapping themes, divergent viewpoints, and strengths/weaknesses
- Builds consensus by identifying the most effective responses and synthesizing best elements
- Delivers actionable results that are fair, balanced, evidence-based, and well-supported
consensus_agent_prompt: Custom system promptconsensus_agent_name: Name for the agentconsensus_agent_description: Descriptionconsensus_agent_model_name: Model to useadditional_consensus_agent_kwargs: Additional configuration
Usage Examples
Financial Analysis with Specialized Agents
Investment Analysis with Consensus Agent
Content Creation with Batch Processing
Research Analysis with Concurrent Processing
Majority Voting with Custom Streaming
The callback receives the consensus agent’s output as it streams. Voters are not streamed.Usage Patterns
Single Task Analysis
Agent or pass them to the consensus agent through additional_consensus_agent_kwargs. run ignores extra keyword arguments.