Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 9/ 160
01. LLM Fundamentals

Chain of Thought

Step-by-step reasoning prompts that improve multi-hop math, planning, and tool selection.

IntermediateStep-by-step · Scratchpad3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

Asking the model to show its working makes it less likely to skip a step and get the answer wrong.

Think of it like…

Long division on paper versus doing it in your head: showing the intermediate lines lets you carry values forward and catch mistakes, but a neat-looking page can still contain an error in line three.

Where you’ve seen it

A logistics assistant answers "If a truck carries 18 pallets and each pallet holds 42 cartons, and 3 pallets are damaged, how many cartons arrive?" Direct answering is right 71 percent of the time on a test set of 300 such problems. With "work through it in steps, then give 'Answer: N'" it reaches 93 percent, at about 5 times the output tokens. The team uses it only for the calculation intent, not for simple lookups.

Words you’ll hear

Chain of thought
Intermediate reasoning written out before the answer.
Zero-shot CoT
Triggering reasoning with an instruction like "think step by step".
Self-consistency
Sample several reasoning chains and take the majority final answer.
Faithfulness
Whether the written reasoning reflects what actually drove the answer.

Visual diagram

Loading interactive diagram

Theory & deep dive

Why writing steps helps

A transformer spends a fixed amount of computation per generated token. A problem needing many sequential operations can't be solved reliably in the single step that produces the answer, but each reasoning token is another step whose output feeds the next, so the chain lets the model decompose the problem. Writing intermediate values also reduces slips from holding too much "in its head".

Two common forms: zero-shot CoT ("Let's think step by step") and few-shot CoT, where examples show the reasoning style. In both, ask for a clearly marked final line ("Answer: …") so your code can extract it. CoT helps most on arithmetic, symbolic and commonsense multi-hop tasks and on choosing among tools; it helps little on simple factual recall or style tasks, where it only adds cost.

A reasoning prompt with an extractable answer

"Work through the problem step by step. Then write the result on a final line as 'Answer: <number>'." Parse the last line with a regex, and treat missing or malformed answers as failures to retry.

Key takeaway: Steps give extra computation and a scratchpad; always request a parsable final answer.

Self-consistency, limits and reasoning models

Self-consistency samples n chains at moderate temperature, extracts each final answer and returns the most frequent one. Independent mistakes rarely coincide, so voting raises accuracy on tasks with a checkable answer, at n times the cost. A cheaper variant uses a verifier: a second call or code that checks the answer, such as running a unit test or recomputing the arithmetic.

Written reasoning is not guaranteed to be faithful: models can produce plausible steps that rationalise an answer reached another way, and a confident chain can still contain an error. Don't show raw reasoning to users as authoritative explanation, and don't skip verification for high-stakes outputs. Reasoning models (a separate lesson) internalise this process with extended hidden deliberation trained by reinforcement learning, which often outperforms prompting a standard model to reason; with them, explicit "think step by step" instructions are usually unnecessary.

Verify instead of trusting

For a generated SQL query, run EXPLAIN or a test query; for arithmetic, recompute in code; for code, run tests. A programmatic check catches wrong chains that look convincing.

Key takeaway: Vote or verify; treat written reasoning as helpful, not proof.

How it works, step by step

Step 1 of 4
Design

1. Find tasks that need steps

Use CoT for multi-step maths, logic, planning and tool choice; skip it for lookups and simple rewrites.

calculation intent → CoT · FAQ → direct
Key numbers at this step
Extra cost
≈ 3 – 5× tokens

When to use it & trade-offs

What it is

Chain-of-thought (CoT) prompting asks a model to write out intermediate reasoning steps before the final answer, either with an instruction ("think step by step") or with worked examples that include reasoning. Generating the steps gives the model more tokens of computation to use and a scratchpad whose earlier lines condition later ones, which improves multi-step maths, logic, planning and tool selection. Self-consistency samples several chains and votes on the final answer.

Why it matters: Questions that need several dependent steps are often answered wrongly when the model must jump straight to a result. CoT is the cheapest way to improve them, and the idea underlies today's reasoning models. It also has costs: more output tokens, higher latency, and reasoning text that looks convincing even when it is wrong, so it needs verification rather than trust.

When to reach for it

  • Arithmetic and word problems
  • Multi-hop questions and planning
  • Choosing which tool to call and with what arguments

When to avoid or be careful

  • CoT on trivial lookups where it only adds cost
  • Showing raw reasoning to users as a trustworthy explanation
  • Accepting reasoning chains without any check on the final answer

Ways to improve multi-step answers

Zero-shot / few-shot CoT
Best for: Standard models on reasoning-heavy prompts

Strengths: Easy to add; large gains on multi-step tasks.

Watch out: More output tokens and latency; steps may be unfaithful.

Self-consistency or verifier
Best for: Tasks with a checkable final answer

Strengths: Further accuracy gains; catches errors.

Watch out: Multiplies cost, or needs a verifier.

Dedicated reasoning model
Best for: Hard maths, code and planning

Strengths: Trained for deliberation; strongest results.

Watch out: Higher latency and price; less control over reasoning.

Interview signals & numbers to know

When this topic comes up

  • “The model gets multi-step math wrong”
  • “What is chain of thought?”
  • “Self-consistency”
  • Reasoning vs non-reasoning models
  • Cost and latency of reasoning

Benchmarks & rules of thumb

≈ 3 – 5× -
typical extra output tokens with CoT
n× -
cost of self-consistency with n samples
last line -
where to put the final answer for parsing

Common mistakes & follow-ups

Common interview mistakes

  • Using CoT everywhere and tripling cost on tasks that were already accurate.
  • Parsing the answer from anywhere in the text, so a number in the reasoning is mistaken for the result.
  • Treating a fluent explanation as evidence the answer is right.

Likely interviewer follow-ups

  • Why does writing out steps improve accuracy?
  • What is self-consistency and when is it worth the cost?
  • How does CoT relate to reasoning models?
Interview tip: Say when you'd skip CoT as well as when you'd use it. Cost-aware judgement is what interviewers look for.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Chain of Thought with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Few-shot Prompting
Next topic
Reasoning Models