LLM 101

Mô hình ngôn ngữ chỉ làm một việc: đoán token kế tiếp.

A language model only does one thing: it predicts the next token.

Câu trên đã được cắt đúng như model GPT-4o nhìn thấy: 18 token. Bản tiếng Anh cùng ý chỉ tốn 14.

The sentence above is split exactly as GPT-4o sees it: 14 tokens. The Vietnamese version of the same idea takes 18.

Mọi thứ khác — viết code, trả lời, "suy nghĩ", bịa chuyện — là hệ quả của việc đó. Tám khái niệm dưới đây, đọc theo thứ tự, biến prompt, RAG và agent thành thứ suy luận được thay vì học thuộc.

Everything else — writing code, answering, "thinking", making things up — follows from that. The eight concepts below, read in order, turn prompting, RAG and agents into things you can reason about instead of memorize.

Tự tay chọn token kế tiếp

Model đưa ra xác suất; bạn quyết định cách bốc. Kéo temperature và top-p rồi bốc thử vài lần.

Sáng nào tôi cũng uống một ly

    Điểm số là số minh hoạ. Công thức thật: chia điểm cho temperature, softmax, cắt theo top-p, bốc thăm.

    Pick the next token yourself

    The model gives probabilities; you decide how to draw. Move temperature and top-p, then sample a few times.

    Every morning I drink a cup of

      Scores are illustrative. The real recipe: divide scores by temperature, softmax, cut by top-p, draw.

      Trước hết: có thể bạn không cần bài nàyFirst: you may not need this lesson

      Nếu chỉ dùng chatbot để viết nháp, tóm tắt thứ bạn tự đọc lại được, hay hỏi ý tưởng, ba quy tắc dưới đây thay thế toàn bộ trang này. Bạn cần phần còn lại khi trả tiền theo token, xây sản phẩm trên API, hoặc phải giải thích vì sao đầu ra lạ.

      If you only use a chatbot to draft, summarize things you can re-read, or brainstorm, the three rules below replace this whole page. You need the rest when you pay per token, build on an API, or have to explain strange output.

      • Đưa đủ ngữ cảnh vào.Give it enough context.Model không biết những gì bạn không viết ra.The model does not know anything you did not write down.
      • Kiểm tra mọi tên, số, trích dẫn, link.Check every name, number, quote and link.Văn trôi chảy không chứng minh điều gì.Fluent prose proves nothing.
      • Câu trả lời dở thì hỏi lại khác đi, hoặc mở chat mới.If an answer is bad, rephrase or start a new chat.Đừng cãi nhau với nó trong một cuộc hội thoại đã dài.Do not argue with it inside an already long thread.

      Đầu ra lạ? Tìm nguyên nhân trong 30 giâyWeird output? Find the cause in 30 seconds

      Đây là phần bạn sẽ dùng hằng ngày. Tìm triệu chứng, nhảy tới khái niệm, áp dụng cách sửa.

      This is the part you will use daily. Find the symptom, jump to the concept, apply the fix.

      • Sai tên, số, ngày, trích dẫn, link, tên hàmWrong names, numbers, dates, quotes, links, function names08 HallucinationĐưa nguồn vào context, đòi trích dẫn, kiểm chứng bên ngoàiPut sources in context, require citations, verify externally
      • Sai ở mức ký tự: đếm chữ, đảo chuỗi, gieo vầnCharacter-level errors: counting letters, reversing, rhymes01 TokenGiao cho code hoặc toolHand it to code or a tool
      • Ban đầu đúng, sau đó quên quy tắc hoặc bỏ sót tài liệuRight at first, then forgets rules or misses documents04 ContextChỉ dẫn ở đầu và cuối, tóm tắt lịch sử, mở chat mớiInstructions at start and end, summarize history, new chat
      • Cùng prompt mà mỗi lần một kiểuSame prompt, different result each time05 SamplingCần một đáp án đúng: hạ temperature, chạy vài lần rồi so khớpNeed one right answer: lower temperature, run several times and compare
      • Sai ở bước giữa của toán, logic, code nhiều bướcFails mid-way through math, logic, multi-step code07 ReasoningBật suy luận hoặc dùng reasoning modelEnable thinking or use a reasoning model
      • Đổi ý ngay khi bạn phản bác, khen quá đàFlips the moment you push back, over-praises06 RLHFHỏi trung tính, yêu cầu nêu cả lập luận ngượcAsk neutrally, request the counter-argument
      • Thêm câu dẫn thừa, sai định dạng, bị cắt cụtExtra lead-in, wrong format, cut off02 Next tokenVí dụ định dạng, structured output, kiểm tra stop_reasonFormat examples, structured output, check stop_reason
      • Hoá đơn hoặc độ trễ cao bất thườngUnusually high bill or latency01 + 04Đo token thật, chuẩn hoá NFC, cắt lịch sử, giới hạn đầu raMeasure real tokens, normalize NFC, trim history, cap output

      Tám khái niệm, theo thứ tựEight concepts, in order

      Mỗi khái niệm dựa trên khái niệm trước nó, nên số thứ tự là thứ tự đọc. Chi tiết đầy đủ nằm trong hai file Markdown đi kèm.

      Each concept builds on the previous one, so the numbers are the reading order. Full detail lives in the two accompanying Markdown files.

      EN11 GPT-244 GPT-430 GPT-4o16

      Cùng một câu: số token bản tiếng Anh, rồi bản tiếng Việt qua ba thế hệ tokenizer.Same sentence: English token count, then Vietnamese across three tokenizer generations.

      01

      Token và tokenizerTokens and tokenizers

      Biểu diễnRepresentation

      Model không đọc chữ mà đọc các mảnh văn bản đã đánh số. Tiếng Việt bị cắt vụn hơn tiếng Anh, mức chênh tuỳ tokenizer.Models read numbered chunks of text, not characters. Vietnamese gets chopped finer than English, by a margin that depends on the tokenizer.

      Dùng choUse it for
      Ước chi phí, chọn model, hiểu lỗi đếm ký tựEstimating cost, choosing a model, explaining character errors
      ĐượcGain
      Từ vựng hữu hạn biểu diễn mọi văn bản; chuỗi ngắn hơn ~4 lần so với cắt theo ký tựA finite vocabulary covers any text; sequences ~4× shorter than characters
      MấtLose
      Tiếng Việt tốn ×1,1–4; model mù ký tự; văn bản NFD tốn gấp 2–2,5 lầnVietnamese costs ×1.1–4; the model is blind to characters; NFD text costs 2–2.5× more

      Chọn tokenizer theo mảnh từ là đổi sự công bằng giữa các ngôn ngữ và khả năng nhìn từng ký tự lấy chuỗi ngắn hơn, nghĩa là model nhanh và rẻ hơn.Choosing subword tokenization trades fairness across languages and character-level visibility for shorter sequences, which means a faster, cheaper model.

      Dấu hiệu nhận biết:Telltale sign: lỗi nằm ở cấp ký tự, hoặc hoá đơn cao hơn dự toán tính theo số từ.errors at the character level, or a bill higher than a per-word estimate.

      ngữ cảnhcontext model token vừa chọn nối vào cuối chosen token appended
      02

      Dự đoán token kế tiếpNext-token prediction

      Lúc chạyRuntime

      Mỗi lượt chạy chỉ cho ra xác suất của đúng một token tiếp theo. "Viết" là lặp lại vòng này cho đến khi gặp token kết thúc.Each pass yields probabilities for exactly one next token. "Writing" is repeating that loop until an end token.

      Dùng choUse it for
      Thiết kế prompt, ép định dạng, tối ưu độ trễPrompt design, forcing formats, cutting latency
      ĐượcGain
      Mục tiêu huấn luyện đơn giản, một cơ chế cho mọi tác vụ, stream đượcA simple training objective, one mechanism for every task, streamable
      MấtLose
      Đầu ra tuần tự nên chậm, đắt gấp vài lần đầu vào (≈4–8 lần); không quay lại sửa đượcSequential output is slow and costs several times input (≈4–8×); no going back to fix

      Chọn sinh tự hồi quy từng token là đổi tốc độ sinh và khả năng quay lại sửa lấy một mục tiêu huấn luyện đơn giản, mở rộng được lên hàng nghìn tỷ token.Choosing token-by-token generation trades speed and the ability to revise for a simple objective that scales to trillions of tokens.

      Dấu hiệu nhận biết:Telltale sign: câu mở đầu sai và cả đoạn sau xây tiếp trên nó.a wrong opening sentence, with everything after built on it.

      mèocat bàntable nóit

      Độ dày cung là mức chú ý. "nó" dồn chú ý vào "mèo", gần như bỏ qua "bàn".Arc weight is attention. "it" focuses on "cat" and nearly ignores "table".

      03

      Attention

      Lúc chạyRuntime

      Mỗi token nhìn lại mọi token phía trước và quyết định nên lấy thông tin từ đâu, nhiều hay ít.Each token looks back at every earlier token and decides where to pull information from, and how much.

      Dùng choUse it for
      Đặt chỉ dẫn trong prompt dài, gọi tên tham chiếu rõ ràngPlacing instructions in long prompts, naming references explicitly
      ĐượcGain
      Nối hai vị trí bất kỳ trong một bước; huấn luyện song song đượcLinks any two positions in one step; trains in parallel
      MấtLose
      Chi phí tăng theo bình phương độ dài; thông tin ở giữa dễ bị bỏ sótCost grows with the square of length; the middle gets missed

      Chọn attention là đổi chi phí tăng theo bình phương độ dài lấy khả năng nối trực tiếp bất kỳ hai vị trí nào chỉ trong một bước.Choosing attention trades a cost that grows with the square of length for direct links between any two positions in one step.

      Dấu hiệu nhận biết:Telltale sign: prompt ngắn chạy đúng, dán thêm tài liệu dài thì model bỏ qua yêu cầu.a short prompt works, but add a long document and requirements get ignored.

      model chỉ thấy phần này all the model can see cũ hơn → bị cắtolder → dropped mới nhấtnewest
      04

      Context window

      Biểu diễnRepresentation

      Toàn bộ bộ nhớ làm việc của model trong một lần gọi, tính cả đầu vào lẫn đầu ra. Nằm ngoài cửa sổ là không tồn tại.The model's entire working memory for one call, input and output combined. Outside the window does not exist.

      Dùng choUse it for
      Lập ngân sách token, chat nhiều lượt, quyết định có cần RAGToken budgets, multi-turn chat, deciding on RAG
      ĐượcGain
      Cách nhanh nhất để model biết thông tin mới, không cần huấn luyện lạiThe fastest way to give the model new information, no retraining
      MấtLose
      Model không có trạng thái; chi phí cộng dồn mỗi lượt; chất lượng tụt trước khi chạm trầnStateless model; cost compounds each turn; quality drops before the limit

      Chọn nhồi thêm nội dung vào context là đổi chi phí, độ trễ và độ tập trung của model lấy việc nó có nhiều tư liệu để dựa vào hơn.Choosing to put more into the context trades cost, latency and focus for more material to rely on.

      Dấu hiệu nhận biết:Telltale sign: sau vài chục lượt, model vi phạm quy tắc đặt ở tin nhắn đầu tiên.after a few dozen turns, the model breaks rules set in the first message.

      top-p được bốceligible đuôi bị cắttail cut

      Temperature đổi chiều cao các cột; top-p đặt vạch cắt. Thử ở bảng phía trên trang.Temperature reshapes the bars; top-p sets the cutoff. Try it at the top of the page.

      05

      Sampling: temperature, top-p

      Lúc chạyRuntime

      Bước chọn ra một token cụ thể từ phân phối. Temperature làm phân phối nhọn hoặc phẳng; top-p cắt bỏ phần đuôi.The step that picks one concrete token from the distribution. Temperature sharpens or flattens it; top-p cuts the tail.

      Dùng choUse it for
      Thấp cho trích xuất và code; vừa cho văn viết; nhiều mẫu để so khớpLow for extraction and code; moderate for prose; multiple samples to compare
      ĐượcGain
      Một núm vặn giữa ổn định và đa dạng, gần như không tốn thêm tiềnOne knob between stability and variety, at almost no cost
      MấtLose
      Không sửa được kiến thức sai; T = 0 vẫn chưa chắc tất địnhCannot fix wrong knowledge; T = 0 is still not guaranteed deterministic

      Chọn temperature cao là đổi tính ổn định và khả năng tái lập kết quả lấy sự đa dạng và bất ngờ.Choosing a high temperature trades stability and reproducibility for variety and surprise.

      Dấu hiệu nhận biết:Telltale sign: cùng prompt, cùng model, mỗi lần chạy ra một kết quả.same prompt, same model, a different result on every run.

      pretrain SFT RLHF/RL lượng dữ liệu (thang log, minh hoạ)data volume (log scale, illustrative) kiến thức → vai trợ lý → hành viknowledge → assistant role → behavior
      06

      Ba giai đoạn huấn luyệnThe three training stages

      Huấn luyệnTraining

      Pretrain trên hàng chục nghìn tỷ token cho kiến thức; SFT dạy vai trợ lý; RLHF và RL dạy điều gì được ưa chuộng và điều gì đúng.Pretraining on tens of trillions of tokens gives knowledge; SFT teaches the assistant role; RLHF and RL teach what is preferred and what is correct.

      Dùng choUse it for
      Chọn base hay instruct, quyết định RAG hay fine-tune, hiểu thói nịnhBase vs instruct, RAG vs fine-tuning, understanding sycophancy
      ĐượcGain
      Tách kiến thức khỏi cách cư xử; InstructGPT 1,3B được ưa hơn GPT-3 175BKnowledge separated from behavior; 1.3B InstructGPT preferred over 175B GPT-3
      MấtLose
      Kiến thức đóng băng; xu hướng nịnh, dài dòng; tự tin lệch khỏi độ đúngFrozen knowledge; flattery and verbosity; confidence drifts from accuracy

      Chọn thêm hậu huấn luyện là đổi một phần độ đa dạng và độ khớp giữa tự tin với độ đúng của base model lấy một trợ lý biết nghe lời, dễ dùng và an toàn hơn.Choosing post-training trades some of the base model's diversity and calibration for an assistant that follows instructions and is easier and safer to use.

      Dấu hiệu nhận biết:Telltale sign: bạn phản bác và model đổi ý ngay dù nó đang đúng.you push back and the model flips even though it was right.

      prompt nháp suy nghĩ (tính tiền)thinking draft (billed) trả lờianswer mỗi token nháp = thêm một lượt tính toáneach draft token = one more pass
      07

      Reasoning model

      Huấn luyệnTraining

      LLM được huấn luyện bằng RL trên bài có đáp án kiểm chứng được, để viết nháp suy nghĩ dài trước khi trả lời — tức mua thêm lượt tính toán.An LLM trained with RL on verifiable problems to write a long thinking draft before answering — buying extra passes of computation.

      Dùng choUse it for
      Toán, logic nhiều bước, gỡ lỗi, đối chiếu tài liệuMath, multi-step logic, debugging, cross-referencing
      ĐượcGain
      Chính xác hơn rõ rệt ở bài nhiều bước, không cần model lớn hơnClearly more accurate on multi-step problems, without a bigger model
      MấtLose
      Chờ vài giây đến vài phút; token nghĩ tính tiền; không thêm kiến thứcSeconds to minutes of waiting; thinking tokens are billed; adds no knowledge

      Chọn reasoning model là đổi thời gian chờ và nhiều token đầu ra lấy độ chính xác cao hơn trên bài toán nhiều bước.Choosing a reasoning model trades waiting time and many output tokens for higher accuracy on multi-step problems.

      Dấu hiệu nhận biết:Telltale sign: model sai ở bước trung gian chứ không phải vì thiếu kiến thức.the model fails at intermediate steps, not from missing knowledge.

      đúngtrue bịamade up đã thấy nhiều lầnseen often hiếm gặp, vẫn phải chọnrare, must still pick cùng một giọng tự tinsame confident tone
      08

      Vì sao LLM bịaWhy LLMs hallucinate

      Hệ quảConsequence

      Khi thiếu kiến thức, model vẫn phải chọn một token, và nó chọn thứ nghe giống câu trả lời. Đây là hệ quả của mục 02 và 06, không phải lỗi phần mềm.When knowledge is missing, the model must still pick a token, so it picks what sounds like an answer. A consequence of 02 and 06, not a software bug.

      Dùng choUse it for
      Mọi câu trả lời chứa tên, số, ngày, trích dẫn, linkAny answer containing names, numbers, dates, quotes, links
      ĐượcGain
      Cùng cơ chế này tạo ra khả năng khái quát và sáng tạoThe same mechanism produces generalization and creativity
      MấtLose
      Giọng văn không báo độ tin cậy; hỏi "có chắc không?" không phải phép kiểm traTone does not signal reliability; "are you sure?" is not a check

      Chọn để model luôn đưa ra câu trả lời là đổi độ tin cậy lấy độ phủ; ép nó biết nói "không biết" thì phải chấp nhận nó từ chối nhiều hơn.Choosing to have the model always answer trades reliability for coverage; making it say "I don't know" means accepting more refusals.

      Dấu hiệu nhận biết:Telltale sign: câu trả lời có chi tiết rất cụ thể mà bạn chưa hề đưa vào prompt.the answer contains very specific details you never put in the prompt.

      Núm vặn nào nằm trong tay bạnWhich knobs are in your hands

      Đọc theo cột cuối: đòn bẩy rẻ và mạnh nhất gần như luôn là nội dung bạn đưa vào context, không phải tham số.

      Read the last column: your cheapest, strongest lever is almost always what you put into the context, not the parameters.

      Khái niệmConcept Ai điều khiểnWho controls it Ảnh hưởng chi phíCost impact Ảnh hưởng độ đúngCorrectness impact Đòn bẩy của bạnYour lever
      TokenNhà cung cấpProviderCao với tiếng ViệtHigh for non-EnglishThấp, trừ tác vụ cấp ký tựLow, except character tasksChọn model, chuẩn hoá NFCModel choice, NFC normalization
      Token kế tiếpNext tokenCố địnhFixedĐầu ra đắt gấp vài lần đầu vào (≈4–8 lần)Output costs several times input (≈4–8×)Gốc rễ của mọi hành viRoot of all behaviorÉp định dạng, giới hạn độ dàiFormat forcing, length caps
      AttentionNhà cung cấpProviderTăng theo độ dài đầu vàoGrows with input lengthGiảm khi context dài, rốiDrops with long, messy contextVị trí và cấu trúc promptPrompt placement and structure
      Context windowBạn chọn nội dungYou choose the contentCao, cộng dồn qua các lượtHigh, compounds over turnsCao: đưa đúng thông tin vàoHigh: the right info goes inChọn lọc, tóm tắt, RAGSelection, summaries, RAG
      SamplingBạnYouKhông đáng kểNegligibleTrung bình: ổn định hay đa dạngMedium: stability vs varietytemperature, top-p
      Pretrain → SFT → RLNhà cung cấpProviderKhông trực tiếpNot directlyQuyết định trần kiến thức và tính cáchSets knowledge ceiling and personalityChọn model, fine-tuneModel choice, fine-tuning
      ReasoningBạn chọn có dùngYou decide to use itCao: token suy nghĩHigh: thinking tokensCao với bài nhiều bướcHigh on multi-step problemsBật/tắt, ngân sách suy nghĩOn/off, thinking budget
      HallucinationKhông ai tắt đượcNobody can switch it offChi phí kiểm chứngCost of verificationChính là rủi ro độ đúngIt is the correctness riskGrounding, kiểm chứng ngoàiGrounding, external checks

      Sai lầm thường gặpCommon mistakes

      Mỗi dòng là một hiểu lầm về cơ chế, kèm cách sửa dựa trên cơ chế đúng.

      Each row is a misunderstanding of the mechanism, with a fix based on the real one.

      Sai lầmMistakeVì sao saiWhy it is wrongSửa thế nàoFix
      Hỏi "bạn có chắc không?"Asking "are you sure?"Model sinh câu trả lời hợp lý cho câu hỏi mới, không tra lại sự thật; có thể đổi đúng thành saiIt generates a plausible reply to the new question without re-checking; may turn right into wrongKiểm chứng bằng nguồn ngoài, công cụ, chạy codeVerify with external sources, tools, running code
      Temperature = 0 để hết bịaTemperature 0 to stop hallucinationTemperature không thêm kiến thức; T = 0 chỉ làm nó bịa nhất quánTemperature adds no knowledge; T = 0 just hallucinates consistentlyGrounding bằng nguồn, cho phép "không biết"Ground with sources, allow "I don't know"
      Ước chi phí theo số từEstimating cost by word countTiếng Việt tốn ×1,1–4 so với tiếng Anh tuỳ tokenizerVietnamese costs ×1.1–4 vs English depending on tokenizerĐếm bằng tokenizer thật trên dữ liệu thậtCount with the real tokenizer on real data
      Dán cả tài liệu vì context đủ lớnPasting everything because the window is bigĐắt, chậm, phần giữa dễ bị bỏ sótCostly, slow, and the middle gets missedLấy đúng đoạn liên quan; chỉ dẫn ở đầu và cuốiRetrieve relevant passages; instructions at start and end
      Tin model nhớ hôm quaBelieving it remembers yesterdayModel không có trạng thái, chỉ thấy lần gọi nàyStateless; it sees only this callỨng dụng tự lưu và gửi lạiThe app stores and resends
      Bắt đếm ký tự, cắt chuỗiAsking for exact character workModel thấy token, không thấy ký tựIt sees tokens, not charactersDùng code hoặc toolUse code or a tool
      Coi chuỗi suy nghĩ là lời giải thích thậtTrusting the chain of thought as the real reasonKhông chắc phản ánh quá trình bên trongIt may not reflect the internal processKiểm tra kết quả cuốiCheck the final result
      Reasoning model cho mọi việcReasoning model for everythingChậm, đắt gấp nhiều lần, không hơn ở việc đơn giảnMany times slower and costlier, no gain on simple tasksModel nhanh làm mặc định, nâng cấp khi cầnFast model by default, escalate when needed
      Fine-tune để model biết tài liệuFine-tuning to teach documentsFine-tune dạy hình thức tốt hơn dạy sự thậtFine-tuning teaches form better than factsRAG trước, fine-tune cho định dạng và giọng vănRAG first; fine-tune for format and voice
      Hỏi tin tức, phiên bản mới nhấtAsking about latest news or versionsKiến thức đóng băng ở knowledge cutoffKnowledge is frozen at the cutoffCấp công cụ tìm kiếm hoặc đưa vào contextProvide search or put it in context

      Ba câu cần nhớThree things to remember

      1. LLM chỉ đoán token kế tiếp theo xác suất. "Hợp lý" và "đúng" trùng nhau phần lớn thời gian, nhưng không phải luôn luôn.An LLM only predicts the next token by probability. "Plausible" and "true" coincide most of the time, but not always.
      2. Model chỉ biết những gì nằm trong tham số và trong context window ngay lúc đó. Muốn nó biết điều gì, đưa điều đó vào context.A model knows only what is in its parameters and its context window right now. To make it know something, put it in the context.
      3. Văn trôi chảy không phải bằng chứng. Mọi sự thật quan trọng phải được kiểm chứng bằng nguồn bên ngoài model.Fluent prose is not evidence. Every important fact must be verified against a source outside the model.