Lesson 4 of 8

Chain of Thought Prompting

Step-by-step reasoning techniques to improve complex problem solving

What is Chain of Thought (CoT)?

Chain of Thought prompting encourages the model to break down complex problems into intermediate reasoning steps. Instead of jumping directly to an answer, the model "thinks through" the problem step by step, dramatically improving accuracy on complex tasks.

This technique was introduced by Google researchers in 2022 and has become one of the most effective methods for improving LLM performance on reasoning tasks.

🧠 Why CoT Works

  • Breaks Complexity: Divides hard problems into manageable steps
  • Reduces Errors: Each step can be verified independently
  • Reveals Reasoning: You can see how the model reached its conclusion
  • Mimics Human Thinking: Mirrors how experts solve complex problems

Standard vs Chain of Thought

// ❌ Standard Prompting (direct answer)
"A store has 23 apples. They sell 17 and receive a shipment 
of 12 more. How many apples do they have?"

Answer: 18 (Model may get this wrong by rushing)

// ✅ Chain of Thought Prompting
"A store has 23 apples. They sell 17 and receive a shipment 
of 12 more. How many apples do they have?

Let's think through this step by step:"

Response:
"Step 1: Start with 23 apples
Step 2: After selling 17: 23 - 17 = 6 apples
Step 3: After receiving 12 more: 6 + 12 = 18 apples

The store has 18 apples."

The Magic Phrase

Simply adding "Let's think step by step" can dramatically improve results. This is called zero-shot CoT.

// Zero-Shot Chain of Thought
"[Complex problem]

Let's think step by step."

// Alternative trigger phrases:
"Let's work through this systematically."
"Let's break this down:"
"Let's solve this step by step:"
"Think through each step carefully:"
"First, let's understand the problem, then solve it:"

// Example: Debugging
"My React component re-renders infinitely when I add 
this useEffect. Why is this happening?

```jsx
useEffect(() => {
  setData(fetchData());
}, [data]);
```

Let's think through this step by step."

// Model will analyze:
// 1. What triggers useEffect
// 2. What happens inside useEffect
// 3. How that affects the dependency
// 4. Why this creates a loop

Few-Shot Chain of Thought

Provide examples that demonstrate the reasoning process you want the model to follow.

// Few-Shot CoT Example
"Solve these coding problems by thinking step by step.

Problem: Is the array [3, 1, 4, 1, 5] sorted in ascending order?
Thinking:
1. Check if 3 <= 1: No, 3 > 1
2. The first comparison failed
Answer: No, the array is not sorted (3 > 1)

Problem: Is the array [1, 2, 3, 4, 5] sorted in ascending order?
Thinking:
1. Check if 1 <= 2: Yes
2. Check if 2 <= 3: Yes
3. Check if 3 <= 4: Yes
4. Check if 4 <= 5: Yes
5. All comparisons passed
Answer: Yes, the array is sorted

Problem: Is the array [1, 3, 2, 4] sorted in ascending order?
Thinking:"

// Model will follow the same reasoning pattern

CoT for Code Analysis

// Analyzing code complexity
"Analyze the time complexity of this function.
Think through each operation step by step.

function findDuplicates(arr) {
  const seen = new Set();
  const duplicates = [];
  
  for (const item of arr) {
    if (seen.has(item)) {
      duplicates.push(item);
    } else {
      seen.add(item);
    }
  }
  
  return duplicates;
}

Analysis:
Step 1: Identify the loops
- One for...of loop iterating through arr (n iterations)

Step 2: Analyze operations inside the loop
- Set.has() - O(1) average
- Set.add() - O(1) average
- Array.push() - O(1) amortized

Step 3: Calculate total complexity
- Loop runs n times
- Each iteration does O(1) work
- Total: O(n)

Time Complexity: O(n)
Space Complexity: O(n) for the Set and duplicates array"

CoT for Architecture Decisions

// Making architecture decisions
"I need to choose between REST and GraphQL for my new API.
Let's think through this systematically.

Context:
- E-commerce app with mobile and web clients
- Complex product data with many relationships
- Need real-time inventory updates
- Team has REST experience

Analysis:

Step 1: Data Fetching Patterns
- Products have categories, reviews, inventory, variants
- Mobile app needs minimal data (bandwidth concerns)
- Web app needs full details
→ GraphQL advantage: clients request exactly what they need

Step 2: Team Experience
- Team knows REST well
- GraphQL has learning curve
→ REST advantage: faster initial development

Step 3: Real-time Requirements
- Need live inventory updates
- REST: Would need WebSocket layer
- GraphQL: Built-in subscriptions
→ GraphQL advantage: native subscription support

Step 4: Caching
- REST: HTTP caching is straightforward
- GraphQL: More complex caching strategies needed
→ REST advantage: simpler caching

Recommendation:
Start with REST for core CRUD operations (faster development),
add GraphQL for complex product queries (mobile optimization),
use WebSocket for real-time inventory updates."

CoT for Debugging

// Debugging with Chain of Thought
"Debug this code. Think through each potential issue:

async function getUserPosts(userId) {
  const user = await fetch(`/api/users/${userId}`);
  const posts = await fetch(`/api/posts?userId=${user.id}`);
  return posts;
}

Step-by-step analysis:

Step 1: First fetch call
- fetch() returns a Response object, not the data
- Missing: .json() or .text() to parse response
→ Bug: 'user' is a Response, not a user object

Step 2: Second fetch call
- Same issue: fetch returns Response
- Also using 'user.id' but 'user' is a Response object
→ Bug: 'user.id' is undefined

Step 3: Return value
- 'posts' is a Response object
- Caller likely expects an array
→ Bug: returning Response instead of data

Fixed code:
async function getUserPosts(userId) {
  const userResponse = await fetch(`/api/users/${userId}`);
  const user = await userResponse.json();
  
  const postsResponse = await fetch(`/api/posts?userId=${user.id}`);
  const posts = await postsResponse.json();
  
  return posts;
}"

Self-Consistency with CoT

Generate multiple reasoning paths and take the majority answer for higher accuracy.

// Self-Consistency: Multiple reasoning paths
"Solve this problem using three different approaches, 
then determine which answer is most likely correct.

Problem: A function receives an array and should return 
true if any two numbers sum to 10. What's the most 
efficient time complexity possible?

Approach 1 (Brute Force Analysis):
- Check every pair: O(n²)
- Can we do better? Yes, with a hash set

Approach 2 (Hash Set Analysis):
- For each number x, check if (10-x) exists
- Set lookup is O(1)
- One pass through array: O(n)

Approach 3 (Sorted Array Analysis):
- Sort: O(n log n)
- Two pointers: O(n)
- Total: O(n log n)

Conclusion: 
Approaches agree that O(n) is achievable with hash set.
Answer: O(n) time complexity"

✅ When to Use Chain of Thought

  • • Math and logic problems
  • • Code debugging and analysis
  • • Architecture and design decisions
  • • Multi-step reasoning tasks
  • • When you need to understand the reasoning
  • • Complex data transformations