LLM Usage
Chain-of-Thought Reasoning β Making LLMs Think Step by Step
Chain-of-thought prompting enables LLMs to solve complex problems by decomposing them into intermediate reasoning steps. This guide covers CoT variants, self-consistency, tree-of-thought methods, and practical applications.
- Zero-Shot CoT β "Let's think step by step" unlocks reasoning capabilities
- Self-Consistency β Sample multiple reasoning paths and majority-vote the answer
- Tree-of-Thought β Explore branching reasoning strategies for harder problems
Thinking step by step is not just for humans anymore.
Chain-of-Thought Reasoning
Chain-of-thought (CoT) prompting enables LLMs to solve complex problems by decomposing them into intermediate reasoning steps. This tutorial covers CoT variants, theoretical foundations, and practical applications.
CoT Variants
Zero-Shot CoT
Simply append "Let's think step by step" to the prompt:
prompt = """Q: A juggeler can juggle 16 balls. Half are golf balls,
and half the golf balls are blue. How many blue golf balls?
A: Let's think step by step.
1. Total balls = 16
2. Golf balls = 16 / 2 = 8
3. Blue golf balls = 8 / 2 = 4
The answer is 4."""
Few-Shot CoT
Provide reasoning examples in the prompt:
prompt = """Q: Roger has 5 tennis balls. He buys 2 more cans of 3. How many does he have now?
A: Roger started with 5 balls. 2 cans of 3 is 6 balls. 5 + 6 = 11. The answer is 11.
Q: The school cafeteria ordered 42 apples for the lunches. They used 6 for Monday's lunches.
Then they bought 4 more cases of 3 apples each. How many apples do they have now?
A: The cafeteria started with 42 apples. They used 6, leaving 42 - 6 = 36.
They bought 4 cases of 3 = 12 apples. 36 + 12 = 48. The answer is 48.
Q: {question}
A: Let's think step by step."""
Self-Consistency
Generate multiple reasoning paths and select the most common answer:
Tree-of-Thought (ToT)
When CoT Helps
Tasks Where CoT Excels
| Task Type | Example | CoT Improvement |
|---|---|---|
| Arithmetic | Multi-step math | +30-40% |
| Logic | Syllogisms | +20-30% |
| Commonsense | Physical reasoning | +15-25% |
| Symbolic | Variable tracking | +25-35% |
| Multi-hop QA | Reading comprehension | +10-20% |
Tasks Where CoT Hurts
- Simple factual recall: CoT adds unnecessary complexity
- Classification: CoT can lead to overthinking
- Creative writing: CoT constrains creativity
- Translation: CoT is not helpful for direct mapping
Mathematical Foundation
CoT as Search
CoT can be viewed as a search problem in the space of reasoning sequences:
Self-Consistency as Ensemble
Self-consistency can be viewed as an ensemble of different reasoning strategies:
Implementation
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf")
def generate_cot(question, n_samples=5, temperature=0.7):
prompt = f"Q: {question}\nA: Let's think step by step.\n"
answers = []
for _ in range(n_samples):
inputs = tokenizer(prompt, return_tensors="pt")
output = model.generate(
**inputs, max_new_tokens=512,
temperature=temperature, do_sample=True,
)
response = tokenizer.decode(output[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True)
# Extract final answer
if "The answer is" in response:
answer = response.split("The answer is")[-1].strip().split(".")[0]
answers.append(answer)
# Majority vote
from collections import Counter
counts = Counter(answers)
return counts.most_common(1)[0][0], dict(counts)
question = "If a train travels at 60 mph for 2.5 hours, then at 80 mph for 1.5 hours, what is the total distance?"
answer, distribution = generate_cot(question)
print(f"Answer: {answer}")
print(f"Distribution: {distribution}")
Practice Exercises
- Empirical: Compare zero-shot, few-shot, and CoT prompting on 10 arithmetic problems. Measure accuracy for each.
- Self-Consistency: Implement self-consistency with 3, 5, 10, and 20 samples. At what point does accuracy plateau?
- ToT: Implement a simple tree-of-thought system for a planning problem. Compare with standard CoT.
- Analysis: Identify 5 problems where CoT helps and 5 where it hurts. What pattern emerges?
Advanced CoT Methods
Auto-CoT
Auto-CoT automatically generates chain-of-thought demonstrations by clustering questions and selecting representative examples from each cluster. This eliminates the need for manual CoT example crafting.
Program-of-Thought
Instead of natural language reasoning, generate executable code that solves the problem. The code is executed to produce the answer. This combines the reasoning capabilities of LLMs with the precision of program execution.
Graph-of-Thought
Extends tree-of-thought by allowing reasoning paths to merge and form a graph structure. This enables sharing of intermediate results across different reasoning branches, improving efficiency and solution quality.
Evaluating CoT Quality
When evaluating CoT, assess both the reasoning process and the final answer. Key evaluation criteria include:
- Logical coherence of the reasoning steps
- Faithfulness to the provided context or problem
- Completeness of the reasoning chain
- Accuracy of the final answer
- Conciseness of the reasoning process
What to Learn Next
-> In-Context Learning Teaching LLMs new tasks without trainingβpurely through prompts.
-> Prompt Engineering Getting the most out of language models through effective input design.
-> RAG System Design Building production-ready retrieval systems for grounded generation.
-> Retrieval-Augmented Generation Combining LLMs with external knowledge for accurate, cited answers.
-> LLM Agent Frameworks Building autonomous agents that reason, plan, and act.
-> Building Production LLM Apps From prototype to production: deploying LLMs at scale.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type β Always start by categorizing the problem
- Incorrect formula application β Verify the formula applies to your specific case
- Unit errors β Always check units are consistent throughout
- Sign errors β Pay careful attention to positive and negative values
- Rounding too early β Keep full precision until the final answer
- Forgetting constraints β Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic β Build on the foundation laid here
- Next Topic β See how this concept extends to more advanced material
- Practice Problems β Additional exercises with solutions
- Video Explanation β Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions