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

  1. 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.
  2. 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.
  3. 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.
  4. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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

  1. 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.
  2. 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

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.
  7. 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.