Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 38 ยท Techniques: Few-shot, Chain-of-Thought and Roles

Chain-of-Thought Prompting

Learn chain-of-thought prompting: asking a model to reason step by step before answering. When it helps, when it does not, and how to separate reasoning from the answer.

Some problems are easy to state and hard to answer in one jump: multi-step arithmetic, logic puzzles, planning, comparing options. Models that must produce the answer immediately tend to make mistakes on these. Chain-of-thought (CoT) prompting asks the model to write out intermediate reasoning steps before the final answer, which often improves accuracy on multi-step tasks.

Why it helps

A model generates one token at a time and has no hidden scratchpad. When it writes out a step, that step becomes part of its context for the next token. Reasoning that lives in the text can be used; reasoning that is skipped cannot. Breaking a problem into steps also reduces how much must be done per token.

flowchart LR
  Q["Question"] --> S1["Step 1"]
  S1 --> S2["Step 2"]
  S2 --> S3["Step 3"]
  S3 --> A["Final answer"]

Each written step becomes context for the next one.

Zero-shot CoT

The simplest form adds an instruction to reason first.

TEXT
A shop sells pens at 3 for $2.40. How much do 10 pens cost?
Think through the problem step by step, then give the final answer on its
own line starting with "Answer:".

A good instruction says what kind of steps to take and where the answer goes. A vague "think carefully" helps less than "list the quantities, choose the operation, then compute".

Few-shot CoT

Show worked examples that include the reasoning. The model imitates both the reasoning style and the final format.

TEXT
Q: A box holds 12 eggs. How many boxes are needed for 30 eggs?
Reasoning: 30 / 12 = 2.5. You cannot buy half a box, so round up to 3.
Answer: 3

Q: A train leaves at 9:40 and the trip takes 2 h 35 min. When does it arrive?
Reasoning: 9:40 + 2 h = 11:40. 11:40 + 35 min = 12:15.
Answer: 12:15

Q: {{new question}}
Reasoning:

Use worked examples that match the kind of problem you will ask.

Keep the reasoning and the answer apart

If a program reads the result, make the final answer easy to extract. Two patterns work well:

  • A fixed final line: Answer: <value>.
  • Tags: <reasoning>...</reasoning><answer>...</answer>.

Then parse only the answer part.

Python Extract the final answer from a reasoning reply

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The third reply shows why a format instruction matters: without a marker, extraction fails and you need a fallback (or a retry).

Verify with code

Reasoning helps, but it can still be wrong, and a plausible chain of steps is not proof. When the answer is checkable, check it. The sample below recomputes the arithmetic from the example above.

Python Verify the reasoning with code

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For arithmetic-heavy tasks, the stronger design is to let the model write the formula or code and let your program execute it (see tool use).

When CoT helps and when it does not

Helps Rarely helps or hurts
Multi-step arithmetic and word problems Simple lookups and classification
Logic and constraint puzzles Tasks where speed or cost dominates
Planning and comparing options Creative tasks with no right answer
Debugging and root-cause analysis Very small models that reason poorly

It also costs tokens and time: reasoning is output, and output is the slow, expensive part. Use it when accuracy on hard cases is worth that cost.

Reasoning models

Some models are trained to reason internally before answering, sometimes with settings for how much to think. With them, an explicit "think step by step" may add little or interfere, and the better prompt often states the problem clearly and the desired answer format, then lets the model work. Check your provider's guidance for the model you use, and measure rather than assume.

A note on faithfulness

The written reasoning is not a guaranteed record of how the model reached its answer. It can read convincingly and still contain a mistake, or justify an answer chosen for other reasons. Treat it as a useful working aid, not as a proof, and verify high-stakes answers independently.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why can writing out steps improve accuracy?

    Reasoning written in the output is available as context, which helps on multi-step problems.
  2. 2. How should you make a CoT answer easy for a program to read?

    A fixed marker or tag lets you parse only the answer.
  3. 3. For which task is CoT least likely to help?

    Simple tasks gain little and the reasoning adds cost.
  4. 4. Is written reasoning proof the answer is correct?

    Reasoning is a working aid, not a guarantee.

Summary

  • Chain-of-thought asks for intermediate steps before the answer, which often helps on multi-step tasks.
  • Use zero-shot ("step by step") or few-shot worked examples, and fix the answer format so it can be extracted.
  • Verify checkable answers with code; offload exact math to tools.
  • It costs tokens, helps less on simple tasks, and may be unnecessary with reasoning models.

Next: Self-Consistency and Prompt Chaining