# Chain-of-thought math word problem — Claude XML example

> Reason step-by-step inside <thinking>, isolate the final number in <answer>.
>
> Source: https://claudexml.com/examples/math-cot/ · Last updated 2026-05-25

Home / Examples / Chain-of-thought math word problem
    Reasoning · beginner

# Chain-of-thought math word problem

    Reason step-by-step inside <thinking>, isolate the final number in <answer>.

    Word problems where the model needs to identify variables, write equations, and solve. The classic CoT use case.


## The prompt

    Copy this verbatim. Replace the `{{ … }}` placeholders with your values.


```xml
<instructions>
Solve the word problem below. Reason inside <thinking>:
- identify unknowns,
- write the equations,
- solve step by step.

Put the final answer — a single number with units, nothing else — inside <answer>.
</instructions>

<problem>{{ math_problem }}</problem>
```


## Sample input


```xml
A train leaves Chicago at 2pm at 60 mph. Another leaves St. Louis at 3pm at 75 mph in the opposite direction. Cities are 300 miles apart. When do they meet?
```


## Expected output


```xml
<thinking>
Let t = hours after 2pm. Train A traveled 60·t miles. Train B traveled 75·(t−1).
60t + 75(t−1) = 300
60t + 75t − 75 = 300
135t = 375
t = 2.78 h ≈ 2 hours 47 minutes after 2pm
</thinking>
<answer>4:47 pm</answer>
```


## Notes & tuning tips


- Explicit "identify unknowns / write equations / solve" beats vague "think step by step" on accuracy.
- If you're using Claude's native extended thinking (Claude 3.7+), skip the  tag — that runs internally.
- Strip the scratchpad before showing the answer to end users unless they've opted in to seeing reasoning.


## What this example uses

    Tags: <instructions> <thinking> <answer>

    Patterns: chain of thought


## More like this


      reasoning
### Constraint-satisfaction logic puzzle
Solve a puzzle by enumerating constraints inside <thinking> and concluding in <answer>.

      reasoning
### Natural language → SQL with a schema
Translate plain-English questions into SQL against a provided schema. Few-shot.

      reasoning
### Code review with severity levels
Review a diff and produce structured findings (bug, perf, style) with severities.




Cite this page

`Chain-of-thought math word problem. claudexml.com. https://claudexml.com/examples/math-cot/`
