Foundations to Frontier
Tests
F. NLP and Large Language Models › F9 Prompting and In-Context Learning

Chain-of-thought, self-consistency

Asking a model to write out its reasoning before answering, chain-of-thought (CoT), dramatically improves multi-step problems such as arithmetic, logic and planning. A Transformer does a fixed amount of serial computation per token, and intermediate tokens give it more steps. Self-consistency samples several reasoning chains and takes a majority vote over their final answers, cancelling independent mistakes. This page covers CoT prompting (few-shot and 'let's think step by step'), answer extraction, self-consistency and its cost, and why CoT helps. In the exercises you extract answers, run a vote and compute chain reliability. You then simulate a fixed-depth model that can do three serial steps per token. On 2-step problems direct answering and CoT were equal (94%). From 4 steps, direct answering collapsed (0–9%) while CoT held up, falling to 61% at 16 steps as errors accumulated. A 20-sample vote restored 100%, but when 15% of problems triggered a misconception shared by all chains, voting plateaued at 85%.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Showing your working

When you solve a long sum in your head you might slip, but if you write each step down it gets much easier. Language models are the same: asking them to ‘think step by step’ and write out the working makes them far better at multi-step problems. Even better, you can ask several times and take the most common answer, because different attempts make different slips. The demo shows how voting over several attempts raises accuracy.

Prompting lab
Majority of 5 reasoning paths is right with probability 83.5% (one path: 60%), assuming independent paths and wrong answers spread over 4 alternatives.
Why engineers care

For multi-step technical questions (calculations, checklists, fault diagnosis), ask for step-by-step reasoning and check the final answer by sampling several times; but be aware that repeated mistakes will not be voted away.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Chain-of-thought asks the model to…
  2. 2. Self-consistency…
  3. 3. Voting fixes mistakes that are…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Few-shot CoT (Wei et al., 2022): demonstrations include worked reasoning before each answer. Zero-shot CoT (Kojima et al., 2022): append ‘Let’s think step by step’.
  • Answer extraction: require a fixed final-answer format so the answer can be parsed.
  • Self-consistency (Wang et al., 2023): sample N chains at temperature around 0.7, extract answers, take the majority. Accuracy rises with N then saturates; cost grows linearly. Adaptive variants stop when the vote is decisive.
  • Why CoT helps: a fixed-depth Transformer can do only a bounded amount of serial computation per token; writing intermediate results lets later tokens build on them (Feng et al., 2023; Merrill and Sabharwal, 2024).
  • Limits: CoT helps little on one-step tasks and small models; written reasoning may not reflect the model’s actual computation (unfaithfulness); shared misconceptions survive voting.
  • Reasoning models (F12) are trained to produce long chains of thought by RL.
📝 Quick check (Level 1)
0/3 answered
  1. 1. Zero-shot CoT trigger phrase:
  2. 2. Self-consistency cost grows…
  3. 3. CoT helps little on…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

CoT and voting in practice

Exercise 1.

  • Extract the final answer after an ‘Answer:’ marker.
  • Majority-vote over sampled chains and report the vote share.
  • Compute the probability that an m-step chain has no error.

Practice.

  • Ask for reasoning and a final line in a fixed format, and parse only that line.
  • Sample 5–20 chains at temperature 0.6–0.8 for self-consistency; use the vote share as a confidence signal.
  • Reasoning models already think internally; adding CoT prompts to them helps little.
  • Check that reasoning text is consistent with the answer, but do not treat it as a faithful explanation.
answers = []
for _ in range(10):
    r = client.chat.completions.create(model=MODEL, temperature=0.7, messages=[
        {"role": "user", "content": question + "\nThink step by step, then finish with 'Answer: <value>'."}])
    answers.append(extract_answer(r.choices[0].message.content))
final, share = Counter(a for a in answers if a).most_common(1)[0]
🐍 Exercise 1: extraction and voting
Implement answer extraction, self-consistency voting and chain reliability.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A typical self-consistency temperature:
  2. 2. The vote share can serve as…
  3. 3. Is CoT text a faithful explanation?

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Serial computation. A Transformer with L layers computes each token with a bounded-depth circuit. Problems needing more serial steps than that depth allows (long iterated arithmetic, composing many functions) cannot be solved reliably in a single token, however wide the model. Intermediate tokens act as a scratchpad: each step’s result is written down and read back, so the total serial depth grows with the number of tokens.

Error accumulation. If each step errs independently with probability e, an m-step chain is fully correct with probability (1 − e)^m, which falls with m (0.97¹⁶ ≈ 0.61).

Voting. With N independent chains, the correct answer wins if it gets more votes than any single wrong answer. When wrong answers are scattered over many values, even a minority of correct chains wins. When errors are shared (the same misconception in every chain), votes agree on the wrong answer and no N fixes it: accuracy plateaus at the fraction of problems without shared errors.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Intermediate tokens increase…
  2. 2. Chain success with independent step errors:
  3. 3. Voting cannot fix errors that are…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

When does chain-of-thought help, and how far can self-consistency push accuracy?

History.

  • J. Wei et al., “Chain-of-thought prompting elicits reasoning in large language models” (NeurIPS 2022).
  • T. Kojima et al., “Large language models are zero-shot reasoners” (NeurIPS 2022).
  • X. Wang et al., “Self-consistency improves chain of thought reasoning” (ICLR 2023).
  • G. Feng et al., “Towards revealing the mystery behind chain of thought” (NeurIPS 2023).

Exercise 2. A stylised model, not a real LLM.

  • Problems: running sums (mod 100) of m two-digit numbers, 1500 per setting.
  • The model: does at most 3 serial steps per token; each step errs with probability 0.03 (adding a random offset).
    • Direct answers must finish in one token, so beyond 3 steps the model shortcuts by adding only last digits.
    • CoT does one step per token.
    • Traps: 15% of problems carry a misconception that every chain repeats.
steps direct CoT CoT + vote of 5 CoT + vote of 20 CoT, traps CoT + vote of 20, traps
2 0.942 0.944 1.000 1.000 0.788 0.840
4 0.002 0.875 1.000 1.000 0.768 0.853
8 0.076 0.786 0.991 1.000 0.651 0.841
16 0.091 0.608 0.932 1.000 0.536 0.846
  • CoT matters only beyond the model’s per-token capacity: at 2 steps it changed nothing; from 4 steps direct answering failed (the 8–9% at 8 and 16 steps is when the shortcut happens to be right).
  • Chains accumulate errors: CoT accuracy followed 0.97^m (0.61 at 16 steps).
  • Voting removes independent errors: here wrong answers were scattered over 99 values, so even 5 votes recovered most problems; real errors cluster more, so real gains are smaller.
  • Shared errors set a ceiling: with traps, 20 votes plateaued at about 0.85, the fraction of trap-free problems.
  • Seeds 1 and 2: within 0.04.
🐍 Exercise 2 (research): when does chain-of-thought help, and how far can self-consistency push accuracy?
Simulate a fixed-depth model on multi-step arithmetic; compare direct answers, chain-of-thought, self-consistency votes, and voting under shared misconceptions.

Reasoning prompts in AI, robotics and aerospace

  • Engineering calculations: step-by-step reasoning with a fixed answer format makes outputs checkable and lets voting flag low-confidence answers.
  • Robot task planning: language-model planners that write out intermediate subgoals succeed more often on long-horizon tasks.
  • Safety cases: shared misconceptions are not caught by sampling more; independent checks (tools, verifiers, humans) are needed.

References

  1. J. Wei et al. (2022). “Chain-of-thought prompting elicits reasoning in large language models.” NeurIPS.
  2. X. Wang et al. (2023). “Self-consistency improves chain of thought reasoning in language models.” ICLR.
  3. T. Kojima, S. S. Gu, M. Reid, Y. Matsuo and Y. Iwasawa (2022). “Large language models are zero-shot reasoners.” NeurIPS.
  4. M. Turpin, J. Michael, E. Perez and S. Bowman (2023). “Language models don’t always say what they think.” NeurIPS.