Home »
AI Prompts Tutorial
Chain-of-Thought Prompts
Chain-of-thought prompting is a prompting technique that encourages an AI model to work through a problem in a sequence of logical steps before providing an answer. It is particularly useful for tasks that require reasoning, calculations, comparisons, or multiple stages of analysis.
Instead of asking the AI model only for the final answer, a chain-of-thought prompt can guide it to consider the problem systematically.
In this chapter, you will learn what chain-of-thought prompts are, how they work, when they can be useful, common patterns, examples, and best practices.
What Is a Chain-of-Thought Prompt?
A chain-of-thought prompt is designed to encourage step-by-step reasoning when solving a problem.
A simple prompt may ask:
What is 25% of 240?
A more structured prompt can ask the model to solve the problem systematically:
Calculate 25% of 240.
Work through the calculation step by step and provide the final answer.
The second prompt explicitly encourages a sequence of reasoning steps.
How Do Chain-of-Thought Prompts Work?
A chain-of-thought prompt generally breaks a complex problem into smaller reasoning steps.
Problem
|
v
Identify the Information
|
v
Determine the Required Steps
|
v
Work Through the Problem
|
v
Check the Result
|
v
Final Answer
This approach can make complex tasks easier to organize because the task is considered as a sequence rather than as a single operation.
Simple Chain-of-Thought Prompt
A simple prompt can ask the AI model to reason through a basic problem.
Example
A store offers a 20% discount on a product priced at $100.
Calculate the discount amount and the final price.
Work through the calculation step by step.
The task requires more than simply retrieving information because the model needs to calculate the discount and then determine the final price.
Chain-of-Thought for Mathematical Problems
Mathematical problems can involve multiple operations. A structured prompt can ask the AI model to solve the problem in an ordered manner.
Example
A student buys 3 books for $20 each and receives a 10% discount
on the total purchase.
Calculate:
1. The original total price.
2. The discount amount.
3. The final price.
Show the calculation for each step.
The prompt divides the problem into smaller tasks, making the required calculation sequence clear.
Chain-of-Thought for Logical Problems
Logical problems can also benefit from structured reasoning.
Example
A library has 120 books.
40 books are fiction and 30 books are science books.
The remaining books are history books.
Determine the number of history books.
Explain the calculation step by step.
The model needs to identify the known quantities and calculate the remaining number of books.
Chain-of-Thought for Programming Problems
Programming problems often involve several stages, such as understanding the requirements, designing an approach, writing code, and checking the result.
Example
Write a Python program to find the largest number in a list.
Before writing the code:
1. Describe the problem.
2. Explain the approach briefly.
3. Write the Python code.
4. Show the expected output for a sample input.
The prompt organizes the programming task into a logical sequence.
Chain-of-Thought for Data Analysis
Data analysis can involve identifying information, applying calculations, comparing results, and drawing conclusions.
Example
Analyze the following monthly sales data:
January: $10,000
February: $12,000
March: $15,000
April: $13,000
Identify:
1. The month with the highest sales.
2. The month with the lowest sales.
3. The difference between the highest and lowest sales.
4. The average monthly sales.
Show the calculation for the numerical results.
The prompt provides a clear sequence for analyzing the data.
Chain-of-Thought for Decision Making
Chain-of-thought-style prompts can be used to organize decision-making tasks by defining the factors that should be considered.
Example
Compare two programming languages for a beginner.
Evaluate them based on:
1. Ease of learning
2. Syntax
3. Community support
4. Common applications
5. Learning resources
Summarize the comparison and provide a recommendation based on these criteria.
The criteria guide the analysis and help produce a structured comparison.
Step-by-Step Instructions vs Chain-of-Thought
Step-by-step instructions and chain-of-thought prompting are related but can serve different purposes.
| Approach |
Purpose |
Example |
| Step-by-Step Instructions |
Defines the sequence of tasks the AI should perform. |
First summarize the article, then list the main points. |
| Chain-of-Thought Prompting |
Encourages systematic reasoning for a problem. |
Work through the calculation step by step before giving the result. |
When to Use Chain-of-Thought Prompts?
Chain-of-thought-style prompting can be useful when a task requires multiple reasoning steps.
Common applications include:
- Mathematical problems
- Logical reasoning
- Multi-step calculations
- Programming problems
- Data analysis
- Complex comparisons
- Problem solving
- Planning tasks
- Decision analysis
Zero-Shot Chain-of-Thought Prompting
Zero-shot chain-of-thought prompting encourages step-by-step reasoning without providing examples.
Example
A train travels 60 kilometers in 1.5 hours.
Calculate its average speed.
Solve the problem step by step and provide the final answer.
No example is provided. The model is simply instructed to work through the problem systematically.
Few-Shot Chain-of-Thought Prompting
Few-shot chain-of-thought prompting provides examples that demonstrate how a particular type of problem should be approached.
Example
Solve the following problems using the demonstrated format.
Example 1:
Problem:
A car travels 100 km in 2 hours.
Solution:
Distance = 100 km
Time = 2 hours
Average speed = Distance / Time
Average speed = 100 / 2 = 50 km/h
Now solve:
A car travels 180 km in 3 hours.
Use the same format.
The example demonstrates the expected calculation structure before the new problem is provided.
Structured Reasoning Prompts
Instead of asking for unrestricted reasoning, a prompt can define specific stages that the AI model should follow.
Example
Analyze the following programming problem.
Step 1: Identify the requirements.
Step 2: Identify the input and output.
Step 3: Describe the solution approach briefly.
Step 4: Write the code.
Step 5: Test the solution with an example.
Problem:
Write a program to find the largest element in an array.
This approach gives the AI model a structured process to follow.
Chain-of-Thought for Complex Questions
Complex questions can be divided into smaller questions or criteria.
Example
Analyze whether Python is suitable for developing a web application.
Consider:
1. Framework availability
2. Database support
3. Development speed
4. Scalability
5. Community support
Evaluate each factor and provide a final recommendation.
The prompt provides a clear structure for analyzing the question.
Chain-of-Thought Prompt Structure
A chain-of-thought-style prompt can follow a structure such as:
Task:
Define the problem that needs to be solved.
Steps:
Identify the relevant information.
Break the problem into smaller parts.
Perform the required calculations or analysis.
Check the result.
Output:
Provide the final answer with a concise explanation.
This structure can be adapted to different types of reasoning tasks.
Use Clear Intermediate Tasks
For complex problems, define the important intermediate tasks instead of giving one broad instruction.
Example
Analyze the following business data.
1. Identify the highest-performing product.
2. Identify the lowest-performing product.
3. Calculate the difference in revenue.
4. Compare the results.
5. Provide a concise conclusion.
Each step contributes to the final analysis.
Chain-of-Thought and Final Answers
For many applications, it is useful to ask for a concise explanation or the key calculation rather than requesting a complete internal reasoning process.
Example
Calculate the total cost of the following purchase.
Show the key calculation and provide the final amount.
Product A: $50
Product B: $30
Tax: 10%
This approach focuses on the useful calculation and final result without requiring a detailed internal reasoning transcript.
Best Practices for Chain-of-Thought Prompts
Follow these practices when creating chain-of-thought-style prompts:
- Use step-by-step instructions for genuinely multi-step tasks.
- Clearly define the problem.
- Break complex tasks into manageable stages.
- Provide relevant information and constraints.
- Specify important calculations or criteria.
- Use examples when a particular reasoning pattern is required.
- Ask for concise explanations or key calculations when appropriate.
- Define the desired final output.
- Check important calculations and factual information independently.
- Refine the prompt if the generated result does not meet the requirements.
Example of an Effective Chain-of-Thought Prompt
The following prompt uses a structured sequence for a multi-step problem:
Example
A company has 500 employees.
20% work in the development department.
30% of the development employees work on AI projects.
Calculate:
1. The number of development employees.
2. The number of development employees working on AI projects.
3. Show the key calculations.
4. Provide the final answer clearly.
The prompt defines the problem, identifies the required steps, and specifies how the result should be presented.
Advertisement
Advertisement