How does Self-Consistency Prompting work?

Authors
How does Self-Consistency Prompting work?

Self-Consistency Prompting is a technique where we ask an LLM to solve the same problem many times, each time thinking step by step in a slightly different way, and then we pick the final answer that comes up the most. It works like taking a vote among many attempts, which makes the answer more reliable.

In this blog, we will learn about how Self-Consistency Prompting works. We will also see why a single answer from an LLM can be wrong, what Chain-of-Thought prompting is, how self-consistency creates many different reasoning paths, how the majority vote picks the final answer, how to write it in code, and where it works well and where it fails.

We will cover the following:

  • The problem with a single answer
  • What is Chain-of-Thought prompting?
  • What is temperature?
  • What is Self-Consistency Prompting?
  • How does Self-Consistency Prompting work?
  • A step-by-step example
  • Why does voting work?
  • Self-Consistency Prompting in code
  • How many answers should we sample?
  • Where it works well and where it fails
  • Greedy Chain-of-Thought vs Self-Consistency

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

The problem with a single answer

LLMs are very good with words. But when a question needs many steps of thinking, like a math word problem, an LLM can do mistakes.

Let's say we ask an LLM:

A shop has 15 apples. It sells 6 apples. Then it buys 4 boxes
with 3 apples in each box. How many apples does the shop have now?

The correct answer is: 15 - 6 = 9, then 4 x 3 = 12, then 9 + 12 = 21.

But the LLM can slip at one step. For example, it can add 4 instead of 12 and say 13. If we ask only once, we get only one answer, and we have no way to know whether this one answer is right or wrong.

So, the question is: how can we make the LLM more reliable without training it again?

Before we see the answer, we must learn two ideas: Chain-of-Thought prompting and temperature.

What is Chain-of-Thought prompting?

Chain-of-Thought prompting means asking the LLM to write its thinking step by step before giving the final answer.

Instead of jumping straight to the answer, the LLM writes like below:

The shop starts with 15 apples.
It sells 6, so 15 - 6 = 9 apples are left.
It buys 4 boxes of 3 apples, so 4 x 3 = 12 new apples.
Total = 9 + 12 = 21.
The answer is 21.

When an LLM writes the steps, it does better on multi-step problems. It is just like a student who solves a problem on paper instead of doing it all in the head.

Here, each written solution is called a reasoning path. A reasoning path is one complete way of thinking from the question to the answer.

Self-consistency is built on top of Chain-of-Thought. Now, let's learn about temperature.

What is temperature?

An LLM writes its answer one small piece at a time. These pieces are called tokens. When an LLM picks the next token, it does not have just one choice. It has many possible tokens, and each one has a chance, which is called a probability.

Let's say, after "The sky is", the LLM gives these chances:

blue    70%
clear   20%
dark    10%

Now, the LLM can pick in two ways:

  • Always pick the top one. This is called greedy decoding. It picks "blue" every time. Same question, same answer, every time.
  • Pick randomly, using the chances. This is called sampling. Most of the time it picks "blue", but sometimes it picks "clear" or "dark".

Temperature is a setting that controls how random the sampling is.

  • Low temperature (close to 0): the LLM almost always picks the top token. Answers are very similar each time.
  • Higher temperature (like 0.7 or 1.0): the LLM picks less likely tokens more often. Answers are more varied.

So, with a higher temperature, if we ask the same question many times, the LLM can think in different ways each time. This is exactly what self-consistency needs.

What is Self-Consistency Prompting?

Self-Consistency Prompting means we let the LLM solve the same problem many times with Chain-of-Thought, using sampling, and then we choose the final answer that appears the most times.

So, we trust the answer that the LLM reaches most often across many different paths.

This technique was introduced in 2022 by researchers at Google Research in the paper "Self-Consistency Improves Chain of Thought Reasoning in Language Models". They showed that it improves the accuracy of LLMs on math and reasoning tasks, compared to taking just one Chain-of-Thought answer. For example, on GSM8K, a well-known set of school-level math word problems, the paper reports an improvement of 17.9 percentage points in accuracy.

Let's understand this with an analogy.

Suppose we are not sure about the answer to a tricky math question. So, we ask 10 friends to solve it on their own. Each friend solves it in his own way.

  • 7 friends say 21.
  • 2 friends say 13.
  • 1 friend says 25.

