Area 01 · LLM · Concept 09 of 20

Chain of Thought

Ask the model to reason step by step before it answers. Helps on multi-step problems.

01–10 · The basePhase 1 · Weeks 3–6≈ 45 min

The idea

Each token a model writes becomes context for the next. If it writes out intermediate steps first, the final answer can build on them. Asking for step-by-step reasoning measurably improves results on maths, logic, planning and multi-part questions.

In an app you usually want the reasoning separate from the answer. Ask for reasoning inside <thinking> tags and the final answer inside <answer> tags, then show only the answer to users. This also gives you something to read when the answer is wrong.

Reasoning costs output tokens and time. For simple lookups or classification it adds cost without benefit, so use it where the task has real steps.

Why it matters

It is a cheap accuracy boost for hard prompts and a debugging window into why the model failed.

Words to know

  • ScratchpadText the model writes for itself before the final answer.

Try it without code

With and without reasoning

  1. Ask the puzzle below and demand only the final number.
  2. Ask again, telling it to think step by step first.
  3. Compare correctness over a few tries.
A shop sells pens at 3 for €2. Ana buys 14 pens and pays with a €20 note. How much change does she get if leftover pens are sold at €0.70 each?

Use it in code

Request tagged reasoning and extract only the answer.

python
import re, anthropic
client = anthropic.Anthropic()

q = "A train leaves at 09:40 and the trip takes 2h 55m with a 25 min delay. When does it arrive?"
msg = client.messages.create(
    model="claude-sonnet-5-5", max_tokens=600,
    messages=[{"role": "user", "content": q + "\nThink inside <thinking> tags, then give only the time inside <answer> tags."}],
)
text = msg.content[0].text
print("ANSWER:", re.search(r"<answer>(.*?)</answer>", text, re.S).group(1).strip())
PRACTICE · TICK IT AFTER THIS

Pick 10 multi-step questions from your domain and measure accuracy with and without reasoning tags.

Common mistakes

  • Showing raw reasoning to end users.
  • Using it on every call and paying for tokens that do not help.

Go deeper