
TL;DR
Claude Code subagents คือ AI ผู้ช่วยที่มี context window แยก จาก main session ของเรา ใช้สำหรับงานเฉพาะทาง เช่น code review, debug, security check ทีม dev ไทยที่ใช้ Claude Code อยู่แล้ว ถ้ายังเปิด chat เดียวสั่งงานทุกอย่าง บอกเลยว่ายังไม่เต็มศักยภาพครับ — เพราะ subagents จะช่วยเก็บ context หลักไว้สะอาด ลด token waste และทำงานพร้อมกันได้แบบ parallel ในบทความนี้ผมสรุปวิธีใช้งานจริงทั้ง built-in และ custom subagents พร้อม 4 templates พร้อมใช้สำหรับทีม dev ไทย
ใช้ Claude Code มาเป็นปี ก็เพิ่งเข้าใจ subagents จริงๆ ตอนนี้
เปิดเรื่องด้วยภาพนี้ก่อนครับ — สมัยทำงานเป็น data scientist ที่ Lazada ผมเคยมีน้องในทีมที่เก่งมากเรื่อง SQL อีกคนเก่งเรื่อง dashboard อีกคนถนัด data pipeline ตอนทำโปรเจกต์ใหญ่ ผมไม่ได้ไปสั่งคนเดียวให้ทำทุกอย่างหรอกครับ — ผมแบ่งงาน ส่งให้แต่ละคนทำในส่วนของตัวเอง แล้วเอาผลลัพธ์มา assemble ตอนสุดท้าย
Subagents ใน Claude Code มันเป็นแบบนั้นเลย ไม่ใช่ AI ตัวเดียวที่พยายามทำทุกอย่าง แต่คือทีมเล็กๆ ที่แต่ละคนมีหน้าที่ของตัวเอง มี context แยก มี tool restrict แยก แล้วส่งสรุปกลับมาให้ main agent ตัดสินใจต่อ
ที่น่าสนใจคือ feature นี้มีมาตั้งแต่ Claude Code v1 แล้ว แต่คนส่วนใหญ่ในไทยยังใช้แบบ “เปิด chat แล้วสั่งงานยาวๆ” ซึ่งพอ context ใกล้ token limit ก็ต้อง compact ใหม่ และเสียบริบทไปเรื่อยๆ — ทั้งที่ subagents แก้ปัญหานี้ได้ตรงๆ (Anthropic Docs)
วันนี้ผมจะพาไปดู 6 เรื่องที่ทีม dev ไทยควรเข้าใจ ก่อนจะเริ่มใช้ subagents ในงานจริง
1. Subagents คืออะไรกันแน่ และต่างจาก slash command ยังไง
นิยามแบบสั้น: subagent คือ AI persona ที่ pre-configured ไว้ ให้ Claude Code มอบหมายงานเฉพาะทางได้ แต่ละตัวมี
- Context window แยก จาก main conversation
- System prompt เฉพาะตัว ที่ define หน้าที่
- Tool list ที่จำกัดได้ (อยากให้ใช้แค่ Read/Grep ก็ได้)
- Model เลือกได้ ใช้ Haiku ประหยัด token หรือ Sonnet สำหรับงานยาก
หลายคนสับสนกับ slash command (/command) เลยขอ clear ตรงนี้ครับ
| เปรียบเทียบ | Slash Command | Subagent |
|---|---|---|
| วิธีเรียก | พิมพ์ /command ตรงๆ |
Claude มอบหมายอัตโนมัติ หรือเรียกชื่อ |
| Context | share กับ main session | แยก ออกมาเลย |
| Use case | workflow ซ้ำๆ | แบ่งงาน, exploration, verification |
| Tool restriction | ไม่จำกัดเป็นพิเศษ | จำกัดได้ละเอียด |
SmartScope สรุปไว้ดีครับ ว่า subagents คือ “การ push exploration ออกไปคนละ window” เพื่อกัน context contamination — สะสม approach ที่ fail แล้วฟุ้งกลับมาที่ main session
💡 ในความเห็นของผม ถ้ายังไม่ชัวร์จะใช้แบบไหน ใช้ subagent เมื่อ output verbose มากๆ (test logs, codebase scan) — เพราะ summary จะกลับมาแค่เนื้อๆ ไม่กิน token หลัก
2. Built-in subagents 3 ตัวที่มากับ Claude Code
ยังไม่ต้องสร้างเองก็ได้ครับ Claude Code มาพร้อม subagents 3 ตัวที่ใช้บ่อยที่สุด
Explore — read-only search agent
– Tools: Read, Glob, Grep, Bash (read commands)
– หน้าที่: ค้นไฟล์, search symbol, อ่าน excerpt
– Claude เรียก auto เมื่อเข้า plan mode หรือถามเรื่องโครงสร้าง codebase
– ระบุ thoroughness ได้: quick, medium, very thorough
Plan — research + architecture
– Tools: Read, Glob, Grep, Bash
– หน้าที่: รวบรวมข้อมูลเพื่อทำแผน implementation
– Auto-invoke ตอน plan mode ทำงาน
General-purpose — workhorse
– Tools: ทุก tool ของ main thread
– หน้าที่: งาน multi-step ที่ต้องทั้งอ่านและแก้ไข
– เรียกผ่าน Task tool หรือเมื่อ Claude เห็นว่างานต้องการ specialization
ลองพิมพ์ /agents ใน Claude Code ดูครับ จะเห็น list ทั้งหมดที่ใช้ได้ในเครื่องเรา รวม built-in, user-level (~/.claude/agents/), project-level (.claude/agents/), และ plugin-level
3. โครงสร้างไฟล์ของ custom subagent
สร้าง subagent ของตัวเองง่ายมากครับ เป็นแค่ Markdown ไฟล์ 1 ไฟล์ใน .claude/agents/ หรือ ~/.claude/agents/ (สำหรับใช้ทุก project)
---
name: code-reviewer
description: Reviews code for quality and best practices. Use proactively after code changes.
tools: Read, Glob, Grep
model: sonnet
permissionMode: default
skills:
- api-conventions
- error-handling-patterns
---
You are a senior code reviewer. Focus on code quality,
security, and best practices. Provide specific, actionable feedback.
Field สำคัญที่ต้องเข้าใจ
name— identifier เรียกใช้ ห้ามเว้นวรรคdescription— Claude อ่านบรรทัดนี้ตัดสินใจเรียก auto ดังนั้นเขียนให้ชัด ใส่ “use proactively” เพื่อให้เรียกบ่อยขึ้นtools— comma-separated; ถ้าตัด field นี้ออก subagent จะ inherit ทุก tool ของ main thread รวม MCP tools ด้วยmodel—haiku/sonnet/opus/inherit(Haiku ประหยัดมากสำหรับงาน routine)skills— preload skill packs เข้า context ตอนเริ่ม
ที่อยากเตือนคือเรื่อง tool scoping ครับ PubNub สรุปไว้ว่า “an agent with narrower permissions is an agent that is less likely to take irreversible actions” — code reviewer ก็ควรอ่านอย่างเดียว ไม่ต้องมี Write หรือ Bash
4. Templates พร้อมใช้: 4 subagents สำหรับทีม dev ไทย
ผมสรุป 4 patterns ที่ทดลองใช้กับทีมแล้วเวิร์กจริง — copy ไปวางใน .claude/agents/ ของ project ได้เลยครับ
4.1 code-reviewer — รีวิว PR ก่อน merge
---
name: code-reviewer
description: Reviews code changes for quality, security, performance. Use proactively after edits.
tools: Read, Glob, Grep
model: sonnet
---
You are a senior code reviewer. Check for:
- Logic errors, edge cases not handled
- Security: injection, auth bypass, data leaks
- Performance: N+1 queries, unnecessary loops
- Naming, comments, test coverage
Output format:
- **Severity**: critical / major / minor
- **Location**: file:line
- **Issue**: clear, actionable
- **Suggested fix**: code snippet
4.2 debugger — แก้ test ที่ fail
---
name: debugger
description: Debugs failing tests and runtime errors. Use when tests fail or errors appear.
tools: Read, Glob, Grep, Bash
model: sonnet
---
You are an expert debugger. Process:
1. Reproduce the error from the stack trace
2. Form a hypothesis (do not guess — read the code)
3. Locate the root cause, not symptoms
4. Propose minimal fix
Always show:
- Root cause analysis
- Diff of the fix
- How to verify (test command)
4.3 security-scanner — สแกนช่องโหว่
---
name: security-scanner
description: Scans code for security vulnerabilities. Use proactively before deploys.
tools: Read, Glob, Grep
model: sonnet
permissionMode: default
---
Scan for OWASP Top 10:
- Injection (SQL, NoSQL, command, LDAP)
- Auth flaws (weak crypto, hardcoded secrets)
- Sensitive data exposure (logs, errors)
- XXE, broken access control
- Misconfigurations (.env exposure, CORS)
For each finding:
- CVSS severity estimate
- Affected file and line
- Exploit scenario (1 sentence)
- Concrete remediation
4.4 doc-writer — อัปเดต README/docs
---
name: doc-writer
description: Writes and updates technical documentation. Use after API or feature changes.
tools: Read, Glob, Grep, Write
model: haiku
---
You write clear, concise docs for Thai dev teams.
- Use Thaiglish where natural
- Show working code examples (not pseudocode)
- Include cURL or sample request for any API
- Add "Common pitfalls" section if API has gotchas
- Match existing project README style
💡 ในความเห็นของผม ตัว doc-writer ใช้ Haiku ก็พอ งานเขียนเอกสารไม่ต้องการ reasoning หนัก แต่ตัว security-scanner และ debugger ต้องใช้ Sonnet ขึ้นไป เพราะต้อง trace logic ลึก
5. เมื่อไหร่ควร/ไม่ควรใช้ subagent
ไม่ใช่ทุกงานเหมาะจะ delegate ครับ Prompt Shelf สรุปไว้ดีว่า subagents มี orchestration overhead ดังนั้นต้องชั่งน้ำหนัก
ใช้ subagent เมื่อ:
– งาน output verbose (test logs, codebase scan, log analysis)
– ต้องการ tool restriction เคร่ง (read-only review)
– งาน parallel ที่อิสระต่อกัน (สแกน security + style พร้อมกัน)
– ต้องการ isolate bias จาก main conversation
ไม่ใช้ subagent เมื่อ:
– งานเล็ก แก้ไม่กี่บรรทัด
– งานต้อง share context หนัก (planning → impl → testing ที่ context ใช้ร่วมกัน)
– งานต้องการ latency ต่ำ (subagent เริ่มต้นใหม่ ต้องใช้เวลา gather context)
Single-responsibility rule: subagent ตัวเดียวควรทำงานเดียว — pm-spec เขียน spec, architect-review validate design, implementer-tester build + test, qa-verify ตรวจ output (PubNub recommendation)
6. ข้อควรระวังและ best practices ที่ทีมไทยมักพลาด
จากที่ลองใช้กับทีมจริงและอ่าน production case studies มา ผมเห็น pattern ที่พลาดบ่อยอยู่ 5 ข้อ
1. Subagent spawn ซ้อนกันไม่ได้ — built-in rule คือ subagent เรียก subagent อื่นไม่ได้ กัน infinite loop ดังนั้นถ้า workflow ต้องการ chain ยาวๆ ต้อง orchestrate จาก main agent
2. Description ที่ vague ทำให้ Claude ไม่เรียก — เขียน description ให้ระบุ trigger ชัด เช่น “Use proactively after code changes” ดีกว่า “Reviews code”
3. ไม่ scope tool ให้ subagent — ถ้าตัด tools field ออก subagent จะมีทุก tool รวม Bash และ Write ซึ่งทำให้ blast radius กว้างเกินจำเป็น
4. Pass context มากเกินไป — subagent มี context window ของตัวเองก็จริง แต่ทุก token ที่เรา pass เข้าไป คือ token ที่มันใช้ทำงานจริงไม่ได้ ส่ง summary ที่จำเป็นพอ
5. Background subagent ถามคำถามไม่ได้ — subagent ที่ run แบบ background ถ้าเจอ permission ที่ไม่ approved ไว้ล่วงหน้า มันจะ auto-deny ไม่หยุดถาม ดังนั้นต้อง pre-approve permission ตอน launch
อีกเรื่องที่ผมเจอกับตัวเองคือ measurement ครับ ลองเอา 3 ตัวเลขนี้ไป track อย่างน้อย 1 สัปดาห์
- Context budget consumed (token จาก subagent output ที่กลับมา main session)
- First-attempt success rate (subagent ทำเสร็จในครั้งเดียวกี่เปอร์เซ็นต์)
- Token efficiency ratio (token รวมของ subagent vs token ที่ main session ใช้ทำเองได้)
ถ้า ratio ไม่ดีกว่า 1:1 แปลว่า spec ยังไม่ดีพอ ไม่ใช่ว่า subagent ไม่เวิร์ก
FAQ คำถามที่เจอบ่อยจากทีม dev ไทย
Subagent กับ Skill ใน Claude Code ต่างกันยังไง?
Skill คือ knowledge pack (markdown + scripts) ที่โหลดเข้า context ตอนต้องใช้ — ไม่ได้รัน agent แยก ส่วน subagent คือ agent instance ที่มี context แยก Skill ใส่ใน subagent ได้ผ่าน field skills: ใน frontmatter เพื่อ preload knowledge ตอน subagent เริ่มทำงาน
Custom subagent เก็บไว้ที่ไหนระหว่าง project กับ user level?
.claude/agents/ ใน project root → ใช้กับ project นั้นเท่านั้น เหมาะกับ subagent ที่เกี่ยว business logic ของโปรเจกต์ ส่วน ~/.claude/agents/ → ใช้ได้ทุก project บนเครื่องเรา เหมาะกับ subagent ทั่วไป เช่น code-reviewer, doc-writer ที่อยากเอาติดตัวไปทุกงาน
ใช้ Haiku กับ Sonnet ใน subagent ต่างกันมากไหม?
ผมทดลองมาเยอะครับ — สำหรับ doc-writer หรือ formatter Haiku ทำได้ดีและประหยัด token เกินครึ่ง แต่สำหรับ debugger, security-scanner, architect-review ที่ต้อง trace logic ลึก Sonnet ขึ้นไปยังจำเป็น เพราะ Haiku จะ miss edge case บ่อยเวลา reasoning ต่อกันหลายชั้น
Subagent ต้องเสียเงินเพิ่มไหมถ้าใช้ Claude Code?
ไม่มี subscription แยกครับ subagent ใช้โควต้า token เดียวกับ main Claude Code subscription ของเรา แต่จำไว้ว่ามันกิน token เพิ่มจริงๆ เพราะมี separate context — ดังนั้นการ scope tool และ pass context ให้พอดีจึงสำคัญในการคุม cost
ถ้าทีมมีคน 5 คน ใช้ subagent ร่วมกันยังไง?
วิธีง่ายสุดคือ commit ไฟล์ใน .claude/agents/ เข้า git — ทุกคนใน project จะได้ subagent set เดียวกัน อีกทางคือทำเป็น Claude Code plugin แล้ว distribute ผ่าน plugin marketplace ภายในองค์กร — (Anthropic เรียกแบบนี้ว่า plugin agents)
#สรุป
Subagents ไม่ใช่ feature สวยงามไว้โชว์ครับ แต่คือเครื่องมือ เก็บ context ให้สะอาด ลด token waste และเพิ่ม throughput ของ Claude Code ในงานจริง ทีม dev ไทยที่ใช้ Claude Code อยู่แล้ว เริ่มจาก 3 ขั้นง่ายๆ คือ
- ลอง built-in 3 ตัวก่อน — Explore, Plan, general-purpose
- เพิ่ม custom 1 ตัวที่ทีมใช้บ่อย — code-reviewer หรือ debugger ก็ได้
- Track ตัวเลข 1 สัปดาห์ — context, success rate, token ratio
ถ้าอยากต่อยอด อ่าน Claude Code Hooks 25 Events ที่ผมเขียนไว้ก่อนหน้า เพราะ hooks + subagents ใช้ร่วมกัน จะกลายเป็น automation ขนาดทีมเล็กที่ทำงานแทนเราได้จริง รวมถึง Claude Code คู่มือฉบับสมบูรณ์ 2026 ที่เป็น pillar รวมทุกหัวข้อของ Claude Code และ คนส่วนใหญ่ยังใช้ Claude Code ไม่เต็มศักยภาพ ที่อธิบาย mental model ของ tool นี้
การมี subagent ที่ scope ดี ก็เหมือนการจ้างทีมงานที่รู้ว่าตัวเองทำอะไรนั่นแหละครับ — ของแบบนี้ไม่ใช่ luxury แต่คือ baseline ของทีม dev ที่ทำงานกับ AI ในปี 2026
Sources:
– Anthropic — Create custom subagents (Claude Code Docs)
– SmartScope — Claude Code Advanced Best Practices 2026
– PubNub — Best practices for Claude Code subagents
– The Prompt Shelf — Claude Code Subagents Best Practices 2026
รับคู่มือ Claude AI + บทความใหม่ก่อนใคร
สมัครรับจดหมายจากอาร์ตี้ — ไม่สแปม ไม่เกิน 1–2 ฉบับ/สัปดาห์
