HxHippy

Chain of Thought Prompting

Use step-by-step reasoning to improve AI accuracy on complex logical and mathematical tasks.

Last updated: 2024-12-18

Chain of Thought (CoT) prompting encourages AI models to show their reasoning process, dramatically improving accuracy on complex tasks.

Why Chain of Thought Works

When you ask a model to "think step by step":

  1. Breaks down complexity: Large problems become manageable steps
  2. Catches errors early: Mistakes in reasoning become visible
  3. Provides transparency: You can verify the logic
  4. Improves accuracy: 20-40% improvement on math/logic tasks

Basic Chain of Thought

Without CoT

Q: If a store has 156 apples and sells 3/4 of them, then
   receives a shipment of 45 more, how many apples does
   it have now?
A: 84 apples [often wrong]

With CoT

Q: If a store has 156 apples and sells 3/4 of them, then
   receives a shipment of 45 more, how many apples does
   it have now?

Let me solve this step by step:
1. Start with 156 apples
2. Sold 3/4, so remaining = 156 - (156 * 3/4) = 156 - 117 = 39
3. Received 45 more: 39 + 45 = 84

A: 84 apples [with verifiable reasoning]

CoT Trigger Phrases

These phrases activate step-by-step reasoning:

"Let's think through this step by step"
"Let's break this down"
"Let me work through this systematically"
"First, let's understand what we're solving for"
"Let's approach this methodically"

Zero-Shot vs Few-Shot CoT

Zero-Shot CoT

Just add "Let's think step by step":

Q: A farmer has 17 sheep. All but 9 run away. How many
   sheep does the farmer have left?

Let's think step by step.

Few-Shot CoT

Provide examples of the reasoning format:

Q: Roger has 5 tennis balls. He buys 2 more cans of
   tennis balls. Each can has 3 balls. How many does
   he have now?
A: Roger started with 5 balls.
   2 cans * 3 balls = 6 balls bought.
   5 + 6 = 11 balls total.
   The answer is 11.

Q: A store had 23 cameras. They sold 17 on Monday and
   got 12 more Tuesday. How many cameras do they have?
A: [Model follows the demonstrated format]

CoT for Code Problems

Debugging with CoT

This code throws an error. Let's debug step by step:

```python
def get_user(users, id):
    return users[id]['name']

Step 1: What does this function do?

  • Takes a users dict and an id
  • Returns the 'name' key from users[id]

Step 2: What could go wrong?

  • users[id] might not exist (KeyError)
  • users[id]['name'] might not exist (KeyError)
  • users might be None (TypeError)

Step 3: Which is most likely?

  • Given the error "KeyError", either the id doesn't exist or there's no 'name' key.

Step 4: How to fix?

def get_user(users, id):
    user = users.get(id)
    if user is None:
        return None
    return user.get('name')

### Algorithm Design with CoT

```text
Design a function to find the longest palindromic substring.

Let me think through this step by step:

Step 1: Understand the problem
- Input: string "babad"
- Output: "bab" or "aba" (both valid)
- A palindrome reads the same forwards and backwards

Step 2: Consider approaches
a) Brute force: O(n^3) - check all substrings
b) Dynamic programming: O(n^2) - build up from small palindromes
c) Expand around center: O(n^2) - expand from each character

Step 3: Choose approach
Expand around center is intuitive and efficient enough.

Step 4: Handle edge cases
- Empty string -> return ""
- Single character -> return that character
- All same characters -> return entire string
- No palindrome > 1 -> return first character

Step 5: Implement
[Code implementation follows]

Self-Consistency with CoT

Generate multiple reasoning paths and vote on the answer:

Solve this problem 3 different ways, then determine
the most likely correct answer:

[Problem statement]

Approach 1:
[First reasoning chain]

Approach 2:
[Second reasoning chain]

Approach 3:
[Third reasoning chain]

Final Answer:
The answer appearing in 2+ approaches is most likely correct.

When to Use CoT

Task Type CoT Benefit
Math problems High
Logic puzzles High
Code debugging High
Step-by-step instructions Medium
Creative writing Low
Simple Q&A Low

Limitations

  • Adds tokens: Reasoning takes more output space
  • Not always faster: Can slow down simple tasks
  • Fabricated reasoning: Model might rationalize wrong answers
  • Overkill for simple tasks: "What is 2+2? Let's think..."

Best Practices

  1. Match complexity: Use CoT for genuinely complex problems
  2. Verify steps: Check each reasoning step, not just the answer
  3. Combine with examples: Few-shot CoT often outperforms zero-shot
  4. Iterate on failures: If reasoning fails, provide corrective examples
intermediate Prompting Updated 2024-12-18
  • chain of thought
  • cot prompting
  • reasoning
  • step by step
  • ai reasoning