Sub topic 1
Step-by-step reasoning in language models
Chain-of-thought prompting asks a language model to write out intermediate steps before it gives a final answer. It is the idea that later led to reasoning models. This chapter covers what it is, why it helps, and where it stops working.
Prerequisites
This chapter assumes you can do the following:
- Call an LLM API from Python.
- Explain what a token is, and what a context window and a maximum output length are.
- Write a basic prompt.
- Read short Python scripts.
The core idea
A language model produces one token at a time. Each token costs roughly the same amount of computation, whether it is a trivial word or the key digit of a hard calculation. If the model must jump straight to an answer, all the work for that answer has to fit into the computation behind a few tokens.
When the model writes intermediate steps first, those steps become part of its context. Each later token can read them. The written text works as scratch space, so a hard problem is split into several easier ones.
Two ways to prompt for steps
Researchers found two main techniques before reasoning models existed.
- Few-shot chain of thought. You include worked examples in the prompt. Each example shows the reasoning, then the answer. The model imitates the format.
- Zero-shot chain of thought. You add an instruction such as "Think step by step." No examples are needed.
Here is a script that asks the same question both ways. It uses a standard chat model, not a reasoning model.
from openai import OpenAI
client = OpenAI()
question = (
"A shop sells pens at 3 for 4 dollars. "
"How much do 18 pens cost?"
)
prompts = {
"direct": question + " Reply with only the number.",
"step_by_step": question + " Think step by step, then give the final answer on the last line.",
}
for name, prompt in prompts.items():
response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[{"role": "user", "content": prompt}],
)
print(f"--- {name} ---")
print(response.choices[0].message.content)
print("completion tokens:", response.usage.completion_tokens)The step-by-step reply will usually use many more completion tokens. On a question this easy, both replies are probably correct. The gap in accuracy appears on multi-step problems, such as longer arithmetic, logic puzzles, and code tracing.
What you pay for
Step-by-step output is not free. Every written step is an output token, so you pay for it and wait for it.
| Approach | Output length | Accuracy on multi-step tasks | Latency |
|---|---|---|---|
| Direct answer | Short | Lower | Low |
| Chain of thought | Longer | Higher | Higher |
In this chapter's script, the visible steps and the answer arrive together in the reply. That detail matters in later chapters, where the steps can be hidden from you but still counted.
Common errors
- Treating the written steps as a faithful record. The text the model writes is a plausible explanation. It does not always match the computation that produced the answer. Do not rely on it as proof of correctness.
- Errors that compound. A wrong early step often carries through to a wrong final answer, and the result still sounds confident.
- Adding steps to easy tasks. Asking for reasoning on a simple lookup or a format conversion adds cost and delay without improving the result.
- Parsing a mixed reply. When steps and answer share one output, your code needs a reliable marker, such as "final answer on the last line."
Why this leads to reasoning models
Chain of thought depends on the user asking for it, and on the model having learned to do it from prompts. Reasoning models take the next step. They are trained so that producing a long chain of thought before answering is their default behavior. The following chapters cover how that works and how to control it.
Exercise
A colleague adds "Think step by step" to every prompt in a pipeline, including one that converts dates from "2026-11-27" to "27/11/2026". Give two reasons this change may be a poor choice for that prompt.
Show answer
The task needs no multi-step reasoning, so accuracy is unlikely to improve. The extra steps add output tokens, which raises cost and latency. The reply also now mixes reasoning text with the date, so the pipeline must parse the final answer out of it.