Bỏ qua, đến nội dung chính
Hiep Lam
Quay lại

Claude Code: Dynamic Workflow vs Subagent - phân biệt đầy đủ

Chỉnh sửa trang

Ghi chú kiến thức - xác minh ngày 2026-06-12. Nguồn: (1) transcript 2 session thực tế trên máy local, (2) tài liệu chính thức Claude Code Docs (code.claude.com/docs), (3) mô tả tool Workflow/Agent trong system prompt của Claude Code. Mọi fact được tag [source]; suy đoán chưa kiểm chứng được tag [unverified].


Mục lục

  1. Bối cảnh thí nghiệm - 2 session thực tế
  2. Câu 1: Khi nào model dùng subagent, khi nào dùng workflow?
  3. Câu 2: Khác biệt cốt lõi - “ai giữ kế hoạch”
  4. Chi tiết về Subagents
  5. Chi tiết về Dynamic Workflows
  6. Script API của Workflow
  7. Quality patterns trong workflow
  8. Khi nào dùng cái nào
  9. Nguồn

1. Bối cảnh thí nghiệm - 2 session thực tế

Cùng một prompt (tìm giá Mac mini M4 24GB/512GB thấp nhất ở VN), chạy 2 lần trên model Fable/Opus:

Session 1Session 2
Session ID4d981e63-6621-426d-93fc-2bda9dc877fcd0a2ae0a-2347-4e2a-9453-df99fbe20d8a
Prompt có nhắc workflow?KhôngCó - nguyên văn “tạo dynamic workflows”
Setting/effort max/model Opus 4.8 (1M context)
Model đã làm gìGọi tool Agent 4 lần (fan-out subagent) + 5 WebFetch + 1 Bash + 2 ReadGọi tool Workflow 1 lần
Quy mô~4 subagentWorkflow spawn 14 agents, 416,398 tokens, 341 tool uses, ~10.7 phút, chạy background

[source: transcript ~/.claude/projects/-Users-todd-lam-WORK-ogp-ai-docs/{4d981e63...,d0a2ae0a...}.jsonl - đếm tool_use bằng jq]

Kết luận từ thí nghiệm: hành vi không ngẫu nhiên. Không yêu cầu workflow → model dùng Agent (subagent). Yêu cầu rõ → model dùng Workflow. Đúng chính xác quy tắc thiết kế ở mục 2.


2. Câu 1: Khi nào model dùng subagent, khi nào dùng workflow?

Nguyên tắc: Workflow là opt-in, Subagent là mặc định

Subagent (tool Agent) không cần opt-in. Claude tự quyết định delegate dựa trên mô tả task: “When Claude encounters a task that matches a subagent’s description, it delegates to that subagent, which works independently and returns results.” [source: Subagents docs]

Workflow (tool Workflow) bắt buộc opt-in. Mô tả tool trong system prompt ghi nguyên văn: “ONLY call this tool when the user has explicitly opted into multi-agent orchestration. Workflows can spawn dozens of agents and consume a large amount of tokens; the user must request that scale, not have it inferred… For any other task - even one that would clearly benefit from parallelism - do NOT call this tool. Use the Agent tool for individual subagents.” [source: Workflow tool spec trong system prompt Claude Code]

4 con đường kích hoạt workflow

[source: Dynamic workflows docs + Workflow tool spec]

  1. Yêu cầu bằng lời trong prompt: “use a workflow”, “run a workflow”, “fan out agents”, “orchestrate this with subagents”, “tạo dynamic workflows”… - “Claude treats a direct request as the same opt-in”. Yêu cầu phải nằm trong lời của user, không phải do model tự suy ra task “sẽ có lợi”.
    • Lưu ý lịch sử: trước v2.1.160, từ khóa kích hoạt nguyên văn là workflow; từ v2.1.160 là ultracode (natural-language vẫn hoạt động ở cả hai). Claude Code highlight từ khóa trong input; bấm Option+W (macOS) / Alt+W để bỏ kích hoạt cho prompt đó; tắt hẳn keyword trigger trong /config.
  2. Từ khóa ultracode trong prompt: chạy 1 task đơn lẻ dưới dạng workflow mà không đổi effort của session.
  3. /effort ultracode cho cả session: kết hợp reasoning effort xhigh + tự động orchestration. Khi bật, Claude tự quyết dùng workflow cho mọi task lớn - 1 request có thể thành nhiều workflow nối tiếp (understand → change → verify). Reset khi mở session mới. Chỉ có trên model hỗ trợ xhigh.
    • Quan trọng: /effort max (dùng ở session 1) không kích hoạt workflow tự động - chỉ ultracode mới có cơ chế này.
  4. Chạy workflow có sẵn: bundled workflow như /deep-research, workflow đã save (.claude/workflows/ hoặc ~/.claude/workflows/), hoặc một skill/slash command mà instructions của nó chỉ định gọi Workflow.

