เรียน claude
🎓
บทที่ 3 · ⏱ 10 นาที

Subagents — แยกงานให้ผู้เชี่ยวชาญเฉพาะทาง

แทนที่ผมจะสั่ง Claude ตัวเดียวทำทุกอย่าง ผมตั้ง subagent ให้แต่ละด้าน — เร็วและลึกกว่ามาก

ตอนใช้ Claude Code ใหม่ๆ ผมพิมพ์ทุกอย่างใส่ Claude หลัก — review โค้ด ตอบลูกค้า วิเคราะห์ตัวเลข — มันทำได้ทุกอย่าง แต่ลึกไม่พอ

หลังจากเริ่มสร้าง subagent ผลต่างกัน — แต่ละ agent มี persona และ scope ของตัวเอง ทำงานลึกกว่าและเร็วกว่ามาก

Subagent คืออะไร

Subagent = Claude อีกตัวที่มี personality และ tools เฉพาะ Claude หลักของเรา delegate งานให้ subagent ที่เชี่ยวชาญด้านนั้น แล้วเอาผลลัพธ์กลับมา

เปรียบเทียบกับชีวิตจริง — แทนที่ผมต้องอ่าน code review เอง ผมจ้างเพื่อนคนนึงที่เก่งเรื่อง security ให้อ่านแทน เขาส่งสรุปให้ผม ผมไม่ต้องอ่านโค้ดทั้งหมด

ทำไมแยก

  • Focus — แต่ละ agent มี prompt เฉพาะ ไม่เบลอ
  • Context แยก — main agent ไม่เห็น chat ลึกของ subagent กิน context น้อย
  • เลือก model ต่างกัน — security ใช้ opus, สรุป log ใช้ haiku
  • Reuse — สร้าง 1 ครั้ง ใช้ทุกโปรเจค

สร้าง Subagent ของตัวเอง

ไฟล์ .claude/agents/[name].md (หรือ ~/.claude/agents/ สำหรับ global)

---
name: thai-copywriter
description: copywriter ภาษาไทยเฉพาะ SMB
model: sonnet
tools: ["Read", "Write", "Edit"]
---

# Thai Copywriter Agent

คุณเป็น copywriter มืออาชีพภาษาไทย เชี่ยวชาญ
- Social media caption (IG, FB, TikTok)
- Email marketing
- Landing page copy
- Product description

## หลักการเขียน
- ภาษาไทยธรรมชาติ ไม่แข็ง
- เข้าใจวัฒนธรรมไทย — ความสุภาพ ความตลก
- SEO-aware ถ้าเป็น web copy
- Target audience เป็นหลัก

## รูปแบบ output
- ให้หลายเวอร์ชันเสมอ อย่างน้อย 3
- อธิบายว่าแต่ละแบบเหมาะกับสถานการณ์ไหน
- emoji ใช้พอเหมาะ ไม่ over

## ข้อห้าม
- อย่าใช้คำทับศัพท์ภาษาอังกฤษถ้ามีคำไทยที่ดี
- อย่าใช้คำว่า "ตัวคุณเอง" / "ตัวท่าน" (แข็ง)
- อย่าสัญญาเกินจริง

เรียกใช้

วิธี 1 — สั่งใน chat หลัก

"ให้ thai-copywriter เขียน caption ขายกาแฟ 5 แบบ"

Claude หลัก spawn subagent → รับผล → ส่งให้คุณ

วิธี 2 — ใช้ /agents ดู list ทั้งหมดและเลือก

Subagent ที่ผมตั้งไว้สำหรับบริษัท

customer-service

---
name: customer-service
description: ตอบลูกค้าใน LINE OA ตามสไตล์บริษัทผม
model: sonnet
tools: ["Read"]
---

คุณเป็น customer service ของบริษัท [ชื่อ]
โทน เป็นกันเองแบบมืออาชีพ ใช้ครับ/ค่ะ
จำ FAQ ของบริษัทและราคามาตรฐานได้

อ่าน data/faq.md และ data/price-list.md ก่อนตอบทุกครั้ง

analytics

---
name: analytics
description: วิเคราะห์ตัวเลขจากตาราง CSV
model: sonnet
tools: ["Read", "Bash(python:*)"]
---

คุณเป็น data analyst
วิเคราะห์ข้อมูลจาก CSV / JSON
ใช้ Python + pandas เพื่อ
- คำนวณ metrics สำคัญ
- สร้างกราฟ matplotlib
- หา pattern ที่ไม่ obvious

Output — markdown report + กราฟ save เป็น PNG

quality-checker

---
name: quality-checker
description: ตรวจงานก่อน release
model: opus
tools: ["Read", "Bash(npm:*)", "Bash(git diff:*)"]
---

คุณเป็น quality gatekeeper

ก่อน approve งานต้องตรวจ
1. typecheck ผ่าน (npm run typecheck)
2. lint ผ่าน (npm run lint)
3. tests ผ่าน + coverage > 80%
4. ไม่มี console.log
5. ไม่มี TODO / FIXME ใหม่
6. commit message ตาม convention

รายงาน
- ผ่าน — ที่ตรวจแล้วโอเค
- คำเตือน — ที่น่ากังวลแต่ไม่ block
- BLOCK — ที่ต้องแก้ก่อน release

Parallel — พลังที่แท้จริง

สั่งให้ subagent หลายตัวทำงานพร้อมกัน

"เรียก 3 subagent พร้อมกัน
1. security-reviewer ตรวจ security
2. performance-reviewer ตรวจ performance
3. accessibility-reviewer ตรวจ a11y

สรุปผลรวมเป็นรายงานเดียว"

Claude หลัก spawn 3 agent พร้อมกัน รับผลทั้งหมด รวมเป็น final report เร็วกว่าทำเอง 3 เท่า ครอบคลุมกว่า

ตัวอย่าง workflow ในบริษัท — Launch สินค้าใหม่

"ใช้ 4 agent parallel
1. thai-copywriter — เขียน description
2. analytics — คาดการณ์ยอดขาย
3. customer-service — เตรียม FAQ
4. photo-prompter — เขียน prompt สำหรับ generate รูป

รวมเป็น product launch kit"

คำสั่งเดียวได้ทุกอย่างที่ต้องใช้ปล่อยสินค้าใหม่ในบริษัทผม

โครงสร้างที่ผมใช้

.claude/
  agents/
    core/
      customer-service.md
      thai-copywriter.md
    analytics/
      sales-analyzer.md
      customer-segmenter.md
    quality/
      code-reviewer.md
      security-reviewer.md
    business/
      pricing-strategist.md
      market-researcher.md

แยกหมวดเพื่อเรียกใช้ง่าย ทีมเข้าใจตรงกัน

ของที่ควรรู้

  • Context cost — แต่ละ subagent = conversation แยก กิน context แยก ใช้ haiku สำหรับ agent ง่ายๆ ประหยัด
  • Agent ไม่เห็นกัน — A ทำอะไร B ไม่รู้ Main Claude คือคน sync ผล
  • Version control — commit .claude/agents/ ทีมใช้ agent เดียวกัน

ลองทำดู: ภารกิจ: agent library ของคุณ

บทนี้มีประโยชน์กับคุณมั้ยครับ?

ผมอ่าน feedback เองทุกอันแล้วเอาไปปรับเนื้อหา