⚡0 XP🔥0

Chain-of-Thought (Step-by-Step Reasoning)

Lesson 8 of 19 ⏱ 4 min read ✎ 2 exercises ❓ 4-question quiz ▶ Runs Python

Improve accuracy on maths, logic and multi-step problems by asking the model to reason step by step - simple CoT, guided steps, separating reasoning from the final answer, and reasoning models.

The problem: jumping to answers#

Models generate answers token by token. On a multi-step problem, if the model writes the final number immediately, it has to "compute" everything in one jump - and often gets it wrong.

A coaching institute charges Rs. 8,000 per course. It gives 10% off for full
payment and an extra Rs. 500 off for students referred by an alumnus.
Ravi pays in full and was referred. A 18% GST applies on the final fee.
How much does Ravi pay? Answer with just the number.

Asking for "just the number" makes errors more likely.

Chain-of-thought (CoT)#

Ask the model to think step by step. Writing out the intermediate steps gives it "space to reason":

...How much does Ravi pay? Work it out step by step, then give the final amount.

Typical answer:

1. Base fee: 8,000
2. 10% full-payment discount: 8,000 - 800 = 7,200
3. Referral discount: 7,200 - 500 = 6,700
4. GST 18%: 6,700 × 1.18 = 7,906
Final amount: Rs. 7,906

Each step is simple, so the final answer is far more reliable - and you can check every step.

हिंदी में समझें

Chain-of-Thought = AI से कहना कि "सीधे जवाब मत दो, पहले कदम-दर-कदम (step by step) सोचो, फिर जवाब दो।"

जैसे गणित की परीक्षा में अगर आप सीधे उत्तर लिखें तो गलती की संभावना ज़्यादा होती है, लेकिन हर step लिखकर हल करें तो गलती कम होती है - और कोई गलती हो भी तो दिख जाती है। AI के साथ भी ठीक यही होता है।

यह तरीका गणित, logic, तुलना (comparison), planning और debugging जैसे कई कदम वाले सवालों में सबसे ज़्यादा काम आता है।

Three levels of CoT#

1. Basic#

Think step by step.

2. Guided steps - you specify the steps#

Decide which laptop suits a BCA student with a Rs. 55,000 budget.
Steps:
1. List the student's needs (programming, battery, portability).
2. Score each laptop below from 1-5 on each need.
3. Check which laptops fit the budget.
4. Recommend one and explain in 3 lines.

<laptops> ... </laptops>

Guided steps are best when you know the right method - for example your company's evaluation process.

3. Structured - separate thinking from the answer#

Solve the problem. Put your reasoning inside <thinking> tags and the final
answer inside <answer> tags.

This lets you show only the <answer> to a student or customer, while still getting the accuracy benefit. Code can extract it easily:

▶ Try it YourselfPython
Ctrl + Enter to run

When to use chain-of-thought#

Use CoT Skip CoT
Maths and word problems Simple facts ("capital of Kerala")
Comparing options with several factors Translation of a sentence
Debugging code ("trace what happens line by line") Short rewrites
Planning and scheduling Formatting / extraction of clearly stated data
Logic puzzles and eligibility rules Brainstorming lists
Analysing a case or contract clause

CoT makes answers longer and slower (more tokens = more cost on APIs), so use it where accuracy matters.

Reasoning models#

Newer AI models include a built-in thinking / reasoning mode - they reason internally before answering (you may see "Thinking..." in the app). With these:

  • You don't need to say "think step by step" as much.
  • Clear problem statements and context still matter most.
  • High-level guidance ("consider tax implications and hidden fees") works better than forcing exact steps.
Tip

Whether or not the model shows its reasoning, verify numbers yourself for anything important - use a calculator, Excel or a quick Python script. AI is good at setting up calculations; code is better at doing them exactly.

▶ Try it YourselfPython
Ctrl + Enter to run

Ask the model to check itself#

Add a verification step at the end:

...After giving the answer, double-check each calculation and confirm
whether the final amount is correct.

You'll learn more self-review techniques in the Iteration and Self-Critique lesson.

Summary#

  • Chain-of-thought = ask the model to reason step by step before answering.
  • It improves accuracy on maths, logic, comparisons, planning and debugging.
  • Guide the steps when you know the method; use <thinking>/<answer> tags to separate reasoning from output.
  • Reasoning models think internally - focus on a clear problem and context.
  • Always verify important calculations with real tools.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Extract the final answer

Write final_answer(reply) that returns the text between <answer> and </answer> (stripped of spaces). If the tags are missing, return None.

2

Verify the AI's arithmetic

An AI said a shop's profit percentage is 25%. Check it with code: cost price is 1,200, selling price is 1,440. Write profit_percent(cost, sell) returning the profit % rounded to 2 decimals, then print whether the AI was right as AI correct: True/False.

Expected output:
AI correct: False

Quick Quiz

Q1.

What is chain-of-thought prompting?

Q2.

For which task is chain-of-thought LEAST needed?

Q3.

Why separate reasoning and the final answer with tags like and ?

Q4.

"Reasoning models" (models with a built-in thinking mode) mean that...