Skip to main content
Create your own

Chain-of-Thought Prompting for Enhanced Reasoning

Hello! Welcome back to our module on LLM Interaction.

In our last lesson, we explored how to guide a model's behavior using zero-shot and few-shot prompting. We saw that by providing examples (or "shots") in the prompt, we can teach a model a new task's format and style without any fine-tuning. This is powerful for tasks that involve pattern recognition.

However, many complex problems require more than just pattern matching; they demand logical, step-by-step reasoning. A standard prompt, even a few-shot one, might pressure the model to give an answer directly, often leading to errors in multi-step problems.

This brings us to today's learning outcome: Implement Chain-of-Thought (CoT) prompting to improve reasoning. We will explore a technique that transforms LLMs from simple guessers into more deliberate reasoners by encouraging them to "show their work."

This lesson will cover:

  • The core concept of Chain-of-Thought (CoT) prompting and why it is so effective.
  • A practical demonstration of how CoT can solve a problem that a standard prompt cannot.
  • The main variations: Zero-Shot CoT and Few-Shot CoT.
  • How to implement these techniques in Python.
  • A brief introduction to an advanced technique called Self-Consistency that builds upon CoT.

1. What is Chain-of-Thought (CoT) Prompting?

At its heart, Chain-of-Thought prompting is a method that encourages a Large Language Model to break down a multi-step problem into intermediate reasoning steps before arriving at a final answer. Instead of asking for an immediate solution, you ask the model to think through the problem logically.

This simple change has a profound impact. Look at the classic example below, which demonstrates the difference.

Standard vs. Chain-of-Thought Prompting Comparison
This image, from the original CoT paper by Wei et al. (2022), shows two scenarios. In standard prompting, the model incorrectly answers a math word problem. In Chain-of-Thought prompting, the model is given an example that includes the reasoning steps. When faced with the same problem, it emulates this step-by-step process and arrives at the correct answer.

The key insight is that forcing the model to generate a reasoning "chain" makes it less likely to make intuitive leaps and more likely to follow a logical path, which is often necessary for arithmetic, commonsense, and symbolic reasoning tasks.

But why does this work? Is it just a prompt "trick," or is something deeper happening? To get a conceptual perspective from an expert, let's hear from Aravind Srinivas, the CEO of Perplexity AI.

Chain-of-thought explained | Aravind Srinivas and Lex Fridman

In this short clip from an interview with Lex Fridman, Aravind Srinivas explains the fundamental idea behind Chain-of-Thought.

Watch the segment from the beginning until 01:00. Focus on how he describes CoT as a way to force the model through a reasoning pathway to avoid overfitting on superficial patterns.

As Srinivas explains, CoT isn't just about getting the right answer; it's about eliciting the model's latent reasoning capabilities that were learned during pre-training. By prompting for steps, we are guiding the model to access and apply this "intelligence" more effectively.

2. A Practical Demonstration: From Failure to Success

Let's see CoT in action. Sometimes, the best way to understand its power is to see it solve a problem that seems impossible for a model using a standard, zero-shot approach. The following video provides a compelling demonstration using a complex riddle.

ChatGPT Prompt Engineering Principles: Chain of Thought Prompting

The video 'ChatGPT Prompt Engineering Principles: Chain of Thought Prompting' by All About AI provides an excellent, hands-on example of using CoT interactively.

Watch from the beginning until 07:08. The video first introduces the concept and then dives into a riddle about Michael in a museum. Notice how the model fails when asked directly, but succeeds when the prompter guides it to break the problem down into sub-problems and solve them one by one.

This interactive, conversational approach is a form of manual CoT. The prompter acts as a guide, asking the model to list the necessary steps and then tackle each one. This demonstrates the core principle: decomposition.

Fortunately, we don't always need an interactive session. We can bake the principles of CoT directly into a single prompt. This leads us to the main types of CoT implementation.

3. The Main Types of CoT Prompting

Just as we had zero-shot and few-shot prompting in our previous lesson, we have similar variants for Chain-of-Thought. An excellent article from Codecademy breaks these down clearly.

Chain of Thought Prompting Explained (with examples)

The article 'Chain of Thought Prompting Explained' provides a structured overview of the different CoT techniques.

Please read the first four sections: 'What is chain of thought prompting?' 'How does chain of thought prompting work?' 'Zero-shot chain-of-thought (Zero-shot CoT) prompting' 'Few-shot chain-of-thought (Few-shot CoT) prompting' Focus on the distinction between adding a simple instruction versus providing full examples.

