Sub topic 8
Asking for Step-by-Step Reasoning
This chapter assumes you can write clear instructions, separate input with delimiters, and request a structured output format. You should also be comfortable reading short Python snippets. Here you will learn when to ask a model to reason before answering, how to phrase that request, and how to keep the reasoning from breaking your output format.
Why reasoning prompts help
A language model writes its reply one token at a time. Each new token is conditioned on the prompt and on every token it has already written. The model has no hidden scratch space. If a question needs several intermediate results, the model can only "hold" them by writing them out.
When you ask for an answer immediately, the model must commit to a result in its first few tokens. When you ask it to work through the problem first, the intermediate steps become part of the context. The final answer can then depend on them.
This helps most on tasks with several dependent steps:
- Arithmetic and unit conversions.
- Logic puzzles and conditional rules ("if A and not B, then...").
- Comparing options against several criteria.
- Checking a claim against a passage of text.
It helps little on simple lookups, rewriting, or tone changes. There, extra reasoning only adds length and cost.
Two ways to ask
The simplest form is a short instruction such as "Think through this step by step before answering." This is often called zero-shot chain-of-thought prompting. You can also give a few worked examples that show the reasoning, which combines this chapter with few-shot prompting.
A more reliable form names the steps you want. Compare these two prompts.
A store sells pens at 3 for $4. Maya buys 12 pens and pays with a $20 bill.
How much change does she get?A store sells pens at 3 for $4. Maya buys 12 pens and pays with a $20 bill.
How much change does she get?
Work through these steps:
1. Find how many groups of 3 pens are in 12 pens.
2. Multiply that number by $4 to get the total cost.
3. Subtract the total from $20.
Write each step on its own line. Then write the final line as "Answer: $X".The second prompt fixes the order of work and gives you a predictable last line. The correct result is 4 groups, a cost of $16, and $4 in change.
Keeping reasoning separate from the answer
Reasoning text is useful for the model but often a nuisance for your program. If you need to parse the final answer, ask for the reasoning and the answer in clearly marked places. A tagged layout works well.
import re
PROMPT = """Decide whether the customer is eligible for a refund.
Rules:
- Refunds are allowed within 30 days of purchase.
- Digital items are not refundable once downloaded.
Case:
\"\"\"
{case}
\"\"\"
First write your reasoning inside <reasoning> tags.
Then write only "yes" or "no" inside <answer> tags.
"""
def extract_answer(reply):
match = re.search(r"<answer>\s*(yes|no)\s*</answer>", reply, re.IGNORECASE)
if match is None:
return None
return match.group(1).lower()
reply = """<reasoning>The purchase was 10 days ago, so it is inside 30 days.
The item is physical, so the digital rule does not apply.</reasoning>
<answer>yes</answer>"""
print(extract_answer(reply))
print(extract_answer("I think yes."))The function returns yes for the tagged reply and None for the untagged one. As with any structured output, treat None as a signal to retry or flag the reply, not as a crash.
Common errors
- Reasoning that sounds right but is wrong. Written steps are not proof. A model can produce a fluent chain that contains an arithmetic slip or an invented fact. Check the final answer against test cases, and do arithmetic in code when exactness matters.
- Reasoning on trivial tasks. Asking for steps on a simple rewrite wastes tokens and can add errors. Use it only where the task has dependent steps.
- Answer before reasoning. If your format puts the answer first, the model commits early and the reasoning only justifies it. Put the reasoning first.
- Leaking reasoning to users. If the output goes straight to a customer, strip the reasoning section or ask for a short explanation instead.
- Unmarked final answer. Without a fixed last line or tag, your parser has to guess where the answer is.
Check the effect
Do not assume a reasoning prompt is better. Score it with the same test cases you use for any prompt change, and compare accuracy and reply length for both versions.
Exercise
A prompt asks a model to "give the final answer first, then explain your step-by-step reasoning." Why is this ordering a weak choice for a multi-step problem, and how would you fix it?
Show answer
The model writes tokens in order, so it must commit to an answer before it has produced any intermediate steps. The reasoning then only justifies a guess. Fix it by asking for the reasoning first, followed by the answer in a fixed, parseable place such as an <answer> tag or a final line starting with "Answer:".
Exercise
Your parser calls extract_answer on each reply and gets None for about 5 percent of replies. Name two likely causes and one response in code.
Show answer
Likely causes: the model omitted or misspelled the <answer> tag, or it wrote something other than "yes" or "no" inside it. In code, retry the request once or twice, and if it still fails, log the reply and route the case to human review instead of guessing.