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

Generative AI, LLMs & RAG

LLM Fundamentals for Architects

Tokens, context window และ temperature ทำงานอย่างไร model selection โดยชั่ง quality, latency และ cost, prompt patterns ที่ใช้ใน production การรับมือ hallucination และ cost estimation จากจำนวน tokens

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

Architect ไม่จำเป็นต้อง train model เอง แต่ต้องรู้ว่า LLM “คิดเงิน” และ “มี limit” ตรงไหน เพราะสองเรื่องนี้กำหนดทั้ง cost, latency และ quality ของระบบ บทนี้มองจากมุมของคน design ระบบ ไม่ใช่ researcher

Anatomy of an LLM request

LLM request: prompt ฝั่ง input ประกอบด้วย system prompt (role, rules, format), retrieved context จาก RAG และ chat history กับ question ทั้งหมดถูก tokenize เข้า LLM ที่มี context window จำกัด เช่น 128k tokens รวม input และ output แล้ว stream response ออกมาทีละ token โดย cost ต่อ request คือ input tokens × input price + output tokens × output price
ทุกอย่างที่ส่งเข้าไปและทุกอย่างที่ได้กลับมา นับเป็น tokens และคิดเงินทั้งหมด
  • Token คือหน่วยย่อยของข้อความที่ model อ่าน คำภาษาอังกฤษหนึ่งคำมักเป็น 1–2 tokens ส่วน ภาษาไทยมักใช้ tokens มากกว่า ต่อความยาวเท่ากัน ขึ้นกับ tokenizer ของแต่ละ model จึงควรวัดจาก data จริงของระบบ ไม่ใช้ตัวเลขจากภาษาอังกฤษ
  • Context window คือจำนวน tokens สูงสุดที่ model รับได้ในหนึ่ง request รวม system prompt, documents, chat history และ response ถ้าเกิน ต้อง truncate หรือ summarize history
  • Response ถูก generate ทีละ token latency จึงขึ้นกับความยาว response โดยตรง metric ที่ user รู้สึกคือ time-to-first-token ระบบ chat จึงมักใช้ streaming

Parameters ที่ architect ต้องกำหนด

Parameterความหมายค่าที่มักใช้ใน enterprise
temperatureความ random ในการเลือก token ต่ำ = consistent สูง = variedต่ำ (0–0.3) สำหรับ Q&A จาก documents หรือ extraction
max output tokenslimit ความยาว responseกำหนดเสมอ เพื่อคุม cost และ latency
structured outputบังคับให้ตอบตาม schema เช่น JSON schemaใช้เมื่อ output ต้องส่งต่อให้ระบบอื่น

แม้ temperature เป็น 0 output ก็ ไม่ deterministic 100% ระบบจึงต้อง design ให้ทนต่อ response ที่ต่างกันเล็กน้อย เช่น validate schema ก่อนใช้งาน

Model selection: quality vs latency vs cost

Model ใหญ่เก่งกว่าในงานซับซ้อน แต่แพงและช้ากว่า model เล็กถูกและเร็ว แต่อาจพลาดในงานที่ต้อง reasoning หลายขั้น แนวทางที่ใช้ใน production

  • เริ่มจาก model เล็กที่สุดที่ผ่าน criteria วัดด้วย eval set ของงานจริง ไม่ใช่ public benchmark
  • Model routing ส่งงานง่าย (classification, summarization สั้น) ให้ model เล็ก และส่งงานยากให้ model ใหญ่
  • Constraints นอกเหนือ quality เช่น region ที่ให้บริการ, data retention terms, quota และ model retirement ซึ่งทำให้ต้องวางแผน upgrade model version เป็นประจำ (บทที่ 9 (MLOps & LLMOps))

Prompt patterns ใน production

ใน production prompt ไม่ได้พิมพ์ด้วยมือทุกครั้ง แต่เป็น prompt template ที่มี version ประกอบจากหลายส่วน

[system]  คุณคือ assistant ตอบคำถามเรื่องบัตรเครดิตของธนาคาร
          ตอบจากเอกสารใน <context> เท่านั้น ถ้าไม่พบให้ตอบว่า "ไม่พบข้อมูล"
          ห้ามขอเลขบัตรหรือรหัสผ่าน และอ้างอิงเลขเอกสารทุกครั้ง
