🧠 Reasoning Models
🌟 Start With a Real-World Scenario
You ask GPT-4 a hard math problem:
"In a tournament, each team plays every other team exactly once.
If there were 45 games total, how many teams participated?"
GPT-4 (Instant Response - 2 seconds):
"There were 10 teams.
Explanation: Using the formula n(n-1)/2 = 45
Solving: n² - n - 90 = 0
Therefore n = 10"
❌ WRONG! (Correct answer is 10 teams, but the math is off!)
You ask o1 (Reasoning Model) the same question:
"In a tournament, each team plays every other team exactly once.
If there were 45 games total, how many teams participated?"
o1 (Thinks for 30 seconds):
*Thinking...*
Let me work through this step by step.
First, I need to recall the formula for combinations.
When n teams each play every other team once, the number of games is:
C(n,2) = n(n-1)/2
So I need to solve: n(n-1)/2 = 45
Multiplying both sides by 2: n(n-1) = 90
Expanding: n² - n = 90
Rearranging: n² - n - 90 = 0
Using quadratic formula: n = (1 ± √(1 + 360))/2
n = (1 ± √361)/2
n = (1 ± 19)/2
So n = 20/2 = 10 or n = -18/2 = -9
Since teams can't be negative, n = 10.
Let me verify: 10(9)/2 = 90/2 = 45 ✓
Answer: 10 teams
✅ CORRECT! And shows all the work!
The Difference:
Regular LLMs (GPT-4, Claude):
- Think time: 1-3 seconds
- Reasoning: Surface-level
- Accuracy on hard problems: 60-70%
- Shows work: Sometimes
- Self-corrects: Rarely
Reasoning Models (o1, o3):
- Think time: 10-60 seconds
- Reasoning: Deep, step-by-step
- Accuracy on hard problems: 85-95%
- Shows work: Always
- Self-corrects: Frequently
Real-World Performance:
Math Olympiad Problems:
- GPT-4: 13% correct
- o1: 83% correct
- 6.4x better! 🤯
Coding Competitions (Codeforces):
- GPT-4: 11th percentile
- o1: 89th percentile
- Better than 89% of human programmers! 💻
PhD-Level Science Questions:
- GPT-4: 56% correct
- o1: 78% correct
- PhD-level reasoning! 🎓
Legal Bar Exam:
- GPT-4: 76th percentile
- o1: 93rd percentile
- Better than most lawyers! ⚖️
How Reasoning Models Work:
Regular LLM (Fast but Shallow):
Question → [Think 2 sec] → Answer
Reasoning Model (Slow but Deep):
Question → [Think 30 sec with internal monologue]
↓
"Let me break this down..."
↓
"First, I'll try approach A..."
↓
"Wait, that doesn't work. Let me try B..."
↓
"Hmm, I need to verify this..."
↓
"Let me check my work..."
↓
"Yes, this is correct!"
↓
Answer (with full reasoning shown)
The "Thinking" Process:
Example: Solving a Logic Puzzle
o1's Internal Monologue:
User: "If all roses are flowers, and some flowers fade quickly,
can we conclude that some roses fade quickly?"
[o1 thinking - 15 seconds]
Thought 1: "This is a logic problem about syllogisms."
Thought 2: "Let me identify the premises:
- P1: All roses are flowers (roses ⊂ flowers)
- P2: Some flowers fade quickly"
Thought 3: "The question asks if we can conclude:
Some roses fade quickly"
Thought 4: "Wait, this is a trap! Just because SOME flowers
fade quickly doesn't mean those flowers are roses."
Thought 5: "Let me draw a Venn diagram mentally:
- Big circle: All flowers
- Small circle inside: All roses
- Some dots in big circle: Flowers that fade quickly
- But those dots might NOT overlap with roses!"
Thought 6: "So the answer is NO, we cannot conclude that."
Thought 7: "Let me verify with a counterexample:
- All roses are flowers ✓
- Some flowers (like daisies) fade quickly ✓
- But roses might not fade quickly ✓
- Counterexample works!"
Answer: "No, we cannot conclude that some roses fade quickly.
The premises only tell us that some flowers fade quickly, but
those flowers might not be roses. This is a logical fallacy."
✅ Correct reasoning with self-verification!
When to Use Reasoning Models:
✅ Perfect for:
- Complex math problems
- Coding challenges
- Logic puzzles
- Scientific reasoning
- Legal analysis
- Strategic planning
- Debugging complex issues
❌ Not needed for:
- Simple questions ("What's the capital of France?")
- Creative writing
- Casual conversation
- Quick facts
- Translation
Why? Reasoning models are:
- 10-20x slower
- 3-5x more expensive
- Overkill for simple tasks
The Cost Trade-off:
GPT-4:
- Speed: 2 seconds
- Cost: $0.03 per query
- Accuracy (hard problems): 60%
o1:
- Speed: 30 seconds (15x slower!)
- Cost: $0.15 per query (5x more expensive!)
- Accuracy (hard problems): 90%
When is it worth it?
- Critical decisions: YES
- High-stakes problems: YES
- Learning/education: YES
- Casual chat: NO
Real Applications:
1. Medical Diagnosis
Symptoms: Complex, ambiguous case
o1 reasoning:
1. Lists all possible diagnoses
2. Evaluates each systematically
3. Rules out unlikely ones
4. Considers rare conditions
5. Recommends specific tests
6. Provides confidence levels
Result: More accurate than GPT-4, safer for patients
2. Legal Contract Analysis
Contract: 50-page agreement with complex clauses
o1 reasoning:
1. Identifies all key terms
2. Spots potential issues
3. Considers edge cases
4. Evaluates risks
5. Suggests modifications
6. Explains reasoning
Result: Catches issues human lawyers might miss
3. Software Debugging
Bug: Complex, intermittent issue
o1 reasoning:
1. Analyzes code flow
2. Identifies potential causes
3. Tests hypotheses
4. Eliminates possibilities
5. Finds root cause
6. Suggests fix with explanation
Result: Solves bugs faster than GPT-4
🗺️ Concept Mind Map
REASONING MODELS
|
+---------------+---------------+
| | |
APPROACHES TECHNIQUES MODELS
| | |
| | |
Fast Thinking Chain-of-Thought GPT-4
- Instant - Step-by-step - Fast
- Intuitive - Show work - Good quality
- Sometimes - Transparent - General use
wrong - Better accuracy
| | |
| | |
Slow Thinking Self-Verification o1 (OpenAI)
- Deliberate - Check answer - Slow (thinks)
- Careful - Catch errors - Very accurate
- More - Self-correct - Math/coding
accurate - Confidence |
| | |
| | o1-mini
System 1 Multi-Path - Faster o1
(Intuition) Reasoning - Cheaper
- Quick - Try multiple - Good enough
- Automatic approaches - Most tasks
- Heuristics - Vote on best
- Biases - Ensemble |
| | |
| | Claude 3
System 2 Tree of Thoughts - Constitutional
(Reasoning) - Branch out reasoning
- Slow - Explore paths - Safe outputs
- Deliberate - Backtrack - Helpful
- Logical - Find best
- Accurate
Fast vs Slow Thinking:
FAST MODEL (GPT-4):
Q: "What's 17 × 23?"
A: "391" (instant, but wrong! ❌)
Time: 0.5 seconds
Cost: $0.001
REASONING MODEL (o1):
Q: "What's 17 × 23?"
Thinking: "Let me break this down...
17 × 20 = 340
17 × 3 = 51
340 + 51 = 391"
A: "391" (correct! ✅)
Time: 3-5 seconds
Cost: $0.015 (15x more expensive)
When to Use Each:
TYPE SPEED COST ACCURACY USE CASE
Fast (GPT-4) ⭐⭐⭐⭐⭐ $ ⭐⭐⭐ General chat
Reasoning(o1) ⭐⭐ $$$ ⭐⭐⭐⭐⭐ Math, coding, logic
Reasoning Techniques:
1. CHAIN OF THOUGHT
"Let's think step by step..."
→ Shows intermediate steps
→ Catches errors early
2. SELF-VERIFICATION
Generate answer → Check it → Correct if wrong
→ Higher accuracy
→ More reliable
3. MULTI-PATH REASONING
Try 3 different approaches → Vote on answer
→ Robust to errors
→ Best for critical tasks
4. TREE OF THOUGHTS
Branch out → Explore → Backtrack → Find best path
→ Complex problems
→ Optimal solutions
Key Insight: Reasoning models trade speed/cost for accuracy. Use them when correctness matters more than speed!
zations](#interactive-tools--visualizations) 3. Fun Facts & Hacks 4. What You'll Learn 5. Core Concept 6. Code Examples 7. Hands-On Exercises 8. Assignments & Practice Problems 9. Common Pitfalls & Debugging 10. Related Concepts 11. Resources 12. Key Takeaways 13. Next Steps
🛠️ Local Setup (15 Minutes)
Set up reasoning model environment:
Step 1: Install Dependencies
pip install openai==1.3.0 \
anthropic==0.7.0 \
langchain==0.1.0 \
guidance==0.1.0
Step 2: Set Up API Keys
echo "OPENAI_API_KEY=your-key" > .env
Step 3: Test Reasoning
from openai import OpenAI
client = OpenAI()
# Use o1-preview for reasoning
response = client.chat.completions.create(
model="o1-preview",
messages=[{
"role": "user",
"content": "Solve: If x + 2 = 5, what is x?"
}]
)
print(response.choices[0].message.content)
print("✅ Reasoning model working!")
🧠 Core Concept
The Problem: Fast But Wrong
Traditional LLMs respond instantly but make mistakes:
# User: "What is 17 * 23?"
# GPT-4: "391" ❌ (Wrong! It's 391)
# (Answered too quickly, didn't verify)
The Solution: Think Before Answering
Reasoning models take time to think:
# User: "What is 17 * 23?"
# o1: *thinking for 5 seconds*
# o1: "Let me calculate: 17 * 23 = 17 * 20 + 17 * 3 = 340 + 51 = 391" ✅
# (Showed work, verified answer)
How Reasoning Models Work
1. Extended Chain-of-Thought:
Problem → Think → Think → Think → Answer
2. Self-Verification:
Answer → Check → Correct if wrong → Final Answer
3. Iterative Refinement:
Draft 1 → Review → Draft 2 → Review → Final
Key Differences
| Feature | GPT-4 | o1-preview |
|---|---|---|
| Response Time | Instant | 10-30 seconds |
| Thinking Tokens | ~100 | ~10,000 |
| Self-Correction | No | Yes |
| Math Accuracy | 85% | 95% |
| Cost | Lower | Higher |
💻 Code Examples
Example 1: Basic Reasoning with o1
from openai import OpenAI
client = OpenAI()
def solve_with_reasoning(problem: str):
"""
Solve problem using o1 reasoning model.
"""
print(f"Problem: {problem}")
print("Thinking...")
response = client.chat.completions.create(
model="o1-preview",
messages=[{
"role": "user",
"content": problem
}]
)
answer = response.choices[0].message.content
print(f"\nAnswer: {answer}")
return answer
# Test with math problem
solve_with_reasoning(
"A train travels 120 km in 2 hours. "
"If it maintains the same speed, how far will it travel in 5 hours?"
)
# Test with logic problem
solve_with_reasoning(
"If all roses are flowers and some flowers fade quickly, "
"can we conclude that some roses fade quickly?"
)
# Test with coding problem
solve_with_reasoning(
"Write a Python function to find the longest palindromic substring. "
"Explain your approach and verify with examples."
)
Example 2: Simulating Reasoning (Without o1)
from openai import OpenAI
client = OpenAI()
class ReasoningSimulator:
"""
Simulate reasoning process with regular LLM.
"""
def __init__(self, model: str = "gpt-4"):
self.model = model
def think_step_by_step(self, problem: str) -> dict:
"""
Break down problem into steps.
"""
# Step 1: Understand the problem
understanding = self._call_llm(
f"Explain what this problem is asking:\n{problem}"
)
# Step 2: Plan approach
plan = self._call_llm(
f"Problem: {problem}\n"
f"Understanding: {understanding}\n"
f"What steps should we take to solve this?"
)
# Step 3: Execute plan
solution = self._call_llm(
f"Problem: {problem}\n"
f"Plan: {plan}\n"
f"Now solve it step-by-step:"
)
# Step 4: Verify answer
verification = self._call_llm(
f"Problem: {problem}\n"
f"Solution: {solution}\n"
f"Is this solution correct? Check for errors."
)
return {
"understanding": understanding,
"plan": plan,
"solution": solution,
"verification": verification
}
def _call_llm(self, prompt: str) -> str:
"""Call LLM with prompt."""
response = client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
temperature=0
)
return response.choices[0].message.content
# Example usage
reasoner = ReasoningSimulator()
problem = "If 5 machines can make 5 widgets in 5 minutes, how long would it take 100 machines to make 100 widgets?"
result = reasoner.think_step_by_step(problem)
print("=== Reasoning Process ===\n")
print(f"Understanding:\n{result['understanding']}\n")
print(f"Plan:\n{result['plan']}\n")
print(f"Solution:\n{result['solution']}\n")
print(f"Verification:\n{result['verification']}")
Example 3: Self-Verification System
from openai import OpenAI
from typing import List, Dict
client = OpenAI()
class SelfVerifyingReasoner:
"""
Reasoning system with self-verification.
"""
def __init__(self, model: str = "gpt-4"):
self.model = model
def solve_with_verification(self, problem: str, max_attempts: int = 3) -> Dict:
"""
Solve problem with self-verification loop.
"""
attempts = []
for attempt in range(max_attempts):
print(f"\n--- Attempt {attempt + 1} ---")
# Generate solution
solution = self._generate_solution(problem)
print(f"Solution: {solution[:100]}...")
# Verify solution
is_correct, feedback = self._verify_solution(problem, solution)
attempts.append({
"attempt": attempt + 1,
"solution": solution,
"is_correct": is_correct,
"feedback": feedback
})
if is_correct:
print("✅ Solution verified!")
break
else:
print(f"❌ Error found: {feedback}")
problem = f"{problem}\n\nPrevious attempt was wrong: {feedback}\nTry again:"
return {
"final_solution": attempts[-1]["solution"],
"attempts": attempts,
"verified": attempts[-1]["is_correct"]
}
def _generate_solution(self, problem: str) -> str:
"""Generate solution."""
response = client.chat.completions.create(
model=self.model,
messages=[{
"role": "user",
"content": f"Solve this problem step-by-step:\n{problem}"
}],
temperature=0.7
)
return response.choices[0].message.content
def _verify_solution(self, problem: str, solution: str) -> tuple:
"""Verify if solution is correct."""
response = client.chat.completions.create(
model=self.model,
messages=[{
"role": "user",
"content": f"""Problem: {problem}
Proposed Solution: {solution}
Is this solution correct? Respond with:
1. "CORRECT" if the solution is right
2. "INCORRECT: [explanation]" if there are errors
Be thorough in checking the logic and calculations."""
}],
temperature=0
)
verification = response.choices[0].message.content
if "CORRECT" in verification and "INCORRECT" not in verification:
return True, verification
else:
return False, verification
# Example usage
reasoner = SelfVerifyingReasoner()
problem = """
A farmer has chickens and cows.
The animals have a total of 30 heads and 74 legs.
How many chickens and how many cows does the farmer have?
"""
result = reasoner.solve_with_verification(problem)
print("\n=== Final Result ===")
print(f"Solution: {result['final_solution']}")
print(f"Verified: {result['verified']}")
print(f"Attempts needed: {len(result['attempts'])}")
Example 4: Multi-Path Reasoning
from openai import OpenAI
from collections import Counter
client = OpenAI()
class MultiPathReasoner:
"""
Generate multiple reasoning paths and pick best answer.
"""
def __init__(self, model: str = "gpt-4", num_paths: int = 3):
self.model = model
self.num_paths = num_paths
def solve_with_consensus(self, problem: str) -> Dict:
"""
Solve using multiple reasoning paths.
"""
solutions = []
print(f"Generating {self.num_paths} reasoning paths...")
for i in range(self.num_paths):
print(f"\nPath {i + 1}:")
solution = self._generate_solution(problem, temperature=0.7)
# Extract final answer
answer = self._extract_answer(solution)
solutions.append({
"path": i + 1,
"reasoning": solution,
"answer": answer
})
print(f"Answer: {answer}")
# Find consensus answer
answers = [s["answer"] for s in solutions]
answer_counts = Counter(answers)
consensus_answer = answer_counts.most_common(1)[0][0]
confidence = answer_counts[consensus_answer] / len(answers)
return {
"solutions": solutions,
"consensus_answer": consensus_answer,
"confidence": confidence,
"agreement": f"{answer_counts[consensus_answer]}/{len(answers)}"
}
def _generate_solution(self, problem: str, temperature: float) -> str:
"""Generate one solution path."""
response = client.chat.completions.create(
model=self.model,
messages=[{
"role": "user",
"content": f"Solve this problem step-by-step:\n{problem}"
}],
temperature=temperature
)
return response.choices[0].message.content
def _extract_answer(self, solution: str) -> str:
"""Extract final answer from solution."""
# Simple extraction - look for "answer is" or similar
lines = solution.lower().split('\n')
for line in lines:
if 'answer' in line or 'therefore' in line or 'final' in line:
return line.strip()
return solution.split('\n')[-1].strip()
# Example usage
reasoner = MultiPathReasoner(num_paths=5)
problem = """
You have a 3-gallon jug and a 5-gallon jug.
How can you measure exactly 4 gallons of water?
"""
result = reasoner.solve_with_consensus(problem)
print("\n=== Consensus Result ===")
print(f"Consensus Answer: {result['consensus_answer']}")
print(f"Confidence: {result['confidence']:.0%}")
print(f"Agreement: {result['agreement']}")
Example 5: Reasoning with Verification Prompts
from openai import OpenAI
client = OpenAI()
class VerifiedReasoner:
"""
Use special prompts to encourage verification.
"""
VERIFICATION_PROMPTS = {
"math": """Solve this math problem step-by-step.
After solving, verify your answer by:
1. Checking your arithmetic
2. Trying a different method
3. Confirming the answer makes sense
Problem: {problem}""",
"logic": """Solve this logic problem.
After solving, verify by:
1. Checking if your conclusion follows from premises
2. Looking for counterexamples
3. Ensuring no logical fallacies
Problem: {problem}""",
"coding": """Write code to solve this problem.
After writing, verify by:
1. Testing with example inputs
2. Checking edge cases
3. Reviewing for bugs
Problem: {problem}"""
}
def solve(self, problem: str, problem_type: str = "math") -> str:
"""
Solve with verification prompt.
"""
prompt = self.VERIFICATION_PROMPTS.get(
problem_type,
self.VERIFICATION_PROMPTS["math"]
).format(problem=problem)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
return response.choices[0].message.content
# Example usage
reasoner = VerifiedReasoner()
# Math problem
math_solution = reasoner.solve(
"If f(x) = 2x + 3 and g(x) = x^2, what is f(g(2))?",
problem_type="math"
)
print("Math Solution:")
print(math_solution)
# Logic problem
logic_solution = reasoner.solve(
"All birds can fly. Penguins are birds. Can penguins fly?",
problem_type="logic"
)
print("\nLogic Solution:")
print(logic_solution)
# Coding problem
code_solution = reasoner.solve(
"Write a function to check if a string is a palindrome",
problem_type="coding"
)
print("\nCoding Solution:")
print(code_solution)
📝 Assignments & Practice Problems
Assignment 1: Reasoning Comparison ⭐⭐
Difficulty: Medium | Time: 60 minutes
Compare reasoning approaches.
Requirements:
- Test 10 problems
- Compare GPT-4 vs o1
- Measure accuracy
- Analyze differences
Assignment 2: Build Reasoner ⭐⭐⭐
Difficulty: Hard | Time: 120 minutes
Build complete reasoning system.
Requirements:
- Multi-step reasoning
- Self-verification
- Error correction
- Performance metrics
❌ Bad: Too much thinking for simple tasks
Using o1 for "What is 2+2?"
✅ Good: Match model to task complexity
Use GPT-4 for simple, o1 for complex
### Pitfall 2: No Verification
```python
# ❌ Bad: Trust first answer
answer = generate_solution(problem)
# ✅ Good: Always verify
answer = generate_solution(problem)
verified = verify_solution(problem, answer)
🔗 Related Concepts
- Previous: Chain of Thought - Basic reasoning
- Next: Multi-modal Models - Beyond text
- Related: AI Agents - Autonomous reasoning
- Foundation: LLMs - Base models
📚 Resources
Papers
- 📄 o1 System Card - OpenAI's reasoning model
- 📄 Chain-of-Thought Prompting
Tools
pip install openai # For o1 access
pip install guidance # Structured reasoning
