Bỏ qua để đến nội dung

Các mẫu Workflow

Thời gian học: ~35 phút

Yêu cầu trước: Module 12.1 (Claude Code + n8n)

Kết quả: Sau module này, bạn sẽ biết 6 workflow pattern tái sử dụng, hiểu khi nào apply từng pattern, và combine pattern cho complex scenario.


Bạn build được one-off n8n workflow. Nhưng mỗi automation mới như bắt đầu từ số 0. Approach không nhất quán, lỗi lặp lại, workflow khó maintain.

Workflow pattern là “công thức” của automation. Giống như software design pattern (Factory, Observer, Strategy), đây là proven solution cho recurring problem. Biết pattern = nhanh chóng assemble solution cho hầu hết AI automation challenge.


[Input] → [Claude: Step 1] → [Claude: Step 2] → [Claude: Step 3] → [Output]

Dùng khi: Task phải theo thứ tự, mỗi step cần output của step trước. Ví dụ: Extract → Analyze → Summarize → Format

┌→ [Claude: Task A] →┐
[Input] ─┼→ [Claude: Task B] →┼→ [Merge] → [Output]
└→ [Claude: Task C] →┘

Dùng khi: Independent task có thể chạy đồng thời. Ví dụ: Analyze document cho sentiment, keyword, và entity cùng lúc.

[Input] → [Claude: Classify] → [Switch] ─→ [Handler A]
├→ [Handler B]
└→ [Handler C]

Dùng khi: Input khác nhau cần processing path khác nhau. Ví dụ: Route support ticket theo category (billing, technical, urgent).

[Input] → [Claude: Draft] → [Wait for Approval] → [IF Approved] → [Execute]
↓ No
[Claude: Revise] → [Back to Wait]

Dùng khi: AI output cần human review trước khi action. Ví dụ: Email draft, code change, content publishing.

[Input List] → [Split In Batches] → [Claude: Process Each] → [Aggregate] → [Output]

Dùng khi: Process nhiều item, cần rate limiting hoặc chunking. Ví dụ: Analyze 100 document, 10 cái một lần.

[Input] → [Claude: Try] → [IF Error] → [Claude: Fix] → [Retry]
↓ Success
[Output]

Dùng khi: Claude có thể fail, cần graceful retry. Ví dụ: Code generation với validation.


Workflow: [Webhook] → [Research] → [Outline] → [Write] → [Edit] → [Output]

Research Node: claude -p "Tìm 5 điểm chính về: {{ $json.topic }}"

Outline Node: claude -p "Tạo outline từ các điểm:\n\n{{ $json.stdout }}"

Write Node: claude -p "Viết bài 500 từ từ outline:\n\n{{ $json.stdout }}"

Edit Node: claude -p "Edit cho clear và SEO:\n\n{{ $json.stdout }}"

Test: curl -X POST http://localhost:5678/webhook/blog -d '{"topic": "remote work"}'

Classify Node: claude -p "Classify thành billing/technical/general/urgent. Trả về CHỈ category:\n\n{{ $json.description }}"

Code Node (clean output):

const category = $input.first().json.stdout.trim().toLowerCase();
return [{ json: { category, original: $('Webhook').first().json } }];

Switch Node: Map billing→0, technical→1, urgent→2, general→3

Connect: Output 0→#billing-support, 1→#engineering, 2→PagerDuty, 3→Auto-response

Test: curl -X POST http://localhost:5678/webhook/ticket -d '{"description": "Payment bị fail!"}'

Split In Batches: Size 10, reset mỗi run

Execute Command: claude -p "Summarize mỗi document:\n\n{{ JSON.stringify($json) }}"

Merge Node: “Merge By Position” để collect tất cả output


Mục tiêu: Build 3-step translation pipeline.

Hướng dẫn:

  1. Webhook nhận text bất kỳ ngôn ngữ
  2. Claude #1: Detect language
  3. Claude #2: Translate sang English
  4. Claude #3: Summarize 1 câu
  5. Return summary
💡 Hint

