Khi chuỗi tuyến tính không đủ: chia nhánh theo loại task, chạy song song để tăng tốc hoặc vote. Chapter 2 + 3 sách Gulli.
Task đa loại → 1 chain không cover hết
Classifier chọn nhánh: LLM · Embedding · Rule
Sectioning · Voting · Ensemble
Code, framework compare, anti-pattern
5 điểm nhớ + homework
• Prompt phình — chứa nhánh if/else cho mọi loại (billing, tech, sales, refund…).
• Model confuse — quá nhiều instruction, quyết định sai nhánh xử lý.
• Cost cao — mọi request đều gọi full model + full tool.
• Latency cao — tuần tự, không tận dụng parallel.
• Router classify loại — gửi vào nhánh chuyên biệt.
• Nhánh nhỏ = prompt ngắn & focused.
• Task độc lập chạy song song — giảm wall-clock time.
• Voting N model ⇒ accuracy cao ở task risky.
2 pattern thường đi cặp: Route để chọn nhánh nào, Parallel để chạy nhánh đó đồng thời với các nhánh khác.
1 LLM call chuyên classify. Linh hoạt, hiểu ngôn ngữ tự nhiên.
+50–200ms · $ trung bình · accuracy cao.
Embed input, so cosine với template embedding. Nhanh & rẻ.
+5–30ms · $ thấp · phù hợp intent set cố định.
Regex, keyword, giá trị form. Deterministic & audit dễ.
<1ms · $ 0 · giòn với input tự nhiên.
Thực chiến hay dùng lai: rule/embedding chạy trước — hit thì dùng, miss thì fallback LLM router.
"Sao thẻ tôi bị khoá?" · "App crash" · "Muốn nâng gói"…
Output JSON: {intent, confidence}. Fallback nếu confidence < 0.7.
Prompt + tool CRM, invoice.
Prompt + KB search, log query.
Prompt + pricing, upsell.
Confidence thấp ⇒ nhân viên.
Chia task thành phần độc lập, mỗi phần một LLM call chạy song song. Merge kết quả.
Ví dụ: review PR — 1 kiểm security, 1 kiểm perf, 1 kiểm style.
Chạy N lần cùng prompt (temp > 0), lấy majority. Đổi cost lấy accuracy.
Ví dụ: math, code gen critical, safety check.
Chạy N model khác nhau (Claude + GPT + Gemini), tổng hợp. Giảm bias single-model.
Ví dụ: eval nghiêm ngặt, sáng tạo cần đa dạng.
Chi phí = N × base. Chỉ dùng khi value từ độ chính xác vượt chi phí tăng.
# --- Router --- async def route(msg: str) -> str: r = await llm.classify(msg, labels=["billing", "tech", "sales"]) return r.label if r.confidence >= 0.7 else "human" # --- Parallel sectioning --- async def review_pr(diff: str) -> dict: security, perf, style = await asyncio.gather( llm.critique(diff, focus="security"), llm.critique(diff, focus="performance"), llm.critique(diff, focus="style"), ) return {"security": security, "perf": perf, "style": style} # --- Voting --- async def vote(prompt: str, n: int = 5) -> str: results = await asyncio.gather(*[ llm.generate(prompt, temperature=0.7) for _ in range(n) ]) return Counter(results).most_common(1)[0][0] # majority
| Framework | Routing | Parallelization | Ghi chú |
|---|---|---|---|
| LangGraph | Conditional edge | Fan-out/fan-in node | Visualize graph rõ ràng |
| CrewAI | Manager LLM delegates | async_execution=True |
Role-based, ít boilerplate |
| Google ADK | LlmRouter |
ParallelAgent |
Native trên Vertex |
| Claude Code | Subagent w/ description matching | Multiple tool calls / turn | Route bằng match agent description |
| Kiểu router | Cơ chế | Ưu / nhược | Dùng khi |
|---|---|---|---|
| LLM-based | Prompt classify → output nhãn | Linh hoạt / chậm, tốn token | Intent tự nhiên, đa dạng |
| Embedding | Vector similarity với template | Nhanh / kém khi intent mới | Semantic routing intent cố định |
| Rule-based | if/else, regex, keyword | Deterministic / giòn | Input có schema, keyword rõ |
| ML classifier | Fine-tuned discriminative model | Nhanh + chính xác / cần data gán nhãn | High traffic, có dataset lịch sử |
Router đặt được ở 3 vị trí: đầu chuỗi (phân loại tác vụ chính), giữa (chọn nhánh xử lý), trong subroutine (chọn tool phù hợp). LangGraph phù hợp cho routing phức tạp phụ thuộc trạng thái tích luỹ.
Tổng hợp từ Cẩm nang thực hành xây dựng HTTM (VN Gulli Ch.2) & kinh nghiệm thực chiến.
Đồng thời: tìm tin, lấy giá cổ phiếu, kiểm mạng xã hội, truy DB công ty.
Song song: sentiment, keyword, phân loại, phát hiện khẩn cấp.
Đồng thời: vé bay, khách sạn, sự kiện, nhà hàng — 4 API.
Song song: tiêu đề, thân bài, ảnh minh hoạ, CTA text.
Song song: check email format, phone, địa chỉ, ngôn từ thô.
Đồng thời: text sentiment + object detection ảnh + mô tả cảnh.
Sinh N biến thể (3 tiêu đề, 5 subject line) rồi so sánh chọn best — ứng dụng cho content ops, ad copy, landing page.
Nguồn: Cẩm nang thực hành xây dựng HTTM (VN Gulli Ch.3) · case đã Việt hoá.
# Route qua subagent mkdir .claude/agents # billing-agent.md # tech-agent.md # sales-agent.md # Claude match description claude # /agents → list & test # Parallel — nhiều tool call # trong 1 turn (native) # Bash + Read + Grep song song # Model routing theo task claude -m haiku-4-5 #nhỏ claude -m sonnet-4-6 #TB
# Composer mode = router # Ask · Edit · Agent # chọn theo intent user # Rules routing # .cursor/rules/ # frontend.mdc # backend.mdc # sql.mdc # Match theo globs # Model per rule cursor . # Settings → Models # Auto pick fast vs slow # Parallel edit nhiều file # qua Composer batch
# Route model theo task route() { case "$1" in simple) M=flash;; hard) M=pro;; esac gemini -m gemini-2.5-$M -p "$2" } # Parallel shell (Linux/macOS) gemini -p "tóm tắt" < a.txt & gemini -p "trích key" < a.txt & gemini -p "sentiment" < a.txt & wait # Windows PowerShell: # Start-Job {gemini ...}
Parallel Windows: PowerShell dùng Start-Job {...} + Wait-Job | Receive-Job. Linux/macOS dùng & + wait.
Route tip: Cursor Auto mode tự chọn model theo độ phức tạp — Claude Code & Gemini CLI cần script route() tự viết. Đo cost/turn để hiệu chỉnh.
Routing = chọn nhánh; Parallelization = chạy đồng thời. Thường đi cặp.
3 kiểu router: LLM · Embedding · Rule. Ưu tiên lai: rule/embedding trước, LLM fallback.
3 biến thể Parallel: Sectioning · Voting · Ensemble. Cost = N × base — tính trước.
Router luôn cần confidence threshold + fallback (human hoặc default agent).
Chỉ parallel khi task thực sự độc lập. Có dependency ⇒ chain lại.