Search

Search papers and lessons

Chain-of-Thought Prompting

Level 4 · 10 min read

Chain-of-thought prompting asks a language model to write out intermediate reasoning steps before its final answer, which often makes it far more accurate on multi-step problems.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Losses & regularization
  • Language & LLMs
  • Neural network basics
  • Sign in to see your progress

TL;DR

Chain-of-thought (CoT) prompting shows a language model a few worked examples in which the answer comes after a short written derivation, and then the model writes its own derivation before answering. On arithmetic, logic and word problems this often lifts accuracy a long way, with no change to the model's weights. The trade is more generated text, and so more time and cost per question.

Where you'll see it: the "show your work" mode of chat assistants, the step-by-step solutions that coding and math tools produce, and the reasoning traces that large models generate before they commit to an answer.

Intuition

Doing the sums on paper

Ask someone to multiply 23 by 47 in their head and reply at once. Most people will guess, and the guess will often be wrong. Give them a pencil and the same person gets it right, because the paper holds the partial results: 23×7=16123 \times 7 = 161, 23×40=92023 \times 40 = 920, then 161+920=1081161 + 920 = 1081. Nothing about the person changed. What changed is that the intermediate steps now exist somewhere they can be read back.

A language model is in a similar position. It produces one token at a time, and each token must be chosen using only what came before it. If the answer is the very first thing it writes, it has to compress the whole problem into a single guess. If it first writes out the steps, every later token can look at those steps, so the answer is conditioned on a worked derivation rather than on the bare question.

That is the core idea of chain-of-thought prompting. The model is not taught any new skill. It is shown that the useful form of an answer includes the reasoning, and it then imitates that form.

Why it helps

There is a mechanical reason as well. A Transformer spends the same fixed amount of computation on each token it produces. A question that needs twelve arithmetic steps gets no more processing if the answer is squeezed into one token. Each reasoning token it writes is another pass through the network, with the earlier steps available to attend to. More tokens means more sequential computation spent on the problem.

0510152000.20.40.60.81number of stepschance every step is right
  • each step right 90% of the time
  • each step right 99% of the time
If each step is independent and right with probability p, a chain of n steps is fully right with probability p^n. Splitting work into easy steps keeps p high; long chains of hard steps decay fast.

The plot above is a toy model, but it shows the pressure that chain-of-thought relieves. A single hard leap with a 30% chance of success is worse than ten small steps, each close to certain.

The math

Let qq be a question, yy the final answer, and rr a reasoning trace, which is the sequence of tokens written between the question and the answer. A language model defines a distribution over whole continuations, and for any sequence the chain rule of probability gives

p(r,y∣q)=p(r∣q) p(y∣q,r).p(r, y \mid q) = p(r \mid q)\, p(y \mid q, r).

Here p(r∣q)p(r \mid q) is the probability that the model writes trace rr after question qq, and p(y∣q,r)p(y \mid q, r) is the probability of answer yy once the trace is in place. Read in words: first the model writes its reasoning, then it writes an answer that is conditioned on that reasoning.

Standard prompting asks for the answer directly, so it effectively uses p(y∣q)p(y \mid q), a single step with no trace in between. Chain-of-thought prompting puts examples of the form (qk,rk,yk)(q_k, r_k, y_k) in the prompt and then asks for (r,y)(r, y) for the new question. The examples are the few-shot part of In-Context Learning; the reasoning inside them is what changes the behaviour.

Marginalising over reasoning

Many different traces can lead to the same correct answer. To score an answer, sum over all traces that produce it:

p(y∣q)=∑rp(r,y∣q).p(y \mid q) = \sum_{r} p(r, y \mid q).

The sum is over every possible trace, which is far too many to compute. In practice we sample a few traces and use them as an estimate. This is the idea behind self-consistency, covered below.

Greedy decoding and temperature

When the model writes a trace, it draws each token from a softmax over its vocabulary. Softmax Temperature divides the logits (the raw scores the network outputs for each possible token) by a temperature before that softmax. At temperature zero the model always picks the most likely token (greedy decoding). At higher temperature it samples more freely. Greedy decoding gives one deterministic trace; higher temperature gives diverse traces, which is what self-consistency needs.

Self-consistency

Sample NN traces r1,…,rNr_1, \dots, r_N at a nonzero temperature, extract an answer yiy_i from each, and take the most common answer:

y^=arg⁡max⁡y∑i=1N1[ yi=y ].\hat{y} = \arg\max_{y} \sum_{i=1}^{N} \mathbf{1}[\,y_i = y\,].

Here 1[⋅]\mathbf{1}[\cdot] is 1 when its condition is true and 0 otherwise. This is a majority vote. It works because wrong traces tend to scatter across many different wrong answers, while correct traces tend to agree.

Worked examples

Example
A shop change problem, answered directly

A cafe sells muffins at 3 dollars each. Ana buys 4 muffins and pays with a 20 dollar note. How much change does she get?

Asked directly, a model might write: "8 dollars". That is correct here, but a model that answers this way often fails on problems with the same structure and more numbers. There is no visible derivation to check, so when an answer is wrong you cannot tell whether it came from a bad multiplication or a bad subtraction.

Example
The same problem with a worked chain of thought

