
Introduction
Have you ever asked ChatGPT a math problem and it answered... wrong? But when you add the line "Please explain step by step", suddenly it answers correct?
That is Chain-of-Thought (CoT) — the strongest prompt engineering technique for problems that require logical reasoning.
Research: Google Brain (2022) proves that CoT increases accuracy from 17.7% → 78.7% on the GSM8K math problem — just by adding "Let's think step by step."
1. What is Chain-of-Thought?
1.1 Main idea
Normal life example: You ask students to solve a math problem:
- No CoT: "What is the answer?" → Students guess randomly and may be wrong
- CoT included: "Explain each step before giving the answer" → Students think more carefully, make fewer mistakes
AI too! When you make the AI "think" before responding, it works much better.
1.2 Comparison: With and without CoT
❌ No CoT:
Q: Một cửa hàng có 35 quả táo. Bán đi 20 quả vào buổi sáng,
nhập thêm 15 quả vào buổi chiều. Hỏi cuối ngày có bao nhiêu?
A: 25 quả (có thể sai nếu model nhỏ)
✅ With CoT:
Q: Một cửa hàng có 35 quả táo. Bán đi 20 quả vào buổi sáng,
nhập thêm 15 quả vào buổi chiều. Hỏi cuối ngày có bao nhiêu?
Giải thích từng bước.
A: Hãy giải từng bước:
1. Ban đầu: 35 quả
2. Buổi sáng bán 20: 35 - 20 = 15 quả
3. Buổi chiều nhập 15: 15 + 15 = 30 quả
Đáp án: 30 quả ✅
2. Two types of CoT
2.1 Zero-Shot CoT — "Magic words"
Just add 1 sentence at the end of the prompt:
[Câu hỏi]
Hãy suy nghĩ từng bước. / Let's think step by step.
The most effective "magic phrases":
| Phrase | Efficiency | When to use |
|---|---|---|
| "Let's think step by step" | ⭐⭐⭐⭐⭐ | All logic problems |
| "Please explain reasoning" | ⭐⭐⭐⭐ | Vietnamese |
| "Before answering, analyze..." | ⭐⭐⭐⭐ | Analysis |
| "Break this into sub-problems" | ⭐⭐⭐⭐ | Big problem |
| "Show your work" | ⭐⭐⭐ | Math, code |
2.2 Few-Shot CoT — Give an example of how to "think"
Giải bài toán, trình bày từng bước.
Q: Một shop bán 3 áo, mỗi áo 200k. Chi phí ship 30k. Tổng tiền?
Thinking:
- 3 áo × 200k = 600k
- Ship: 30k
- Tổng: 600k + 30k = 630k
A: 630,000 VNĐ
Q: Một nhóm 5 người ăn tối, bill 1.2 triệu. Chia đều, mỗi người
tip thêm 10%. Mỗi người trả bao nhiêu?
Thinking:
- Bill chia đều: 1,200,000 ÷ 5 = 240,000/người
- Tip 10%: 240,000 × 0.1 = 24,000
- Tổng mỗi người: 240,000 + 24,000 = 264,000
A: 264,000 VNĐ
Q: [Bài toán mới của bạn]
2.3 Comparison of Zero-Shot vs Few-Shot CoT
| Zero-Shot CoT | Few-Shot CoT | |
|---|---|---|
| Setup | Add 1 sentence | Write 2-3 examples |
| Accuracy | Good | Better |
| When | Popular tasks | Task domain-specific |
| Effort | Low | Average |
| Token cost | Low | Higher |
3. CoT for Code & Debug
3.1 Debug code
Đoạn code sau bị lỗi. Phân tích TỪNG BƯỚC trước khi đưa ra fix:
1. Đọc code và hiểu intent
2. Trace execution step-by-step với input mẫu
3. Xác định chính xác dòng nào sai và tại sao
4. Đề xuất fix
5. Verify fix bằng cách trace lại
```python
def avg_positive(nums):
total = 0
count = 0
for n in nums:
if n > 0:
total += n
return total / count
**AI output (with CoT):**
- Intent: Tính trung bình các số dương
- Trace với input [3, -1, 5, -2]:
- n=3: total=3 (count không tăng!!)
- n=-1: skip
- n=5: total=8
- n=-2: skip
- return 8/0 → ZeroDivisionError! 💥
- Bug: count KHÔNG ĐƯỢC TĂNG trong vòng lặp
- Fix: thêm
count += 1sautotal += n - Verify: [3,-1,5,-2] → total=8, count=2 → 8/2=4.0 ✅ Edge case: [] → count=0 → cần handle ZeroDivisionError
### 3.2 Code Design
Thiết kế hệ thống notification cho app e-commerce.
Trước khi code, hãy:
- Liệt kê các loại notification cần hỗ trợ
- Vẽ flow diagram (text-based)
- Xác định design pattern phù hợp
- Liệt kê edge cases
- Sau đó mới viết code
Yêu cầu: Python, support email + SMS + push notification.
> **💡 Exercise 3:** Take a real bug in the code you are working on. Use CoT prompt in 3.1 to debug. Compare output with CoT vs without CoT.
---
## 4. CoT for Analysis & Decision Making
### 4.1 Business Analysis
Phân tích liệu công ty nên launch feature X. Suy nghĩ theo framework:
- Problem: Feature X giải quyết vấn đề gì?
- Market: Ai cần? Market size? Competitor có chưa?
- Effort: Bao lâu? Bao nhiêu resource?
- Impact: Revenue potential? User retention impact?
- Risks: Điều gì có thể sai? Mitigation?
- Decision: Go / No-Go / Need more data
Feature X: Tính năng "dark mode" cho app ngân hàng.
### 4.2 Pros-Cons Analysis
Đánh giá quyết định: chuyển từ monolith sang microservices.
Phân tích theo framework SWOT, nghĩ kỹ từng điểm:
For each point:
- State the point clearly
- Explain WHY it matters
- Give a concrete example
- Rate impact: High/Medium/Low
Sau SWOT, đưa recommendation cuối cùng.
---
## 5. When is CoT NOT effective?
CoT is **not a cure-all** — there are cases where it **doesn't help** or **does harm**:
| Situation | CoT effective? | Reason |
|-----------|---------|-------|
| Logic and math problems | ✅ Very effective | Need multi-step inference |
| Code debugging | ✅ Very effective | Need trace execution |
| Decision making | ✅ Effective | Many factors need to be considered |
| Translation | ❌ No need | Task pattern matching, no inference needed |
| Creative writing | ⚠️ Depends | Can limit creativity |
| Factual Q&A | ❌ No need | "Capital of Vietnam?" → no need step-by-step |
| Simple classification | ❌ Fees for tokens | Sentiment analysis → zero-shot enough |
> **Rule:** CoT is good for problems that require **multi-step reasoning**. When the task is simple, CoT **costs tokens** without increasing accuracy.
### 5.1 CoT costs
Không CoT: Input 100 tokens + Output 50 tokens = 150 tokens Có CoT: Input 100 tokens + Output 300 tokens = 400 tokens (~2.7×)
→ CoT tốn gấp ~2-3× tokens. Chỉ dùng khi cần!
> **💡 Exercise 5:** Run the same CoT yes/no question on GPT-4o-mini. Count output tokens. How much extra does CoT cost? Is improving Accuracy worth it?
---
## 6. Advanced CoT Patterns
### 6.1 Self-Verification CoT
Make the AI **check** its own answer:
Giải bài toán sau. Sau khi giải xong:
- Giải lần 1
- Kiểm tra lại: thử đáp án vào đề bài, có khớp không?
- Nếu sai, giải lại
- Đưa đáp án cuối cùng
Bài: Tìm x: 2x + 5 = 17
### 6.2 Debate CoT
Getting the AI to **debate** with itself:
Câu hỏi: "React hay Vue tốt hơn cho startup?"
Hãy:
- Lập luận ủng hộ React (3 điểm)
- Lập luận ủng hộ Vue (3 điểm)
- Phản biện mỗi lập luận
- Đưa kết luận cuối cùng dựa trên specific context: team 3 người, app MVP, deadline 2 tháng
### 6.3 Structured CoT Template
Phân tích vấn đề theo framework:
🤔 Understanding
[Hiểu problem statement]
🔍 Analysis
[Phân tích từng khía cạnh]
💡 Options
[Liệt kê các giải pháp]
⚖️ Trade-offs
[So sánh pros/cons mỗi option]
✅ Recommendation
[Chọn option tốt nhất + giải thích]
🚀 Next Steps
[Action items cụ thể]
---
## Summary
| Concepts | Remember |
|--------|--------|
| **CoT** | Getting AI to "think" before "speaking" → higher accuracy |
| **Zero-Shot CoT** | Add "Let's think step by step" |
| **Few-Shot CoT** | Give an example of reasoning |
| **Good for** | Logic, math, debug, analysis, decision |
| **No need to give** | Translation, factual Q/A, simple classification |
| **Cost** | Costs 2-3× tokens — only used when accuracy is important |
## General exercises
1. ✅ Complete small exercises (3, 5)
2. **CoT vs Direct:** Choose 10 logic/math problems. Ask AI 2 ways: direct vs CoT. Count accurately each way.
3. **Custom CoT Template:** Create a CoT template for a task at work (review design, estimate project, triage bugs). Test 5 cases.
4. **Self-Verification:** Use technique 6.1 for 5 problems. Compare accuracy with/without self-verify.
> **Next article:** Tree-of-Thoughts, Self-Consistency & Step-Back — more advanced techniques than CoT, exploring many avenues of reasoning.