Which answer will we trust? 21. Different people, using different ways, all reached 21. It is very unlikely that so many of them made mistakes and still landed on the same wrong number.

Self-consistency does the same thing, but all the "friends" are the same LLM, thinking a little differently each time.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

How does Self-Consistency Prompting work?

Self-consistency works in three steps.

Step 1: We prompt with Chain-of-Thought. We write a prompt that asks the LLM to think step by step. We can also give a few solved examples with steps inside the prompt. This is called few-shot Chain-of-Thought.

Step 2: We sample many reasoning paths. We send the same prompt many times, say 10 times, with a temperature above 0. Because of the randomness, each reply follows a slightly different reasoning path. Some paths are right, some have small mistakes.

Step 3: We take a majority vote on the final answers. We ignore the reasoning text. We look only at the final answer of each path. Then, we count how many times each answer appears, and pick the one with the most votes.

Let's visualize it as below:

        Question + "think step by step"
               (temperature > 0)
                       ↓
  +------+------+------+------+------+------+
  ↓      ↓      ↓      ↓      ↓      ↓      ↓
Path 1 Path 2 Path 3 Path 4 Path 5 Path 6 Path 7
  ↓      ↓      ↓      ↓      ↓      ↓      ↓
  21     21     13     21     25     21     21
  ↓      ↓      ↓      ↓      ↓      ↓      ↓
  +------+------+------+------+------+------+
                       ↓
              Vote: 21 = 5 votes
                    13 = 1 vote
                    25 = 1 vote
                       ↓
               Final answer: 21

Here, we can see that the same question goes out many times, each path reaches its own answer, and the vote picks 21.

Note: The vote is on the final answer only, not on the reasoning steps. Two paths can use different steps and still reach the same answer. Both count as votes for that answer.

To master Prompt Engineering and Chain of Thought (CoT) Prompting from the ground up, check out our AI and Machine Learning Program at Outcome School.

A step-by-step example

The best way to learn this is by taking an example.

We ask our apple question 5 times with temperature 0.7. Let's see the 5 reasoning paths.

Path 1:

15 - 6 = 9. Then 4 x 3 = 12. 9 + 12 = 21. The answer is 21.

Path 2:

First, the new apples: 4 boxes x 3 = 12. Total before selling: 15 + 12 = 27.
After selling 6: 27 - 6 = 21. The answer is 21.

Path 3:

15 - 6 = 9. It buys 4 more, so 9 + 4 = 13. The answer is 13.

Path 4:

After selling, 9 apples are left. 4 boxes of 3 apples is 12 apples.
9 + 12 = 21. The answer is 21.

Path 5:

15 + 4 x 3 = 15 + 12 = 27. Minus 6 is 21. The answer is 21.

Now, let's count the final answers:

  • 21: Path 1, Path 2, Path 4, Path 5 = 4 votes
  • 13: Path 3 = 1 vote

The final answer is 21.

Here, we can notice two things:

  • Path 3 made a mistake. It added 4 boxes instead of 12 apples. If we had asked only once and got Path 3, we would have got the wrong answer.
  • Path 2 and Path 5 used a different order of steps from Path 1, but they reached the same correct answer.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

Why does voting work?

Now, the question is, why does the correct answer usually win the vote?

There are many correct ways to reach the right answer, but the wrong answers are usually scattered.

When the LLM makes a mistake, it can make it in many different places. One path adds wrong, another multiplies wrong, another reads the question wrong. Each mistake leads to a different wrong answer, like 13, 25, or 30. So, the wrong answers split their votes.

But the correct paths, even if they take different routes, all land on the same answer, 21. So, the correct answer collects the votes together.

Note: Voting helps only when the LLM gets the right answer more often than any single wrong answer. If the LLM keeps making the same mistake on most paths, the vote will pick that wrong answer too.

Self-Consistency Prompting in code

Let's see the code for self-consistency in Python. Here, I have kept only the important lines of the code and removed other lines for brevity.

from collections import Counter

def ask_llm(prompt, temperature):
    # call your LLM API here and return the reply text
    ...

def extract_answer(reply):
    # find the text after "The answer is" and return it
    return reply.split("The answer is")[-1].strip(" .")