[context] <context> ...chunks ที่ retrieve ได้ 3–5 ชิ้น... </context>
[history] ...summary ของบทสนทนาก่อนหน้า...
[user]    ค่าธรรมเนียมรายปีบัตร Platinum เท่าไร

Patterns ที่ควรรู้จัก

  • แยก instructions กับ context ชัดเจน ช่วยให้ model แยกคำสั่งออกจาก data และลด risk ของ prompt injection (บทที่ 10 (Security, Governance & Compliance))
  • Few-shot ใส่ตัวอย่าง question–answer ที่ดี 2–3 คู่ ช่วยให้ output format คงที่
  • Step-by-step reasoning สำหรับงานที่ต้องใช้เหตุผล แต่ trade-off คือ tokens ที่เพิ่มขึ้น
  • กำหนด fallback เมื่อไม่รู้ เช่น “ถ้าไม่พบให้ตอบว่าไม่พบข้อมูล” เป็นหนึ่งในวิธีลด hallucination ที่ได้ผลที่สุด

Hallucination: design to fail safely

Hallucination คือการที่ model generate ข้อมูลที่ฟังดูน่าเชื่อแต่ไม่จริง เป็นธรรมชาติของ LLM ไม่ใช่ bug ที่แก้หมดได้ architect จึงลดทั้ง likelihood และ impact ไปพร้อมกัน

Layerวิธี
ลด likelihoodgrounding ด้วย RAG, temperature ต่ำ, อนุญาตให้ตอบ “ไม่พบข้อมูล”
ทำให้ verifiableบังคับ citations และให้ user คลิกดู source ได้
ลด impactไม่ให้ model ตัดสินเรื่องเงินหรือสิทธิ์เอง ตัวเลขสำคัญดึงจาก API ไม่ใช่ให้ model generate
Measureeval set ที่วัด groundedness อย่างต่อเนื่อง (บทที่ 9 (MLOps & LLMOps))

Cost estimation

สูตรต่อ request

cost=input tokens106×Pin+output tokens106×Pout\text{cost} = \frac{\text{input tokens}}{10^6} \times P_\text{in} + \frac{\text{output tokens}}{10^6} \times P_\text{out}

โดย PinP_\text{in} และ PoutP_\text{out} คือ price ต่อ 1M tokens ซึ่ง output price มักแพงกว่า input หลายเท่า

ตัวอย่าง (ราคาสมมติ) ระบบตอบคำถามพนักงาน 20,000 requests ต่อวัน แต่ละ request มี input 3,000 tokens (system prompt + chunks + question) และ output 500 tokens ราคาสมมติ input $2.50 และ output $10 ต่อ 1M tokens

  • Input ต่อวัน: 20,000 × 3,000 = 60M tokens → 60 × $2.50 = $150
  • Output ต่อวัน: 20,000 × 500 = 10M tokens → 10 × $10 = $100
  • รวม $250 ต่อวัน หรือราว $7,500 ต่อเดือน (30 วัน)

สังเกตว่า input คิดเป็น 60% ของ cost เพราะ chunks จาก RAG ยาว จึงเป็นจุดแรกที่ควรดู เช่น ลดจำนวนหรือความยาวของ chunks หรือใช้ prompt caching กับ system prompt ที่ซ้ำทุกครั้ง (ถ้า provider รองรับ) แต่ output price ต่อ token แพงกว่าหลายเท่า การจำกัดความยาว response จึงได้ผลเช่นกัน ให้คำนวณทั้งสองทางแล้วเทียบกัน

แบบทดสอบ

Context window นับ tokens ส่วนใดบ้าง
ระบบตอบคำถามเรื่องค่าธรรมเนียมจาก policy documents ควรตั้ง temperature แบบใด
วิธีใดลด impact ของ hallucination เมื่อลูกค้าถามยอดเงินในบัญชี

ระบบหนึ่งรับ 10,000 requests ต่อวัน แต่ละ request มี input 2,000 tokens และ output 400 tokens ราคาสมมติ input $2.50 และ output $10 ต่อ 1M tokens cost ต่อวันเท่ากับกี่ดอลลาร์

จากตัวอย่าง cost ในบทนี้ (20,000 requests ต่อวัน ราคาสมมติเดิม) ทางใดประหยัดได้มากกว่าต่อวัน