aiwithgrant
about me
LESSON 6 OF 126 min read

Chain-of-Thought Reasoning

Chain-of-Thought Reasoning

Chain-of-thought prompting asks the AI to show its reasoning step by step. This dramatically improves accuracy on complex tasks like math, logic, and analysis.

The Problem with Direct Answers

Without chain-of-thought, models often jump to conclusions:

text
Q: A store has 45 apples. They sell 60% on Monday, then
receive a shipment of 30 on Tuesday. On Wednesday they
sell half of what they have. How many are left?

A: 21

Sometimes the answer is wrong because the model skipped steps. Let's fix that.

Adding Chain-of-Thought

Simply ask the model to think step by step:

text
Q: A store has 45 apples. They sell 60% on Monday, then
receive a shipment of 30 on Tuesday. On Wednesday they
sell half of what they have. How many are left?

Think through this step by step before giving your answer.

A: Let me work through this:
- Start: 45 apples
- Monday: sell 60% → 45 × 0.6 = 27 sold → 45 - 27 = 18 left
- Tuesday: receive 30 → 18 + 30 = 48 apples
- Wednesday: sell half → 48 / 2 = 24 left

Answer: 24 apples

When to Use It

Chain-of-thought works best for:

Math problems — multi-step calculations
Logic puzzles — deductive reasoning
Analysis tasks — weighing pros and cons
Debugging — finding issues in code or arguments

The Magic Phrases

These phrases trigger step-by-step reasoning:

"Think through this step by step"
"Show your reasoning"
"Walk me through your thought process"
"Let's break this down"

Advanced: Chain-of-Thought in Code Analysis

text
Review this function for bugs. Think step by step:
1. What does the function intend to do?
2. Trace through with a sample input
3. Identify where it could fail
4. Suggest the fix

function findMax(arr) {
  let max = 0;
  for (let i = 1; i < arr.length; i++) {
    if (arr[i] > max) max = arr[i];
  }
  return max;
}

By forcing the model to reason step by step, it's much more likely to catch that max should be initialized to arr[0] and that the function doesn't handle empty arrays.