How do LLMs work? A hands-on guide for beginners
ChatGPT, Claude and Gemini are large language models (LLMs). This guide explains, in plain English, what is going on inside them, and every idea comes with something you can try on My Tiny Brain, a free tiny language model that you train yourself, from nothing, right in your browser.
1. The big idea: an LLM guesses the next word
Underneath everything, a language model does one simple job: given some text, it guesses what comes next. Not a whole answer at once, just the next small piece. It adds that piece to the text, then guesses again, and again, until the reply is finished. A long, thoughtful-looking answer is thousands of these guesses in a row.
The model never looks anything up. Everything it "knows" is stored as patterns in a huge collection of numbers (called parameters or weights) that were tuned while it practised guessing the next word on enormous amounts of text. Big chatbots have billions of these numbers; My Tiny Brain has from a quarter of a million up to about a hundred million.
2. The oldest language model: just counting
Before neural networks, language models simply counted: "after the words once upon a, the word time came next 97 times." To write, they picked a likely next word from those counts. This is called an n-gram model, where n is how many words back it looks.
It has a built-in problem. Look back only one word and the sentences are nonsense. Look back many words and it can only copy sentences it has already seen, because longer word sequences are almost never repeated. Modern LLMs were invented to escape this trade-off.
3. Tokens: how an LLM reads text
LLMs don't read letters or whole words. They read tokens: chunks of text such as the, ing or fri. Common words become one token; rare words are spelled out of several. The list of tokens is built before training with a method called byte-pair encoding (BPE): start with single letters, find the pair that appears side by side most often (say t + h), glue it into a new token (th), and repeat thousands of times.
Why bother? With whole words, every word needs its own slot and any new word, name or typo is a complete blank. With tokens, a fixed list of a few tens of thousands of pieces can spell any word, even made-up ones. This is also why chatbots are priced and limited "per token" rather than per word.
4. Embeddings: turning words into numbers
A neural network can only do arithmetic, so each token is turned into a list of numbers called an embedding. At first these numbers are random. During training they are nudged so that tokens used in similar ways end up with similar numbers. Nobody tells the model that Monday is like Tuesday, or that he is like she: it discovers it, because those words appear in similar places.
You can picture an embedding as a position on a map of meaning. Real LLMs use maps with thousands of directions; we can squash them down to two or three to look at them.
5. How a neural network learns
A neural network is a big recipe of multiplications and additions that turns the embeddings of the text so far into a score for every possible next token. Learning works like this:
- Take a bit of real text and hide the next word.
- Let the network guess. Measure how wrong it was (this measure is called the loss).
- Work out, for every single number in the network, which way to nudge it to make that guess a little less wrong (this is backpropagation), and nudge them all a tiny bit (gradient descent).
- Repeat millions of times.
No rule of grammar is ever written in. Everything comes from these tiny nudges adding up.
6. Attention and the transformer
The breakthrough behind ChatGPT came in 2017 with the transformer, introduced in a research paper famously titled Attention Is All You Need. Its central trick is attention: for each token, the model looks back over all the earlier tokens and decides which ones matter right now. To continue "The king said that heβ¦", it's useful to look back at "king", not at "that".
A transformer is built from layers. Each layer does one round of "look back with attention, then think about what you found", and each layer has several attention heads that look for different things at the same time. Stacking more layers lets the model build up more complex understanding. GPT-style chatbots are this same design, just far bigger: more layers, longer embeddings and a much longer window of text they can see at once (the context window).
Remarkably, nobody programs what each head should look for. One head ends up tracking the previous word, another follows names, another punctuation, all on its own, through training.
7. How ChatGPT and Claude are trained
Chatbots are made in stages:
- Pre-training. The model practises next-word prediction on a vast amount of text: books, websites, code. This is where it picks up language, facts and reasoning patterns. It is by far the most expensive stage.
- Fine-tuning. It practises on examples of questions and good answers, so it learns to behave like an assistant instead of just continuing text.
- Learning from feedback. People (and other models) rate its answers, and it is nudged towards the kinds of replies that were rated well. This is often called RLHF, reinforcement learning from human feedback, and it's what the thumbs-up and thumbs-down buttons in chat apps feed into.
A real risk when teaching a model something new is catastrophic forgetting: practising one new lesson too hard can damage what it already knew. A common cure is replay: mixing each new lesson with a bit of the original practice material.
8. Overfitting: memorising instead of learning
A model can get very good at the text it practises on simply by memorising it, without learning how the language really works. That's called overfitting. To catch it, a small slice of text is held back and never practised on: the test set. If the model keeps improving on its practice text but gets worse on the test text, it has started memorising.
The cures are more and more varied text, and a model that isn't too big for the amount of text it has. Model size and data have to grow together: a huge model with little to read just learns it by heart.
9. Temperature and top-p: how a reply is chosen
The model's output for each step is a list of probabilities, one per token. The chatbot still has to pick one. Temperature decides how adventurous that pick is: low temperature almost always takes the favourite (safe but repetitive), high temperature takes long shots more often (varied but error-prone). Top-p only allows the most likely tokens that together add up to, say, 90% of the probability, cutting off the weird tail. These settings change how a model sounds without changing anything it knows.
10. Why AI needs graphics cards (GPUs)
Almost all the work in a transformer is multiplying big grids of numbers. A graphics card has thousands of small cores that can all do the same multiplication at once, which is exactly what that needs. That's why AI companies buy GPUs by the tens of thousands. My Tiny Brain uses your own graphics card through WebGPU in Chrome or Edge, so its larger sizes, up to about the size of 2019's GPT-2, can train in a browser tab.
11. Why LLMs make things up ("hallucinations")
Because a language model is trained to produce a likely-sounding next word, not a checked fact, it can write fluent sentences that are simply wrong. Where its training text was thin or contradictory, the most plausible-sounding continuation may be invented. Big chatbots reduce this with more training, feedback and tools such as web search, but the underlying mechanism is the same next-word guess you can watch in a tiny model.
12. Glossary of AI words
- LLM (large language model)
- A neural network trained on huge amounts of text to predict the next token. ChatGPT, Claude and Gemini are built on LLMs.
- Token
- A chunk of text the model reads and writes: a word or part of a word.
- Tokenizer / BPE
- The method that splits text into tokens. Byte-pair encoding builds tokens by repeatedly gluing together the most common neighbouring pieces.
- Embedding
- The list of numbers that stands for a token. Similar words end up with similar numbers.
- Parameters / weights
- All the numbers inside the model that training adjusts.
- Attention
- How a transformer decides which earlier tokens to look back at when choosing the next one.
- Transformer
- The neural network design, built from layers of attention, used by modern LLMs.
- Context window
- How many tokens back the model can see at once.
- Loss
- A score for how wrong the model's guesses are. Lower is better.
- Pre-training / fine-tuning
- Practising on lots of general text, then on examples of the behaviour you want.
- Overfitting
- Memorising the practice text instead of learning general patterns.
- Temperature / top-p
- Settings that control how adventurous the choice of each next token is.
- n-gram model
- An old kind of language model that counts which word follows the previous n words.
- RLHF
- Reinforcement learning from human feedback: improving a model using people's ratings of its replies.
The best way to understand an LLM is to build one.
My Tiny Brain is free, runs entirely in your browser, and keeps everything you add on your own computer.