Back to Prompting Recipes

Self-Consistency

EnsembleVoting

Ensemble

Sample multiple reasoning paths and aggregate via majority vote for more reliable answers.

Summary

Self-Consistency is a decoding strategy that samples multiple reasoning paths from the LLM and selects the most frequent answer. By aggregating diverse reasoning traces, this technique reduces errors from single-path reasoning and improves answer reliability. It builds on Chain of Thought by sampling multiple CoT outputs rather than using greedy decoding.

How it works

  1. Generate multiple paths: Sample N reasoning traces for the same problem
  2. Extract answers: Parse final answers from each trace
  3. Vote/aggregate: Select the most common answer
  4. Return result: Output the consensus answer

Key considerations

  • Sample count: More samples increase accuracy but cost
  • Aggregation: Majority vote, weighted, or confidence-based
  • Diversity: Temperature and sampling parameters affect variety
  • Speed: Parallel generation can mitigate latency

When to use

  • Tasks where multiple reasoning paths exist
  • Applications requiring high reliability
  • Scenarios where cost-latency trade-off is acceptable
  • Math, logic, and factual reasoning tasks
Code View

Self-Consistency Implementation

// Self-Consistency recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Use the prompting technique to answer the request clearly and precisely. Recipe: Self-Consistency. Description: Sample multiple reasoning paths and aggregate via majority vote for more reliable answers. Focus: Ensemble Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});