ข้ามไปที่เนื้อหา

RAG vs Agentic RAG: เมื่อ AI รู้จักคิดก่อนค้น

คุณถาม AI assistant ไปหนึ่งคำถาม ได้คำตอบกลับมาแบบมั่นใจเต็มร้อย แต่คำตอบนั้นผิด เก่าไปแล้ว หรือแต่งขึ้นมาเองทั้งดุ้น อาการนี้เรียกว่า hallucination และเป็นปัญหาที่น่าหงุดหงิดที่สุดข้อหนึ่งของ large language model (LLM) ที่หยิบมาใช้ตรง ๆ

RAG (Retrieval-Augmented Generation) เกิดขึ้นมาเพื่อแก้ปัญหานี้โดยเฉพาะ ส่วน Agentic RAG พาทางแก้นี้ไปอีกขั้น โพสต์นี้จะแกะทั้งสอง architecture ตั้งแต่ศูนย์ ว่าคืออะไร ทำงานทีละขั้นยังไง และงานแบบไหนควรใช้ตัวไหน


Part 1: RAG เกิดมาแก้ปัญหาอะไร

LLM รู้ได้แค่ถึงวัน Knowledge Cutoff

LLM ทุกตัว train จากข้อมูลที่เก็บมาถึงวันใดวันหนึ่ง เลยจากวันนั้นไปก็ไม่รู้อะไรใหม่อีกเลย ลองถาม GPT-4 เรื่อง product ที่เพิ่งเปิดตัวเมื่อเดือนก่อนดู ถ้าไม่ตอบว่า "ไม่รู้" ก็แย่กว่านั้นคือแต่งคำตอบมาให้อย่างมั่นใจ

ต่อให้เป็นช่วงเวลาที่อยู่ใน training data ก็เถอะ LLM ไม่เคยเห็นเอกสารภายในของคุณ ทั้ง wiki บริษัท record ใน database หรือ ticket ของลูกค้า พวกนี้ไม่เคยผ่านตา model มาก่อนเลย

The fundamental mismatch:
  LLM knowledge = frozen snapshot of the public internet
  Real-world needs = live, private, domain-specific data

ช่องว่างตรงนี้แหละที่ RAG เข้ามาปิด


Part 2: Traditional RAG — เติมข้อมูลเข้า Prompt แล้วคำตอบก็ดีขึ้น

ไอเดียหลัก

RAG ย่อมาจาก Retrieval-Augmented Generation แค่ชื่อก็อธิบายครบแล้ว:

  • Retrieval: ไปหาข้อมูลที่เกี่ยวข้องจากแหล่งภายนอก
  • Augmented: ใส่ข้อมูลนั้นเพิ่มเข้าไปใน context window
  • Generation: ให้ LLM เขียนคำตอบจากข้อมูลจริง

ไม่ต้องพึ่งความรู้ที่ค้างอยู่ใน model อีกต่อไป คุณยัดข้อมูลสด ๆ ที่ตรงกับคำถามเข้าไปใน prompt เลย ทำแบบนี้ตอนจะถามทุกครั้ง

RAG Pipeline 6 ขั้น

ส่ง query เข้าระบบ traditional RAG หนึ่งครั้ง เบื้องหลังเกิดอะไรขึ้นบ้าง ดูได้ตามนี้:

Step 1: User sends a Prompt + Query
        "What are our Q1 2026 sales numbers for the APAC region?"

Step 2: The Server receives the query and
        forwards it to a Search component.

Step 3: The Search component queries Knowledge Sources:
        → PDFs (your uploaded reports)
        → Databases (your CRM or data warehouse)
        → Documents (your internal wiki, Notion pages)
        → Code repositories
        → Web search (for live internet results)
        → APIs (for structured external data)

Step 4: The most relevant chunks of information
        are returned as "Enhanced Context."

Step 5: The Server constructs a new, enriched prompt:
        [Original Prompt] + [Original Query] + [Retrieved Context]
        and sends it to the LLM.

Step 6: The LLM generates a grounded response —
        one that's based on actual retrieved facts,
        not hallucinated guesses.

ตัวอย่างจริง