Nếu thấy model dùng workflow mà không yêu cầu?

Nguyên nhân khả dĩ: (a) session đang bật /effort ultracode, (b) một skill tự gọi Workflow (hợp lệ theo opt-in rule), (c) model sampling không deterministic nên đôi khi diễn giải lệch - riêng (c) là [unverified], không có transcript để kiểm chứng.

Phê duyệt trước khi chạy (approval)

[source: Dynamic workflows docs]

Permission modeKhi nào bị hỏi
Default, accept editsMỗi lần chạy, trừ khi đã chọn “Yes, and don’t ask again” cho workflow đó trong project đó
AutoChỉ lần đầu; chọn Yes thì ghi consent vào user settings. Bỏ qua hoàn toàn khi ultracode bật
Bypass permissions, claude -p, Agent SDKKhông bao giờ hỏi - chạy ngay

Prompt phê duyệt hiển thị các phase dự kiến; Ctrl+G mở script trong editor, Tab chỉnh prompt trước khi chạy. Subagent do workflow spawn luôn chạy ở mode acceptEdits và kế thừa tool allowlist của bạn, bất kể mode của session - file edits được auto-approve; Bash/WebFetch/MCP ngoài allowlist vẫn có thể prompt giữa chừng.


3. Câu 2: Khác biệt cốt lõi - “ai giữ kế hoạch”

Workflow không phải là “loại agent khác” - nó là một script JavaScript do Claude viết, để runtime thực thi, điều phối chính các subagent ở quy mô lớn. Subagent là “the worker primitive workflows orchestrate”. [source: Dynamic workflows docs]

Bảng so sánh đầy đủ 4 cơ chế multi-step, lấy nguyên từ docs chính thức [source]:

SubagentsSkillsAgent teamsWorkflows
Bản chấtMột worker do Claude spawnInstructions Claude làm theoMột lead agent giám sát các peer sessionMột script do runtime thực thi
Ai quyết bước tiếp theoClaude, từng lượtClaude, theo promptLead agent, từng lượtChính script
Kết quả trung gian nằm đâuContext window của ClaudeContext window của ClaudeShared task listBiến trong script
Cái gì tái sử dụng đượcĐịnh nghĩa workerInstructionsĐịnh nghĩa teamToàn bộ orchestration
Quy môVài task mỗi lượtNhư subagentsVài peer chạy dàiHàng chục–hàng trăm agent mỗi run
Bị ngắtChạy lại lượtChạy lại lượtTeammates chạy tiếpResume được trong cùng session

Trích docs: “A workflow moves the plan into code. With subagents, skills, and agent teams, Claude is the orchestrator: it decides turn by turn what to spawn or assign next, and every result lands in a context window. A workflow script holds the loop, the branching, and the intermediate results itself, so Claude’s context holds only the final answer.” [source]

Hệ quả thực tế:


4. Chi tiết về Subagents

[source: Subagents docs]

Bản chất

Built-in subagents

AgentModelToolsMục đích
ExploreHaikuRead-onlyTìm file, search code, khám phá codebase. Có 3 mức thoroughness: quick / medium / very thorough
PlanInheritRead-onlyResearch trong plan mode
general-purposeInheritTất cảTask phức tạp nhiều bước, vừa explore vừa sửa
statusline-setupSonnet-Khi chạy /statusline
claude-code-guideHaiku-Câu hỏi về tính năng Claude Code

Explore và Plan bỏ qua CLAUDE.md và git status để chạy nhanh/rẻ; mọi subagent khác load cả hai. Explore/Plan là one-shot, không resume được.

Context khởi tạo của subagent (non-fork)

Subagent bắt đầu với context mới hoàn toàn - không thấy lịch sử hội thoại, không thấy file Claude đã đọc. Context gồm: (1) system prompt riêng của agent + environment details (không phải full system prompt của Claude Code), (2) task message do Claude viết khi delegate, (3) CLAUDE.md toàn bộ hierarchy (trừ Explore/Plan), (4) git status snapshot (trừ Explore/Plan), (5) skills được preload qua field skills.

Custom subagent

File Markdown + YAML frontmatter, đặt tại (ưu tiên giảm dần): managed settings → --agents CLI flag → .claude/agents/ (project) → ~/.claude/agents/ (user) → plugin agents/. Quản lý qua lệnh /agents.

Frontmatter fields chính: name, description (bắt buộc - Claude dựa vào description để quyết định delegate); tools / disallowedTools; model (sonnet/opus/haiku/fable/full ID/inherit); permissionMode; maxTurns; skills (preload full nội dung skill); mcpServers (scope MCP riêng cho subagent); hooks (PreToolUse/PostToolUse/Stop scoped theo subagent); memory (user/project/local - thư mục persistent memory xuyên session); background; effort; isolation: worktree (chạy trong git worktree tạm, tự dọn nếu không có thay đổi); color; initialPrompt.