Let's summarize the two primary methods from the article:

  1. Zero-Shot CoT: This is the simplest method. You don't provide any examples. Instead, you append a simple "magic phrase" to your prompt. The most famous one is "Let's think step by step." This simple instruction is often enough to trigger the model's reasoning capabilities for moderately complex problems.

    • Prompt Example:
      Question: A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls are there?
      
      Let's think step by step.
      
  2. Few-Shot CoT: This is a direct extension of the few-shot prompting we learned previously. Here, the examples you provide include not just the question and final answer, but also the intermediate reasoning steps. This is more powerful than zero-shot CoT because it gives the model a clear template for the kind of reasoning you expect.

    • Prompt Example (from the image we saw earlier):
      Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
      A: Roger started with 5 balls. 2 cans of 3 tennis balls is 6 tennis balls. 5 + 6 = 11. The answer is 11.
      
      Q: A juggler can juggle 16 balls...
      

4. Implementation in Python

Now, let's move from theory to practice. Since you're fluent in Python, we can look at how to implement these prompts using a framework like LangChain. The same Codecademy article provides clear, concise code for this.

Chain of Thought Prompting Explained (with examples)

Let's continue with the same Codecademy article to see how CoT prompts are constructed in code.

Now, read the section 'How to implement chain of thought prompting in LangChain applications?'. Pay close attention to how the PromptTemplate is modified first for Zero-Shot CoT (by adding text to the template) and then for Few-Shot CoT (by including full examples in the template string).

As the article demonstrates, implementing CoT is fundamentally about string manipulation.

  • For Zero-Shot CoT, you simply add your instruction ("Solve this problem step by step.") to the prompt template.
  • For Few-Shot CoT, you hardcode the examples, including their reasoning chains, directly into the prompt template.

This makes it a very accessible but powerful technique to integrate into any application you build on top of an LLM.

Test your understanding!

You want an LLM to solve a simple logic puzzle. The puzzle is:

Puzzle: In a race, a runner named Alex finished before Ben. Chris finished after David. Ben finished before David. Who finished first?

Construct a few-shot CoT prompt to solve this. Your prompt should include one different, fully-reasoned example before presenting the new puzzle.

Show answer

An effective few-shot CoT prompt would look something like this. The key is that the example demonstrates how to break the problem down into logical facts and then synthesize them.

Prompt:

Q: John is taller than Mike. Mike is taller than Sarah. Who is the shortest?
A: Let's break it down.
1. John > Mike
2. Mike > Sarah
Combining these gives us: John > Mike > Sarah.
Therefore, Sarah is the shortest. The answer is Sarah.
---
Q: In a race, a runner named Alex finished before Ben. Chris finished after David. Ben finished before David. Who finished first?
A:

When given this prompt, the model is guided to follow the reasoning pattern and would likely produce an output like:

Let's break it down.
1. Alex finished before Ben (Alex > Ben).
2. Chris finished after David (David > Chris).
3. Ben finished before David (Ben > David).
Combining these gives us: Alex > Ben > David > Chris.
Therefore, Alex finished first. The answer is Alex.

5. Advanced Technique: Self-Consistency

Chain-of-Thought is not infallible. Sometimes the model can make a mistake in its reasoning chain, leading to a wrong answer. A powerful technique to mitigate this is called Self-Consistency.

The core idea is simple and may remind you of ensemble methods in machine learning:

  1. Use few-shot CoT to prompt the model for an answer.
  2. Do this multiple times (e.g., 5-10 times), using a non-zero temperature to generate different reasoning paths.
  3. Take the final answers from all the generated responses and choose the one that appears most frequently (a majority vote).

The intuition is that while there might be many ways to reason incorrectly, there are often fewer ways to reason correctly. By sampling multiple reasoning paths, the correct answer is likely to be the most consistent one.

Chain-of-Thought Prompting: Techniques, Tips, and Code ...

The article 'Chain-of-Thought Prompting: Techniques, Tips, and Code...' from Helicone.ai has a great section that introduces this concept.

Read the section titled 'Advanced Technique: Self-Consistency Sampling'. This will give you a clear definition and show a high-level Python implementation concept.

This method significantly improves the accuracy of CoT on complex reasoning tasks and is a crucial tool in your prompt engineering toolkit.

Conclusion

Today, we've taken a significant step beyond simple prompting. By guiding LLMs to "think step by step," we can unlock much more robust and reliable reasoning capabilities.

Key Takeaways:

  • Chain-of-Thought (CoT) prompting improves reasoning by breaking down complex problems into a sequence of intermediate steps.
  • Zero-Shot CoT is a simple but effective method where you just add an instruction like "Let's think step by step."
  • Few-Shot CoT is more powerful and provides the model with complete examples of reasoned-out problems, setting a clear template for it to follow.
  • CoT is easily implemented by carefully structuring your prompt string, as we saw in the Python examples.
  • Self-Consistency is an advanced technique that enhances CoT's robustness by generating multiple reasoning paths and taking a majority vote on the final answer.

Preview of the Next Lesson:

We've just touched on the idea of Self-Consistency as a way to improve the robustness of CoT. In our next lesson, we will dedicate our entire session to this technique. We'll dive deeper into how to apply self-consistency effectively, explore its implementation in more detail, and discuss the trade-offs involved, such as increased computational cost versus improved accuracy.

Can't find a good explanation? Sign up and we'll make it for you

Sign up