Skip to main content

สร้าง Subagents แบบกำหนดเอง

สร้างและใช้ AI subagents เฉพาะทางใน Claude Code สำหรับ workflow เฉพาะงานและการจัดการ context ที่ดีขึ้น

Subagents คือผู้ช่วย AI เฉพาะทางที่จัดการงานประเภทเฉพาะ ใช้งานเมื่องานรองจะทำให้การสนทนาหลักเต็มไปด้วยผลการค้นหา, logs, หรือเนื้อหาไฟล์ที่คุณจะไม่อ้างอิงอีก: subagent ทำงานนั้นใน context ของตัวเองและส่งกลับเพียงสรุปผล กำหนด subagent แบบกำหนดเองเมื่อคุณมักจะสร้าง worker ประเภทเดียวกันด้วยคำแนะนำเดิมซ้ำๆ

Subagent แต่ละตัวทำงานใน context window ของตัวเองพร้อมกับ system prompt แบบกำหนดเอง, การเข้าถึง tool เฉพาะ, และสิทธิ์อิสระ เมื่อ Claude พบงานที่ตรงกับคำอธิบายของ subagent จะมอบหมายงานให้ subagent นั้น ซึ่งทำงานโดยอิสระและส่งผลลัพธ์กลับมา

note

Subagents ทำงานภายใน session เดียว หากต้องการรัน session อิสระหลายๆ ตัวพร้อมกันและตรวจสอบจากที่เดียว ดูที่ background agents หากต้องการ session ที่สื่อสารกันได้ ดูที่ agent teams

Subagents ช่วยให้คุณ:

  • รักษา context โดยแยกการสำรวจและการ implement ออกจากการสนทนาหลัก
  • บังคับใช้ข้อจำกัด โดยจำกัด tools ที่ subagent สามารถใช้ได้
  • นำ configurations กลับมาใช้ใหม่ ในทุกโปรเจกต์ด้วย user-level subagents
  • ทำให้พฤติกรรมเฉพาะทาง ด้วย system prompts ที่เน้นเฉพาะโดเมน
  • ควบคุมต้นทุน โดยส่งงานให้ model ที่เร็วกว่าและถูกกว่าอย่าง Haiku

Claude ใช้คำอธิบายของ subagent แต่ละตัวในการตัดสินใจว่าจะมอบหมายงานเมื่อใด เมื่อสร้าง subagent ให้เขียนคำอธิบายที่ชัดเจนเพื่อให้ Claude รู้ว่าควรใช้เมื่อไหร่

Subagents ที่มีในตัว

Claude Code มี subagents ในตัวหลายตัวที่ Claude ใช้โดยอัตโนมัติเมื่อเหมาะสม

Explore

Agent อ่านอย่างเดียวที่รวดเร็ว เหมาะสำหรับการค้นหาและวิเคราะห์ codebase

  • Model: Haiku (รวดเร็ว, latency ต่ำ)
  • Tools: Read-only tools (ปฏิเสธการเข้าถึง Write และ Edit tools)
  • วัตถุประสงค์: การค้นหาไฟล์, การค้นหา code, การสำรวจ codebase

Plan

Agent วิจัยที่ใช้ระหว่าง plan mode เพื่อรวบรวม context ก่อนนำเสนอแผน

  • Model: สืบทอดจากการสนทนาหลัก
  • Tools: Read-only tools
  • วัตถุประสงค์: การวิจัย codebase สำหรับการวางแผน

General-purpose

Agent ที่มีประสิทธิภาพสำหรับงานหลายขั้นตอนที่ซับซ้อนซึ่งต้องการทั้งการสำรวจและการดำเนินการ

  • Model: สืบทอดจากการสนทนาหลัก
  • Tools: Tools ทั้งหมด
  • วัตถุประสงค์: การวิจัยซับซ้อน, การดำเนินการหลายขั้นตอน, การแก้ไข code

เริ่มต้นอย่างรวดเร็ว: สร้าง subagent ตัวแรกของคุณ

Subagents ถูกกำหนดใน Markdown files ที่มี YAML frontmatter คุณสามารถ สร้างด้วยตนเอง หรือใช้คำสั่ง /agents

ขั้นตอนการสร้าง

  1. เปิดอินเทอร์เฟซ subagents รัน /agents ใน Claude Code

  2. เลือกที่เก็บ สลับไปที่แท็บ Library เลือก Create new agent จากนั้นเลือก Personal เพื่อบันทึกไปยัง ~/.claude/agents/

  3. สร้างด้วย Claude เลือก Generate with Claude และอธิบาย subagent:

    A code improvement agent that scans files and suggests improvements
    for readability, performance, and best practices. It should explain
    each issue, show the current code, and provide an improved version.
  4. เลือก tools สำหรับ reviewer แบบ read-only ยกเลิกการเลือกทุกอย่างยกเว้น Read-only tools

  5. เลือก model เลือก Sonnet สำหรับตัวอย่างนี้

  6. เลือกสี เลือกสีพื้นหลังเพื่อระบุ subagent ใน UI

  7. ตั้งค่าหน่วยความจำ เลือก User scope เพื่อให้ subagent มีไดเรกทอรีหน่วยความจำถาวร

  8. บันทึกและลองใช้งาน กด s หรือ Enter เพื่อบันทึก

