Advanced Prompt Techniques Chain of Thought & Few Shot

Zaheer Ahmad 6 min read min read
Python
Advanced Prompt Techniques Chain of Thought & Few Shot

Introduction

Artificial Intelligence tools such as ChatGPT, Claude, and other large language models are transforming how people write code, analyze data, and solve problems. However, the quality of results you get from these tools depends heavily on how you ask the question. This skill is known as prompt engineering.

Prompt engineering is the practice of designing effective inputs (prompts) that guide AI models to produce accurate, useful, and structured outputs. While beginners usually write simple prompts like “Explain Python loops”, advanced users apply more sophisticated methods to improve AI reasoning.

Two of the most powerful advanced techniques are:

  • Chain-of-Thought (CoT) Prompting
  • Few-Shot Prompting

These techniques allow AI models to reason step-by-step, follow patterns, and produce higher-quality answers. They are especially useful in programming, mathematics, data analysis, and problem-solving tasks.

For Pakistani students studying programming, data science, or AI, mastering these techniques can significantly improve productivity. Imagine a student named Ahmad from Lahore who is learning Python. Instead of receiving incomplete or generic answers, he can guide the AI to think step-by-step and generate correct code solutions. Similarly, Fatima from Karachi can train the AI using examples to classify Urdu text, analyze datasets, or automate tasks.

In this tutorial, you will learn:

  • What chain-of-thought prompting is and how it improves reasoning
  • How few-shot prompting teaches AI patterns through examples
  • Practical coding examples using these techniques
  • Common mistakes beginners make
  • Exercises to help you practice

By the end of this guide, you will be able to apply advanced prompt engineering techniques to solve real-world problems.

Prerequisites

Before learning advanced prompt techniques, you should have basic knowledge of the following topics:

  • Basic prompt engineering concepts
  • Familiarity with AI chat tools like ChatGPT or Claude
  • Basic understanding of programming (preferably Python)
  • Knowledge of logic and problem-solving
  • Ability to read and write simple code snippets

If you are completely new to prompts, it is recommended that you first read introductory tutorials on prompt engineering before moving to advanced techniques.


Core Concepts & Explanation

Understanding Chain-of-Thought Prompting

Chain-of-Thought (CoT) prompting is a technique where you instruct the AI to explain its reasoning step-by-step before giving the final answer.

Instead of producing a direct answer, the AI walks through the logic required to solve the problem.

Simple Prompt (Without CoT)

What is 25 × 16?

The AI will simply give the result:

400

Chain-of-Thought Prompt

Solve step-by-step:
What is 25 × 16?

Response:

Step 1: Multiply 25 × 10 = 250
Step 2: Multiply 25 × 6 = 150
Step 3: Add the results → 250 + 150 = 400

Final Answer: 400

The AI now shows the reasoning, making it easier to verify correctness.

Why Chain-of-Thought Works

Large language models perform better when they are encouraged to break complex tasks into smaller steps.

This is particularly useful for:

  • Mathematics
  • Programming problems
  • Logical reasoning
  • Data analysis
  • Algorithm explanations

For example, a student Ali from Islamabad learning algorithms might ask:

Explain step-by-step how binary search works.

The AI will describe each stage of the algorithm rather than giving a vague explanation.


Understanding Few-Shot Prompting

Few-shot prompting means giving the AI a few examples of input and output so it learns the pattern before solving a new problem.

Instead of explaining the task, you demonstrate it.

Example Without Few-Shot

Prompt:

Translate English to Urdu:
Computer

The AI will likely answer correctly, but accuracy decreases with complex tasks.

Example With Few-Shot

Translate English to Urdu

Dog → کتا
Book → کتاب
School → اسکول

Computer →

The AI recognizes the pattern and outputs:

کمپیوٹر

Why Few-Shot Prompting Works

AI models are very good at pattern recognition. When they see examples, they imitate the format.

Few-shot prompting is useful for:

  • Data classification
  • Formatting responses
  • Text transformation
  • Code generation
  • Structured outputs

For example, Fatima from Karachi could build a dataset labeling tool:

Classify sentiment:

"I love this product." → Positive
"This is terrible." → Negative
"Excellent quality." → Positive

"The service was slow." →

The AI responds:

Negative

Few-shot prompting effectively teaches the AI the task.


Practical Code Examples

Example 1: Using Chain-of-Thought for Problem Solving

Let’s create a Python program that sends a Chain-of-Thought prompt to an AI model.

prompt = """
Solve the following problem step-by-step:

Ahmad bought 3 books costing 500 PKR each.
How much did he pay in total?
"""

print(prompt)

Line-by-Line Explanation

Line 1

