How AI Language Models Work
Tokens, embeddings, attention, training, RAG and agents, each built in a few lines of plain Python so you can watch every number. No libraries, no black boxes.
Text as Numbers
- Tokens: Turning Text into Numbers: Language models only work with numbers. Watch a sentence become a list of token ids, and see what happens to a word the model has never seen.
- Byte-Pair Encoding: Learning Word Pieces: Real tokenizers are learned, not written by hand. Start from single letters and keep merging the most common pair, and useful pieces like "low" appear by themselves.
- Embeddings: Meaning as Numbers: Give every word a vector, and words with similar meanings end up pointing the same way. Find the word closest to "kitten" with cosine similarity.
- Positional Encoding: Where Is Each Word?: Attention looks at all the words at once, so on its own it cannot tell "dog bites man" from "man bites dog". Adding a position signal to each word fixes that.
Inside a Language Model
- Predicting the Next Word: A language model guesses the next word, again and again. Build a tiny one by counting which word follows which, then let it write a sentence.
- Softmax and Temperature: Turn a model's scores into chances with softmax, then turn the temperature down and up to watch its answers become surer or more adventurous.
- Top-k and Top-p: Choosing Safely at Random: Picking the next word at random makes text lively, but should never pick nonsense. Top-k and top-p cut off the unlikely tail before rolling the dice.
- Attention: Words Looking at Words: "Bank" means something different next to "river" than next to "money". Attention lets each word look at the others and borrow meaning from the ones that matter.
- Multi-Head Attention: Asking Several Questions: One attention head can only look for one thing. Give "chased" two heads, and one finds who did the chasing while the other finds who was chased.
- The Transformer Block: A transformer is the same block stacked many times: attention adds context, a normalising step keeps numbers steady, and a small network works on each word. Follow "bank" through one block.
- The Context Window: Why Models Forget: A model reads only a fixed number of tokens at once. As a chat grows, the oldest words fall out of the window, and the model can no longer see them.
Training a Language Model
- How a Language Model Is Trained: Show the model the real next word, measure how surprised it was (the loss), and nudge its scores. Repeat this billions of times and you have a language model.
- Fine-Tuning and RLHF: Learning to Be Helpful: A pre-trained model only continues text. To make an assistant, people compare answers and a reward model learns what they prefer. That reward then steers the chat model.
Building with Models
- Prompting: Shaping the Answer: A prompt is just text, but its shape matters: an instruction, a few worked examples, and an ending that invites exactly the answer you want.
- Tool Calling: Models Asking for Functions: The model never runs code. It writes a request in JSON: which tool, which arguments. Your program reads it, runs the function, and sends back the result.
- RAG: Look It Up, Then Answer: A model only knows what it was trained on. Retrieval-augmented generation finds the right document first and puts it in the prompt.
- Chunking: Cutting Documents for Retrieval: A long document is cut into overlapping chunks, and only the chunk that matches the question goes into the prompt.
- Agents: A Model in a Loop: An agent lets a model use tools. The model picks an action, the program runs it, the result goes back to the model, and the loop repeats until it answers.
- Evaluation: Is the Model Any Good?: You cannot improve what you do not measure. Ask the model questions with known answers, compare fairly, and count.
- Prompt Injection: When Data Gives Orders: Text your app reads (web pages, emails, files) can contain instructions aimed at the model. See why it is dangerous and the basic defences.