Skip to main content
Prompt Engineering Tutorial
CHAPTER 08 Beginner

Chain-of-Thought Prompting

Updated: May 14, 2026
25 min read

# CHAPTER 8

Chain-of-Thought Prompting

1. Introduction

Large Language Models are excellent at predicting language, but they are terrible at "thinking." If you give an LLM a complex math word problem and ask for the final answer immediately, it will usually guess wrong. However, researchers discovered a simple phrase that literally boosts an AI's IQ. In this chapter, we will master Chain-of-Thought (CoT) Prompting, the technique that forces an AI to use logical reasoning.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define Chain-of-Thought (CoT) Prompting.
  • Understand the mechanical reason why CoT improves AI accuracy.
  • Implement Zero-Shot CoT using the "magic phrase."
  • Design Few-Shot CoT prompts for complex analytical tasks.

3. Beginner-Friendly Explanation

Imagine giving a 3rd grader a massive math equation: (45 * 3) - (12 + 6). If you demand they shout out the final answer in 1 second, they will panic and guess a random number. If you say, *"Take a piece of paper. Solve the multiplication first. Then solve the addition. Then subtract. Show me your work,"* they will get the correct answer. An LLM works exactly the same way. If you demand the final answer immediately, it panics and guesses. If you instruct the AI to write out its "scratchpad thoughts" step-by-step *before* giving the final answer, its accuracy skyrockets. This is Chain-of-Thought Prompting.

4. Why Chain-of-Thought Works

Remember Next-Token Prediction? The AI generates text one word at a time. If it tries to guess the final answer immediately, it has no mathematical "runway" to do the calculation. When you force the AI to write out its reasoning (e.g., *"Step 1: John has 5 apples. Step 2: John eats 2 apples... "*), the AI is literally using the words it just generated as "memory." By breaking the problem down, the Next-Token Prediction engine calculates the smaller, easier chunks sequentially, naturally arriving at the correct logical conclusion.

5. Zero-Shot Chain-of-Thought (The Magic Phrase)

In 2022, AI researchers discovered a phrase that universally improves LLM performance on complex logic tasks. You simply append this sentence to the end of any prompt: "Let's think step by step." By adding this sentence, the AI automatically outputs its scratchpad reasoning, massively decreasing hallucination rates and logic errors.

6. Prompt Example: Standard vs. CoT

Standard Prompt (Likely to fail):
text
1
Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 balls. How many does he have now?

*Output:* (The AI guesses randomly: "Roger has 13 balls" or "Roger has 10 balls").

Zero-Shot CoT Prompt (Highly accurate):

text
12
Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 balls. How many does he have now? 
Let's think step by step.

*Output:* "Step 1: Roger starts with 5 balls. Step 2: He buys 2 cans, each containing 3 balls. So he bought 2 * 3 = 6 new balls. Step 3: We add the original 5 to the new 6. 5 + 6 = 11. Roger has 11 tennis balls."

7. Few-Shot Chain-of-Thought

For highly complex enterprise tasks (like financial auditing), "Let's think step by step" isn't enough. You must combine Few-Shot examples with CoT reasoning. You show the AI exactly *how* to think.
text
12345678
Q: A store has 10 cars. It sells 2, then buys 5. How many are left?
A: 10 - 2 = 8. 8 + 5 = 13. The answer is 13.

Q: A farm has 20 cows. 5 run away. The farmer buys 10 more. How many are left?
A: 20 - 5 = 15. 15 + 10 = 25. The answer is 25.

Q: You have $50. You spend $10 on lunch and earn $30 at work. How much do you have?
A:

8. Python Example: Extracting the Final Answer

In production, you want the AI to "think" out loud, but you only want to show the user the final answer. Developers use formatting tricks to separate the reasoning from the result.
python
1234567891011121314
prompt = """
Solve the math problem. 
Write your reasoning inside <scratchpad> tags. 
Write ONLY the final number inside <answer> tags.

Problem: What is 15 + 25?
"""

# The AI outputs:
# <scratchpad> 10+20 is 30. 5+5 is 10. 30+10 is 40. </scratchpad>
# <answer>40</answer>

# The developer then writes a Python regex to hide the scratchpad 
# and only show "40" to the user!

9. Mini Project

The Detective Prompt: You want an AI to determine if an email is a phishing scam. Write a Chain-of-Thought prompt that explicitly tells the AI the 3 steps it must take in its "mind" before declaring the email Safe or Scam. *(Answer Example: "Analyze this email. Step 1: Look at the sender's email address and check for misspellings. Step 2: Read the text and check if it asks for urgent passwords. Step 3: Review the links. Finally, based on these 3 steps, output SAFE or SCAM.")*

10. Best Practices

  • Forced Formatting: If an AI is struggling with a logic task, do not rewrite the question. Simply add: *"Explain your reasoning before providing the final answer."* This single sentence solves 90% of logical hallucinations.

11. Common Mistakes

  • Using CoT for Simple Tasks: Do not use CoT for basic formatting or translation. Asking an AI to "Think step-by-step" before translating "Hello" to Spanish wastes tokens, increases API costs, and slows down the response time for no reason.

12. Exercises

  1. 1. Explain the mechanical reason why asking an LLM to write out its "scratchpad thoughts" improves its ability to solve math problems.

13. MCQs with Answers

Question 1

What is the core mechanism of "Chain-of-Thought" (CoT) Prompting?

Question 2

Which phrase is famously known in AI research for instantly triggering Zero-Shot Chain-of-Thought reasoning?

14. Interview Questions

  • Q: How does Chain-of-Thought prompting manipulate the "Next-Token Prediction" architecture of an LLM to solve complex logical problems that a Zero-Shot prompt would fail at?
  • Q: Explain how you would structure a prompt to utilize Chain-of-Thought reasoning for accuracy, while hiding the verbose "scratchpad" text from the final end-user.

15. FAQs

Q: Do new models like GPT-4 still need Chain-of-Thought? A: Yes. While GPT-4 is much smarter than previous models, its underlying architecture is the same. For high-level reasoning (like writing complex software code or legal analysis), CoT remains a mandatory prompt engineering technique.

16. Summary

In Chapter 8, we discovered the secret to making AI smarter. By exploiting the Next-Token Prediction engine, we learned that LLMs cannot "hold thoughts" in their heads; they must write them down. By utilizing Zero-Shot CoT ("Let's think step by step") and structured Few-Shot CoT, we force the AI to slow down, break complex problems into sequential chunks, and arrive at logical conclusions, virtually eliminating guesswork and hallucinations.

17. Next Chapter Recommendation

You have mastered logic and structure. Now it is time to master tone. Proceed to Chapter 9: Role-Based Prompting Techniques to learn how to change the AI's personality.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·