How large language models work

Tokens, transformers, training and why context windows matter. About 1 h, intermediate level.

You'll be able to

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:

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.

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:

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:

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