Skip to main content

Overview

The MajorityVoting 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.
Parameters:
  • task (str): The task or question to be analyzed by the agent panel
  • streaming_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, and is_final is always False.
  • *args, **kwargs: Accepted for compatibility and ignored. They are not forwarded to the agents.
Returns: The conversation formatted according to output_type Process Flow:
  1. Starts a fresh conversation and adds the task to it, so a reused instance never carries an earlier task’s votes
  2. 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 user turn, its own earlier answers as assistant turns, and peers’ answers and earlier consensus as user turns 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
  3. Saves the conversation to the workspace when autosave=True
  4. 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.
Parameters:
  • 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
Returns: List of consensus results, one for each input task

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.
Parameters:
  • tasks (List[str]): List of tasks or questions to be processed
  • max_workers (int, keyword): Number of threads. Defaults to os.cpu_count()
  • *args, **kwargs: Forwarded to each task’s run call
Returns: List of consensus results in input order, so element i is the vote for tasks[i]

reliability_check()

Performs validation checks on the majority voting system configuration.
Raises:
  • ValueError: If agents list is empty or None
  • ValueError: If max_loops is less than or equal to 0

Consensus Agent

The MajorityVoting system automatically creates a specialized consensus agent that analyzes and synthesizes responses from all participating agents. This consensus agent:
  1. Comprehensively evaluates each agent’s response across accuracy, depth of analysis, relevance, clarity, and unique perspectives
  2. Performs comparative analysis by identifying overlapping themes, divergent viewpoints, and strengths/weaknesses
  3. Builds consensus by identifying the most effective responses and synthesizing best elements
  4. Delivers actionable results that are fair, balanced, evidence-based, and well-supported
The consensus agent can be customized through the constructor parameters:
  • consensus_agent_prompt: Custom system prompt
  • consensus_agent_name: Name for the agent
  • consensus_agent_description: Description
  • consensus_agent_model_name: Model to use
  • additional_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

To change sampling settings, set them on each voter Agent or pass them to the consensus agent through additional_consensus_agent_kwargs. run ignores extra keyword arguments.

Batch Processing

Concurrent Processing

Performance Considerations

Source Code

View the source code on GitHub