# Chain of Thought Prompting

Course: [Prompt Engineering](https://www.devtutorials.ai/learn/prompt-engineering) · Section: Advanced Prompting
Source: https://www.devtutorials.ai/learn/prompt-engineering/advanced-prompting/chain-of-thought-prompting

> Ask the model to reason before it answers: why chain of thought works, zero-shot and few-shot CoT, a fixed answer line, checking steps in code, and when CoT is worth the cost.

Ask a model this question: "A shop sells pens in packs of 12. Asha buys 7 packs and gives away 15 pens. How many are left?" A small model may answer right away with a wrong number. Now add "Think step by step", and it writes: 7 × 12 = 84, then 84 − 15 = 69. This time the answer is right.

When you ask the model to write out its thinking before it gives the answer, this is called **chain of thought** prompting, or **CoT** for short. It is one of the best-known prompting techniques, and one of the easiest to use.

**The main idea:** a model writes one token at a time. If it writes the steps first, each step gives it more text to use for its next guess. So the final answer is more likely to be right.

## A Real-World Example

Think about a maths test at school.

- If you write only the final number, one small mistake ruins the answer, and the teacher can't see what went wrong.
- If you show your working, you can catch mistakes as you go, and the teacher can see exactly which step went wrong.

Chain of thought asks the model to show its working. This helps the model get the answer right, and it helps you see where it went wrong.

## What You Will Learn

In this lesson, you will learn:

1. What chain of thought is and why it helps.
2. Zero-shot CoT: the "think step by step" instruction.
3. Few-shot CoT: showing a worked example.
4. How to put the final answer in a place where code can find it.
5. How to check the thinking steps with code.
6. When CoT helps, and when it only costs more.

The model in your browser is small, so it makes mistakes when it reasons. This makes it a good place to see how CoT helps. For harder problems, choose Qwen2.5 1.5B from the model list in the sandbox.

## Why Showing the Steps Helps

A model has no hidden notepad. It cannot "think quietly" first and then answer. Any work it does must be written out as tokens.

- **Direct answer:** the model must guess "69" as its very first token. That is a big jump.
- **With steps:** it first writes "7 × 12 = 84", which is an easy guess. Then "84 − 15 = 69" is easy too, because "84" is already in the text.

Each step is a small guess that is likely to be right. A chain of small guesses can reach an answer that one big guess often misses.

Try it:

## Zero-Shot Chain of Thought

The simplest way to use CoT is to add one extra line: "Think step by step." You don't need any examples, so this is called **zero-shot CoT**. Always add a fixed line for the final answer too, so that code can find it.

```js runnable title="Add chain of thought to any question"
function withCoT(question) {
  return question + "\nThink step by step. Then write the final answer on the last line as: Answer: <value>";
}

console.log(withCoT("A train leaves at 9:40 and the trip takes 2 hours 35 minutes. When does it arrive?"));
```

Expected output:

```text
A train leaves at 9:40 and the trip takes 2 hours 35 minutes. When does it arrive?
Think step by step. Then write the final answer on the last line as: Answer: <value>
```

## Find the Final Answer

A CoT reply is long, but your code only needs the final answer. Because the prompt asked for `Answer:` on the last line, code can find it every time.

```js runnable title="Pull the final answer out of the reasoning"
function finalAnswer(reply) {
  const match = reply.match(/Answer:\s*(.+)\s*$/i);
  return match ? match[1].trim() : null;
}

const reply = "7 packs x 12 pens = 84 pens.\nShe gives away 15, so 84 - 15 = 69.\nAnswer: 69";
console.log(finalAnswer(reply));
console.log(finalAnswer("I think it is about 70."));
```

Expected output:

```text
69
null
```

The second reply has no `Answer:` line, so the code returns `null` instead of guessing. Count these replies in your tests. They show how often the model does not follow the format.

Now run a real test. Each reply must end with the correct `Answer:` line.

**Easy Way to Remember:** CoT means **"show your working, then put the answer on its own line."** The first part helps the model. The second part helps your code.

## Check the Steps

You can see the steps, so you can check them. For maths, code can work out each sum again and point to the first wrong line. The next box checks two sample replies written for this example.

```js runnable title="Check each sum in the reasoning"
function checkSums(reply) {
  const ops = { "+": (a, b) => a + b, "-": (a, b) => a - b, "x": (a, b) => a * b, "*": (a, b) => a * b, "/": (a, b) => a / b };
  const sums = [...reply.matchAll(/(\d+)\s*([+\-x*/])\s*(\d+)\s*=\s*(\d+)/g)];
  for (const [text, a, op, b, result] of sums) {
    if (ops[op](Number(a), Number(b)) !== Number(result)) return "wrong step: " + text;
  }
  return sums.length + " steps, all correct";
}

console.log(checkSums("7 x 12 = 84. Then 84 - 15 = 69. Answer: 69"));
console.log(checkSums("7 x 12 = 74. Then 74 - 15 = 59. Answer: 59"));
```

Expected output:

```text
2 steps, all correct
wrong step: 7 x 12 = 74
```

In the second reply, the method was right, but one sum was wrong. Without the steps, you would only see a wrong answer. With the steps, you can see the exact mistake.

## Few-Shot Chain of Thought

You can also show the model one or two **worked examples** with their steps. The model then copies the way of thinking, not just the format. This is called **few-shot CoT**. It helps when your problems all follow the same pattern.

```js runnable title="Build a few-shot chain of thought prompt"
const worked = [
  {
    q: "A pack has 6 eggs. I buy 4 packs and use 5 eggs. How many are left?",
    steps: "4 packs x 6 eggs = 24 eggs. 24 - 5 = 19.",
    answer: "19",
  },
];

function fewShotCoT(examples, question) {
  const shots = examples.map((e) => "Q: " + e.q + "\nSteps: " + e.steps + "\nAnswer: " + e.answer).join("\n\n");
  return shots + "\n\nQ: " + question + "\nSteps:";
}

console.log(fewShotCoT(worked, "A box holds 8 cups. I buy 6 boxes and 5 cups break. How many are left?"));
```

Expected output:

```text
Q: A pack has 6 eggs. I buy 4 packs and use 5 eggs. How many are left?
Steps: 4 packs x 6 eggs = 24 eggs. 24 - 5 = 19.
Answer: 19

Q: A box holds 8 cups. I buy 6 boxes and 5 cups break. How many are left?
Steps:
```

The prompt ends with `Steps:`, so the model starts by writing steps in the same style, and then writes `Answer:`.

## When to Use CoT

CoT is not free. Every thinking step adds more tokens, and more tokens take more time.

```js runnable title="Compare the cost of a direct answer and CoT"
const direct = "69";
const cot = "7 packs x 12 pens = 84 pens.\nShe gives away 15, so 84 - 15 = 69.\nAnswer: 69";

const tokens = (t) => Math.ceil(t.length / 4);
console.log("Direct:", tokens(direct), "token(s)");
console.log("CoT:", tokens(cot), "tokens");
console.log("At 8 tokens per second, CoT adds about", Math.round((tokens(cot) - tokens(direct)) / 8), "seconds");
```

Expected output:

```text
Direct: 1 token(s)
CoT: 19 tokens
At 8 tokens per second, CoT adds about 2 seconds
```

Use CoT when it is worth the extra cost:

| Use CoT for | Skip CoT for |
|---|---|
| Maths and word problems | Simple facts ("capital of France") |
| Logic and rules with many steps | Sorting things into clear labels |
| Planning, making schedules, and comparing choices | Short rewrites and translations |
| Finding bugs in code step by step | Tasks where speed matters most |

Thinking that **looks** careful can still be wrong. A model can write neat steps with a wrong sum in them, or it can reach the right answer through wrong steps. When it matters, check both the steps and the answer with code. Never treat written steps as proof.

## Common mistakes

| Mistake | Wrong idea | Correct idea | What goes wrong |
|---|---|---|---|
| No answer line | "I'll find the answer in the text." | Ask for "Answer: value" on the last line. | Code picks a number from the middle of the steps. |
| CoT everywhere | "Thinking always helps." | Use it only for problems with many steps. | Simple tasks become slow and cost more. |
| Trusting neat steps | "It showed its work, so it must be right." | Check the sums and the answer. | Neat but wrong thinking gets used. |
| Asking for the number only | "Short answers are cleaner." | Let the model think first, then give the answer line. | The model jumps to a wrong answer. |
| Ignoring missing answers | "Every reply has an answer." | Count the replies with no answer line. | Format mistakes stay hidden inside your scores. |
| Small model, hard problem | "CoT fixes any problem." | Use a bigger model for hard thinking tasks. | The model writes long steps and is still wrong. |

## Exercises

In each exercise, the last line should give back a value. Press **Check** to compare your value with the expected answer.

## What to Remember

- **Chain of thought** asks the model to write its thinking before the answer.
- It helps because each written step becomes **context** for the next guess.
- **Zero-shot CoT** adds "Think step by step". **Few-shot CoT** shows worked examples.
- Always ask for a **fixed answer line**, and get the answer from it with code.
- **Check the steps** with code when you can.
- Use CoT for **problems with many steps**. Skip it for simple tasks.
- Neat steps are **not proof**. Always check the answer.

Next: **Tree of Thoughts**, where the model tries several ways of thinking and keeps the best one.
