Agent Forking với tmux: Kỹ thuật Spawn Sub-agents Song song không cần SDK phức tạp
Điểm nổi bật
Nhấn để đến mục tương ứng
- 1 Hạn chế thực tế ít review nào nhắc đến: Optional feature: khi forked window's transcript grows beyond threshold, script automatically summarize trước khi inject vào. Trade-off: accuracy vs brevity — set threshold conservatively. Biết trước những điểm này giúp bạn lập plan B và phân bổ resource hợp lý ngay từ đầu.
- 2 Ứng dụng sáng tạo vượt xa mục đích ban đầu: Bước 1: Coding session bình thường trong tmux Start Claude Code (hoặc Codex, Gemini CLI) trong tmux session như bình thường. Terminal familiarity là prerequisite — không có GUI. Những use case này minh chứng rằng tiềm năng thực sự chỉ được mở ra khi người dùng dám thử nghiệm.
- 3 Không đơn thuần là khái niệm mới — Kaushik Gopal giải quyết vấn đề này với agent forking : spawn một tmux window mới với exact same context từ session hiện tại,. Sự khác biệt nằm ở cách nó tích hợp vào quy trình làm việc hàng ngày, tạo giá trị tích lũy theo thời gian.
- 4 Đặt cạnh nhau mới thấy rõ sự khác biệt: Cả hai kỹ thuật dùng tmux nhưng cho purposes khác nhau: Chiều Agent Forking (Kaushik) Agent Teams (Anthropic) Purpose. Chọn đúng công cụ phụ thuộc vào workflow hiện tại của bạn, không phải bảng xếp hạng chung trên mạng.
- 5 Quan sát then chốt: Trước khi build, Kaushik define rõ constraints — đây là phần có giá trị nhất của bài viết: 1. Minimal Overhead Chỉ cần Bash script + tmux. Hiểu chi tiết này cho phép khai thác tối đa tiềm năng của công cụ thay vì chỉ chạm bề mặt tính năng.
Forking là gì và tại sao nó quan trọng?
Khi coding với Claude Code, bạn thường gặp tình huống: đang implement feature A, phát hiện ra thread B thú vị cần explore (test một approach khác, research một library, viết documentation) — nhưng explore ngay thì mất context của A, ignore thì lose insights.
Kaushik Gopal giải quyết vấn đề này với agent forking: spawn một tmux window mới với exact same context từ session hiện tại, pursue tangential thought trong window đó, và manually copy-paste kết quả hữu ích về session chính. Đơn giản, pragmatic, và không cần SDK phức tạp.
6 Requirements Thiết kế của Kaushik
Trước khi build, Kaushik define rõ constraints — đây là phần có giá trị nhất của bài viết:
1. Minimal Overhead
Chỉ cần Bash script + tmux. Không có thick abstraction layers. Rationale quan trọng:
"I'm deliberately not trying to build on top of existing agents. I use claude code, codex cli & gemini daily, and they change fast enough that anything with a thick layer will lag behind on features. So: a Bash script and tmux. That's it." — Kaushik Gopal
Insight này resonates với nhiều developer: SDK wrappers on top of SDKs on top of SDKs = technical debt khi tools update. Thin scripting layer = maintainable.
2. Tool Agnosticism
Session có thể start trong Claude Code, fork sang Codex, rồi fork lại sang Gemini. Context passes qua tools. Không bị lock vào một ecosystem.
Use case: "Bắt đầu plan trong Codex, fork sang Claude Code để code, fork Gemini để tạo diagram."
3. Interactive Sessions
Subagent forks phải là interactive — không phải one-shot operations. Bạn có thể follow up, ask clarifying questions, go deeper. tmux's mouse selection = clipboard integration để copy results.
4. Context Management không bloat session chính
Subagent context được separate từ main session — tránh context bloat. Khi forked window's transcript grow quá dài, optional automatic summarization.
Principle: main session giữ focus. Forks là exploration spaces. Chỉ merge insights, không merge raw context.
5. No Persona Framework
Thay vì setup "agent personas" phức tạp (Reviewer Agent, Implementor Agent...), Kaushik crafts specific instructions directly vào forked prompts. Simpler, more flexible, easier to maintain.
6. Clear Labeling
tmux windows được label rõ ràng để track what each subagent handles. "Sub: research redis alternatives" vs "Sub: draft auth flow." Không mất thời gian remember what's in which window.
How It Works: Workflow thực tế
Bước 1: Coding session bình thường trong tmux
Start Claude Code (hoặc Codex, Gemini CLI) trong tmux session như bình thường. Terminal familiarity là prerequisite — không có GUI.
Bước 2: Khi muốn fork
Khi cần explore tangent:
- Script capture current transcript lines từ active pane
- Prompt: mô tả subagent task
- Script spawn tmux window mới với:
<context>
[raw transcript hoặc summarized version]
</context>
<task>
[your specified task for this fork]
</task>
Bước 3: Explore trong fork
Window mới mở với exact context từ session chính. Claude (hoặc Codex/Gemini) nhận context + task. Bạn interact, explore, iterate trong window này.
Bước 4: Manual merge
Kết quả hữu ích? Copy-paste qua tmux mouse selection về main session. Không cần automated result merging — manual coordination thực ra pragmatic hơn cho most cases.
Cross-tool Forking: Tận dụng strengths của mỗi model
Pattern thú vị nhất: fork sang different tools cho different strengths:
| Task Type | Tool tốt nhất | Rationale |
|---|---|---|
| Architecture planning | Claude Code | Long-context reasoning |
| Code review | Codex (GPT) | Catches different bugs |
| Diagram generation | Gemini | Multimodal strength |
| Research queries | Perplexity | Web-grounded answers |
| Quick calculations | Any | Tool agnostic |
Kaushik: "Bắt đầu plan trong Codex, fork sang Claude Code để code, fork Gemini để tạo diagram." Một workflow spanning 3 different AI tools, unified qua tmux.
Context Summarization: Khi transcript quá dài
Optional feature: khi forked window's transcript grows beyond threshold, script automatically summarize trước khi inject vào subagent context. Trade-off: accuracy vs brevity — set threshold conservatively.
Kaushik's recommendation: summarize chỉ khi transcript > X lines (X tuỳ project). Short transcripts: inject raw. Long transcripts: summarize → inject.
Caveats và Limitations quan trọng
Kaushik honest về limitations:
- Script được optimize cho Ghostty + Fish + tmux setup cụ thể — cần customize cho other configurations. GitHub Gist available như starting point.
- Marginal diminishing returns khi fork quá nhiều — 2-3 active forks optimal. Nhiều hơn = harder to track, harder to synthesize insights.
- Summarization trades accuracy — context summary có thể miss nuances. Không reliable cho high-stakes decisions.
- Manual copy-paste là feature, không phải bug — automating result merging sounds appealing nhưng thực ra thêm complexity mà không add much value trong practice.
Khi nào dùng Agent Forking vs Agent Teams?
Cả hai kỹ thuật dùng tmux nhưng cho purposes khác nhau:
| Chiều | Agent Forking (Kaushik) | Agent Teams (Anthropic) |
|---|---|---|
| Purpose | Explore tangents mà không mất context | Parallel work trên large projects |
| Coordination | Manual copy-paste | Automated task list + messaging |
| Cross-tool | Yes (Claude + Codex + Gemini) | Claude only |
| Setup | Bash script + tmux | Claude Code với team feature |
| Best for | Individual exploration | Team-scale parallel work |
Forking phù hợp: solo developer exploring approaches, research tangents, quick experiments. Agent Teams phù hợp: structured parallel work với clear file ownership.
Kỹ thuật Tổ chức tmux cho AI workflow
Tips từ Kaushik về tmux setup cho AI-heavy development:
- Label windows descriptively: Không chỉ "window 1, 2, 3" — dùng names như "main-impl", "sub-redis-research", "sub-auth-draft"
- Use sessions như workspaces: Mỗi project = một tmux session với multiple windows
- Master Ctrl+B navigation: Switch windows nhanh là prerequisite cho forking workflow hiệu quả
- tmux mouse mode: Enable cho easy selection và copy
Philosophy: Thin layers win long-term
Core message của Kaushik vượt ra ngoài technical details: trong môi trường AI tools changing rapidly, thin abstraction layers beat thick frameworks.
Complex SDK frameworks có thể impressive ngay hôm nay, nhưng khi Claude Code release feature mới, framework cần update. Bash script + tmux: update ngay, không cần wait cho framework maintainers.
Đây là contrast rõ ràng với The Adventuring Party approach của Alex Ivison (Go orchestration, custom TUI) — cả hai valid, tùy vào complexity bạn muốn manage. Kaushik's approach = simpler, more accessible. Alex's approach = more powerful, more maintenance.
Tài nguyên để bắt đầu
- Bash script: Available trên GitHub Gist của Kaushik (kau.sh)
- Terminal stack: Ghostty + Fish + tmux (adaptable)
- Node 22+ nếu dùng /last30days skill cùng forking workflow
Tổng kết
Agent forking là kỹ thuật elegant cho một vấn đề thực tế: làm thế nào explore parallel thoughts mà không mất context của main session. Kaushik's implementation — Bash + tmux, cross-tool, manual merge — là proof rằng minimal tooling thường là best tooling khi tools đang evolve nhanh.
Nếu bạn đã quen với tmux và muốn level up AI coding workflow, script này là solid starting point. Tìm trên GitHub Gist của kau.sh và adapt cho setup của bạn.
Nguồn tham khảo
Bai viet co huu ich khong?
Bản quyền thuộc về tác giả. Vui lòng dẫn nguồn khi chia sẻ.




