Chain-of-Thought (CoT) prompting is a technique that improves LLM reasoning by instructing the model to show its work step by step before giving a final answer. When the model breaks a complex problem into intermediate reasoning steps, it produces more accurate results on math, logic, and multi-step tasks.
At a Glance
- Problem: On multi-step problems such as math, logic, or tracing execution order, the model jumps to a confident wrong answer, collapsing dependent steps into a single leap and dropping context between them.
- Forces: Accuracy on dependent reasoning steps versus token cost and latency; an auditable trace versus reasoning that may be a post-hoc narrative; step-by-step output helps hard tasks but can make simple ones worse.
- Solution: Ask the model to write out intermediate reasoning before its final answer, zero-shot with a "think step by step" instruction or few-shot with worked examples, then extract the answer from a marked position.
- Consequences: Gains: higher accuracy on multi-step tasks and a trace you can inspect. Costs: responses three to ten times longer, more latency, an extra answer-extraction step, and thorough-looking chains that hide an early error.
What problem does Chain-of-Thought solve?
Ask a language model to solve a word problem, a logic puzzle, or a multi-step calculation and you will often get a confident, wrong answer. The model skips straight to a conclusion without working through the intermediate pieces. The model can reason; its default generation behavior just favors short, direct responses.
This matters most when the task involves several dependent steps: a math problem that requires unit conversion before multiplication, a scheduling question where constraints interact, or a debugging session where you need to trace execution order. In each case, the model has the knowledge to get things right, but it collapses the reasoning into a single leap and lands somewhere incorrect.
If you walk through the problem yourself, each individual step is straightforward. The hard part is chaining them together without dropping context along the way.
How does Chain-of-Thought work?
At its most basic, Chain-of-Thought prompting means asking the model to show its work. By generating intermediate reasoning steps before arriving at a final answer, the model allocates more computation to the problem and keeps track of partial results in its own output.
Three variants are worth knowing. The first is zero-shot Chain-of-Thought, where you append something like "Let's think step by step" to your prompt, with no examples. That instruction alone can raise accuracy on reasoning tasks a great deal, because it moves the model off its default shortcut. The second variant is few-shot Chain-of-Thought. Here you provide a handful of worked examples that demonstrate the step-by-step reasoning format you want. The model picks up on the pattern and applies it to new inputs. This tends to be more reliable than zero-shot because the model has a concrete template to follow. The third variant, Auto-CoT, builds the few-shot examples automatically: it clusters the questions, picks one representative from each cluster, and generates a reasoning chain for it with the zero-shot "Let's think step by step" prompt, then uses those chains as demonstrations. It takes more infrastructure than hand-written examples, and it removes the need to write them for each new problem type.
The prompt changes the generation pattern so the model uses reasoning abilities it already has; it teaches the model nothing new. When the model writes out "First, I need to convert miles to kilometers" before doing the conversion, it is giving itself a working memory that persists across tokens. Each step constrains the next, which lowers the chance of a wrong final answer.
When should you use Chain-of-Thought?
Chain-of-Thought works best when the task has a clear sequence of logical steps. Math problems, multi-hop question answering, code analysis, and planning tasks are all good candidates. If you find yourself thinking "the model should be able to do this" but it keeps getting the answer wrong, try CoT.
It is also useful when you need to audit the model's reasoning. A bare answer gives you nothing to debug, while a step-by-step trace shows where the logic went off track, which makes it far easier to fix your prompt or identify edge cases.
You probably do not need CoT for simple factual retrieval, straightforward classification, or tasks where the model already performs well. Adding "think step by step" to a sentiment analysis prompt is unlikely to help and spends extra tokens.
When not to use it: Skip it on latency-sensitive paths where a response three to ten times longer breaks the budget; develop with Chain-of-Thought offline and ship a more direct prompt instead. If a single chain keeps building on an early mistake, one trace will not catch it; use Self-Consistency to vote across several chains, or Self-Check to verify the final answer. When the task needs real computation rather than a narrated one, use Code Execution instead.
Implementation
# Using OpenAI SDK for illustration — swap client for any provider
from openai import OpenAI
import re
client = OpenAI()
def chain_of_thought(question: str) -> str:
"""Zero-shot CoT: ask the model to think step by step."""
response = client.chat.completions.create(
model="gpt-4o",
messages=[{
"role": "user",
"content": f"{question}\n\nThink step by step, then give your final answer after 'ANSWER:'."
}],
)
full_response = response.choices[0].message.content
# Extract the final answer after the reasoning
match = re.search(r"ANSWER:\s*(.+)", full_response, re.DOTALL)
return match.group(1).strip() if match else full_response
# Usage
result = chain_of_thought("If a store has 3 shelves with 8 books each, and 5 books are removed, how many remain?")
print(result) # 19
Worked Example: Cost and Latency
An internal tool answers 20,000 multi-step billing questions a day with the large model. Assume a 200-token question, a 50-token direct answer, and a step-by-step response of 400 tokens, which is eight times longer and inside the three-to-ten-times range this page gives.
| Setup | Calls per request | Tokens per request (in / out) | Cost per 1,000 requests | Latency per request |
|---|---|---|---|---|
| Direct answer | 1 | 200 / 50 | $1.35 | ≈ 1.3 s |
| Zero-shot Chain-of-Thought | 1 | 250 / 400 | $6.75 | ≈ 7.2 s |
Direct: 200 input tokens × $3.00 per million = $0.0006, plus 50 output tokens × $15.00 per million = $0.00075, so $1.35 per 1,000 requests. Chain-of-Thought adds 50 tokens of instruction and 350 tokens of reasoning: 250 × $3.00 per million = $0.00075, plus 400 × $15.00 per million = $0.006, so $6.75 per 1,000, five times the direct cost. Output tokens dominate both cost and time: 0.5 s + 400 / 60 s ≈ 7.2 s, against 0.5 s + 50 / 60 s ≈ 1.3 s. At 20,000 questions a day that is $27 versus $135.
The numbers favor Chain-of-Thought when a wrong answer costs more than the extra half a cent per request and the user can wait several seconds; for simple lookups or interactive paths with tight latency budgets, the direct answer wins.
What are the common pitfalls?
The most common failure mode is verbose but wrong reasoning. The model generates a plausible-looking chain of steps that contains a subtle error early on, and then the rest of the reasoning faithfully builds on that mistake. The step-by-step format can make this harder to catch, because the output looks thorough and confident.
Another issue is faithfulness. The reasoning the model writes out is not necessarily the reasoning it is actually using internally. Sometimes the model arrives at an answer through pattern matching and then constructs a post-hoc justification. The steps might look logical and still be a story told after the fact rather than a record of the computation.
Over-reasoning causes trouble too. On simple tasks, forced step-by-step output can send the model down rabbit holes: it considers edge cases that do not apply, second-guesses itself, and ends up producing a worse answer than it would have with a direct response.
Finally, watch out for prompt sensitivity. Small changes in how you phrase the CoT instruction can lead to very different reasoning patterns. "Think step by step" and "Break this down into steps" and "Show your work" can all produce different quality outputs depending on the model.
What are the trade-offs?
The first cost is tokens. A step-by-step response is typically three to ten times longer than a direct answer. That means higher latency and higher API costs. For a single query this hardly matters, but at scale the cost adds up.
There is also a reliability trade-off. CoT improves average accuracy but introduces more variance in output format. You need to parse the final answer out of a longer response, which means you need either a reliable extraction step or a consistent output format.
The debugging advantage holds, with one caveat. You are debugging the model's stated reasoning, which may not reflect its actual reasoning process. Treat the chain of thought as a useful signal rather than ground truth about the model's internals.
For latency-sensitive applications, the extra generation time may rule it out. Consider whether you can run CoT offline for prompt development and then distill the results into a more direct prompt for production.
Goes Well With
Self-Consistency is the usual companion to Chain-of-Thought. Generate multiple reasoning paths for the same problem and take the majority answer. This addresses the variance problem: a single chain might go wrong, but the consensus across many chains is much more reliable.
ReAct Loop extends Chain-of-Thought by interleaving reasoning steps with tool use. Instead of only thinking through a problem, the model can pause to look something up, run a calculation, or check a fact before continuing its reasoning chain.
Prompt Optimization can automate the search for the best CoT format. Rather than manually tweaking your "think step by step" instruction, let an optimizer find the phrasing and few-shot examples that produce the most accurate chains for your specific task.
References
- Wei, J., et al. (2022). Chain-of-Thought Prompting Elicits Reasoning in Large Language Models. NeurIPS 2022. arXiv
- Kojima, T., et al. (2022). Large Language Models are Zero-Shot Reasoners. NeurIPS 2022. arXiv
- Zhang, Z., et al. (2022). Automatic Chain of Thought Prompting in Large Language Models. arXiv preprint (Auto-CoT). arXiv
Further Reading
- Wei et al., "Chain-of-Thought Prompting Elicits Reasoning in Large Language Models" (2022). The original paper, showing that prompting models to show intermediate reasoning steps sharply improves accuracy on arithmetic, commonsense, and symbolic reasoning benchmarks. arXiv:2201.11903