All steps
Step by step
Without the steps
Parts of this pattern
- Question Input / output
- A multi-step word problem: '3 muffins cost $5. What do 12 muffins and 2 coffees at $3.50 cost?'
- Model LLM call
- The one model call starts here, prompted to show its working. The steps after it are text it writes in that same reply.
- Step 1 LLM call
- The first reasoning step the model writes: how many packs of three make up twelve muffins.
- Step 2 LLM call
- The second step, which uses the first step's result: the cost of the four packs of muffins.
- Step 3 LLM call
- The third step prices the coffees; with all the parts written down, the total is a simple sum.
- Answer Input / output
- The final line of the same reply, 'Answer: $27', which your code parses out.
- Question → Model
- The question goes to the model with worked examples, or with the line 'Let's think step by step.'
Q: 3 muffins cost $5. What do 12 muffins and 2 coffees at $3.50 cost? Let's think step by step.
- Model → Step 1step by step
- Instead of answering at once, the model starts writing numbered steps.
1. 12 muffins is 12 ÷ 3 = 4 packs of three.
- Step 1 → Step 2
- Each step is written after the one before, so it can use that step's result.
2. 4 packs × $5 = $20 for the muffins.
- Step 2 → Step 3
- The model moves on to the next part of the question, keeping the running result in view.
3. 2 coffees × $3.50 = $7.
- Step 3 → Answer
- The reply ends with one answer line, built from the steps above it.
4. $20 + $7 = $27. Answer: $27
- Model → Answerno steps
- Without the prompt to show its working, the model jumps straight to an answer and can skip a step.
Answer: $25
← → neighboursesc atlas
Chain-of-Thought
Also known as CoT · Step-by-step reasoning · Zero-shot CoT
The model writes out its intermediate reasoning steps before the final answer, all inside one call.
Introduced by Wei et al. (2022) · Google Research & Brain Team
Wei et al. prompted with worked examples (few-shot). Later in 2022, Kojima et al. showed that the single line 'Let's think step by step' gets much of the same gain with no examples (zero-shot CoT).
Measured over one run
- Nodes
- 6. Boxes in the diagram: models, tools, stores, people and plain code.
- LLM calls
- 5. Model invocations in the first scenario: each time a model or agent node starts thinking, plus each branch it decides.
- Tool calls
- 0. Messages sent to a tool node in the first scenario.
- Steps
- 3. Steps in the first scenario, with repeated steps counted every time they run.
- Scenarios
- 2. Distinct example runs you can switch between on the diagram.
- Loop
- no. Whether the flow cycles back on itself until a condition is met.
- Parallel
- no. Whether two or more messages are in flight at the same time.
- Branch
- yes. Whether the flow takes one path out of several, chosen at run time.
- Human in the loop
- no. Whether a person appears as a node, to approve, correct or answer.
- Autonomy
- low. How much the model decides the control flow itself, from fixed code paths (low) to open-ended (high).
- Cost
- low. Relative token spend per request.
- Latency
- medium. Relative wall-clock time per request.
Counted from the first scenario's timeline when the site is built.
Problem
Ask a model “3 muffins cost $5. What do 12 muffins and 2 coffees at $3.50 cost?” and ask for the number only. Often it gets it right. Sometimes it says $25. The question needs three small calculations, and a model asked to answer at once has to do all of them in the single step of writing the first token of its answer. There is nowhere to keep the intermediate results.
How it works
Chain-of-thought gives the model that space by asking it to write the working out before the answer. The question goes to the model with a few worked examples that show numbered steps, or simply with the line “Let’s think step by step.”
The model then writes step 1 (12 ÷ 3 = 4 packs), step 2 (4 × $5 = $20) and step 3 (2 × $3.50 = $7). Each step is generated after the one before, so it can read that step’s result instead of holding it in its head. The reply ends with one line, “Answer: $27”, which your code parses out as the answer.
All of this happens in one call. The steps are not separate requests and your code does not run between them; they are just the first part of the reply. That is the whole pattern, and it is the building block for the rest of this family.
Wei et al. introduced the idea in 2022 with worked examples (few-shot). Kojima et al. showed the same year that the one line “Let’s think step by step” gets much of the gain without any examples. Reasoning models now do this natively: they write hidden thinking before answering, which is the same mechanism trained in rather than prompted.
When to use
- The task needs several steps of arithmetic, logic or symbol manipulation, and the model skips or fumbles them when it answers directly.
- You want to see why a model reached an answer, for debugging or review.
- You are composing a larger pattern: voting over several reasoning paths, or a decomposition such as least-to-most.
When to avoid
- The question is a single lookup or a classification. Extra steps add tokens and latency and do not help.
- Latency is critical. The reasoning can be many times longer than the answer.
- The model is small. The original paper found the steps help only at large scale, and can hurt smaller models.
- You are already using a reasoning model. It thinks before it answers anyway, and asking it to write its steps again mostly duplicates that work.
Trade-offs
You pay for the reasoning tokens on every call, and the answer arrives later because it comes last. In exchange you get a large accuracy gain on multi-step problems, a trace you can read, and a simple design: one call, no orchestration. The trace is not a guaranteed explanation, though. The model can write plausible steps that are not what actually drove its answer.
Failure modes
- Unparseable output: the reply never reaches an “Answer:” line, or is cut off by the token limit. Leave room for the reasoning, take the answer from the last answer line only, and fall back to an extraction call.
- Confident wrong steps: one bad step carries through to the end, and the neat working makes the error look trustworthy. Check important results, or sample several chains and vote.
- Arithmetic slips inside the steps: writing the steps down does not make the model a calculator. If the numbers matter, let code do them, as in program-aided reasoning.
- Leaking the working: the steps may contain things the end user should not see. Show only the answer unless the product needs the working.
Build it with your AI
Paste this into Claude Code, Cursor, ChatGPT or any AI coding assistant.
Implement the Chain-of-Thought pattern in this codebase, using the LLM client and language we already use.
## Goal
Answer a multi-step question with one model call whose output shows the working first and then a single, machine-readable final answer line.
## How it works
1. Keep 2 to 4 worked examples for the task type, each with a question, numbered reasoning steps and a final line that starts with 'Answer:'.
2. Fill in the Step-by-step prompt with the examples and the user's question, and call the model once.
3. Split the reply at the last line that starts with 'Answer:'. Everything before it is the reasoning; the rest of that line is the answer.
4. Return the answer to the caller and keep the reasoning for logs and debugging, not for the end user, unless the product needs to show its working.
5. If no 'Answer:' line is found, call the model once more with the reply and the Answer extraction prompt to pull the answer out.
## Prompts
### Step-by-step prompt
```markdown
Solve the problem. Work through it step by step, one short numbered
step per line, then give the result on a final line that starts with "Answer:".
{{#each examples}}
Question: {{question}}
{{reasoning}}
Answer: {{answer}}
{{/each}}
Question: {{question}}
{{#unless examples}}Let's think step by step.{{/unless}}
```
### Answer extraction prompt
```markdown
Below is a worked solution that does not end with a clear answer line.
<question>{{question}}</question>
<solution>{{reasoning}}</solution>
Reply with exactly one line: "Answer: " followed by the final answer
the solution reaches. If it reaches none, reply "Answer: unknown".
```
## Stop rule
Stop after the one reasoning call, or after the single extraction call when the answer line is missing. If extraction also fails, return no answer with a status saying the reply could not be parsed.
## Requirements
- Keep the worked examples in a data file, not inline in code, so they can be swapped per task type.
- Parse the answer from the last 'Answer:' line only, so a step that mentions the word earlier is never taken as the answer.
- Leave room in the output token limit for the reasoning, and log when a reply is cut off before its answer line.
- Make the examples optional: with none, the prompt falls back to the zero-shot line 'Let's think step by step.'
- Add a test with a stub LLM that returns three reasoning lines then 'Answer: $27'; assert exactly one model call and that the returned answer is exactly '$27'.
Prompt templates
Solve the problem. Work through it step by step, one short numberedstep per line, then give the result on a final line that starts with "Answer:".
{{#each examples}}Question: {{question}}{{reasoning}}Answer: {{answer}}
{{/each}}Question: {{question}}{{#unless examples}}Let's think step by step.{{/unless}}Below is a worked solution that does not end with a clear answer line.
<question>{{question}}</question><solution>{{reasoning}}</solution>
Reply with exactly one line: "Answer: " followed by the final answerthe solution reaches. If it reaches none, reply "Answer: unknown".Related patterns
Composes well with:
Sources
From the Agentic Atlas by Joran Kikke · Edit on GitHub · Suggest a correction