User query:  "Summarize our refund policy for subscription products."

Without RAG (pure LLM):
  "Typically, SaaS products offer a 30-day money-back guarantee..."
  → Generic, not your policy. Possibly wrong.

With RAG:
  Step 2-4: Fetches your actual policy.pdf from the knowledge base
  Step 5:   Prompt now includes the exact policy text
  Step 6:   "According to your policy (updated March 2026):
             Subscriptions are eligible for a full refund within 14 days
             of purchase if no more than 2 sessions have been used..."
  → Precise, grounded, correct.

RAG ทำอะไรได้ดี

ความสามารถ Traditional RAG
ตอบจากเอกสารภายใน ✅ ดีมาก
ข้อมูลทันสมัย ✅ ถ้าต่อ live search
Query ง่าย ๆ เรื่องเดียว ✅ ดีมาก
Multi-step reasoning ⚠️ ทำได้จำกัด
ข้อมูล dynamic จากหลายระบบ ⚠️ ต่อเชื่อมยุ่ง
แก้ตัวเองและ retry ❌ ไม่มี

ข้อจำกัด: Pipeline ยิงทีเดียวจบ

Traditional RAG เป็น stateless และวิ่งเป็นเส้นตรง ถามครั้งเดียว retrieve ครั้งเดียว generate ครั้งเดียว ถ้า query ง่าย ๆ แบบนี้ใช้ได้ดีมาก

แต่ลองเจอคำถามซับซ้อนแบบนี้ดู:

"เทียบเทรนด์การใช้งานไตรมาสนี้ของลูกค้า enterprise 3 อันดับแรก ดูด้วยว่ามีใครมี support ticket ค้างอยู่ไหม แล้วร่าง renewal email แบบ personalized ให้ทีละราย"

Search query เดียวตอบไม่ได้แน่ ต้องดึงข้อมูลจาก CRM ระบบ ticketing และ database ฝั่ง usage analytics แล้วต้องทำหลายขั้นต่อกันอย่างเป็นระบบด้วย ถึงตรงนี้ traditional RAG ก็ไปต่อไม่ไหว

นี่แหละจังหวะที่ Agentic RAG เข้ามา


Part 3: Agentic RAG — AI คิดก่อน แล้วค่อยค้น

ไอเดียหลัก

Agentic RAG เอา pipeline แบบค้นแล้วดึงมาตรง ๆ ออก แล้วใช้ ทีม AI agent เฉพาะทาง แทน โดยมี Aggregator Agent เป็นตัวกลางคุมทั้งหมด ระบบไม่ได้แค่ค้นอย่างเดียว แต่ วางแผน แจกงาน จำ และ คิดเป็นเหตุเป็นผล ให้เสร็จก่อน แล้วค่อยเขียนคำตอบ

เทียบให้เห็นภาพแบบนี้:

Traditional RAG:  One librarian. One shelf. One lookup.
Agentic RAG:      A research team. Each expert in their domain.
                  A project lead who coordinates them, tracks progress,
                  and synthesizes the final report.

Agentic RAG Pipeline 6 ขั้น

Step 1: User sends a Prompt + Query
        "Compare our top 3 customers' usage, open tickets,
         and draft renewal emails for each."

Step 2: The query goes to the Aggregator Agent —
        the brain of the entire system.

Step 3: The Aggregator Agent creates a PLAN.
        It uses reasoning frameworks to decompose the task:
        ├── ReACT   (Reason + Act: think, act, observe, repeat)
        ├── CoT     (Chain-of-Thought: break into logical steps)
        └── Planning (set goals, assign sub-tasks, track state)

        The plan might look like:
        [1] Fetch top 3 customer usage data from analytics DB
        [2] Check open support tickets for each customer
        [3] Pull customer history from CRM
        [4] Draft personalized renewal emails

Step 4: The Aggregator Agent FETCHES by dispatching
        specialized sub-agents in parallel:

        Agent 1 → MCP Servers → Local Data Sources
                  (internal databases, files, documents)

        Agent 2 → Search Engine → Web Search
                  (public information, news, competitor data)

        Agent 3 → Cloud Engine → AWS / Azure
                  (cloud-hosted data warehouses, APIs, storage)

