Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.swarms.world/llms.txt

Use this file to discover all available pages before exploring further.

The 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

ParameterTypeDefaultDescription
namestr"Self-Consistency-Agent"Name of the agent
descriptionstr"An agent that uses self consistency..."Description of the agent’s purpose
system_promptstrCONSISTENCY_SYSTEM_PROMPTSystem prompt for the reasoning agent
model_namestrRequiredThe underlying language model to use
num_samplesint5Number of independent responses to generate
max_loopsint1Maximum number of reasoning loops per sample
majority_voting_promptOptional[str]majority_voting_promptCustom prompt for majority voting aggregation
evalboolFalseEnable evaluation mode for answer validation
output_typeOutputType"dict"Format of the output
random_models_onboolFalseEnable random model selection for diversity

Methods

MethodDescriptionReturns
run(task, img?, answer?)Generates multiple responses and aggregates themUnion[str, Dict[str, Any]]
aggregation_agent(responses, prompt?, model_name?)Aggregates responses via majority votingstr
check_responses_for_answer(responses, answer)Checks if an answer appears in any responsebool
batched_run(tasks)Run the agent on multiple tasks in batchList[Union[str, Dict[str, Any]]]

Examples

Basic Usage

from swarms.agents.consistency_agent import SelfConsistencyAgent

agent = SelfConsistencyAgent(
    name="Math-Reasoning-Agent",
    model_name="claude-sonnet-4-6",
    max_loops=1,
    num_samples=5
)

task = "What is the 40th prime number?"
final_answer = agent.run(task)
print("Final aggregated answer:", final_answer)

Evaluation Mode

from swarms.agents.consistency_agent import SelfConsistencyAgent

agent = SelfConsistencyAgent(
    name="Validation-Agent",
    model_name="claude-sonnet-4-6",
    num_samples=3,
    eval=True
)

result = agent.run("What is 2 + 2?", answer="4", eval=True)
if result is not None:
    print("Validation passed:", result)
else:
    print("Validation failed - expected answer not found")

Random Models for Diversity

from swarms.agents.consistency_agent import SelfConsistencyAgent

agent = SelfConsistencyAgent(
    name="Diverse-Reasoning-Agent",
    model_name="claude-sonnet-4-6",
    num_samples=5,
    random_models_on=True
)

result = agent.run("What are the benefits of renewable energy?")
print("Diverse reasoning result:", result)

Batch Processing

from swarms.agents.consistency_agent import SelfConsistencyAgent

agent = SelfConsistencyAgent(
    name="Batch-Processing-Agent",
    model_name="claude-sonnet-4-6",
    num_samples=3
)

tasks = [
    "What is the capital of France?",
    "What is 15 * 23?",
    "Explain photosynthesis in simple terms."
]

results = agent.batched_run(tasks)
for i, result in enumerate(results):
    print(f"Task {i+1} result: {result}")

How It Works

  1. Generates Multiple Independent Responses: Creates several reasoning paths for the same problem
  2. Analyzes Consistency: Examines agreement among different reasoning approaches
  3. Aggregates Results: Uses majority voting or consensus building
  4. Produces Reliable Output: Delivers a final answer reflecting the most reliable consensus
The agent uses ThreadPoolExecutor to generate multiple responses concurrently, improving performance while maintaining independence between reasoning paths.

Output Formats

  • "dict": Dictionary format with conversation history
  • "str": Simple string output
  • "list": List format
  • "json": JSON formatted output

Best Practices

  1. Sample Size: Use 3-7 samples for most tasks; increase for critical decisions
  2. Model Selection: Choose models with strong reasoning capabilities
  3. Evaluation Mode: Enable for tasks with known correct answers
  4. Custom Prompts: Tailor majority voting prompts for specific domains
  5. Batch Processing: Use batched_run for multiple related tasks