Skip to content

Self-Consistency & Verification

One model call gives you one answer. Five calls give you five answers. If four of them agree, you can be more confident than if you'd only asked once. Self-consistency runs the same prompt multiple times with temperature > 0 and takes the majority answer. This lesson covers the technique, when it's worth the cost, and how to implement it.

Gnome mascot

What you'll learn

  • Self-consistency: run the same prompt N times (usually 3-7), take the most common answer
  • Only works with temperature > 0, deterministic output (temp=0) gives the same answer every time
  • Best for tasks with a clear right answer: math, classification, factual QA. Not for creative or open-ended tasks

Build it

Implementing self-consistency involves running the prompt multiple times and aggregating the results to find the majority vote.

python
from collections import Counter

def self_consistent_prompt(prompt, n=5, temperature=0.7):
    answers = []
    for _ in range(n):
        response = call_llm(prompt, temperature=temperature)
        answers.append(extract_answer(response))

    # Majority vote
    counts = Counter(answers)
    most_common = counts.most_common(1)[0]

    return most_common[0], most_common[1] / n  # answer and confidence

With Chain-of-Thought

Combining self-consistency with Chain-of-Thought reasoning significantly improves reliability on complex tasks with definitive answers.

python
prompt = """Solve this math problem. Show your work, then give your final answer
as "Answer: [number]".

Problem: {question}"""

answer, confidence = self_consistent_prompt(prompt, n=5)
print(f"Answer: {answer} (confidence: {confidence:.0%})")
# "Answer: 42 (confidence: 80%)" means 4 of 5 runs gave 42

When to use

Task typeUse self-consistency?Why
Math problemsYesClear right answer, CoT improves accuracy further
ClassificationYesMultiple runs catch inconsistent classifications
Factual QAYesHallucinations vary; consensus filters them
Creative writingNoNo right answer to vote on
SummarizationMaybeCan help catch missed points, but expensive

What goes wrong

MistakeHow you notice itThe fix
Temp too lowAll answers identical, no benefitSet temperature >= 0.5. At temp=0, every run produces the same output
Temp too highAnswers vary wildly, no consensusLower temperature to 0.3-0.5. You want diversity, not chaos
Cost5x API calls per queryOnly use for high-value tasks. Start with n=3 and increase only if needed
Wrong answers agreeMajority is confidently wrongSelf-consistency amplifies patterns, not correctness. Verify against ground truth

Next: Meta-Prompting