Step 5: Results flow back to the Aggregator Agent,
        which also draws on MEMORY:
        ├── Short-Term Memory: current conversation context
        └── Long-Term Memory:  past interactions, preferences,
                               learned user-specific knowledge

        The Aggregator assembles the enriched context:
        [Original Prompt] + [Query] + [All Retrieved Data]

Step 6: A Generative Model (GPT, Gemini, Claude)
        produces the final grounded, multi-faceted response.

หัวใจของเรื่อง: Reasoning Framework

ตัวที่ทำให้ Agentic RAG ต่างจาก RAG แบบเดิมจริง ๆ คือ planning layer Aggregator Agent ไม่ได้ยิง keyword ค้นทีเดียวแล้วจบ แต่คิดก่อนว่า จะตอบคำถามนี้ยังไง แล้วค่อยลงมือ

ReACT (Reason + Act)

ReACT คือการวน loop: คิด → ลงมือ → ดูผล → คิดต่อ

Thought: "I need usage data for the top 3 customers.
          I'll query the analytics database."
Action:  Call Agent 1 → Analytics DB
Observe: Returns Customer A: 89%, Customer B: 72%, Customer C: 91%

Thought: "Now I need their open tickets."
Action:  Call Agent 1 → Ticketing System
Observe: Customer B has 2 open P1 issues.

Thought: "Customer B has issues. The renewal email
          should acknowledge this before pitching renewal."
Action:  Draft differentiated emails.

Agent ไม่ได้แค่ดึงข้อมูลมาเฉย ๆ แต่อ่านผลที่ได้ ปรับแผน และลองใหม่ตามสิ่งที่เจอ

Chain-of-Thought (CoT)

CoT แตกคำถามยาก ๆ ออกเป็นลำดับเหตุผลที่เขียนออกมาให้เห็นทีละข้อ:

Query: "Should we offer Customer A a discount?"

CoT reasoning:
  1. What is Customer A's current contract value? → $48,000/yr
  2. What is their usage trend? → Declining (-12% MoM)
  3. Do they have outstanding issues? → None
  4. What's their renewal date? → 45 days from now
  5. What's our churn risk model say? → High risk (73%)

Conclusion: Yes, a 10% loyalty discount is warranted.
            Recommend proactive outreach.

ถ้าไม่มี CoT LLM อาจตอบตามความรู้สึก พอมี CoT ก็ย้อนดูได้ว่าได้คำตอบมาจากเหตุผลข้อไหน และตรวจทานได้

Memory ช่วยอะไร

Memory เป็นของใหม่ชิ้นใหญ่ที่สุดชิ้นหนึ่งที่ Agentic RAG เพิ่มเข้ามา:

ประเภท Memory เก็บอะไร ตัวอย่าง
Short-Term Context ของบทสนทนาตอนนี้ "เมื่อกี้คุณพูดถึง Budget Q2 2026..."
Long-Term ประวัติการคุยและสิ่งที่ user ชอบ "User คนนี้อยากได้ข้อมูลเป็นตารางทุกครั้ง"

พอมี long-term memory ระบบก็ เก่งขึ้น เรื่อย ๆ กับ user แต่ละคน ไม่ต้องเริ่มนับหนึ่งใหม่ทุก query

MCP Server — ตัวเชื่อมกลางที่ต่อได้ทุกอย่าง

ใน architecture diagram Agent 1 ต่อเข้าข้อมูล local ผ่าน MCP Server MCP ย่อมาจาก Model Context Protocol เป็น open standard ที่กำลังมาแรง มี Anthropic เป็นหัวหอก ให้ AI agent ต่อเข้า tool หรือแหล่งข้อมูลภายนอกได้ทุกตัวอย่างปลอดภัย และใช้ interface แบบเดียวกันหมด

Without MCP:  Each integration is custom-coded.
              Agent ↔ custom connector ↔ Database A
              Agent ↔ different custom connector ↔ Database B

With MCP:     One standard protocol.
              Agent ↔ MCP Server ↔ [Database A, Database B,
                                    File System, API, anything]

