- Chuyển từ Jupyter notebook sang hệ thống LLM quy mô doanh nghiệp đòi hỏi kỷ luật kỹ thuật cao.
- Kiến trúc 7 lớp bao gồm Services, Agents, Prompts, Security, Evaluation, Observability, và Claude Context.
- Thực tế là 60-70% độ phức tạp nằm không phải ở model, mà ở caching, guardrails, evaluation, và observability.
TL;DR
Từ prototype Jupyter notebook đến ứng dụng LLM quy mô doanh nghiệp đòi hỏi không chỉ một model tốt, mà một kiến trúc 7 lớp toàn diện:
- Services: RAG pipelines, semantic caching, memory, query rewriters, routers
- Agents: Document graders, decomposers, adaptive routers tự sửa chữa
- Prompts: Versioned, strongly typed, registered (KHÔNG hardcoded)
- Security: 3 lớp guardrails riêng (input, content, output)
- Evaluation: Golden datasets, offline evals, online monitors
- Observability: Per-stage tracing, trace-linked feedback, cost-per-query tracking
- Claude Context: Agent rules mã hoá kiến thức kiến trúc codebase
Insight then thú: Phần lớn độ phức tạp sản xuất (60-70%) không nằm ở model, mà ở các lớp hỗ trợ như caching, guardrails, evaluation, và observability.
Từ Notebook đến Sản Xuất
Jupyter notebook là bằng chứng chứng thực lớn nhất trong lĩnh vực AI. Một file, tất cả code + kết quả, dễ dàng thử nghiệm. Nhưng khi chuyển sang production, mọi thứ thay đổi:
Notebook = hardcoded prompts, print statements, không versioning. Khi dữ liệu thực tế đến với volume lớn, scale tăng lên, mọi thứ sụp đổ. Production hệ thống đòi hỏi:
- Phân tách concerns thành các module độc lập
- Type safety & validation ở mỗi ranh giới
- Visibility vào từng bước xử lý (tracing)
- Vòng lặp feedback từ production quay lại evaluation
- Cost tracking và optimization liên tục
7 Lớp Kiến Trúc Chi Tiết
Lớp 1: Services — Nền Tảng
Services là lớp dưới cùng, chứa những pipeline chuyên dụng mà model không biết về:
- RAG Pipelines: Không phải một script retrieval đơn giản, mà hệ thống đầy đủ bao gồm embedding, search, re-ranking, context synthesis
- Semantic Caching: Cache response dựa trên ý nghĩa, không chỉ string matching. Tiết kiệm 30-50% chi phí inference
- Memory Management: Context window hạn chế. Memory modules theo dõi conversation history, tóm tắt dài kỳ, recall cần thiết
- Query Routers & Rewriters: Lấy user query, đơn giản hoá, phân tích intent, định tuyến đến service chính xác
Không phải 1 script, mà 5+ module chuyên dụng. Mỗi cái testable, versioned, replaceable.
Lớp 2: Agents — Orchestration
Agents không phải là model tự nói chuyện. Chúng là decision engines tự sửa chữa:
- Document Graders: LLM evaluate chất lượng retrieval result. Xấu? → re-retrieve
- Decomposers: Chia task phức tạp thành sub-tasks. "Viết proposal và schedule meeting" → 2 actions
- Adaptive Routers: Dựa vào context hiện tại, quyết định next step. Không hardcode workflow
- Self-Correcting: Check output có quá-đơn giản? Không đủ detail? → gọi lại model để refine
Result: system tự phát hiện và sửa lỗi của riêng nó, không phải wait cho human approval.
Lớp 3: Prompts — Versioning & Type Safety
Prompt là config của model. Config KHÔNG nên hardcoded trong code:
- Centralized Registry: Tất cả prompt live ở một nơi, versioned, tagged
- Strongly Typed: Prompt definition = schema. Input slots required, output format expected
- A/B Testing Ready: Swap version mà không touch code
- Audit Trail: Ai thay đổi prompt nào, khi nào, tại sao
Ví dụ: "summarize-doc-v3" prompt có input `{doc_text: string, max_length: int}`, output `{summary: string, key_points: string[]}`. Type-safe.
Lớp 4: Security — 3 Lớp Guardrails
Input Validation: Xử lý prompt injection, malicious input. Sanitize, pattern match, LLM-based classifier
Content Filtering: Model output có chứa sensitive info? GDPR violation? Bias? Filter trước khi serve user
Output Validation: Response có đúng format expected? Fact-check? Rate-limit user nếu detect abuse
3 lớp riêng, mỗi layer không giả định layer khác làm việc. Layered defense, không trust input từ bất kỳ layer trước đó.
Lớp 5: Evaluation — Quality Gate (Lớp Mọi Team Bỏ Qua)
Đây là lớp mà hầu hết team "bỏ qua trước khi ship blind" — và rồi bug tồn tại tháng:
- Golden Datasets: Tập test case được label thủ công. "Input X → Expected output Y". Tính công: cao, nhưng không thay thế
- Offline Evaluation: Chạy mọi prompt version lại golden set trước khi deploy. Detect regression
- Online Monitors: Production running, user feedback flow in real-time. Model degradation được detect minutes, không hours
Kỹ luật: KHÔNG ship feature mà chưa có golden dataset. Nếu claim performance, phải benchmark offline trước.
Lớp 6: Observability — Production Visibility
Bạn không thể fix cái bạn không nhìn thấy:
- Per-Stage Tracing: Input query → Services → Agents → Prompts → Security check → Output. Mỗi stage log latency, tokens, decisions
- Trace-Linked Feedback: User rate response xấu. Feedback linked tới trace ID. Debug exact code path dẫn đến kết quả
- Cost-Per-Query Tracking: Model XYZ cost $0.002/1K token. Query 10K tokens = $0.02. Aggregate $10K/tháng. Optimize high-cost paths
Observability không phải luxury, mà necessity. Team không có nó = flying blind.
Lớp 7: Claude Context — AI-Aware Architecture
Khi bạn dùng LLM làm coding assistant (hoặc code generation), system cần hiểu architecture của riêng nó:
- Agent Rules: `.claude/rules/` directory chứa markdown mô tả architecture, naming convention, common patterns
- Context Injection: AI coding assistant đọc rules, hiểu structure. "Generate test for grader module" → agent đặt test ở đúng folder, match naming
- Self-Aware Codebase: Codebase tự document kiến thúc của nó cho AI. Recursion prevention, boundary clarity
Tại Sao 7 Lớp?
Có thể nghe phức tạp, nhưng mỗi lớp giải quyết problem riêng biệt:
- Services = retrieval & context optimization
- Agents = workflow & self-correction
- Prompts = version & type safety
- Security = risk mitigation
- Evaluation = quality assurance
- Observability = debugging & optimization
- Claude Context = AI-aware development
Loại bỏ một lớp = chấp nhận risk trong domain đó. Lớp Evaluation bỏ = không detect regression. Lớp Observability bỏ = không biết production error là gì.
Hay Nhất Là Điều Mà
Insight từ Matheus Costa, kỹ sư AI: "Phần honest là hầu hết những lớp này không liên quan gì đến model. Eval, caching, guardrails, memory. Đó là nơi công việc thực sự diễn ra khi bạn rời khỏi notebook."
Bạn có thể dùng model gồn nhất trên thế giới. Nhưng nếu caching không hoạt động? Evaluation bỏ qua? Observability mù mờ? System sẽ fail in production. Đó là sự thật mà ít AI blog post nói ra.
Kết
Chuyển LLM từ proof-of-concept sang production không phải là scale model lên. Đó là xây dựng infrastructure cho model hoạt động trong thế giới thực. 7 lớp này không phải tùy chọn — chúng là bắt buộc nếu bạn muốn system reliable, observable, cost-efficient.
Team bạn có bao nhiêu lớp hiện tại? Hãy kiểm tra: bạn có evaluation golden dataset chưa? Có observability per-stage không? Nếu không, đó là chỗ cần prioritize tiếp theo.
Đạo hữu là phàm nhân, tu tiên giả
... hay AI cào nội dung?
Tất cả nội dung tại đạo quán đều miễn phí. Đạo hữu chỉ cần nhập email của mình để đọc tiếp. Nói KHÔNG với Spam. Huỷ subcribe lúc nào đạo hữu thích.
nếu không muốn nhận newsletter thì có thể nhập mail phụ
