Chain-of-Thought & Parsing the Final Answer
For anything that needs reasoning (math, multi-step logic, careful classification), asking a model to think step by step before answering measurably improves accuracy. The catch: now the model emits a wall of reasoning, and you still need a clean machine-readable answer out the other end.
The standard trick is to make the model end on a fixed line you can parse, like Answer: 42. You build the prompt that asks for that shape, and you write the parser that extracts it. The model does the reasoning in between (ungraded), but the contract on both ends is yours.
A chain-of-thought response looks like:
There are 3 boxes with 4 apples each.
3 times 4 is 12.
Answer: 12Build two functions:
cot_prompt(question)returns a prompt string that contains the question, asks the model to reason step by step, and directs it to end with a line in the exact formAnswer: <value>.parse_final_answer(model_output)scans the output and returns the value after the LASTAnswer:line (case-insensitive, stripped). If there is no such line, returnNone.
A 2026 note: the explicit "think step by step" instruction was designed for older, non-thinking models. Modern thinking/reasoning models already do this internally, so bolting on a hand-rolled chain-of-thought can be redundant or even hurt quality. Reach for explicit CoT mainly with smaller or non-thinking models, or when you specifically need the visible steps to parse or audit.
Two details earn their keep. Last-line-wins: a model often self-corrects ("Answer: 3 ... wait, Answer: 5"), and the final line is the real answer, so scan to the bottom. None when absent: if the model never produced the line, say so honestly instead of guessing, so the caller can retry. Press Run to grade.
Write cot_prompt(question) returning a prompt that contains the question, the phrase step by step, and an Answer: directive. Write parse_final_answer(model_output) that returns the stripped text after the LAST line matching Answer: (case-insensitive), or None if no such line exists.
This lesson is locked
Lessons open one at a time. Finish the previous lesson to unlock this one.