Chain of Thought Prompting Explained (With Examples)
Chain of thought (CoT) prompting is one of the most powerful techniques for improving AI reasoning. It's simple to use and can dramatically improve accuracy on complex tasks.
What is Chain of Thought Prompting?
Chain of thought prompting means asking the AI to show its reasoning step by step before giving a final answer. Instead of jumping to conclusions, the AI "thinks out loud."
The key phrase is usually some variation of: "Think step by step" or "Show your reasoning."
Why It Works
Research shows that having AI models explain their reasoning actually improves accuracy. It works because:
- It breaks complex problems into manageable steps
- Intermediate steps can catch errors before the final answer
- It prevents the model from "jumping to conclusions"
- You can see where the reasoning goes wrong if the answer is incorrect
Basic Chain of Thought Example
Without Chain of Thought:
If a store has 23 apples and receives a shipment of 17 more, then sells 31, how many apples remain?
Answer: 9 apples
With Chain of Thought:
If a store has 23 apples and receives a shipment of 17 more, then sells 31, how many apples remain? Think step by step.
Let me work through this:
1. Starting apples: 23
2. After receiving shipment: 23 + 17 = 40
3. After selling 31: 40 - 31 = 9
Answer: 9 apples remain
The answer is the same here, but on harder problems, chain of thought significantly improves accuracy.
When to Use Chain of Thought
Use it for:
- Math and logic problems
- Multi-step reasoning tasks
- Complex analysis
- Debugging code (trace through logic)
- Decision-making with multiple factors
Skip it for:
- Simple factual questions
- Creative writing
- Tasks where speed matters more than accuracy
- When the answer is obvious
Variations of Chain of Thought
1. Basic CoT
Solve this problem step by step...
2. Let's Think
Let's think about this carefully...
3. Show Your Work
Show your reasoning, then give your answer...
4. Self-Consistency
Solve this three different ways, then tell me which answer you're most confident in...
Practical Example: Code Debugging
This function should return the factorial of n, but it's broken. Think step by step to identify the bug.
function factorial(n) {
if (n === 0) return 0;
return n * factorial(n - 1);
}
Let me trace through this:
1. factorial(3) should return 3 * 2 * 1 = 6
2. With this code: factorial(3) = 3 * factorial(2)
3. factorial(2) = 2 * factorial(1)
4. factorial(1) = 1 * factorial(0)
5. factorial(0) = 0 (here's the bug!)
6. So factorial(1) = 1 * 0 = 0
7. And everything becomes 0
The bug: Base case returns 0 instead of 1.
Fix: Change "return 0" to "return 1"
Combining with Other Techniques
Chain of thought works great with other prompt techniques:
- Role + CoT: "You are a senior engineer. Think through this architecture decision step by step..."
- Few-shot + CoT: Show examples of step-by-step reasoning, then ask for the same
- Constraints + CoT: "Walk through your reasoning, then give a final recommendation in under 100 words"
Chain of thought is one of the most universally useful prompt techniques. When in doubt, asking the AI to "think step by step" rarely hurts and often helps significantly.
One Shotr Team
The One Shotr team helps people write better prompts for AI tools.