MCP ทำให้ขยายระบบ Agentic RAG ง่ายขึ้นเยอะ อยากเพิ่มแหล่งข้อมูลใหม่ก็แค่ spin up MCP server ขึ้นมาอีกตัว


Part 4: เทียบกันตรง ๆ

Feature Traditional RAG Agentic RAG
รับ query ยังไง Query เดียว retrieve ครั้งเดียว Retrieve หลายขั้น ใช้หลาย agent
Planning ไม่มี ReACT / CoT / วางแผนชัดเจน
แหล่งข้อมูล หนึ่งหรือไม่กี่แหล่ง ตั้งไว้ล่วงหน้า Dynamic หลายแหล่ง เพิ่มได้เรื่อย ๆ
Memory Stateless (ไม่มี memory) Short-term + long-term memory
แก้ตัวเอง ไม่มี Agent retry และปรับแผนได้
Latency ต่ำ (round trip เดียว) สูงกว่า (orchestrate หลายขั้น)
ต้นทุน ต่ำกว่า สูงกว่า (เรียก LLM หลายรอบ)
ความซับซ้อน ต่ำ สูง
เหมาะกับ Q&A ตรงประเด็น งานซับซ้อนที่คร่อมหลาย domain

Part 5: งานไหนใช้ตัวไหน

ใช้ Traditional RAG เมื่อ:

  • งานหลักคือ ตอบคำถาม จากชุดเอกสารที่มีอยู่แล้ว
  • Query ไม่ซับซ้อน ถามเรื่องเดียว
  • ต้องการ latency ต่ำ (chat bot, real-time assistant)
  • งบน้อย เพราะเรียก agent เพิ่มทุกครั้งก็เสียเงินเพิ่ม
  • Knowledge base ไม่ค่อยเปลี่ยนและจัดโครงสร้างไว้ดี

ตัวอย่าง use case: FAQ bot ของทีม customer support, chatbot ตอบเรื่อง policy ภายใน, สรุปเอกสาร, coding assistant ที่ค้นข้อมูลประกอบได้

ใช้ Agentic RAG เมื่อ:

  • ต้อง เอาข้อมูลจากหลายระบบมาประกอบเป็นคำตอบเดียว
  • งานต้อง คิดหรือตัดสินใจหลายขั้น
  • อยากได้ คำตอบแบบ personalized ที่ดีขึ้นเรื่อย ๆ (ใช้ memory)
  • ข้อมูลกระจายอยู่หลายแหล่งคนละแบบ: cloud DB, API, ไฟล์ local, เว็บ
  • กำลังสร้าง autonomous workflow ไม่ใช่แค่ระบบถามตอบ

ตัวอย่าง use case: AI sales assistant, research analyst อัตโนมัติ, onboarding agent แบบ personalized, DevOps incident responder ที่ช่วยวิเคราะห์ปัญหาเองได้


Part 6: ลงมือสร้างระบบ RAG ตัวแรก (ไกด์ปี 2026)

Traditional RAG ฉบับเล็กสุดใน Python

# 1. Embed your documents
from sentence_transformers import SentenceTransformer
import numpy as np

model = SentenceTransformer("all-MiniLM-L6-v2")

docs = [
    "Our refund policy allows returns within 14 days.",
    "Subscription plans renew automatically each month.",
    "Enterprise customers receive dedicated support.",
]
doc_embeddings = model.encode(docs)

# 2. At query time: retrieve the closest chunk
query = "Can I get a refund?"
query_embedding = model.encode([query])
scores = np.dot(doc_embeddings, query_embedding.T).flatten()
best_match = docs[np.argmax(scores)]

# 3. Augment the prompt
augmented_prompt = f"""
Answer the question using the context below.

Context: {best_match}

Question: {query}
"""

# 4. Send to LLM (using Anthropic's Claude)
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=256,
    messages=[{"role": "user", "content": augmented_prompt}]
)
print(response.content[0].text)

โครง Agentic RAG แบบย่อ

