AI Fundamentals — How Modern AI Works›02 · How a large language model works
Learning Hub / AI Fundamentals & AI-Assisted Engineering / AI Fundamentals — How Modern AI Works

Lesson 02 of 13 · How models work

How a large language model works

How a large language model produces an answer: text is cut into tokens, the model predicts the most likely next token again and again, and each prediction considers all the text before it (attention). How it learned to do that: pre-training on huge amounts of text, then tuning to follow instructions and be helpful.

Foundation
Key wordsLLMtokentokenizernext-token predictionparameterstransformerattentionpre-trainingfine-tuninginstruction tuningRLHFreasoning models

Step 1: text becomes tokens

A model does not read letters or whole words. A tokenizer cuts text into tokens, common chunks of text, and gives each a number.

"Kubernetes restarts failing pods."
→ [ "K", "ubern", "etes", " restarts", " failing", " pods", "." ]
→ [ 42, 81721, 17829, 50323, 22109, 54163, 13 ]   (example numbers)

Common words are one token; rare words are split into pieces. A useful rule of thumb for English: one token is about four characters, or three quarters of a word. Tokens matter because models are priced, limited and slowed by token counts (lesson 3).

Step 2: predict the next token, again and again

Given the tokens so far, the model calculates a score for every token in its vocabulary (often 100,000 or more) for "what comes next". One is chosen, added to the text, and the whole thing repeats.

"The capital of France is"            → " Paris" (very likely), " a" , " the" …
"The capital of France is Paris"      → "." (very likely), "," …
"The capital of France is Paris."     → (end) …

That is the entire mechanism. A long, thoughtful answer is thousands of these small predictions in a row. This explains a lot of behaviour:

  • Answers appear word by word (streaming) because they are generated word by word.
  • The model never "looks up" a fact; it produces the text that is most likely to follow, which is usually right for well-known facts and less reliable for rare ones.
  • The same question can get different answers, because the choice of next token can include some randomness (lesson 3).

It's like the game where everyone adds one word to a story. A model has read so many stories, articles and manuals that its next word is almost always sensible. Play the game 500 times in a row and you get a whole essay.

Step 3: attention, or how it keeps track of context

Predicting the next word needs the right earlier words. In "The server ran out of disk, so it stopped", "it" means the server. The transformer design used by modern LLMs has a mechanism called attention: for every new token, the model weighs how relevant each earlier token is. That is how it follows a long conversation, matches a closing bracket, or keeps a variable name consistent.

How the model learned: three stages

1. Pre-training     huge text collection (web pages, books, code, articles)
                    task: predict the next token, trillions of times
                    result: knows language, facts, code; but just continues text
2. Instruction      thousands of examples of "instruction → good answer"
   tuning           result: answers questions instead of rambling on
3. Learning from    people (and models) rank several answers; the model
   feedback (RLHF)  is trained toward the preferred ones
                    result: helpful, polite, refuses harmful requests
  • After stage 1 alone, asking "What is DNS?" might be continued with "What is DHCP? What is NAT?", because a list of exam questions is a likely continuation. Stages 2 and 3 make it answer.
  • Reasoning models add training that rewards working through a problem step by step before answering. They are slower and use more tokens, and are better at maths, code and multi-step problems.

Bigger is not the whole story

Parameters are the learned numbers. More parameters can capture more patterns, but quality also depends heavily on the training data and tuning. A well-trained smaller model can beat an older, larger one, and smaller models are faster and cheaper (lesson 11).

Recap

  • Text is split into tokens; roughly four characters each in English.
  • An LLM only predicts the next token, repeatedly; long answers are many small predictions.
  • Attention lets each prediction take all earlier text into account.
  • Pre-training gives knowledge; instruction tuning and feedback make it a helpful assistant.

This site is a public version of my personal engineering knowledge hub. It intentionally excludes confidential company information and internal operational details.