In this post you’ll learn what chain‑of‑thought (CoT) prompting is, why it often makes large language models (LLMs) give better answers, and how to keep that reasoning faithful to the question. It matters because a model that can explain its steps is easier to trust and debug, especially when you use it for code, math, or any critical decision.
What is Chain‑of‑Thought Prompting?
Chain‑of‑thought prompting is a simple technique: instead of asking the model for a final answer straight away, you ask it to think out loud. You give an example where the model writes its reasoning step by step before giving the answer.
Example Prompt
Q: If a train travels 60 miles per hour for 3 hours, how far does it go?
A: First, multiply speed (60 miles per hour) by time (3 hours).
60 * 3 = 180.
So the train travels 180 miles.
Answer: 180 miles.
Why it works: The model sees a pattern – a question, then a series of logical steps, then the answer. It learns to reproduce that pattern for new questions.
Benefits of CoT Prompting
- Improved Accuracy – Many studies show that when LLMs are forced to reason step‑by‑step, they make fewer careless mistakes.
- Transparency – The intermediate steps act like a “scratch pad”. You can read them to see where the model went wrong.
- Better Generalisation – The model learns a reasoning habit that transfers to different domains (math, code, logic puzzles).
The Faithfulness Problem
Even with CoT, a model can produce plausible‑looking steps that don’t actually follow the question. This is called reasoning unfaithfulness: the answer is correct, but the steps are fabricated or irrelevant. When you rely on the explanation – for example, to audit code generated by the model – unfaithful reasoning defeats the purpose.
How to Encourage Faithful Reasoning
1. Use Explicit Instruction
Add a short instruction before the examples: "Think carefully and show each step before answering. Do not skip steps."
2. Provide Counter‑Examples
Show a bad reasoning trace and label it as incorrect. The model learns what to avoid.
3. Limit the Length of the Reasoning
If the model is allowed to write unlimited text, it may wander. Tell it: "Use at most three short sentences before the answer."
4. Verify with a Secondary Prompt
After the first answer, ask the model: "Did you use any assumptions? List them." This second pass often catches hidden gaps.
5. Use Self‑Consistency Sampling
Generate several CoT answers (e.g., 5 samples) and pick the most common final answer. The idea is that the correct reasoning will appear more often than a random hallucination.
A Minimal Working Example in Python
Below is a tiny script that uses OpenAI’s gpt-3.5-turbo (or any chat model) to ask a math question with CoT prompting and then checks consistency.
import openai, random
question = "A box contains 4 red, 5 blue, and 6 green balls. If you pick two balls without replacement, what is the probability both are red?"
# Base prompt with instruction and few‑shot example
base_prompt = [
{"role": "system", "content": "You are a helpful assistant that solves problems step by step."},
{"role": "user", "content": "Q: If a train travels 60 mph for 3 hours, how far does it go?\nA: First, multiply speed by time. 60 * 3 = 180. So the train travels 180 miles. Answer: 180 miles."},
{"role": "assistant", "content": "Got it. I will think step by step before answering."},
{"role": "user", "content": f"Q: {question}\nA:"}
]
answers = []
for _ in range(5): # self‑consistency sampling
resp = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=base_prompt,
temperature=0.7,
max_tokens=200
)
answers.append(resp['choices'][0]['message']['content'])
# Simple voting on the final numeric answer
final_numbers = []
for ans in answers:
# extract the number after "Answer:" (very naive)
if "Answer:" in ans:
final_numbers.append(ans.split("Answer:")[1].strip().split()[0])
most_common = max(set(final_numbers), key=final_numbers.count)
print("Most common answer:", most_common)
base_promptdefines the system role, a few‑shot example, and the instruction to think step by step.- The loop generates five independent completions, giving a small pool for self‑consistency.
- The script extracts the number after the word Answer: and picks the most frequent one.
- This pattern works for many reasoning tasks, not just math.
Common Mistakes Beginners Make
Skipping the few‑shot example. Without at least one demonstration, the model may not know you want CoT.
Using too high a temperature. A high temperature adds randomness, which can break the step‑by‑step flow.
Assuming the model’s reasoning is always correct. Always double‑check the steps, especially for critical code.
When Not to Use CoT
If the task is purely factual lookup (e.g., “What is the capital of France?”) the extra steps add latency without benefit. Also, for very short answers where the model already has high accuracy, CoT may be overkill.
Quick Checklist for Faithful CoT Prompts
- Add a clear instruction to think step by step.
- Provide at least one good example and one bad example if possible.
- Limit the number of reasoning sentences.
- Use self‑consistency sampling for high‑stakes questions.
- Follow up with a verification prompt.
FAQ
How many examples do I need for effective CoT prompting?
Usually one good example is enough, but adding a second contrasting example can improve faithfulness.
Does chain‑of‑thought work for code generation?
Yes. Prompt the model to write comments describing each line before the actual code. It helps catch logical gaps early.
Can I use CoT with open‑source models?
Absolutely. The technique is model‑agnostic; just include the instruction and examples in your prompt.
Wrapping up
Chain‑of‑thought prompting is a low‑effort way to get LLMs to reason more carefully. By adding a clear instruction, a few examples, and a sanity‑check step, you can keep that reasoning faithful and useful. Try it next time you ask a model to solve a math problem, write a function, or explain a concept – you’ll see fewer surprising mistakes and more transparent answers.