Chain-of-Thought (CoT) & Tree of Thoughts
Standard Transformer generation performs a constant amount of floating-point math per output token. When forced to output an answer immediately, the model has only a single forward pass to compute complex deductions.Chain-of-Thought (CoT) unlocks latent reasoning capability by turning intermediate token generation into a working computational scratchpad.
1. The Computational Bottleneck: Why LLMs Need Tokens to "Think"
Consider a multi-step logic puzzle:
If you demand: "Answer with just the number:", the model must predict the token in one single pass through its layers. It frequently misfires with "8" (doing 17 - 9). However, if the prompt instructs: "Think through the problem step by step before stating the answer:", the model writes:
"The problem states that 'all but 9 die'. This means exactly 9 sheep survived. Therefore, there are 9 sheep left."By generating the explanation tokens first, those tokens enter the model's in-context attention memory as inputs for subsequent tokens. The model effectively uses its own generated output as external working RAM!
2. Zero-Shot CoT vs. Few-Shot CoT
The Magic Trigger Phrase
Simply appending "Let's think step by step" to a prompt increases accuracy on GSM8K math benchmarks from 17.7% to 78.7%!
Exemplar Reasoning Trajectories
Supplying 2 to 3 demonstrations where the answers are preceded by clear mathematical or logical explanations.
A: Roger started with 5. He bought 2 cans of 3 balls (2*3=6). 5+6 = 11. The answer is 11."
3. Self-Consistency: Majority Voting Across Reasoning Paths
Wang et al. (2022) observed that complex reasoning tasks often possess multiple valid reasoning paths that lead to the same correct solution. In Self-Consistency, you sample N independent CoT chains at temperature = 0.7 and aggregate answers via majority voting:
import collections
from openai import OpenAI
client = OpenAI()
prompt = """
Q: Janet's ducks lay 16 eggs per day. She eats 3 for breakfast every morning
and bakes muffins with 4 every day. She sells the remainder at the farmers'
market for $2 per egg. How much does she make each day?
Think step-by-step and end your response with: 'Final Answer: $<amount>'.
"""
def extract_answer(text: str) -> str:
# Extracts the final numerical dollar value
if "Final Answer:" in text:
return text.split("Final Answer:")[-1].strip().replace("$", "")
return "UNKNOWN"
# Sample 5 diverse reasoning chains in parallel
responses = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
temperature=0.7,
n=5 # Generate 5 candidate completions simultaneously
)
answers = [extract_answer(choice.message.content) for choice in responses.choices]
vote_counts = collections.Counter(answers)
majority_winner, count = vote_counts.most_common(1)[0]
print(f"Sampled Answers: {answers}")
print(f"Majority Consensus Winner: USD {majority_winner} ({count}/5 votes)")4. Tree of Thoughts (ToT): Search with Backtracking
For non-linear tasks (such as solving the 24 Game, theorem proving, or architectural design), Chain-of-Thought can make an early flawed assumption and stubbornly follow it to an incorrect conclusion.Tree of Thoughts (Yao et al., 2023) maintains a tree of candidate intermediate thoughts:
Generates 3 possible candidate next steps
Evaluates candidates: (Sure / Maybe / Impossible)
Prunes dead ends and backtracks to parent node
Discovers globally optimal reasoning path
5. The Modern Frontier: Built-in Reasoning Models (OpenAI o1 & o3)
With the introduction of models like OpenAI's o1 and o3, Chain-of-Thought has moved from developer prompt engineering into the model's fundamental training loop via Large-Scale Reinforcement Learning. These models generate hidden reasoning tokens, evaluating hypotheses, identifying errors, and backtracking autonomously before delivering the user-visible answer.
Prompting Rule for o1/o3: Less is More
When using reasoning models, do not include "think step by step" or verbose few-shot CoT chains in your prompt. Explicit CoT instructions can degrade o1 performance by clashing with its internal reinforcement-learned reasoning patterns. Keep prompts direct, explicit, and focused on goal criteria and constraints.
ReAct & Tool Calling Loops
Continue to the next hands-on prompt engineering guide in this series.
Explore Related Tools
Hand-picked utilities and calculators related to this tool.
