How large language models work
Tokens, transformers, training and why context windows matter. About 1 h, intermediate level.
You'll be able to
- Describe how a large language model turns text into tokens, relates them with attention and predicts the next token.
- Explain what pre-training, fine-tuning and feedback training each give a model.
- Diagnose forgotten instructions, outdated facts and miscounted letters by linking each to tokens, the context window or the training cut-off.
Tokens: how a model reads
Before a model sees your text, a tokenizer splits it into tokens. Common words are often a single token; rarer or longer words are split into several pieces; spaces and punctuation are folded into tokens too. Each token becomes a number, and each number is then turned into an embedding: a long list of numbers the network can calculate with.
This has practical effects:
- Letters are hidden. A model might see “straw” + “berry”, not s-t-r-a-w-b-e-r-r-y, so counting letters, spelling backwards or making anagrams can trip it up.
- Limits and prices are in tokens. Context windows, output limits and API pricing are all counted in tokens, usually with input and output counted separately.
- Languages differ. Tokenizers are usually built from English-heavy text, so many other languages need more tokens to say the same thing.
The key idea: the model works with tokens, not letters or whole words.
Attention: relating tokens to each other
Modern LLMs are transformers: a stack of layers that repeatedly refine each token's embedding. The key step in each layer is attention. For every token, attention weighs how relevant each other token in the context is, and mixes in information from the ones that matter most.
Take “The trophy didn't fit in the suitcase because it was too big.” To handle “it”, the model needs to connect it to “trophy”, not “suitcase”. Attention is the mechanism that lets distant words influence each other like this. Each layer has several attention “heads”, which learn to pick up different kinds of relationships.
In a model that generates text, each position can only attend to the tokens before it, since the later ones haven't been written yet. After the final layer, the model turns the last position into a probability for every possible next token.
Transformers also process all the tokens of an input in parallel, which made training on very large datasets practical.
The key idea: attention is how any part of the input can shape the next token.
Pre-training, fine-tuning and feedback
A chat model goes through several stages of training.
- Pre-training. The model learns to predict the next token across a huge collection of text. It's self-supervised: the text itself supplies the right answers, so no one has to label it. This is where the model picks up grammar, facts, writing styles and patterns of reasoning. The result is a base model: it continues text well, but it isn't an assistant. Ask it a question and it might just continue with more questions.
- Fine-tuning. The base model is trained further on examples of instructions paired with good responses, so it learns to follow requests, use formats and hold a conversation.
- Feedback training. People (and sometimes other models) compare responses, and the model is trained toward the preferred ones. Reinforcement learning from human feedback (RLHF) is one well-known method. This makes answers more helpful and safer, but it can also reward answers that simply please the rater.
The key idea: pre-training gives knowledge and language; later training shapes behavior.
The context window
The context window is everything the model can see at once: the system instructions set by the app, the conversation so far, any files or pasted text, tool results, and the reply it is writing. It is measured in tokens, and every model has a maximum.
The model has no memory outside this window. Each reply is computed fresh from what's in it. When a conversation grows past the limit, the app has to drop, trim or summarize older material, and that's when early instructions and details quietly disappear.
Even inside the limit, long contexts aren't used evenly. Details buried in the middle of a very long input are more likely to be overlooked than those near the start or end.
Practical habits:
- Restate key instructions when a chat gets long.
- Start a fresh chat for a new task.
- Paste the relevant section rather than the whole document.
The key idea: if it isn't in the window, the model can't use it.
The training cut-off
Pre-training data is collected up to a certain date, the training cut-off. The model has no knowledge of anything after it unless a tool, such as web search or an uploaded document, places that information in the context window.
This causes quieter problems too:
- Outdated facts. A policy, software version, price or officeholder may have changed since the cut-off, and the model will state the old one confidently.
- Thin coverage near the cut-off. Events shortly before the cut-off were often written about less at the time, so the model's picture of them can be patchy.
- Guessing about the present. Asked about recent events without tools, a model may fill in something plausible instead of saying it doesn't know. It may not even be sure of its own cut-off date.
When you need something current, turn on search or give it the source, and check dates in its answer.
The key idea: for anything recent or likely to change, ground the answer in a dated source.
Why the assistant “forgot” the rule
A TA pastes a 30-page syllabus into a chat and writes: “Answer students' questions in two sentences and cite the syllabus section number.”
Early on: answers are short and cited. The instruction and the syllabus are both in the context window, and the instruction is close by.
Twenty turns later, after pasting two more long documents: answers are long and uncited. What happened?
- The conversation may have outgrown the window, so the app trimmed or summarized the oldest part, which held the instruction.
- Even if nothing was dropped, the instruction is now one short line among thousands of tokens, and long contexts aren't attended to evenly.
The fix: start a fresh chat with the instruction first, then only the syllabus, and restate the rule after the syllabus. For questions about one policy, paste just that section.
Bonus check: the TA asks “When were the latest university guidelines on AI use updated?” with search off. The model gives a plausible date. Because that date could be after its training cut-off, the TA checks the university website instead.
Try it
Notes from your context-window probe: the model's first and later answers about the article's opening paragraph, whether it still got them right after twenty turns, and what it said about an event after its training cut-off, with one sentence explaining each result.