กำหนดค่า Subagents

เลือกขอบเขต Subagent

ที่เก็บขอบเขตลำดับความสำคัญ
Managed settingsทั้งองค์กร1 (สูงสุด)
--agents CLI flagSession ปัจจุบัน2
.claude/agents/โปรเจกต์ปัจจุบัน3
~/.claude/agents/ทุกโปรเจกต์ของคุณ4
Plugin agents/ directoryที่ติดตั้ง plugin5 (ต่ำสุด)

เขียนไฟล์ Subagent

Subagent files ใช้ YAML frontmatter สำหรับ configuration ตามด้วย system prompt ใน Markdown:

---
name: code-reviewer
description: Reviews code for quality and best practices
tools: Read, Glob, Grep
model: sonnet
---

You are a code reviewer. When invoked, analyze the code and provide
specific, actionable feedback on quality, security, and best practices.

Field Frontmatter ที่รองรับ

Fieldจำเป็นคำอธิบาย
nameใช่ตัวระบุที่ไม่ซ้ำกัน ใช้ตัวอักษรพิมพ์เล็กและขีดกลาง
descriptionใช่บอก Claude ว่าควรมอบหมายให้ subagent นี้เมื่อใด
toolsไม่Tools ที่ subagent สามารถใช้ได้ สืบทอด tools ทั้งหมดหากละเว้น
disallowedToolsไม่Tools ที่ปฏิเสธ ถูกลบออกจากรายการที่สืบทอดหรือระบุ
modelไม่Model ที่ใช้: sonnet, opus, haiku, fable หรือ ID เต็ม
permissionModeไม่Permission mode: default, acceptEdits, auto, bypassPermissions หรือ plan
maxTurnsไม่จำนวน turns สูงสุดก่อน subagent หยุด
skillsไม่Skills ที่โหลดล่วงหน้าในช่วงเริ่มต้น subagent
mcpServersไม่MCP servers ที่พร้อมใช้งานสำหรับ subagent นี้
hooksไม่Lifecycle hooks ที่กำหนดขอบเขตสำหรับ subagent นี้
memoryไม่ขอบเขตหน่วยความจำถาวร: user, project หรือ local
backgroundไม่ตั้งเป็น true เพื่อรัน subagent นี้เป็น background task เสมอ
effortไม่ระดับความพยายาม: low, medium, high, xhigh, max
isolationไม่ตั้งเป็น worktree เพื่อรัน subagent ใน git worktree ชั่วคราว
colorไม่สีแสดงผลสำหรับ subagent: red, blue, green, yellow, purple, orange, pink หรือ cyan
initialPromptไม่Auto-submitted เป็น user turn แรกเมื่อ agent นี้รันเป็น main session agent

ทำงานกับ Subagents

เรียก Subagents อย่างชัดเจน

Claude มักจะเลือก subagent ที่เหมาะสมโดยอัตโนมัติ แต่คุณสามารถระบุตัวที่ต้องการใช้ได้:

Use the code-reviewer agent to check this PR

รัน Subagents ใน Foreground หรือ Background

  • Foreground subagents แสดง permission prompts เดียวกับที่คุณเห็นในการสนทนาหลัก
  • Background subagents ไม่แสดง prompts และรัน auto-deny หากต้องการ permission ที่ไม่ได้รับอนุญาต

เปิดใช้งานหน่วยความจำถาวร

เพิ่ม memory: user ใน frontmatter เพื่อให้ subagent บันทึก insights ข้ามการสนทนา:

---
name: code-reviewer
description: Code review specialist
memory: user
---

ไดเรกทอรีหน่วยความจำอยู่ที่:

  • ~/.claude/agent-memory/ สำหรับ user scope
  • .claude/agent-memory/ สำหรับ project scope
  • .claude/local-memory/ สำหรับ local scope

ควบคุมความสามารถของ Subagent

กำหนดข้อจำกัด tool:

---
name: safe-explorer
description: Read-only codebase explorer
tools: Read, Grep, Glob
---

ปิดการใช้งาน subagents เฉพาะ:

{
"permissions": {
"deny": ["Agent(Explore)"]
}
}

ตัวอย่าง Subagents

Security Reviewer

---
name: security-reviewer
description: Reviews code for security vulnerabilities and best practices
tools: Read, Grep, Glob
model: sonnet
color: red
---

You are a security expert. Review code for:
- SQL injection vulnerabilities
- XSS and CSRF vulnerabilities
- Authentication/authorization issues
- Sensitive data exposure
- Dependency vulnerabilities

Provide specific findings with line numbers and remediation steps.

Test Generator

---
name: test-generator
description: Generates unit tests for functions and modules
tools: Read, Write, Grep, Glob, Bash
model: sonnet
color: green
---

You are a test generation expert. When given a file or function:
1. Analyze the code to understand its behavior
2. Generate comprehensive unit tests covering edge cases
3. Use the project's existing testing framework
4. Ensure tests are readable and well-documented

ดูเพิ่มเติม

  • Tools reference: รายการ tools ทั้งหมดที่พร้อมใช้งานสำหรับ subagents
  • Permissions: ระบบ permission และ subagent permission modes
  • Agent teams: ประสานงาน subagents หลายตัวในงานซับซ้อน
  • Context window: เห็นว่า subagents ช่วยรักษา context อย่างไร