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).
These are a new breed trained with Reinforcement Learning specifically to reason.
Key differences:
Feature
Classic CoT
Reasoning Models (o1, R1, QwQ)
Length of reasoning
Short (~100 tokens)
Very long (1,000–100,000 tokens!)
Backtracking
No — moves forward only
✅ Yes — says "Wait, that's wrong, let me reconsider..."
Self-correction
Rare
✅ Built-in
Training
SFT on reasoning examples
RL with verifiable rewards (math/code answers checked)
Visibility
User sees reasoning
Reasoning often hidden (o1) or shown (R1, QwQ)
Compute cost
Normal
10–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
Aspect
How It Works
CoT mechanism
Just next-token prediction — but intermediate tokens act as "scratchpad"
Why it works
Each 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. 🧠✨
Understanding RAG (Retrieval-Augmented Generation): A Complete Beginner's Guide
Ever asked ChatGPT about your company's internal documents and got a confused answer? That's because LLMs only know what they were trained on. RAG fixes this by giving the LLM a "library card" to look things up before answering.
Let's break it down simply. 🚀
🤔 What is RAG?
RAG = Retrieval + Augmented + Generation
Think of it like an open-book exam:
📚 Retrieval → Find the right pages in the book
➕ Augmented → Add those pages to your question
✍️ Generation → Write the answer using those pages
Instead of the LLM guessing from memory, it looks up relevant information first, then answers.
Big documents are cut into smaller chunks (usually 200–1000 tokens) because:
LLMs have context limits
Smaller chunks = more precise retrieval
Code
📄 Full Document (10,000 words)
│
▼ Split
┌────┬────┬────┬────┬────┐
│ C1 │ C2 │ C3 │ C4 │ C5 │ ← each ~500 words with slight overlap
└────┴────┴────┴────┴────┘
✂️ Chunking Strategies (with Examples & How to Implement)
1. Fixed-size — Blindly splits text every N words/tokens, ignoring meaning. Fast but may cut mid-sentence.
Example:"The cat sat on the mat. The dog ran fast. The bird flew" → split every 5 words → ["The cat sat on the", "mat. The dog ran fast.", "The bird flew"]
How to implement: Use LangChain's CharacterTextSplitter(chunk_size=100, chunk_overlap=20) — just set a size and let it slice.
2. Recursive — Tries to split at natural boundaries first (paragraphs → sentences → words), only going smaller if a chunk is still too big. Keeps meaning intact.
Example:"Intro paragraph.\n\nSecond paragraph with two sentences. Like this one." → splits by paragraphs first, then sentences → ["Intro paragraph.", "Second paragraph with two sentences.", "Like this one."]
How to implement: Use LangChain's RecursiveCharacterTextSplitter(chunk_size=200, separators=["\n\n", "\n", ".", " "]) — it walks the separator list top-down.
3. Semantic — Uses an embedding model to detect where the topic shifts and splits there, so each chunk covers one idea.
Example:"I love pizza. Pasta is great too. Meanwhile, quantum physics studies particles." → detects topic change → ["I love pizza. Pasta is great too.", "Meanwhile, quantum physics studies particles."]
How to implement: Use LangChain's SemanticChunker(OpenAIEmbeddings()) — it embeds each sentence and splits where similarity drops sharply.
4. Document-aware — Understands the document's structure (Markdown headers, code blocks, HTML tags, tables) and never breaks them mid-way.
An AI agent decides when and how to retrieve — multi-step, tool-using.
Code
Query ──► Agent ──► Decides: "I need to search docs + call API + calculate"
│
├─► Tool 1: Vector DB
├─► Tool 2: SQL Database
└─► Tool 3: Web Search
│
▼
Answer
🟠 6. Multi-Modal RAG
Retrieves not just text but also images, tables, charts, audio.
Code
📄 Text ─┐
🖼️ Image ─┼─► Multi-modal Embedding ─► Vector DB ─► LLM (GPT-4V, Gemini)
📊 Table ─┘
⚫ 7. Self-RAG / Corrective RAG (CRAG)
The model critiques its own retrieval and re-searches if the results are bad.
Code
Retrieve ──► Evaluate: "Is this relevant?"
│
┌─────────┴─────────┐
▼ ▼
✅ Good ❌ Bad
Generate Re-search / Web search
📊 Quick Comparison Table
Strategy
Best For
Complexity
Naive RAG
Prototypes, simple Q&A
⭐
Advanced RAG
Production chatbots
⭐⭐
Hybrid RAG
Technical docs, product catalogs
⭐⭐
Graph RAG
Reasoning, connected data
⭐⭐⭐⭐
Agentic RAG
Complex workflows, multi-tool
⭐⭐⭐⭐
Multi-Modal RAG
Docs with images/charts
⭐⭐⭐
Self/Corrective
High-accuracy needs
⭐⭐⭐
✅ Why RAG is a Game-Changer
🎯 Reduces hallucinations — answers grounded in real data
🔄 Always up-to-date — just update the docs, no retraining
💰 Cheaper than fine-tuning
🔒 Keeps your data private — no need to send it to model training
📎 Cites sources — users can verify answers
🎬 Final Thoughts
RAG isn't magic — it's just a smart librarian + a good writer working together. Start simple with Naive RAG, then add rerankers, hybrid search, or agents as your needs grow.
💡 Pro tip: 80% of RAG quality comes from good chunking + good embeddings + good retrieval. Fancy strategies help, but nail the basics first.