AI Solution Architect: from System Analyst to AI Architect
บทที่ 5/12

Generative AI, LLMs & RAG

AI Agents & Orchestration

Tool calling และ agent loop, orchestration patterns ตั้งแต่ single agent, planner–executor ถึง multi-agent, Model Context Protocol (MCP), guardrails และ human-in-the-loop และเมื่อไรที่ไม่ควรใช้ agent

⏱ ประมาณ 30 นาที

RAG ทำให้ LLM รู้ data ขององค์กร ส่วน AI agent ทำให้ LLM ลงมือทำ ได้ เช่น ดูสถานะบัตร, create ticket หรือ draft email โดยให้ model ตัดสินใจเองว่าจะเรียก tool ใดและเมื่อไร capability ที่เพิ่มขึ้นมาพร้อม risk ที่เพิ่มขึ้น หน้าที่ของ architect คือกำหนด boundary ให้ชัด

Tool calling: the model asks, your system acts

Model ไม่ได้เรียก API เอง app ส่ง tool definitions พร้อม schema ไปกับ prompt, model ตอบกลับมาว่า “อยากเรียก tool นี้ด้วย parameters นี้” แล้ว orchestrator ของเราเป็นผู้ validate และเรียกจริง

{
  "name": "get_card_status",
  "description": "ดูสถานะบัตรของลูกค้าที่ authenticated แล้ว (read-only)",
  "parameters": {
    "type": "object",
    "properties": {
      "card_last4": { "type": "string", "pattern": "^[0-9]{4}$" }
    },
    "required": ["card_last4"]
  }
}

สังเกตว่า tool นี้ ไม่รับ customer ID จาก model orchestrator ใส่ identity ของลูกค้าจาก authenticated session เอง ถ้าให้ model ส่ง ID มา user อาจหลอกให้ดู data ของคนอื่นได้

The agent loop

AI agent loop: user ให้ goal, LLM วางแผนและเลือก next step ถ้าต้องใช้ tool จะทำ tool call ภายใน guardrails (least privilege, max iterations) แล้ว observation ส่งกลับให้ LLM วน loop จนเสร็จจึงให้ final answer ส่วน tool call ที่มี risk ต้องผ่าน human approval ก่อน
Agent คือ loop ของ plan → tool call → observation จนกว่าจะเสร็จหรือถึง limit

Pattern นี้มักเรียกว่า ReAct (Reason + Act) model สลับระหว่างการ reasoning ว่าจะทำอะไรต่อ กับการเรียก tool แล้วอ่าน observation ทุกรอบเพิ่ม tokens และ latency จึงต้องมี max iterations เสมอ

Orchestration patterns

Patternลักษณะเหมาะกับ
Deterministic workflow + LLMโค้ดกำหนด steps, LLM ทำเฉพาะบาง step เช่น classification หรือ summarizationงานที่ steps รู้ล่วงหน้า audit ง่ายที่สุด
Single agent + toolsLLM ตัวเดียวเลือก tools เองใน loopงานที่ steps ต่างกันตามคำถาม tools ไม่เกินราวสิบกว่าตัว
Planner–executorตัวหนึ่ง plan เป็น steps อีกตัว execute ทีละ stepงานยาวหลาย steps ต้องการ plan ที่ review ได้ก่อนลงมือ
Multi-agentหลาย agents แต่ละตัวเชี่ยวชาญด้านหนึ่ง มี supervisor ประสานระบบใหญ่ที่หลายทีมดูแลคนละ domain

MCP: a standard way to connect tools

Model Context Protocol (MCP) เป็น open standard สำหรับเชื่อม AI apps กับ tools และ data sources ทีมเจ้าของระบบเขียน MCP server ครั้งเดียว แล้ว agents หรือ AI apps หลายตัวเรียกใช้ได้ ในมุม architecture MCP server คือ อีกหนึ่ง API ที่ต้องมี governance ต้องมี authentication, authorization, logging และผ่าน gateway เหมือน API อื่น (บทที่ 6 (APIs, Microservices & Legacy Integration))

Guardrails & human-in-the-loop

Guardrails คือ controls ที่ enforce ด้วยโค้ดและ infrastructure ไม่ใช่ด้วยการขอร้องใน prompt

  • Least privilege agent ได้เฉพาะ tools ที่จำเป็น แยก read tools กับ write tools และ credentials ของ tool มี scope แคบที่สุด
  • Input & output validation ทั้ง harmful content, PII และ parameter schema
  • Resource limits max iterations, timeout และ token budget ต่อ request
  • Audit log บันทึกทุก tool call พร้อม parameters และ results

Human-in-the-loop (HITL) ให้คน approve ก่อนทำ action ที่มีผลจริง แบ่งตาม risk level

Risk levelตัวอย่างControl
Read-onlyดูสถานะบัตร, search policiesagent ทำได้เอง
Reversible writecreate ticket, draft emailagent ทำได้ แต่ log และ review ย้อนหลัง
Money or permissionsอายัดบัตร, refund ค่าธรรมเนียมagent propose, คน approve

When NOT to use an agent

  • Steps ตายตัวและรู้ล่วงหน้า → ใช้ workflow ธรรมดา ถูกกว่าและ audit ง่ายกว่า
  • ต้องการผลลัพธ์ deterministic 100% เช่น คำนวณดอกเบี้ย → ใช้โค้ด ให้ LLM แค่อธิบายผล
  • Latency ต้องต่ำมาก → loop หลายรอบอาจใช้เวลาหลายวินาที
  • ยังไม่มี eval set และ audit log → ยังไม่พร้อมให้ AI ทำงานแทนคน

แบบทดสอบ

ใน tool calling ใครเป็นผู้เรียก API จริง
การ refund ค่าธรรมเนียมให้ลูกค้าควร design อย่างไร
งาน classify อีเมลร้องเรียนแล้ว route ไปแผนกที่รับผิดชอบ มี steps เหมือนกันทุกฉบับ ควรใช้ pattern ใด
Tool “ดูยอดค้างชำระ” ควรได้ identity ของลูกค้ามาจากที่ใด