27%

1.3 Subagent Invocation and Context Passing

Subagent Invocation คืออะไร

Subagent Invocation คือกระบวนการที่ agent หลักสร้าง agent ย่อย (subagent) ขึ้นมาทำงานเฉพาะ แล้วรับผลลัพธ์กลับมา — เหมือนหัวหน้าทีมที่มอบหมายงานให้ลูกทีมแล้วรอรายงาน ใน Claude ecosystem นี่คือหัวใจของการ scale agent systems เพราะ subagent แต่ละตัวมี context window เป็นของตัวเอง ทำให้ไม่ถูกจำกัดด้วย token limit ของ agent ตัวเดียว

ใน Claude Agent SDK ฟังก์ชัน agent() เป็นวิธีหลักในการ spawn subagent:

# ใน workflow script
const result = await agent("วิเคราะห์ไฟล์ src/main.py หา bugs", {
    label: "bug-finder",
    schema: BUG_SCHEMA
})

Context Passing Strategies

1. Explicit Context in Prompt

ใส่ข้อมูลที่จำเป็นตรงๆ ใน prompt ของ subagent:

research_result = await agent("หาข้อมูลเรื่อง MCP protocol")

# ส่ง research result เป็น context ให้ subagent ตัวถัดไป
summary = await agent(f"""
สรุปข้อมูลนี้เป็น bullet points:

{research_result}
""", {label: "summarizer"})

2. Shared File System

Subagents อ่าน/เขียนไฟล์ร่วมกัน — ใช้ filesystem เป็น communication channel:

# Agent A เขียน output ลงไฟล์
await agent("วิเคราะห์ requirements แล้วเขียนผลลงไฟล์ /tmp/analysis.md")

# Agent B อ่านผลจากไฟล์
await agent("อ่านไฟล์ /tmp/analysis.md แล้วสร้าง implementation plan")

3. Structured Output Passing

ใช้ JSON schema เพื่อให้ subagent return structured data ที่ agent ถัดไปใช้ง่าย:

const FINDINGS_SCHEMA = {
    type: "object",
    properties: {
        bugs: { type: "array", items: { type: "object", properties: {
            file: { type: "string" },
            line: { type: "number" },
            severity: { type: "string", enum: ["low", "medium", "high"] },
            description: { type: "string" }
        }}}
    }
}

const findings = await agent("หา bugs ใน codebase", {
    schema: FINDINGS_SCHEMA
})

// findings เปน parsed object ใชอไดเลย
findings.bugs.forEach(bug => ...)

Context Isolation vs Sharing

Isolation (แยก context)Sharing (แชร์ context)
แต่ละ subagent เริ่มใหม่ สะอาดส่ง conversation history ต่อ
ไม่มี bias จาก context ก่อนหน้าSubagent เข้าใจบริบทเดียวกัน
ใช้ tokens น้อยกว่าใช้ tokens มากขึ้น
เหมาะกับ independent tasksเหมาะกับ sequential refinement

Worktree Isolation

สำหรับ subagents ที่แก้ไขไฟล์พร้อมกัน — ใช้ isolation: "worktree" เพื่อป้องกัน conflicts:

// แตละ agent ทำงานใน git worktree แยก
const results = await parallel([
    () => agent("Fix bug in auth.py", {isolation: "worktree"}),
    () => agent("Fix bug in db.py", {isolation: "worktree"}),
    () => agent("Fix bug in api.py", {isolation: "worktree"})
])

Key Concepts

  • Agent Spawning — การสร้าง subagent ใหม่ มี overhead (context setup, model call) ไม่ควร spawn สำหรับ trivial tasks
  • Context Window Independence — subagent มี token budget เป็นของตัวเอง ไม่แชร์กับ parent
  • Structured Return — ใช้ schema บังคับ return type เพื่อให้ orchestrator parse ผลได้ง่าย
  • Null Handling — subagent อาจ return null (ถ้า fail หรือถูก skip) ต้อง filter ก่อนใช้: .filter(Boolean)

Exam Tips

  • agent() ที่ไม่มี schema จะ return text (string); ที่มี schema จะ return parsed object
  • Subagent ที่ fail จะ return null — เสมอ filter ก่อนใช้ results
  • Worktree isolation มี overhead (~200-500ms setup) ใช้เฉพาะเมื่อ agents แก้ไฟล์พร้อมกัน
  • Context passing ผ่าน prompt ดีกว่า shared state เพราะ explicit และ traceable
  • ข้อสอบอาจถามว่าเมื่อไหร่ควร isolate context vs share — ตอบ: isolate เป็น default, share เฉพาะเมื่อ subagent ต้องรู้ context ก่อนหน้าจริงๆ