AW Dev Rethought

🕵️ Debugging is like being the detective in a crime movie where you are also the murderer - Filipe Fortes

🧠 AI with Python – 🧠 Chain-of-Thought Prompting


Description:

Large Language Models are capable of answering an incredible variety of questions, from writing code to solving mathematical problems. However, not every task is equally straightforward. Questions involving logic, calculations, planning, or multiple reasoning steps often require the model to "think" before producing the final answer.

This is where Chain-of-Thought (CoT) Prompting becomes one of the most influential Prompt Engineering techniques.

Instead of asking an LLM to immediately provide an answer, we encourage it to reason through the problem step by step. This simple change in prompting often improves the quality, clarity, and reliability of responses for tasks that require reasoning rather than simple recall.


What is Chain-of-Thought Prompting?

Chain-of-Thought Prompting is a technique where we explicitly instruct the model to explain its reasoning before giving the final answer.

For example, instead of asking:

If a store sells notebooks for $5 each and you buy 8 notebooks, how much do you pay?

we ask:

If a store sells notebooks for $5 each and you buy 8 notebooks, how much do you pay?

Let's think step by step.

Those few extra words encourage the model to break the problem into smaller reasoning steps before arriving at the final answer. Rather than producing only the result, the model attempts to explain how it reached that conclusion.


Why Does It Work?

Modern Large Language Models have been trained on enormous amounts of text containing explanations, worked examples, tutorials, and logical reasoning. By asking the model to "think step by step," we encourage it to follow those learned reasoning patterns instead of jumping directly to an answer.

For simple questions, both a normal prompt and a Chain-of-Thought prompt may produce the same result. However, when problems involve multiple calculations, logical decisions, or planning, reasoning step by step often leads to responses that are easier to understand and, in many cases, more accurate.


Building Reusable Reasoning Prompts

Rather than writing reasoning instructions every time, we can create a reusable prompt template in Python.

cot_template = """
Question:
{question}

Let's think step by step before giving the final answer.
"""

By replacing the {question} placeholder with different inputs, the same reasoning template can be reused across many different tasks. This makes Prompt Engineering more consistent while keeping prompts clean and maintainable.


When Should You Use Chain-of-Thought Prompting?

Chain-of-Thought Prompting is most effective when the model needs to perform reasoning rather than simply recall information.

Some common examples include:

  • Mathematical problem solving
  • Logical reasoning
  • Multi-step planning
  • Programming problems
  • Financial calculations
  • Scientific reasoning
  • Educational tutoring
  • AI agents performing complex workflows

For straightforward tasks such as translation, text summarization, sentiment analysis, or basic information extraction, Chain-of-Thought Prompting usually provides little benefit. In these cases, a simple instruction is often sufficient and results in shorter, more efficient responses.

Choosing the right prompting technique for the task is an important part of Prompt Engineering.


How It Fits into Prompt Engineering

Throughout this Prompt Engineering series, we've explored several ways to guide Large Language Models.

  • Zero-shot Prompting relies on clear instructions without examples.
  • Few-shot Prompting improves responses by providing sample inputs and outputs.
  • Prompt Templates make prompts reusable and easier to maintain.
  • Chain-of-Thought Prompting focuses on improving the model's reasoning process by encouraging it to solve problems step by step.

Each technique serves a different purpose, and understanding when to use each one allows developers to build more reliable AI applications.


Chain-of-Thought in Modern AI Applications

Chain-of-Thought Prompting is widely used in AI systems that require reasoning before generating an answer.

For example, AI tutors explain mathematical solutions step by step instead of simply displaying the final answer. Coding assistants often break programming problems into smaller logical steps before generating code. AI agents can plan a sequence of actions before executing a workflow, while decision-support systems evaluate multiple factors before making recommendations.

It's also worth noting that newer reasoning-focused language models can often perform internal reasoning automatically. Even so, understanding Chain-of-Thought Prompting remains valuable because it introduced one of the foundational ideas in Prompt Engineering and continues to be highly effective with many instruction-tuned models.


Final Thoughts

Chain-of-Thought Prompting is one of the most impactful Prompt Engineering techniques because it encourages Large Language Models to reason before responding. Rather than producing only an answer, the model is guided to work through a problem in a logical sequence, often leading to clearer and more reliable results for complex tasks.

In this article, we explored how Chain-of-Thought Prompting works, why it improves reasoning, when it should be used, and how it complements other Prompt Engineering techniques we've covered in this series. As AI systems continue to evolve beyond simple chatbots into intelligent assistants and autonomous agents, knowing how to guide a model's reasoning will remain an essential skill for every AI developer.


Code Snippet:

from transformers import pipeline

# =========================================================
# Load Instruction-Following Model
# =========================================================

print("=" * 70)
print("Loading Instruction-Following Language Model...")
print("=" * 70)

generator = pipeline(
    "text-generation",
    model="google/gemma-2b-it"
)

# =========================================================
# Standard Prompt
# =========================================================

standard_prompt = """
A store sells notebooks for $5 each.

If you buy 8 notebooks, how much do you pay?
"""