Mỗi Execute Command output ở $json.stdout. Reference với {{ $json.stdout }} trong node tiếp theo.

✅ Solution

Node 1 - Detect: claude -p "Language này là gì? Trả lời tên ngôn ngữ thôi:\n\n{{ $json.text }}"

Node 2 - Translate: claude -p "Translate sang English:\n\n{{ $('Webhook').first().json.body.text }}"

Node 3 - Summarize: claude -p "Summarize 1 câu:\n\n{{ $json.stdout }}"

Mục tiêu: Route message theo type khác nhau.

Hướng dẫn:

  1. Claude classify: question, complaint, feedback, spam
  2. Switch route tới 4 Slack channel khác nhau
  3. Test với 10 input khác nhau
💡 Hint

Cho Claude return CHỈ category name. Dùng Code node clean/lowercase output trước Switch node.

✅ Solution

Classify prompt: "Classify thành question/complaint/feedback/spam. Trả về CHỈ từ đó:\n\nMessage: {{ $json.message }}"

Code node:

return [{ json: { type: $input.first().json.stdout.trim().toLowerCase() } }];

Switch rules: Map mỗi type tới output 0-3, connect tới Slack node tương ứng.

Mục tiêu: Process 20 item theo batch 5.

Hướng dẫn:

  1. Webhook nhận array 20 item
  2. Split In Batches (size 5)
  3. Claude summarize mỗi batch
  4. Merge tất cả result
💡 Hint

Sau Split In Batches, workflow chạy 4 lần (20/5). Dùng Merge node cuối để collect output.

✅ Solution

Split In Batches: Batch Size = 5

Execute Command: claude -p "Summarize các item:\n\n{{ JSON.stringify($json) }}"

Merge node: Mode = “Merge By Position”


ScenarioPattern
Multi-step transformationSequential Pipeline
Independent parallel taskFan-Out/Fan-In
Xử lý khác nhau theo typeClassification Router
Cần human approvalHuman-in-the-Loop
Nhiều item cần processBatch Processing
Có thể fail, cần retryError Recovery Loop
NodeMục đích
Split In BatchesChunk array thành nhóm nhỏ
MergeCombine parallel branch
SwitchMulti-way routing (3+ path)
WaitPause chờ external webhook
IFBinary branching (2 path)
{{ $json.stdout }} // Claude output từ node trước
{{ $('NodeName').first().json.field }} // Field cụ thể từ node có tên
{{ JSON.stringify($json) }} // Serialize cho Claude prompt

❌ Sai✅ Đúng
Một Claude call khổng lồChia thành sequential step với prompt rõ ràng
Sequential khi parallel đượcFan-out cho independent task (nhanh 3x)
Không human review cho risky actionHuman-in-the-loop cho email, payment, publishing
Process 1000 item cùng lúcBatch processing với rate limiting (10-20/batch)
Không error handlingError Recovery pattern cho production
Hardcoded routing ruleClaude classify, Switch node route
Mix pattern randomChoose primary pattern, compose intentionally

Scenario: Công ty e-commerce Việt Nam process 200+ customer review mỗi ngày. Cần: analyze sentiment, extract product issue, route team phù hợp, respond.

Problem: Manual process tốn 4 tiếng/ngày. Response không consistent. Negative review đôi khi bị miss.

Multi-Pattern Solution:

Pattern 1: Batch Processing
└─ 200 review → batch 20
Pattern 2: Sequential Pipeline (per review)
└─ Analyze sentiment → Extract issue → Generate response
Pattern 3: Classification Router
└─ positive → Marketing | negative → Support | neutral → Product
Pattern 4: Human-in-the-Loop
└─ Negative review cần manager approval

Result (sau 1 tháng):

  • Processing time: 4 tiếng → 30 phút
  • Response consistency: 100% theo cùng format
  • Escalation: Zero negative review bị miss
  • Customer satisfaction: +15%

Quote: “Pattern cho build 2 ngày thay vì 2 tuần custom code. Team support giờ chỉ review và approve.”


Tiếp theo: Module 12.3: Điều phối n8n + SDK