# Pseudo-code for an Agentic RAG orchestrator
class AggregatorAgent:
    def __init__(self, agents, memory):
        self.agents = agents   # [LocalDataAgent, WebSearchAgent, CloudAgent]
        self.memory = memory   # short + long-term memory store

    def plan(self, query):
        """Use CoT to decompose the query into sub-tasks."""
        # In practice: send query to LLM with a planning prompt
        return [
            {"task": "Fetch internal docs", "agent": "local"},
            {"task": "Search web for recent news", "agent": "web"},
        ]

    def execute(self, plan):
        """Run sub-tasks, potentially in parallel."""
        results = {}
        for step in plan:
            agent = self.agents[step["agent"]]
            results[step["task"]] = agent.fetch(step["task"])
        return results

    def synthesize(self, query, results, context_from_memory):
        """Combine all retrieved data and generate final answer."""
        enriched_context = "\n\n".join([
            context_from_memory,
            *[f"[{k}]: {v}" for k, v in results.items()]
        ])
        # Send enriched context + query to generative model
        return llm.generate(query, enriched_context)

    def answer(self, query):
        memory_context = self.memory.recall(query)
        plan = self.plan(query)
        results = self.execute(plan)
        response = self.synthesize(query, results, memory_context)
        self.memory.store(query, response)  # update memory
        return response
Component Tool / Framework
Embedding model sentence-transformers, OpenAI text-embedding-3-small
Vector database Pinecone, Weaviate, pgvector (Postgres)
Orchestration LangGraph, LlamaIndex Workflows, เขียนเอง
MCP integration @modelcontextprotocol/sdk, MCP server ของ Anthropic
LLM backbone Claude Sonnet 4.6, GPT-4o, Gemini 2.0 Flash
Memory Redis (short-term), Postgres/vector DB (long-term)

Summary

RAG กับ Agentic RAG คือสองขั้นของการทำให้ AI ตอบโดยอิงข้อมูลจริง ขั้นแรกเรียบง่าย ขั้นที่สองโตกว่า

Traditional RAG แก้ hallucination ด้วยการเอาเอกสารที่ retrieve มาใส่ใน prompt ของ LLM ทำงานเป็นเส้นตรงทีเดียวจบ เหมาะกับ Q&A ที่ถามตรงประเด็นและ assistant ที่ตอบจากเอกสาร งานแบบนี้ต้องการความเรียบง่ายและ latency ต่ำ

Agentic RAG เพิ่ม planning, agent เฉพาะทาง, memory และ reasoning loop (ReACT, Chain-of-Thought) เข้าไป Aggregator Agent แตกคำถามยากออกเป็นงานย่อย ส่งต่อให้ sub-agent แต่ละตัวไปดึงข้อมูลจากฝั่ง local, เว็บ และ cloud แล้วรวมออกมาเป็นคำตอบที่ครบและแม่นกว่า ราคาที่ต้องจ่ายคือต้นทุนและความซับซ้อนที่สูงขึ้น

เลือกง่าย ๆ แบบนี้:

  • คำถามพิมพ์ลงช่อง search ช่องเดียวได้ → ใช้ Traditional RAG
  • คำถามต้องมี project manager คอยประสานผู้เชี่ยวชาญหลายคน → ใช้ Agentic RAG

ปี 2026 ทั้งสองแบบพร้อมขึ้น production แล้ว เริ่มจาก RAG ก่อน วัดดูว่าพังตรงไหน แล้วค่อยขยับไป Agentic RAG เมื่อความซับซ้อนที่เพิ่มขึ้นคุ้มจริง ๆ ไม่ว่าเลือกแบบไหน เป้าหมายก็เหมือนเดิม: ส่งข้อมูลที่ถูกเรื่อง ในจังหวะที่ใช่ และในรูปที่ LLM ใช้ได้


มีคำถาม หรืออยากเห็น implementation ที่รันได้ครบทั้งระบบ? ทิ้ง comment ไว้ข้างล่าง หรือทักมาหาผมตรง ๆ ได้เลย

มีคำถามหรืออยากคุยต่อ? ติดต่อผมได้ทาง LinkedIn, X หรือ email

Discussion

Have thoughts on this post? Share them below — questions, corrections, or your own experience are all welcome.