สวัสดีทุกคนค่าาา

EP นี้พูดถึงสิ่งที่ทำให้ Claude Code ไม่ใช่แค่ "AI ฉายเดี่ยว" อีกต่อไป นั่นก็คือ Subagents นั่นเอง


Subagents คืออะไร?

Subagent คือ Claude instance ใหม่ที่ถูกสร้างขึ้นมาทำงานเฉพาะ task หนึ่งๆ ใน isolation (แยกออกมาจาก context หลัก) อารมณ์มีลูกน้องแต่ละแผนกนั่นแหละ

Claude หลัก (orchestrator):
"ฉันต้องการ: 1) review code 2) run tests 3) check security"
        ↓
สร้าง Subagent 1 → review code     [isolated context]
สร้าง Subagent 2 → run tests       [isolated context]
สร้าง Subagent 3 → check security  [isolated context]
        ↓
รับผลลัพธ์กลับ → สรุป

แต่ละ subagent ไม่รู้เรื่องของกันและกัน ทำงานเป็นอิสระ เป็นลูกน้องที่ไม่ชอบสาระ(แน) 5555


ทำไมต้อง Isolation หรือ ฉายเดี่ยว?

ถ้าให้ Claude ตัวเดียวทำทุกอย่าง มันต้องแบก context ของทุกอย่างไว้ด้วยกัน (ใครยังงงว่า context คืออะไร มันอารมณ์แบบความทรงจำ memory ของ AI อ่ะ)

Claude ตัวเดียว:
[context หลัก + review code + test results + security scan + ...]
→ context window เต็ม → ประสิทธิภาพลดลง → แพงสลัด

แต่ Subagents ทำแบบนี้

Subagent 1: [แค่ context ของ code review]
Subagent 2: [แค่ context ของ tests]
Subagent 3: [แค่ context ของ security]
→ แต่ละอันทำงานด้วย full context ของตัวเอง
→ ประสิทธิภาพสูง และ parallel ได้

Critic-Fixer Pattern

หนึ่งใน patterns ที่ใช้ Subagents ได้ดีมากคือ Critic-Fixer

Subagent 1 (Critic) — read-only, ห้ามแก้ไขไฟล์:
"วิจารณ์ code นี้ หา bugs, anti-patterns, security issues"
        ↓
ส่ง feedback ไปให้
Subagent 2 (Fixer) — แก้ได้:
รับ feedback จาก Critic → แก้ไข → ส่งกลับ
        ↓
Critic ตรวจซ้ำ
        ↓
วนซ้ำจนผ่าน

ทำไม Critic ต้อง read-only? เพราะถ้า Critic แก้ได้ด้วย มันมีแนวโน้มจะ "ยืดหยุ่น" กับงานของตัวเองมากเกินไป แยก role ออกจากกันทำให้ได้ผลดีกว่า


วิธีใช้ Subagents ใน Claude Code

# ส่ง subagent ไปทำงาน
claude agents "review code ใน /src/auth/ แล้วรายงานปัญหาที่เจอ"

# spawn หลาย subagents พร้อมกัน
claude agents "รัน unit tests ทั้งหมดแล้วสรุป" &
claude agents "scan security vulnerabilities" &
wait

# ใช้ ! เพื่อ spawn background shell (รันไฟล์ ให้ทำงานเป็นเบื้องหลัง)
claude agents ! node scripts/generate-types.js

เมื่อไหรควรใช้ Subagents?

✅ ใช้เมื่อ:
- งาน research ที่ต้องการ isolation (ไม่อยากให้ผลมาผลาญcontext)
- งาน review ที่ต้องการความเห็นที่ไม่ bias
- งาน parallel ที่ทำพร้อมกันได้ เช่น run tests + build + lint ไปพร้อมกัน

❌ ไม่ต้องใช้เมื่อ:
- งานที่ต้องการ shared context ระหว่าง tasks
- งานเล็กๆ ง่ายๆ (overhead ไม่คุ้ม)

สรุป

Subagents เปลี่ยน Claude Code จาก "solo developer" เป็น "team of specialists" แต่ละคนมีหน้าที่ชัดเจน และทำงานได้อย่างมีประสิทธิภาพ


EP ถัดไป: Plugins — แพ็กทุกอย่างที่ทำใน series นี้เป็น bundle เดียว แชร์ให้ทีมได้เลย

ขอบคุณที่อ่านแล้วก็ปวดหัวด้วยกันจนจบ ขอให้มีวันที่ดีค่า!


Tags: Claude Code, AI, Subagents, Multi-Agent, Parallel Processing, Developer Tools