Mục Tiêu Bài Học
Sau khi hoàn thành module này, bạn sẽ có thể:
- Hiểu được vấn đề của context poisoning và serial slowness trong agent loop truyền thống
- Sử dụng công cụ
delegate_taskđể tạo sub-agents với context sạch và độc lập - Tối ưu chi phí bằng cách sử dụng mô hình rẻ hơn cho sub-agents
- Triển khai parallelization với nhiều sub-agents chạy đồng thời
- Kiểm soát độ sâu và chế độ thực thi của delegation tree
- Phân biệt khi nào nên dùng delegate_task so với các công cụ khác
1. Giới Thiệu: Tại Sao Cần Ủy Thác?
1.1 Hai Vấn Đề Cốt Lõi Của Agent Loop Truyền Thống
Vấn đề 1: Context Poisoning (Nhiễm độc Context)
- Mọi tool call, retry, và ngõ cụt từ subtask dài đều tích tụ trong context window
- Context bị đầy → chất lượng suy giảm → model bắt đầu hallucinate
- Hậu quả: Agent làm việc kém hiệu quả theo thời gian
Vấn đề 2: Serial Slowness (Chậm Tuần Tự)
- Agent chỉ có thể làm một việc tại một thời điểm
- 10 tác vụ nghiên cứu = 10 lần thời gian thực thi
- Hậu quả: Tốn thời gian gấp bội với công việc scale lớn
1.2 Giải Pháp: Sub-agents & Delegation
Sub-agent là gì?
- Một agent hoàn toàn mới, độc lập
- Có conversation, terminal session, và tool set riêng
- Nhận “brief” từ parent agent và trả về báo cáo cuối cùng
- Chỉ summary quay trở lại parent - không có thông tin trung gian
Mô hình Manager-Worker:
Parent Agent (Manager)
↓ Ủy thác brief
Child Agent (Worker) - Làm việc trong "clean room"
↓ Chỉ trả về summary
Parent Agent nhận kết quả tổng hợp
2. Công Cụ delegate_task
2.1 Cú Pháp Cơ Bản
delegate_task(
goal="Mục tiêu cụ thể của subtask",
context="Thông tin đầy đủ để thực hiện task",
tool_sets=["file", "web", "terminal"] # Scope tools tối thiểu
)
2.2 Quy Tắc Vàng: “Sub-agents Know Nothing”
VÍ DỤ SAI:
goal = "Fix the error"
context = ""
→ Sub-agent không biết lỗi gì, ở đâu, làm thế nào
VÍ DỤ ĐÚNG:
goal = "Fix the type error in API handlers.py line 47"
context = """
- File path: /project/src/api/handlers.py
- Error trace: TypeError: 'NoneType' object is not subscriptable
- Project root: /project
- Python version: 3.11
- Stack: ...
"""
→ Sub-agent có đầy đủ thông tin để làm việc độc lập
2.3 Tool Scoping
| Tool Set | Sử Dụng Cho |
|---|---|
file + terminal |
Code work, debugging, edits, builds |
web |
Research, fact-checking, doc lookups |
file (chỉ) |
Read-only analysis, code review (không execution) |
Blocked Tools cho Leaf Children (mặc định):
delegate_task(không recursion)clarify(không hỏi user)memory(không truy cập shared memory)send_message(không gửi tin nhắn)execute_code(không escape sandbox)
3. Tối Ưu Chi Phí: Cheap Children, Strong Parent
3.1 Chiến Lược
| Vai Trò | Model | Chi Phí | Công Việc |
|---|---|---|---|
| Parent | GLM 5.2 / GPT-5 / Claude | Cao | Orchestration, synthesis, judgment |
| Children | Nvidia Nemotron 3 | Thấp (10x rẻ hơn) | Grunt work, research, scanning |
3.2 Cấu Hình Trong Dashboard
- Vào Hermes Agent Dashboard
- Chọn Config → Delegation
- Thiết lập:
- Provider: Neu (hoặc Neus)
- Model: Nvidia Nemotron 3
- Lưu cấu hình
3.3 Kết Quả Demo
Task: Research 3 topics về soccer data và ML techniques
| Phương Án | Thời Gian | Chi Phí | Chất Lượng |
|---|---|---|---|
| Single Agent (GLM 5.2) | ~9 phút | 66¢ | Tốt |
| Sub-agents (Nemotron + GLM parent) | ~9 phút | 13¢ | Tương đương |
Tiết kiệm 5x chi phí với cùng chất lượng!
4. Parallelization & Concurrency
4.1 Cơ Chế Hoạt Động
- Nhiều subtasks được truyền vào
delegate_taskdưới dạng array - Chạy song song trong thread pool
- Kết quả trả về theo thứ tự input index
- Mặc định: 3 sub-agents concurrent
4.2 Concurrency Control
max_concurrent_children = 3 # Mặc định
Lưu ý:
- Batch lớn hơn limit → Lỗi (không queue hoặc drop)
- Cần raise limit hoặc chia nhỏ batch
- Nếu parent bị interrupt → Tất cả children bị cancel
4.3 Agent Monitor
Sử dụng lệnh /agents để xem trạng thái:
- Số lượng sub-agents đang chạy
- Tiến độ từng agent
- Tool calls đang thực hiện
Desktop App Visualization:
- Hiển thị trực quan tree structure
- Xem real-time activity của từng sub-agent
- Dễ dàng phát hiện lỗi
5. Kiểm Soát Độ Sâu và Chế Độ Thực Thi
5.1 Depth Control (max_spawn_depth)
Mặc định: max_spawn_depth = 1 (Flat Tree)
Parent (depth 0)
├── Child 1 (depth 1) - KHÔNG thể delegate thêm
├── Child 2 (depth 1) - KHÔNG thể delegate thêm
└── Child 3 (depth 1) - KHÔNG thể delegate thêm
Với role = orchestrator và max_spawn_depth = 2:
Parent (depth 0)
├── Orchestrator 1 (depth 1)
│ ├── Worker 1A (depth 2)
│ ├── Worker 1B (depth 2)
│ └── Worker 1C (depth 2)
├── Orchestrator 2 (depth 1)
│ ├── Worker 2A (depth 2)
│ └── Worker 2B (depth 2)
└── Orchestrator 3 (depth 1)
├── Worker 3A (depth 2)
└── Worker 3B (depth 2)
Cảnh báo: Depth 3 với 3 children/level = 27 concurrent leaves → Chi phí nhân lên!
5.2 Execution Modes
Synchronous (Mặc định)
delegate_task(goal="...", context="...")
# Block parent turn cho đến khi child finish
# Nếu parent bị interrupt → children bị cancel
Asynchronous (background = true)
delegate_task(goal="...", context="...", background=True)
# Child chạy nền, không block parent turn
# Kết quả re-enter conversation khi finish
# Single session - die khi session ends
6. Demo: 3-Level Research Tree
6.1 Cấu Trúc
Parent Agent (GPT 5.5)
├── Orchestrator 1: Baseball
│ ├── Worker 1A: Data sources
│ ├── Worker 1B: Advanced stats
│ └── Worker 1C: ML uses
├── Orchestrator 2: Basketball
│ ├── Worker 2A: Data sources
│ ├── Worker 2B: Advanced stats
│ └── Worker 2C: ML uses
└── Orchestrator 3: Football
├── Worker 3A: Data sources
├── Worker 3B: Advanced stats
└── Worker 3C: ML uses
Tổng số sub-agents: 12 (3 orchestrators + 9 workers)
6.2 Cấu Hình
max_spawn_depth: 2
orchestrator_enabled: true
role: orchestrator # Cho 3 sport agents
max_concurrent_children: 3
child_timeout_seconds: 1200 # Tăng từ default 60s
max_iterations: 15
6.3 Prompt Mẫu
“Research the state of machine learning in sports. Delegate one orchestrator per sport: baseball, basketball, football. Have each orchestrator delegate three research workers: What data exists, what advanced stats exist, and how machine learning has been used. Each orchestrator synthesizes its sport, then you synthesize all three into one report.”
6.4 Kết Quả
- 12 sub-agents chạy concurrent
- Mỗi orchestrator tổng hợp 3 worker reports
- Parent tổng hợp 3 báo cáo thành final report
- Report chất lượng cao với cross-sports synthesis
7. Khi Nào Nên Dùng Công Cụ Nào?
| Công Cụ | Use Case | Đặc Điểm |
|---|---|---|
| Single Loop | Sequential steps, cần memory, user interaction | <50 turns, có memory |
| delegate_task | Independent subtasks, parallel fanout, cheap grunt work | Ephemeral, summary only |
| Cron Job | Durable, scheduled work, survives session restart | Lịch trình, persistent |
| execute_code | Deterministic pipelines, không cần LLM reasoning | Mechanical, multi-step |
| Kanban Board | Multi-agent collaboration, persistent, durable | Module 9 |
Nguyên tắc: Không phải cứ nhiều sub-agents là tốt. Chọn công cụ phù hợp với nhu cầu thực tế.
8. Tổng Kết
Key Takeaways
-
Delegation giải quyết 2 vấn đề:
- Context poisoning (context sạch)
- Serial slowness (parallel execution)
-
Sub-agents know nothing: Phải cung cấp goal + context đầy đủ
-
Cheap children, strong parent: Tiết kiệm 5x chi phí với model rẻ hơn
-
Parallelization: Mặc định 3 concurrent, có thể tăng nhưng cẩn thận chi phí
-
Depth control: Opt-in, mặc định flat tree (depth 1)
-
Async mode:
background = truecho non-blocking execution -
Chọn đúng công cụ: Single loop vs Delegate vs Cron vs Code vs Kanban
9. Bài Tập Thực Hành
Bài 1: Cơ Bản
Tạo parent agent để research 3 frameworks React khác nhau sử dụng sub-agents với model rẻ.
Bài 2: Parallelization
So sánh thời gian và chi phí giữa single agent và sub-agents cho task: “Summarize 5 research papers”.
Bài 3: Depth Control
Xây dựng 2-level tree để research về “AI trends 2026” với các categories và sub-categories.
Bài 4: Async Mode
Thiết lập background sub-agent để kiểm tra trạng thái API và trả về kết quả sau khi hoàn thành mà không block conversation.
10. Tài Liệu Tham Khảo
- Module 7: Cron & Automation (work stretched across time)
- Module 9: Profiles & Kanban Board (multi-agent collaboration, persistent)
- Video gốc: Hermes Agent Masterclass - Module 8
Chuẩn bị cho Module 9: Bạn sẽ học về Profiles - các Hermes instances độc lập với persona, memory, skills, và gateways riêng, cùng với Kanban Board cho collaboration durable.