Chain-of-Thought
Ask the model to reason step by step to improve accuracy on hard problems.
For problems that need reasoning — math, logic, multi-step decisions — telling the model to think step by step before answering often improves accuracy. This is called chain-of-thought prompting.
Why it helps
Writing out intermediate steps gives the model room to work through the problem instead of jumping straight to a guess. Each step conditions the next.
Keeping the final answer clean
If you only want the final answer in your app, ask the model to reason and then output the answer in a clearly marked place you can parse.
Example
{
"messages": [{
"role": "user",
"content": "A shirt costs $40 and is 25% off. Think step by step, then give the final price on a line starting with ANSWER:."
}]
}When to use it
- A finance app prompts the model to 'think step by step' when calculating amortisation schedules, catching arithmetic errors that direct answers miss.
- A medical-decision support tool asks the model to reason through differential diagnoses one step at a time before giving a final recommendation.
- A logic-puzzle game uses chain-of-thought prompting so the model explains each deduction, letting players see the reasoning process.
More examples
Simple step-by-step reasoning prompt
Appending 'Think step by step' triggers the model to show each calculation before giving the final answer.
from openai import OpenAI
client = OpenAI()
r = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content':
'A train travels 120 km in 1.5 hours. '
'Another train travels 200 km in 2.5 hours. '
'Which is faster? Think step by step.'
}]
)
print(r.choices[0].message.content)Chain-of-thought with system instruction
Moves the chain-of-thought instruction to the system prompt so it applies to every user question.
from openai import OpenAI
client = OpenAI()
r = client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{'role': 'system', 'content':
'Always reason through the problem step by step before giving your final answer.'},
{'role': 'user', 'content':
'If I have 3 boxes each containing 12 apples and I give away 17 apples, how many remain?'}
]
)
print(r.choices[0].message.content)Extract final answer after reasoning
Asks the model to mark its final answer with a prefix so it can be parsed separately from the reasoning.
from openai import OpenAI
client = OpenAI()
r = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content':
'Solve: 17 × 24. Reason step by step, '
'then put your final numeric answer on the last line prefixed with ANSWER:'
}]
)
full = r.choices[0].message.content
answer_line = [l for l in full.splitlines() if l.startswith('ANSWER:')]
print(answer_line[0] if answer_line else full)
Discussion