Chain-of-Thought (CoT)

Forcing models to reason step-by-step.

Making LLMs Think Step by Step

CoT Reasoning Flow

Question "r's in strawberry?" Step 1 spell it out s-t-r-a-w-b-e-r-r-y Step 2 find r's: st[r]awbe[r][r]y Answer: 3 ✓ "Let's think step by step..."

Chain-of-Thought prompting improves LLM performance on reasoning tasks by asking the model to show its work. Instead of jumping to an answer, the model breaks down the problem into intermediate steps.

Why It Works

LLMs generate one token at a time. When forced to generate reasoning steps, each step provides additional context for the next token prediction, leading to more accurate final answers.

Variants

  • Manual CoT: You provide step-by-step examples
  • Zero-shot CoT: Simply add "Let's think step by step" to your prompt
  • Self-consistency: Generate multiple CoT paths and vote on the most common answer
  • Tree of Thought: Explore multiple reasoning branches, backtrack when needed

Code Example

Adding "Let's solve this step by step" dramatically improves accuracy on math and logic problems.

python
1from openai import OpenAI
2client = OpenAI()
3
4# Without CoT (often wrong for complex math)
5response_no_cot = client.chat.completions.create(
6    model="gpt-4o",
7    messages=[{"role": "user", "content": 
8        "If a store has 5 apples and gets 3 shipments of 12 apples each, "
9        "then sells 60% of all apples, how many are left?"
10    }]
11)
12
13# With CoT (much more reliable)
14response_cot = client.chat.completions.create(
15    model="gpt-4o",
16    messages=[{"role": "user", "content": 
17        "If a store has 5 apples and gets 3 shipments of 12 apples each, "
18        "then sells 60% of all apples, how many are left?\n\n"
19        "Let's solve this step by step."
20    }]
21)

Use Cases

Mathematical reasoning and word problems
Multi-step logical deductions
Code debugging (explain the bug step by step)
Complex decision-making with multiple factors

Common Mistakes

Using CoT for simple tasks where it wastes tokens without benefit
Not validating the reasoning steps — the model can produce plausible but wrong reasoning
Forgetting that CoT increases output token count and thus cost

Interview Insight

Relevance

High - Key reasoning technique

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In