prompt = """

Creates a multi-line string containing the prompt.

Line 2

Solve the following problem step-by-step:

This instruction activates chain-of-thought reasoning.

Line 4

Ahmad bought 3 books costing 500 PKR each.

Defines the problem using a real-world Pakistani example.

Line 5

How much did he pay in total?

Asks the final question.

Line 7

print(prompt)

Outputs the prompt to the console before sending it to an AI API.

Expected AI response:

Step 1: Price of one book = 500 PKR
Step 2: Number of books = 3
Step 3: Total cost = 500 × 3 = 1500 PKR

Final Answer: 1500 PKR

Example 2: Real-World Application (Few-Shot Prompting)

Suppose a Pakistani startup wants to categorize customer messages.

We can train the AI using a few examples.

prompt = """
Classify customer feedback as Positive or Negative.

"The delivery was fast." → Positive
"The product was damaged." → Negative
"Great service!" → Positive

"The package arrived late." →
"""

print(prompt)

Line-by-Line Explanation

Line 1

prompt = """

Creates a multi-line prompt string.

Line 3

Classify customer feedback as Positive or Negative.

Defines the task clearly.

Lines 5–7

"The delivery was fast." → Positive
"The product was damaged." → Negative
"Great service!" → Positive

These lines provide few-shot examples demonstrating the pattern.

Line 9

"The package arrived late." →

The AI predicts the correct label based on earlier examples.

Line 12

print(prompt)

Outputs the prompt for debugging or testing.

Expected response:

Negative

Common Mistakes & How to Avoid Them

Mistake 1: Asking Complex Questions Without Structure

Beginners often ask extremely complicated questions in a single prompt.

Example:

Explain Python loops and write a program and compare them with Java loops and optimize them.

This confuses the AI.

Fix

Break the task into steps.

Step 1: Explain Python loops.
Step 2: Provide an example program.
Step 3: Compare with Java loops.

Structured prompts improve accuracy.


Mistake 2: Not Providing Examples in Few-Shot Tasks

If you ask the AI to perform a classification task without examples, it may misunderstand the format.

Bad prompt:

Label sentiment: This restaurant is amazing.

Better prompt:

Label sentiment:

"I love this restaurant." → Positive
"The food was terrible." → Negative

"This restaurant is amazing." →

Providing examples ensures consistent formatting and accuracy.


Practice Exercises

Exercise 1: Step-by-Step Math Reasoning

Problem

Use chain-of-thought prompting to solve this:

Ali buys 4 notebooks costing 250 PKR each.
What is the total cost?

Try writing a prompt that encourages step-by-step reasoning.

Solution

Example prompt:

Solve step-by-step:

Ali buys 4 notebooks costing 250 PKR each.
What is the total cost?

Expected response:

Step 1: Price of one notebook = 250 PKR
Step 2: Number of notebooks = 4
Step 3: Total cost = 250 × 4 = 1000 PKR

Final Answer: 1000 PKR

Exercise 2: Text Classification Using Few-Shot Prompting

Problem

Create a prompt that classifies spam vs non-spam messages.

Solution

Example prompt:

Classify messages as Spam or Not Spam.

"Win a free iPhone now!" → Spam
"Meeting at 5 PM today." → Not Spam
"Click here to claim your prize." → Spam

"Your order has been delivered." →

Expected response:

Not Spam

This technique is widely used in chatbots, customer support, and email filtering.


Frequently Asked Questions

What is chain-of-thought prompting?

Chain-of-thought prompting is a prompt engineering technique that encourages AI models to explain their reasoning step-by-step before giving a final answer. This improves accuracy for complex tasks like mathematics, programming, and logical reasoning.

How do I use few-shot prompting effectively?

Few-shot prompting works best when you provide clear examples of input and output. These examples teach the AI the pattern it should follow when answering new questions.

When should I use chain-of-thought prompting?

Use chain-of-thought prompting when the task involves multi-step reasoning, such as solving math problems, debugging code, or explaining algorithms.

Can chain-of-thought and few-shot prompting be combined?

Yes. Many advanced prompts combine both techniques by providing example reasoning steps. This approach can significantly improve AI performance.

Is prompt engineering important for programmers?

Yes. Prompt engineering helps programmers generate better code, debug faster, and automate repetitive tasks using AI tools.


Summary & Key Takeaways

  • Prompt engineering is a crucial skill for effectively using AI tools.
  • Chain-of-Thought prompting helps AI reason step-by-step.
  • Few-shot prompting teaches AI patterns through examples.
  • Combining both techniques can dramatically improve AI output quality.
  • These techniques are useful for programming, data analysis, and automation.
  • Pakistani students can use them to improve productivity in coding and learning.

To continue learning prompt engineering, explore these tutorials on theiqra.edu.pk:

  • Learn the fundamentals in Prompt Engineering for ChatGPT
  • Understand how AI works in ChatGPT vs Claude AI
  • Build automation workflows in ChatGPT Automation Tutorial
  • Integrate AI into applications in ChatGPT API Integration

These tutorials will help you move from basic prompting to building real AI-powered applications.

Practice the code examples from this tutorial
Open Compiler
Share this tutorial:

Test Your Python Knowledge!

Finished reading? Take a quick quiz to see how much you've learned from this tutorial.

Start Python Quiz

About Zaheer Ahmad