SelfConsistencyAgent generates multiple independent responses to a given task and aggregates them into a single, consistent final answer. It leverages concurrent processing and employs a majority voting mechanism to ensure reliability.
Based on the research paper: Self-Consistency Improves Chain of Thought Reasoning in Language Models (Wang et al., 2022).
Class: SelfConsistencyAgent
Parameters
Methods
aggregation_agent(responses, prompt?, model_name?) is a module-level helper function (not a method on SelfConsistencyAgent) used internally to synthesize the final answer via majority voting. Import it separately as from swarms.agents.consistency_agent import aggregation_agent if you need it directly. run() calls it with its defaults, so the aggregator always runs on "gpt-5.4", whatever model_name you set.
Examples
Basic Usage
Evaluation Mode
Random Models for Diversity
Batch Processing
How It Works
- Generates Multiple Independent Responses: Creates several reasoning paths for the same problem
- Analyzes Consistency: Examines agreement among different reasoning approaches
- Aggregates Results: Uses majority voting or consensus building
- Produces Reliable Output: Delivers a final answer reflecting the most reliable consensus
ThreadPoolExecutor to generate the responses concurrently. Each sample gets its own freshly built reasoning Agent, so no sample sees another sample’s answer and the draws stay independent. Responses are collected in the order they finish.
When eval=True and you pass answer, run() checks whether answer appears in any response. If none contains it, run() returns None and skips aggregation. Without answer, the check is skipped.
run() records the task, the list of responses, and the aggregated answer (as "Majority Voting Agent") in agent.conversation. That conversation is not cleared between calls, so a second run() or batched_run() returns the earlier tasks’ messages too. Create a new SelfConsistencyAgent, or read only the last message, when you need one task’s result on its own.
Output Formats
output_type takes any value listed in output types. Common choices:
"dict": The conversation as a list of message dicts"final": Only the aggregated answer, as a string"str": The conversation as a string"json": The conversation as a JSON string
Best Practices
- Sample Size: Use 3-7 samples for most tasks; increase for critical decisions
- Model Selection: Choose models with strong reasoning capabilities
- Evaluation Mode: Enable for tasks with known correct answers
- Final Answer Only: Set
output_type="final"to get just the aggregated answer - Batch Processing: Use
batched_runfor multiple related tasks, and remember that results accumulate in one conversation