Thứ tự resolve model của subagent: env CLAUDE_CODE_SUBAGENT_MODEL → param model per-invocation → frontmatter model → model của conversation chính.

Cách gọi

Foreground vs Background

Resume & transcript

Fork (biến thể đặc biệt)

Fork = subagent kế thừa toàn bộ hội thoại thay vì bắt đầu mới (cùng system prompt, tools, model, history). Tool calls của fork vẫn không vào context chính - chỉ kết quả cuối quay về. Rẻ hơn spawn subagent mới vì reuse prompt cache của parent. Gọi bằng /fork <directive> (mặc định bật từ v2.1.161). Fork không thể fork tiếp.

Tắt subagent

permissions.deny: ["Agent(Explore)", ...] để chặn từng loại; deny tool Agent để chặn toàn bộ delegation.


5. Chi tiết về Dynamic Workflows

[source: Dynamic workflows docs + Workflow tool spec trong system prompt]

Bản chất & yêu cầu

Vòng đời một run

  1. Claude viết script (bắt đầu bằng export const meta = {name, description, phases} - pure literal).
  2. User phê duyệt (tùy permission mode - xem mục 2).
  3. Runtime chạy nền; script được ghi ra file dưới ~/.claude/projects/<session>/ - đọc được, diff được, sửa được rồi relaunch bằng {scriptPath}.
  4. Theo dõi qua /workflows hoặc task panel dưới ô input.
  5. Kết quả cuối đổ về session một lần duy nhất.

Giới hạn runtime

Giới hạnLý do
Không nhận input user giữa run (chỉ permission prompt mới pause được)Cần sign-off giữa các stage → chạy mỗi stage thành workflow riêng
Script không có filesystem/shell/Node API - agents mới là người đọc/ghi/chạy lệnhScript chỉ điều phối
Tối đa 16 agent đồng thời (= min(16, cores − 2)), ít hơn trên máy yếuGiới hạn tài nguyên local
Tối đa 1.000 agent / runChặn runaway loop
Tối đa 4.096 items / 1 lệnh pipeline()/parallel()Lỗi tường minh, không silent truncate
Date.now(), Math.random(), new Date() không args → throwBảo toàn khả năng resume

Resume

Stop/pause rồi resume: agent đã hoàn thành trả kết quả cache, phần còn lại chạy live. Cùng script + cùng args → 100% cache hit. Resume chỉ trong cùng session - thoát Claude Code khi workflow đang chạy thì session sau chạy lại từ đầu. Relaunch bằng Workflow({scriptPath, resumeFromRunId}).

Save & tái sử dụng

Chi phí & model

Tắt workflow

Toggle trong /config; hoặc "disableWorkflows": true trong ~/.claude/settings.json; hoặc env CLAUDE_CODE_DISABLE_WORKFLOWS=1; org-wide qua managed settings. Khi tắt: bundled workflow biến mất, từ khóa ultracode vô hiệu, ultracode rời menu /effort.


6. Script API của Workflow

[source: Workflow tool spec trong system prompt Claude Code - chi tiết API này chưa thấy liệt kê đầy đủ trên trang docs public, mức độ đầy đủ của docs public là [unverified]]

Script là plain JavaScript (không TypeScript), body chạy trong async context. Các hàm có sẵn:

Subagent trong workflow được dặn rằng text cuối của nó là return value (raw data), không phải tin nhắn cho người đọc. Mọi MCP tool của session đều với tới được qua ToolSearch.


7. Quality patterns trong workflow

[source: Workflow tool spec; docs public xác nhận ý tưởng “independent agents adversarially review each other’s findings” và “draft a plan from several angles”]

Vì plan nằm trong code, các pattern này chạy deterministic và lặp lại được - điều fan-out subagent thuần (Claude tự quyết từng lượt) không đảm bảo:

Session 2 của thí nghiệm chính là pattern fan-out + synthesis: 14 agent search giá song song nhiều nguồn, kết quả tổng hợp một lần.


8. Khi nào dùng cái nào

[source: tổng hợp từ cả hai trang docs]


9. Nguồn


Chỉnh sửa trang
Chia sẻ bài viết:

Bài trước
Ghì cương AI agent bằng kiến trúc, không bằng lời dặn
Bài sau
Cross-Cutting Concern trong Lập Trình: tại sao logging, security, transaction lại làm rối code và cách tách chúng ra

Đọc tiếp

Vài bài viết khác cùng mạch.

Muốn đọc lúc rảnh hơn? Đăng ký qua RSS