27%

1.2 Multi-Agent Orchestration

Multi-Agent Orchestration คืออะไร

Multi-Agent Orchestration คือการออกแบบระบบที่ใช้ Claude หลายตัว (instances) ทำงานร่วมกัน โดยแต่ละตัวมีบทบาทเฉพาะ — แทนที่จะให้ agent ตัวเดียวทำทุกอย่าง เราแบ่งงานออกเป็นส่วนๆ ให้ agent เฉพาะทางรับผิดชอบ เหมือนทีมงานที่แต่ละคนมี expertise ต่างกัน

ข้อดีของ multi-agent คือ: แต่ละ agent มี context window เป็นของตัวเอง (ไม่แย่ง tokens กัน), สามารถใช้ model ต่างกัน (Opus สำหรับคิด, Sonnet สำหรับทำ), และง่ายต่อการ debug เพราะแยก responsibility ชัดเจน

Orchestration Patterns

1. Orchestrator-Worker Pattern

Agent ตัวหลัก (Orchestrator) รับ task มาแล้ววางแผน จากนั้น delegate subtasks ไปให้ Worker agents:

# Orchestrator วางแผน
plan = orchestrator.create_plan(task)

# แจก subtasks ให้ workers
results = []
for subtask in plan.subtasks:
    worker = get_specialist_worker(subtask.type)
    result = worker.execute(subtask)
    results.append(result)

# Orchestrator สรุปผลรวม
final = orchestrator.synthesize(results)

2. Pipeline Pattern

Agents เรียงกันเป็น chain — output ของตัวแรกเป็น input ของตัวถัดไป:

# Stage 1: Research
research = researcher_agent.run("หาข้อมูลเรื่อง X")

# Stage 2: Analysis
analysis = analyst_agent.run(f"วิเคราะห์ข้อมูลนี้: {research}")

# Stage 3: Writing
output = writer_agent.run(f"เขียนรายงานจาก: {analysis}")

3. Parallel Fan-Out

ส่งงานไปหลาย agents พร้อมกัน แล้วรวม results:

import asyncio

async def parallel_research(topics):
    tasks = [
        research_agent.run_async(topic)
        for topic in topics
    ]
    results = await asyncio.gather(*tasks)
    return synthesize(results)

4. Router Pattern

Agent ตัวแรกทำหน้าที่ route request ไปหา specialist ที่เหมาะสม:

# Router ดู intent แล้วส่งต่อ
route = router_agent.classify(user_request)

if route == "code":
    response = code_agent.handle(user_request)
elif route == "research":
    response = research_agent.handle(user_request)
elif route == "creative":
    response = creative_agent.handle(user_request)

เมื่อไหร่ควรใช้ Multi-Agent vs Single Agent

ใช้ Single Agent เมื่อใช้ Multi-Agent เมื่อ
Task ไม่ซับซ้อน ทำจบใน context เดียวTask ต้องการ expertise หลากหลาย
Context window พอข้อมูลมากเกินสำหรับ context เดียว
ต้องการ latency ต่ำต้องการ parallelism
Budget จำกัดคุณภาพสำคัญกว่า cost

Key Concepts

  • Context Isolation — แต่ละ agent มี conversation history แยกกัน ไม่เห็น messages ของ agent อื่น
  • Handoff Protocol — วิธีส่งต่อ task ระหว่าง agents รวมถึง context ที่จำเป็น
  • Agent Registry — catalog ของ agents ที่มีพร้อมใช้ orchestrator ดึงมาตาม role ที่ต้องการ
  • Result Synthesis — orchestrator รวม results จาก workers ให้เป็นคำตอบเดียว

Exam Tips

  • เข้าใจ trade-off ระหว่าง single agent (simple, cheap, fast) vs multi-agent (capable, expensive, complex)
  • Router pattern เหมาะกับระบบที่มี domain ชัดเจนหลาย domain
  • Pipeline pattern เหมาะกับ workflow ที่มีลำดับชัดเจน (เช่น research → analyze → write)
  • Parallel fan-out ช่วยลด latency แต่เพิ่ม cost (ใช้ tokens มากขึ้น)
  • Orchestrator ควรใช้ model ที่เก่งด้าน reasoning (เช่น Opus) ส่วน workers ใช้ model ที่เร็วและถูกกว่าได้