Skip to content

Chain of Thought Prompts

Prompt Engineering - Chain-of-Thought (CoT) Prompting

Section titled “Prompt Engineering - Chain-of-Thought (CoT) Prompting”

Chain-of-Thought (CoT) prompting is an advanced technique that significantly improves the reasoning abilities of Large Language Models (LLMs), especially for complex tasks requiring arithmetic, commonsense, or symbolic reasoning. Instead of just asking for a final answer, CoT encourages the model to generate a series of intermediate reasoning steps.

This chapter explores the concept of CoT prompting, its benefits, and how to implement it to elicit more accurate and explainable responses from LLMs like ChatGPT.

Chain-of-Thought prompting involves instructing the LLM to output its reasoning process step-by-step before arriving at the final answer. This mimics how humans often break down complex problems. By externalizing the reasoning process, the model is more likely to follow a logical path and arrive at a correct solution.

There are two main types:

  • Zero-shot CoT: Appending a simple phrase like “Let’s think step by step.” or “Show your work.” to the question.
  • Few-shot CoT: Providing a few examples (shots) in the prompt where each example includes both the question and the detailed reasoning steps leading to the answer.

CoT prompting offers several significant advantages:

  • Improved Accuracy: Particularly for tasks requiring multi-step reasoning, CoT significantly boosts the accuracy of LLM responses.
  • Enhanced Interpretability: The generated chain of thought provides insights into how the model arrived at its answer, making the process more transparent and easier to debug.
  • Better Problem Decomposition: It encourages the model to break down complex problems into smaller, manageable steps.
  • Robustness: Models using CoT can sometimes be more robust to slight variations in the input prompt.

This is the simplest way to try CoT. You just append a trigger phrase to your prompt.

User: 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? Let's think step by step.
ChatGPT (expected CoT output):
Okay, let's break this down:
1. Roger starts with 5 tennis balls.
2. He buys 2 cans of tennis balls.
3. Each can has 3 tennis balls.
4. So, the new balls from the cans are 2 cans * 3 balls/can = 6 tennis balls.
5. In total, Roger now has his initial 5 balls + 6 new balls = 11 tennis balls.
The final answer is 11.

Provide one or more complete examples of questions with their reasoning chains and final answers. This guides the model more strongly.

User:
Q: Natalia sold clips to 48 of her friends. She had 12 clips left. How many clips did she have at first?
A: Natalia sold 48 clips. She had 12 clips left. So, at first, she had the clips she sold plus the clips she had left. 48 + 12 = 60. The final answer is 60.
Q: 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?
A: The juggler can juggle 16 balls. Half of the balls are golf balls, so there are 16 / 2 = 8 golf balls. Half of the golf balls are blue, so there are 8 / 2 = 4 blue golf balls. The final answer is 4.
Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have?
A: Let's think step by step.

The model would then continue with its reasoning for the last question.

Best Practices for Chain-of-Thought Prompting

Section titled “Best Practices for Chain-of-Thought Prompting”

To maximize the effectiveness of CoT prompting:

  • Use for Complex Tasks: CoT is most beneficial for problems that require reasoning, calculation, or multi-step inference.
  • Ensure Clarity in Few-Shot Examples: The reasoning steps in your examples should be clear, logical, and correct.
  • Experiment with Phrasing: For zero-shot CoT, try different trigger phrases (e.g., “Show your work,” “Explain your reasoning.”).
  • Consider Model Capabilities: Larger, more capable models (e.g., GPT-4) tend to benefit more from CoT and can follow more complex reasoning.
  • Verify the Reasoning: Even with CoT, models can make mistakes in their reasoning steps. Always review critical outputs.

Example Application: Python Script for CoT Problem Solving

Section titled “Example Application: Python Script for CoT Problem Solving”

Let’s use Python to send a zero-shot CoT prompt to the OpenAI API.

import openai
import os
# Securely load your API key
# openai.api_key = os.getenv("OPENAI_API_KEY")
# For this example, replace 'YOUR_API_KEY_HERE' with your actual key:
openai.api_key = 'YOUR_API_KEY_HERE'
def solve_with_cot(problem_statement):
try:
# Append a zero-shot CoT trigger phrase
prompt_content = f"{problem_statement} Let's think step by step."
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Or a newer, more capable model like gpt-4 for complex reasoning
messages=[
{"role": "system", "content": "You are a helpful assistant that solves problems by showing your reasoning steps."},
{"role": "user", "content": prompt_content}
],
max_tokens=300, # Ensure enough tokens for reasoning and answer
temperature=0.2, # Lower temperature for more focused, analytical reasoning
n=1,
stop=None
)
return response.choices[0].message['content'].strip()
except Exception as e:
return f"An error occurred: {e}"
# Example usage:
math_problem = "If a train travels at 60 miles per hour, how many miles will it travel in 2.5 hours?"
solution = solve_with_cot(math_problem)
print(f"Problem: {math_problem}")
print(f"Solution with CoT:\n{solution}")
Problem: If a train travels at 60 miles per hour, how many miles will it travel in 2.5 hours?
Solution with CoT:
Okay, let's think step by step.
1. The train's speed is 60 miles per hour. This means in 1 hour, it travels 60 miles.
2. We need to find out how far it travels in 2.5 hours.
3. To find the distance, we multiply the speed by the time: Distance = Speed × Time.
4. Speed = 60 miles/hour.
5. Time = 2.5 hours.
6. Distance = 60 miles/hour × 2.5 hours.
7. Let's calculate 60 × 2.5:
60 × 2 = 120
60 × 0.5 (which is half of 60) = 30
120 + 30 = 150.
8. So, the train will travel 150 miles in 2.5 hours.
The final answer is 150 miles.

Chain-of-Thought prompting is a powerful technique in prompt engineering that significantly enhances the reasoning capabilities of LLMs. By guiding the model to articulate its intermediate steps, CoT leads to more accurate, reliable, and interpretable results for complex tasks.

As you tackle more challenging problems with LLMs, incorporating CoT prompting (either zero-shot or few-shot) into your toolkit can unlock new levels of performance. For deeper understanding, refer to original research papers on Chain-of-Thought prompting (e.g., by Wei et al., Kojima et al.).