[Hermes] Module 8: Sub-agents và Ủy Thác (Delegation)

Mục Tiêu Bài Học

Sau khi hoàn thành module này, bạn sẽ có thể:

  1. Hiểu được vấn đề của context poisoning và serial slowness trong agent loop truyền thống
  2. Sử dụng công cụ delegate_task để tạo sub-agents với context sạch và độc lập
  3. Tối ưu chi phí bằng cách sử dụng mô hình rẻ hơn cho sub-agents
  4. Triển khai parallelization với nhiều sub-agents chạy đồng thời
  5. Kiểm soát độ sâu và chế độ thực thi của delegation tree
  6. 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”

:cross_mark: VÍ DỤ SAI:

goal = "Fix the error"
context = ""

→ Sub-agent không biết lỗi gì, ở đâu, làm thế nào

:white_check_mark: 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

  1. Vào Hermes Agent Dashboard
  2. Chọn Config → Delegation
  3. Thiết lập:
    • Provider: Neu (hoặc Neus)
    • Model: Nvidia Nemotron 3
  4. 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

:white_check_mark: 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_task dướ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

  1. Delegation giải quyết 2 vấn đề:

    • Context poisoning (context sạch)
    • Serial slowness (parallel execution)
  2. Sub-agents know nothing: Phải cung cấp goal + context đầy đủ

  3. Cheap children, strong parent: Tiết kiệm 5x chi phí với model rẻ hơn

  4. Parallelization: Mặc định 3 concurrent, có thể tăng nhưng cẩn thận chi phí

  5. Depth control: Opt-in, mặc định flat tree (depth 1)

  6. Async mode: background = true cho non-blocking execution

  7. 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.

video https://www.youtube.com/watch?v=_6DtQkDpcEs&list=PLmpUb_PWAkDx-VWjh00tVCji794xAa_IX&index=8&pp=iAQB