Sunday, July 12, 2026

🧠 Chain-of-Thought (CoT) — Deep Dive

 

🧠 Chain-of-Thought (CoT) — Deep Dive


🤔 The Core Confusion

You're right: an LLM only does one thing — predict the next token. So how does it "think"?

Answer: It doesn't actually think. CoT is a trick where we make the LLM generate intermediate reasoning tokens before the final answer — and those tokens act as a "scratchpad" that improves the final prediction.


🎯 What is Chain-of-Thought?

Without CoT:

Code
Q: If I have 5 apples, give away 2, then buy 3 more, how many do I have?
A: 6  ❌ (might be wrong — model guesses)

With CoT:

Code
Q: If I have 5 apples, give away 2, then buy 3 more, how many do I have?
A: Let's think step by step.
   - Start with 5 apples
   - Give away 2 → 5 - 2 = 3
   - Buy 3 more → 3 + 3 = 6
   Final answer: 6  ✅

Same model, same weights — but way better accuracy just by forcing it to "show its work."


🔍 How Does It Actually Work Mechanically?

Remember: LLMs predict one token at a time, each new token conditioned on all previous ones.

Code
Input:  "Q: 5 apples, give 2, buy 3. How many? A:"
                                                    │
                                                    ▼
Predict token 1: "Let's"
Predict token 2: "think"  (now sees "...A: Let's")
Predict token 3: "step"   (now sees "...A: Let's think")
Predict token 4: "by"
...
Predict token N: "5"
Predict token N+1: "-"
Predict token N+2: "2"
Predict token N+3: "="
Predict token N+4: "3"    ← This "3" is now in context!
...
Predict final: "6"        ← Model sees "3 + 3 =" and easily predicts "6"

💡 The Magic Insight

By generating "5 - 2 = 3" as tokens, that intermediate result becomes part of the context for predicting the next token. The model has essentially written the answer to a sub-problem into its own input, making the next step trivial.

Without CoT, the model has to jump from question → answer in one shot (hard). With CoT, it breaks it into many easy shots (easy).

Think of it like: solving 47 × 83 in your head vs. on paper. Same brain — but paper (extra tokens) helps a lot.


🎓 Do LLMs Get Trained on CoT?

Two layers here:

Layer 1: Emergent CoT (older models like GPT-3)

  • GPT-3 wasn't specifically trained for CoT.
  • But since the internet is full of step-by-step explanations (math tutorials, Stack Overflow, textbooks), the model learned the pattern of reasoning naturally.
  • You just had to prompt it with "Let's think step by step" to trigger this pattern.
  • This is called emergent reasoning — it appeared in models >~60B parameters.

Layer 2: Trained CoT (modern models like GPT-4, Claude, Gemini)

  • Modern LLMs are explicitly fine-tuned on CoT-style data using techniques like:
    • SFT (Supervised Fine-Tuning) on datasets with step-by-step reasoning
    • RLHF rewarding step-by-step answers
    • Rejection sampling — generate many CoT answers, keep only correct ones, retrain on those
  • So yes — modern models are trained to do CoT by default, even without prompting.

🆚 CoT vs. "Reasoning Models" (Qwen-QwQ, DeepSeek-R1, OpenAI o1)

This is where it gets interesting. There are two generations of CoT:

🥉 Classic CoT (GPT-4, Claude 3.5, Llama)

  • Model produces a short reasoning chain (~5–20 steps).
  • Reasoning is linear — no backtracking.
  • Triggered by prompt or trained lightly.
Code
Question → [reason step 1] → [reason step 2] → [answer]

🥇 Reasoning Models (o1, DeepSeek-R1, Qwen-QwQ, Gemini Thinking)

These are a new breed trained with Reinforcement Learning specifically to reason.

Key differences:

FeatureClassic CoTReasoning Models (o1, R1, QwQ)
Length of reasoningShort (~100 tokens)Very long (1,000–100,000 tokens!)
BacktrackingNo — moves forward onlyYes — says "Wait, that's wrong, let me reconsider..."
Self-correctionRare✅ Built-in
TrainingSFT on reasoning examplesRL with verifiable rewards (math/code answers checked)
VisibilityUser sees reasoningReasoning often hidden (o1) or shown (R1, QwQ)
Compute costNormal10–100× more tokens = slower + expensive

Example — Qwen QwQ / DeepSeek-R1 style:

Code
Q: What's 15% of 240?

Reasoning (internal):
"Let me calculate. 15% = 0.15. So 0.15 × 240...
Actually, let me try another way. 10% of 240 = 24. 
5% = 12. So 15% = 24 + 12 = 36.
Wait, let me double-check: 0.15 × 240 = 36. ✓
Hmm, both methods give 36. I'm confident."

Final Answer: 36

Notice the "Wait, let me double-check" — this is a learned behavior from RL, not a prompt trick.


🏗️ How These Reasoning Models Are Built (Simplified)

DeepSeek-R1's recipe (roughly):

Code
1. Start with base LLM (e.g., DeepSeek-V3)
       │
       ▼
2. Generate 1000s of math/code problems
       │
       ▼
3. Let model produce long CoT solutions
       │
       ▼
4. Auto-check answers (math has right/wrong)
       │
       ▼
5. Reward correct reasoning chains with RL
       │
       ▼
6. Model learns: "Long thinking + self-check = reward"
       │
       ▼
7. Result: model that naturally does deep CoT

The model discovers on its own that backtracking, verifying, and exploring multiple paths leads to correct answers — because RL rewards it.


🎯 Summary Table

AspectHow It Works
CoT mechanismJust next-token prediction — but intermediate tokens act as "scratchpad"
Why it worksEach reasoning token becomes context for the next, breaking hard problems into easy ones
Old LLMs (GPT-3)CoT emerged from training data (internet text with reasoning)
Modern LLMs (GPT-4, Claude)Explicitly fine-tuned to do CoT
Reasoning LLMs (o1, R1, QwQ)RL-trained to do long, self-correcting CoT — a whole different level

💡 Final Intuition

CoT is just the LLM talking to itself out loud so it can think better. Reasoning models are LLMs that were rewarded (via RL) for talking to themselves really well — including saying "wait, I was wrong."

The mechanism is the same (next-token prediction). The difference is training and length of reasoning. 🧠✨

No comments:

Post a Comment