Chain-of-Thought and Step-by-Step Reasoning
Use chain-of-thought prompts to guide the model through complex problems step by step, dramatically improving accuracy on math, logic, and multi-hop reasoning tasks.
What Is Chain-of-Thought Prompting?
Chain-of-Thought (CoT) prompting is a technique where you ask the model to show its reasoning step by step before giving a final answer. Instead of jumping straight to the conclusion, the model produces intermediate reasoning steps that lead to the answer. This simple change dramatically improves accuracy on complex tasks involving math, logic, and multi-step reasoning.
The technique was introduced in a 2022 Google Brain paper that showed CoT prompting could enable large language models to solve math word problems that they completely failed at with direct prompting. The key insight is that generating intermediate steps forces the model to allocate more computation to hard problems.
The Magic of 'Let's Think Step by Step'
The simplest form of chain-of-thought prompting is adding the phrase 'Let's think step by step' to your prompt. This zero-shot CoT trigger was discovered by researchers who found that this single phrase caused models to spontaneously produce reasoning chains even without few-shot examples.
import openai
client = openai.OpenAI()
# Without CoT - the model often gets multi-step math wrong
direct_prompt = 'A store has 48 apples. They sell 3/4 of them, then receive a new shipment of 20. How many apples do they have?'
# With zero-shot CoT
cot_prompt = direct_prompt + "\n\nLet's think step by step."
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': cot_prompt}]
)
print(response.choices[0].message.content)All lessons in this course
- Zero-Shot and Few-Shot Prompting
- Chain-of-Thought and Step-by-Step Reasoning
- System Prompts and Persona Definition
- Prompt Iteration and Debugging