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

str
default:"swarm_id()"
Unique identifier for the majority voting system.
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"
Whether to autosave conversations.
bool
default:"False"
Whether to enable verbose logging.
int
default:"1"
Maximum number of consensus building loops.
OutputType
default:"dict"
Output format: “str”, “dict”, “list”, or other.
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.

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
  • *args (Any): Variable length argument list passed to individual agents
  • **kwargs (Any): Arbitrary keyword arguments passed to individual agents
Returns: The consensus result formatted according to the specified output_type Raises:
  • ValueError: If the agents list is empty or None
Process Flow:
  1. Adds the input task to the conversation history
  2. For each loop (up to max_loops):
    • Runs all agents concurrently on the current conversation state
    • Collects agent responses and adds them to conversation history
    • Runs the consensus agent to analyze and synthesize responses
    • Adds consensus output to conversation history
  3. Returns the final result in the specified output format

batch_run()

Executes the majority voting system for multiple tasks sequentially.
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 using thread pooling.
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 in completion order (not input order)
Uses os.cpu_count() workers for optimal performance. Results are returned in completion order, not input order.

reliability_check()

Performs validation checks on the majority voting system configuration.
Raises:
  • ValueError: If agents list is empty or None

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

Usage Patterns

Single Task Analysis

Batch Processing

Concurrent Processing

Performance Considerations

Source Code

View the source code on GitHub