The prompt contains one worked example first, with its derivation written out. The model then answers the new question in the same form:

  1. Cost of the muffins: 4×3=124 \times 3 = 12 dollars.
  2. Amount paid: 20 dollars.
  3. Change: 20−12=820 - 12 = 8 dollars.

Answer: 8 dollars.

Each line depends on the previous one. If the model had written "4×3=154 \times 3 = 15", the error would be visible at line 1 and the rest would follow from it. Interpretation: the reasoning is not a decoration. It is the computation, and it can be checked line by line.

Example
A realistic case: multi-step word problems

The GSM8K benchmark of grade-school word problems (Cobbe et al., 2021) requires two to eight arithmetic steps per problem. Wei et al. (2022) reported that a 540-billion-parameter model, prompted with a handful of worked examples, roughly tripled its accuracy when the examples included reasoning, going from under 20% with plain answers to about 57%.

Their result also showed the gain depended on scale. Smaller models produced chains of text that looked like reasoning but often contained arithmetic or logic errors, so prompting with chains did little or nothing for them. The technique needs a model large enough to carry a coherent derivation.

Kojima et al. (2022) found a variant with no examples at all: appending "Let's think step by step" to the question was enough to elicit a trace on many tasks. This is zero-shot chain of thought.

Tradeoffs and failure modes

Chain-of-thought is not free, and the costs fall in predictable places.

SituationWhat goes wrongBetter choice
Simple factual lookup or classificationExtra tokens add latency and cost, and reasoning can talk the model out of a correct first answerAnswer directly
Multi-step arithmetic, logic or planningDirect answers are often wrong, and errors are invisibleChain of thought, with a checkable final answer
High-stakes answer where one sample is riskyA single trace can be confidently wrongSelf-consistency: sample several traces and vote
Small modelFluent but invalid steps; accuracy may not improveUse a larger model, or fine-tune on reasoning data
Reasoning is needed for a decision that must be auditedThe written trace may not be the real cause of the answerVerify the answer with a separate check, do not rely on the trace alone

Three failure modes are worth knowing.

Plausible wrong steps. A trace can read well and still contain a false step. Because the answer is conditioned on the trace, one bad step often carries through to a confidently wrong result.

Unfaithful explanations. Turpin et al. (2023) showed that a model's stated reasoning can be shaped by a hint in the prompt that the model then does not mention, so the written trace is not always an account of what drove the answer. Treat the trace as a useful scratchpad, not as a proof.

Cost and latency. A chain can be many times longer than the answer. This matters for interactive products and for large batch jobs. Self-consistency multiplies the cost again by the number of samples.

History

Wei, Wang, Schuurmans and colleagues at Google published the chain-of-thought paper in 2022, during the period when researchers were finding that very large language models could do tasks they were not trained on, given only a prompt. Their question was narrow: why do large models fail at multi-step reasoning, and can the prompt change that? Their answer, that writing the steps out is enough for sufficiently large models, was simple and easy to test, which helped it spread quickly.

Kojima and colleagues showed later in 2022 that a short instruction could replace the worked examples. Wang and colleagues introduced self-consistency in the same year as a way to get more reliable answers from the same sampling machinery. These three papers together made chain-of-thought a standard prompting method, and later models were often trained to produce reasoning traces by default.

Check yourself

QuestionRecall: in one sentence, what does chain-of-thought prompting ask the model to do before it gives its answer?Show answer

It asks the model to write out intermediate reasoning steps first, so that the final answer is generated after, and conditioned on, a visible derivation.

QuestionComputation: a model answers a question correctly with probability 0.9 per reasoning step, and a problem needs 10 independent steps. What is the chance every step is right, using the toy model from the plot? Give your answer to three decimal places.Show answer

Each step is right with probability p=0.9p = 0.9, and the steps are treated as independent, so the chance all 10 are right is p10=0.910p^{10} = 0.9^{10}. Computing 0.910≈0.34870.9^{10} \approx 0.3487, so the answer is 0.349 to three decimal places. The point of the calculation is that even a high per-step accuracy compounds into a low chance of a fully correct chain.

QuestionWhy: why does writing reasoning steps help a transformer, given that it spends the same computation on every token?Show answer

A transformer does a fixed amount of work per token, so a question that needs several sequential steps cannot be solved within one token's computation. Each reasoning token is another pass through the network, and later tokens can attend to the earlier steps. Writing the steps therefore gives the model more sequential computation and a record it can use, rather than forcing the whole answer into one guess.

QuestionApplied: you are building a support assistant that answers 'What are your opening hours?' from a fixed FAQ, and another that calculates refunds from a multi-line order. Which should use chain-of-thought, and why?Show answer

The refund calculator should use it, because it has several dependent arithmetic steps where an intermediate error would be invisible in a bare answer, and the derivation can be checked. The opening-hours lookup is a single retrieval with no computation; chain-of-thought would add latency and cost and could introduce errors into a simple answer. Use a direct answer there.

QuestionWhy, applied: a team samples 5 traces at temperature 0.8 and takes the majority answer. A colleague says this is no better than sampling once. Who is right, and what would change the answer?Show answer

The colleague is right only if the traces are not diverse. If every sample is the same trace, the vote adds nothing. When wrong traces scatter across different wrong answers and correct traces agree, the majority is more reliable than any single sample. The gain depends on temperature producing real variety and on the correct answer being the most common one across samples. Checking the agreement rate on a labelled set would show whether the vote helps.

Sources