Tình huống: bạn có 10-K (báo cáo tài chính) 800 trang của Apple. User hỏi:
- Hiểu vấn đề RAG giải quyết: document quá lớn cho prompt
- Phân biệt 2 option: stuff vs chunk+retrieve
- Biết trade-off RAG: complexity tăng, nhưng scale + cost cải thiện
- Nhận diện khi nào cần RAG vs gửi full doc
Mở đầu: 800 trang tài liệu, câu hỏi đơn giản
Risk factors của công ty là gì?
Options:
Option 1: Stuff — Dump toàn bộ
prompt = f"""Answer based on document.
<question>{user_q}</question>
<document>
{800_pages_of_text}
</document>"""
Vấn đề:
- 800 trang ≈ 400K tokens → có thể vượt context window (Sonnet 1M OK, Opus 200K fail)
- Cost: 400K × $3/M = $1.20 mỗi query
- Latency: 30-60s processing
- Quality: Claude "loãng" với context dài — miss info
Option 2: RAG — Chunk + Retrieve
1. Chunk doc thành 2000 pieces × 500 tokens mỗi
2. Khi user hỏi, tìm top 5 chunks liên quan
3. Chỉ gửi 5 chunks (2500 tokens) vào prompt
Benefits:
- Cost: 2500 × $3/M = $0.008 mỗi query (150x cheaper)
- Latency: 2-3s
- Quality: focused context → Claude trả lời chính xác
- Scale: work với 1000+ documents
Trade-off: Setup RAG pipeline phức tạp hơn.
Công thức RAG
┌──────────────────────────────────────────────────┐
│ │
│ PREPROCESSING (một lần): │
│ 1. Chunk docs thành pieces │
│ 2. Generate embeddings cho mỗi chunk │
│ 3. Store vào vector database │
│ │
│ QUERY TIME (mỗi request): │
│ 4. Embed user question │
│ 5. Search similar chunks (top K) │
│ 6. Include top K chunks in prompt │
│ 7. Send to Claude → response │
│ │
└──────────────────────────────────────────────────┘
3 bước preprocessing (one-time), 4 bước runtime (mỗi query).
Khi nào dùng RAG?
Cần RAG khi
- Document > 100K tokens
- Multi-doc corpus (search across all)
- Cost-sensitive (high query volume)
- User questions unpredictable (can't pre-select relevant sections)
- Long-term knowledge base (docs update, re-index)
Không cần RAG khi
- Doc < 20K tokens → stuff vào prompt OK
- Static doc, few queries → cost stuff is fine
- Need holistic view (summarize entire doc) → stuff
- Q&A về structure global → RAG miss picture
Consider hybrid
- Doc 100K-1M tokens: dùng Sonnet 1M context + cache. Có thể không cần RAG.
Benefits vs Challenges
| Benefits | Challenges |
|---|---|
| Scale to huge corpora | Preprocessing setup |
| Lower cost per query | Need search mechanism |
| Faster responses | Retrieval quality affects answer |
| Focused context → better answers | Chunk strategy matters |
| Works with multi-docs | Relevant context might miss |
Ví dụ: Corporate wiki
Setup
Company wiki: 5000 articles, 2M tokens total.
Without RAG
Impossible — exceed all context windows, costs 100x.
With RAG
- Preprocessing: chunk 5000 articles → 20K chunks. Embed. Store.
- Query: "Policy về WFH?" → retrieve top 5 relevant chunks → send to Claude.
- Cost per query: $0.01
- Response time: 2-3s
100 users, 10 queries each/day = $10/day. Affordable.
Ví dụ: Customer support KB
Scenario
500 product docs. Customer asks: "Why is my device not charging?"
RAG flow
- Embed question
- Retrieve: "Troubleshooting charging issues" article (top match)
- Claude receives article + question → response
- Claude cite article source
Result: Accurate answer, source-backed, scale qua 500 docs.
Technical decisions trong RAG
You'll decide:
- Chunk size (500-2000 tokens typical)
- Chunk overlap (10-20% for context preservation)
- Chunking method (size-based, sentence, structural, semantic)
- Embedding model (VoyageAI, OpenAI, Cohere)
- Vector DB (Pinecone, Chroma, Qdrant, Weaviate)
- Top K (5-20 chunks retrieve)
- Re-rank (use Claude or dedicated reranker to refine top K)
- Hybrid search (semantic + keyword)
Mỗi decision có trade-off. Module 7 cover từng piece.
Anti-patterns preview
RAG cho mọi task
Simple Q&A 1 doc 10 trang → stuff vào prompt, không cần RAG.
Chunk không có overlap
Info cắt giữa câu → chunk missing context.
Top K=1
Chỉ 1 chunk → thiếu context.
No eval on retrieval
Không biết top K có chứa relevant info không.
Full anti-patterns ở mỗi bài tiếp theo.
Áp dụng ngay
Bài tập 1: Assess RAG need (10 phút)
Cho app của bạn:
- Total corpus size: ___
- Queries/day: ___
- Context window model: ___
- Cost per query (stuffed): ___ vs (RAG): ___
Decision: RAG yes/no? Why?
Bài tập 2: Chunk sketch (10 phút)
Lấy 1 sample doc. Plan chunk strategy:
- Method: size/sentence/structure?
- Chunk size: ___
- Overlap: ___
Tóm tắt bài học
- RAG = chunk docs, retrieve relevant, inject into prompt. Scale + cost.
- Preprocessing once, query runtime. 7-step pipeline.
- Trade-off: complexity up, cost + scale dramatic improvement.
- Use RAG khi: big corpus, high volume, unpredictable queries.
- Module 7 cover từng piece: chunking, embeddings, search, hybrid, multi-index.