Article
promptingreasoningchain-of-thoughtcotllm-techniqueszero-shot-cotprompt-engineeringlogic
Improve LLM Reasoning with Chain-of-Thought Prompting
Force LLMs to 'show their work' before giving a final answer. Chain-of-Thought (CoT) prompting guides the model through step-by-step reasoning, dramatically improving accuracy on logic, math, and multi-step problems by making its thought process explicit and verifiable.
beginner15 min4 steps
The play
- Understand the Baseline: Standard PromptingFirst, ask an LLM a multi-step reasoning question directly. Notice how it might jump to an incorrect conclusion because it doesn't break down the problem. This establishes why a more structured approach is needed.
- Use Zero-Shot Chain-of-ThoughtFor capable models, you can trigger step-by-step reasoning without providing full examples. Simply append a phrase like 'Let's think step by step.' to your question. This is the easiest way to implement Chain-of-Thought and often works very well.
- Apply Few-Shot Chain-of-Thought for Harder ProblemsFor more complex tasks, guide the model by providing examples ('shots') that demonstrate the reasoning process. In your prompt, include a complete question-and-answer pair where the answer explicitly breaks down the logic. This teaches the model the desired format.
- Verify and Debug the Reasoning PathThe key benefit of Chain-of-Thought is the explicit reasoning path. Read the model's step-by-step output to verify its logic. If the final answer is wrong, you can often pinpoint the exact step where the error occurred, making it easier to debug and refine your prompt or logic.
Starter code
import os
from openai import OpenAI
# Make sure to set your OPENAI_API_KEY environment variable.
# export OPENAI_API_KEY='your-api-key'
# For OpenAI compatible endpoints, you can set OPENAI_API_BASE
client = OpenAI()
# A multi-step reasoning problem
problem = "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?"
# Zero-Shot Chain-of-Thought prompt
# We append "Let's think step by step." to trigger the reasoning process.
prompt = f"""Q: {problem}
Let's think step by step."""
print(f"--- Sending Prompt ---\n{prompt}\n----------------------")
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": prompt}
],
temperature=0
)
result = response.choices[0].message.content
print(f"--- LLM Response ---\n{result}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure your OPENAI_API_KEY is set correctly.")