print("\n" + "=" * 70)
print("Standard Prompt")
print("=" * 70)
print(standard_prompt.strip())

standard_response = generator(
    standard_prompt,
    max_new_tokens=80,
    do_sample=False
)

print("\nStandard Response:")
print("-" * 70)
print(standard_response[0]["generated_text"])

# =========================================================
# Chain-of-Thought Prompt
# =========================================================

cot_prompt = """
A store sells notebooks for $5 each.

If you buy 8 notebooks, how much do you pay?

Let's think step by step.
"""

print("\n" + "=" * 70)
print("Chain-of-Thought Prompt")
print("=" * 70)
print(cot_prompt.strip())

cot_response = generator(
    cot_prompt,
    max_new_tokens=150,
    do_sample=False
)

print("\nChain-of-Thought Response:")
print("-" * 70)
print(cot_response[0]["generated_text"])

# =========================================================
# Compare Standard vs Chain-of-Thought
# =========================================================

print("\n" + "=" * 70)
print("Comparison")
print("=" * 70)

print("\nStandard Prompt Output")
print("-" * 70)
print(standard_response[0]["generated_text"])

print("\nChain-of-Thought Output")
print("-" * 70)
print(cot_response[0]["generated_text"])

# =========================================================
# Solve Multiple Reasoning Problems
# =========================================================

questions = [
    "If a train travels 60 km in one hour, how far will it travel in 5 hours?",
    "John has 12 apples. He gives away 5 and buys 3 more. How many apples does he have now?",
    "A rectangle has a length of 8 and a width of 4. What is its area?"
]

print("\n" + "=" * 70)
print("Multiple Chain-of-Thought Examples")
print("=" * 70)

for question in questions:

    prompt = f"""
Question:
{question}

Let's think step by step before giving the final answer.
"""

    response = generator(
        prompt,
        max_new_tokens=150,
        do_sample=False
    )

    print("\nQuestion:")
    print(question)

    print("\nResponse:")
    print("-" * 70)
    print(response[0]["generated_text"])

# =========================================================
# Build a Reusable Prompt Template
# =========================================================

cot_template = """
Question:
{question}

Let's think step by step before giving the final answer.
"""

prompt = cot_template.format(
    question="A car travels 240 km in 4 hours. What is its average speed?"
)

print("\n" + "=" * 70)
print("Reusable Chain-of-Thought Template")
print("=" * 70)
print(prompt.strip())

response = generator(
    prompt,
    max_new_tokens=150,
    do_sample=False
)

print("\nGenerated Response:")
print("-" * 70)
print(response[0]["generated_text"])

# =========================================================
# Compare Different Prompt Styles
# =========================================================

comparison_questions = [
    "A shop gives a 20% discount on a $150 item. What is the final price?",
    "There are 24 students. They are divided equally into 6 groups. How many students are in each group?"
]

print("\n" + "=" * 70)
print("Prompt Style Comparison")
print("=" * 70)

for question in comparison_questions:

    print("\nQuestion:")
    print(question)

    normal_prompt = question

    reasoning_prompt = f"""
Question:
{question}

Let's think step by step.
"""

    normal_output = generator(
        normal_prompt,
        max_new_tokens=60,
        do_sample=False
    )

    reasoning_output = generator(
        reasoning_prompt,
        max_new_tokens=150,
        do_sample=False
    )

    print("\nStandard Prompt:")
    print("-" * 70)
    print(normal_output[0]["generated_text"])

    print("\nChain-of-Thought Prompt:")
    print("-" * 70)
    print(reasoning_output[0]["generated_text"])

# =========================================================
# Reusable Helper Function
# =========================================================

def generate_reasoning(question):

    prompt = cot_template.format(
        question=question
    )

    response = generator(
        prompt,
        max_new_tokens=150,
        do_sample=False
    )

    return response[0]["generated_text"]

# =========================================================
# Generate Responses Using the Helper Function
# =========================================================

sample_questions = [
    "A triangle has a base of 10 and a height of 6. What is its area?",
    "Emily saved $25 every week for 8 weeks. How much did she save in total?"
]

print("\n" + "=" * 70)
print("Reusable CoT Function")
print("=" * 70)

for question in sample_questions:

    print("\nQuestion:")
    print(question)

    print("\nResponse:")
    print("-" * 70)
    print(generate_reasoning(question))

# =========================================================
# Chain-of-Thought for Planning
# =========================================================

planning_prompt = """
A student has exams in Mathematics, Physics, and Chemistry next week.

Create a study plan.

Let's think step by step before giving the final plan.
"""

print("\n" + "=" * 70)
print("Planning with Chain-of-Thought")
print("=" * 70)

planning_response = generator(
    planning_prompt,
    max_new_tokens=180,
    do_sample=False
)

print(planning_response[0]["generated_text"])

# =========================================================
# Program Completed
# =========================================================

print("\n" + "=" * 70)
print("Chain-of-Thought Prompting Demo Completed Successfully!")
print("=" * 70)

Link copied!

Comments

Add Your Comment

Comment Added!