def self_consistency(question, num_paths=10):
    prompt = question + "\nLet's think step by step. End with 'The answer is X.'"

    answers = []
    for _ in range(num_paths):
        reply = ask_llm(prompt, temperature=0.7)
        answers.append(extract_answer(reply))

    votes = Counter(answers)
    best_answer, count = votes.most_common(1)[0]
    return best_answer

Here, we have:

  • ask_llm: sends the prompt to the LLM with the given temperature and returns the reply.
  • extract_answer: pulls out only the final answer from the long reasoning text. This is why we asked the LLM to always end with "The answer is X."
  • self_consistency: adds the "think step by step" instruction, asks the LLM num_paths times with temperature 0.7, and collects all the final answers.
  • Counter: counts how many times each answer appears.
  • most_common(1): gives the answer with the most votes.

If the answers are ["21", "21", "13", "21", "21"], the function returns "21".

Note: We must ask the LLM to give the final answer in a fixed format. If one reply says "21" and another says "21 apples", the counter treats them as two different answers. So, we must clean the answers before counting.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

How many answers should we sample?

More paths give a more reliable vote, but each path is one full LLM call.

  • 1 path: this is normal Chain-of-Thought. No vote.
  • 5 paths: a good improvement for a small cost.
  • 10 to 40 paths: even better, but the cost grows.

In the original paper, the accuracy kept going up as the number of paths increased, and the gains became smaller after a point. The authors reported results with up to 40 paths.

So, if we sample 10 paths, we pay about 10 times the cost and wait longer, unless we send the requests at the same time. We must pick the number depending on our use case: how important the correct answer is, and how much cost and time we can afford.

Spending more compute at answer time to get a better answer is also a key idea behind reasoning models. We have a detailed blog on Large Reasoning Models that covers this in depth.

Where it works well and where it fails

Advantages:

  • It improves accuracy on reasoning tasks like math, logic puzzles, and common-sense questions.
  • It needs no extra training. It works with any LLM that supports sampling.
  • It is easy to add on top of Chain-of-Thought.
  • The vote count tells us how confident we can be. 9 out of 10 votes is much more trustworthy than 4 out of 10.

Disadvantages:

  • It costs more, because we call the LLM many times for one question.
  • It is slower, unless we send the requests in parallel.
  • It needs a clear final answer to vote on, like a number, a letter, or a short word. For open-ended tasks, like writing an essay, there is no simple way to count which essay "appears the most".
  • If the LLM has a strong wrong belief, most paths make the same mistake, and the vote does not help.

Greedy Chain-of-Thought vs Self-Consistency

Let me tabulate the differences between normal greedy Chain-of-Thought and Self-Consistency for your better understanding.

PointGreedy Chain-of-ThoughtSelf-Consistency
Number of LLM calls1Many, like 5 to 40
Temperature0 or very lowAbove 0, like 0.7
Reasoning pathsOneMany different ones
How the final answer is chosenThe only answer we getMajority vote
Accuracy on reasoning tasksGoodBetter
Cost and timeLowHigher
Confidence signalNoneVote count
Best forQuick, cheap answersImportant questions with a clear final answer

Here, we can see that self-consistency trades more cost for more reliability.

If we want the LLM to judge each step and go back when a path fails, instead of only voting on the final answers, we have a detailed blog on Tree of Thoughts that explains this step by step.

Now we must have understood how Self-Consistency Prompting works.

Frequently Asked Questions

Does Self-Consistency Prompting need extra training?

No. Self-Consistency Prompting needs no extra training. It works with any LLM that supports sampling, and we add it on top of Chain-of-Thought prompting. We only change how we ask the LLM and how we pick the final answer.

Can we use Self-Consistency Prompting with temperature 0?

No. With temperature 0, the LLM uses greedy decoding and gives the same answer every time, so every vote would be the same. Self-consistency needs a temperature above 0, like 0.7, so that each reply follows a slightly different reasoning path.

Does Self-Consistency Prompting work for open-ended tasks like writing an essay?

Not well. Self-consistency needs a clear final answer to vote on, like a number, a letter, or a short word. For open-ended tasks, like writing an essay, there is no simple way to count which essay appears the most.

Can Self-Consistency Prompting still give a wrong answer?

Yes. Voting helps only when the LLM gets the right answer more often than any single wrong answer. If the LLM has a strong wrong belief and makes the same mistake on most paths, the vote picks that wrong answer too.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.