Sub topic 10
Showing Reasoning in Examples
This chapter assumes you can read short Python, know how system, user, and assistant roles work in chat prompts, and have already written few-shot prompts with consistent input and output pairs. Here you add reasoning steps to the examples, so the model works through a problem before it answers.
Why plain input and output pairs sometimes fail
A classification example such as "review in, label out" works because the label follows quickly from the text. Some tasks need several steps. Examples are word problems, multi-condition rules, and questions that need a lookup and then a comparison. If every example jumps straight to the answer, the model learns to jump straight to an answer. On a multi-step task, that often means a guess that looks confident.
Few-shot examples with reasoning steps are often called chain-of-thought examples. Each example contains the input, a short written derivation, and then the final answer. The model tends to copy that shape. It writes its own steps for the new query, and those steps give it intermediate results to build on.
Anatomy of a reasoning example
A reasoning example has three parts in a fixed order.
- The input, as in any other example.
- The reasoning, written as short steps that each use only facts from the input or from the previous step.
- The answer, in a fixed final line that your code can find.
Here is a plain-text prompt for a shipping-cost task.
Compute the shipping cost. Show your steps, then give the final line as "Answer: <amount>".
Order: 3 books at 12 dollars each. Shipping is 5 dollars, free if the subtotal is over 30.
Steps: Subtotal is 3 x 12 = 36. 36 is over 30, so shipping is free. Total is 36.
Answer: 36
Order: 2 mugs at 9 dollars each. Shipping is 5 dollars, free if the subtotal is over 30.
Steps: Subtotal is 2 x 9 = 18. 18 is not over 30, so shipping is 5. Total is 18 + 5 = 23.
Answer: 23
Order: 4 pens at 7 dollars each. Shipping is 5 dollars, free if the subtotal is over 30.
Steps:The model continues from "Steps:". It writes a derivation and ends with an "Answer:" line.
Notice that the two examples take different branches of the rule. One gets free shipping and one pays for it. Reasoning examples should show the branches the task contains, for the same reason ordinary examples should cover the input range.
Parsing the answer
Because the output now contains steps, you can no longer compare the whole reply to a label. Extract the final line instead.
import re
def extract_answer(reply):
matches = re.findall(r"^Answer:\s*(.+)$", reply, flags=re.MULTILINE)
return matches[-1].strip() if matches else ""
reply = "Subtotal is 4 x 7 = 28. 28 is not over 30, so shipping is 5. Total is 33.\nAnswer: 33"
print(extract_answer(reply))This prints 33. Taking the last match protects you if the model mentions "Answer:" earlier in its steps. An empty string signals a reply that broke the format, which you should count as a failure when you evaluate.
You can reuse the accuracy function from the evaluation chapter by wrapping your model call so that predict returns extract_answer(reply).
In chat format
In a chat message list, put the steps and the answer together in the assistant turn. Do not split them across turns.
SYSTEM = "Compute the shipping cost. Show your steps, then end with 'Answer: <amount>'."
examples = [
("3 books at 12 dollars each. Shipping is 5, free over 30.",
"Subtotal is 3 x 12 = 36. 36 is over 30, so shipping is free.\nAnswer: 36"),
("2 mugs at 9 dollars each. Shipping is 5, free over 30.",
"Subtotal is 2 x 9 = 18. 18 is not over 30, so shipping is 5. Total is 23.\nAnswer: 23"),
]
def build_messages(examples, query):
messages = [{"role": "system", "content": SYSTEM}]
for text, reply in examples:
messages.append({"role": "user", "content": text})
messages.append({"role": "assistant", "content": reply})
messages.append({"role": "user", "content": query})
return messagesTrade-offs and common errors
- Cost. Reasoning increases output length, so each call costs more tokens and takes longer than a direct answer.
- Unneeded reasoning. For simple tasks such as basic sentiment labels, steps add cost and can introduce errors without raising accuracy. Test both versions.
- Inconsistent step style. If one example uses sentences and another uses equations, the model's steps vary, and so does your parsing.
- Missing final line. If you do not fix the answer format, the extraction step fails on some replies.
Some current models reason on their own when asked to, or have a built-in reasoning mode. In that case, check whether written steps in the examples still help before you pay for them.
Exercise
A reasoning example ends with "Total is 33." but has no "Answer:" line, while the other examples all have one. What problems does this cause, and how do you fix it?
Show answer
The model may copy either form, so some replies lack an "Answer:" line and your extraction returns an empty string, which counts as a failure. The inconsistency also weakens the format signal. Fix it by adding "Answer: 33" to the example so every example ends the same way.