Inside a Language Model
A hands-on visual guide to how LLMs work, from tokens and attention to training and their limits
ChatGPT, Claude and Gemini can feel like they think. Underneath, each one is doing a single job over and over: reading some text and guessing what comes next.
This guide follows a piece of text through the machine, one stage at a time, and explains the terms along the way. Itâs written for anyone curious, no maths or coding background needed. The figures are interactive, so poke at them.
Every answer an LLM writes is built from guesses like this, one piece at a time.
What âlarge language modelâ means
A Large Language Model (LLM) is a computer program that has learned patterns in human language. Language because text is what it reads and writes. Model because it is a simplified, mathematical imitation of something real, in this case how words tend to follow each other. Large because it has billions of adjustable numbers inside it and learned from a huge slice of the internet, books and code.
An LLM is a kind of neural network: a big web of simple maths operations, loosely inspired by brain cells, whose behaviour is set by numbers called parameters. Nobody writes rules like âafter sat on the, say matâ. The model works those patterns out for itself during training. The sections below follow the order things happen when you send a message.
1. Text becomes tokens
Computers only do maths on numbers, so step one is turning your text into numbers. The model doesnât use letters or whole words. It uses tokens: chunks of text that are often a whole common word, part of a longer word, a space plus a word, or a single punctuation mark.
A program called the tokenizer does the chopping. It has a fixed list of every token it knows, called the vocabulary, usually somewhere between 50,000 and 200,000 entries. Each token has an ID number. Common words get their own token; rare words get built from pieces.
This demo uses simple rules to imitate a real tokenizer, so the exact splits and ID numbers are made up. Real ones learn their chunks from data with a method called byte-pair encoding, but the effect looks much like this. In English, one token averages about 4 characters, or ž of a word.
Notice the space usually sticks to the front of the next word, and that strawberry may be split into pieces. The model never sees the individual letters r-r-r, which is one reason LLMs have famously struggled with âhow many râs are in strawberry?â (more on that in section 8).
2. Tokens become points in space
A token ID like 9,281 is just a label; the number itself means nothing. So the model swaps each ID for a long list of numbers called an embedding. A list of numbers like this is called a vector, and you can think of it as coordinates: a location in space.
The trick is that tokens with similar meanings end up near each other. Nobody places them by hand. The positions are learned during training, because putting cat near kitten helps the model make better predictions.
Real embeddings donât have 2 coordinates, they have thousands. Each coordinate is called a dimension. You canât picture 4,000-dimensional space (nobody can), but the maths works the same way as on this flat map. Directions carry meaning too: the step from man to woman points roughly the same way as the step from king to queen.
3. Attention: words look at other words
A single wordâs meaning depends on its neighbours. âBankâ means something different in âriver bankâ and âbank accountâ. LLMs handle this with a mechanism called attention. At each position, the model asks âwhich earlier words matter for understanding this one?â and blends in information from them.
The design that uses attention is called the transformer, invented by Google researchers in 2017. It is the T in GPT (Generative Pre-trained Transformer). Almost every modern LLM is a transformer.
Faded words come later in the sentence. In a model like GPT or Claude, each word can only look backward, because when the text is being written, later words don't exist yet.
What does âitâ refer to? Read on its own, âitâ could be the animal or the street, so the model splits its attention. Once it reaches tired, it can resolve the puzzle: animals get tired, streets donât. Switch to wide and the attention flips to street. Real models run many attention patterns side by side (called attention heads), each tracking different relationships: grammar, who-did-what, rhyme, code brackets and much more.
Layers
The model is built as a tall stack of identical blocks called layers. Each layer has an attention step (words share information) and a feed-forward step (each wordâs vector gets processed on its own, which is where a lot of stored knowledge seems to live).
Early layers tend to pick up simple things like grammar and word pieces. Deeper layers build more abstract ideas: topic, tone, whether a statement is true, what the writer is trying to do. Big models have dozens to over a hundred layers. This is where the âdeepâ in deep learning comes from: many layers stacked deep.
4. Prediction: a score for every token
At the top of the stack, the model produces a score for every single token in its vocabulary, tens of thousands of them. These raw scores are called logits. A function called softmax turns them into percentages that add up to 100%. That list of percentages is a probability distribution.
Then the model has to pick one. Choosing randomly, weighted by those percentages, is called sampling. A setting called temperature controls how adventurous the choice is.
Low temperature (near 0) sharpens the distribution, so the top choice almost always wins. You get safe, predictable, repetitive text, which is good for maths or facts. High temperature flattens it, so unlikely tokens get picked more. You get more creative and surprising text, and at very high values, nonsense. This is why asking the same question twice can give different answers.
5. Generation: one token at a time, on repeat
Predicting one token isnât an answer. So the model runs a loop: predict a token, stick it on the end of the text, then feed the whole text back in and predict again. Generating text by feeding your own output back in is called being autoregressive. Itâs why chatbot replies appear word by word: youâre watching the loop run.
The highlighted bar is the token chosen this round. Generation stops when the model predicts a special end-of-text token, or hits a length limit.
Everything the model can see at once (your message, the earlier conversation, its own reply so far) is called the context. The maximum amount it can hold is the context window, measured in tokens. Early models managed a couple of thousand tokens; recent ones handle hundreds of thousands or more, enough for several novels. Anything outside the window, the model simply cannot see.
Running a trained model to get answers is called inference. Each token needs a pass through every layer, billions of multiplications, which is why LLMs run on GPUs: chips originally built for video games that are very good at doing huge amounts of maths at once.
6. Training: learning from mistakes, billions of times
A brand-new model has random parameters and outputs gibberish. It gets good through pretraining: it is shown enormous amounts of text, its training data, and plays the same game over and over. Hide the next token, let the model guess, check the real answer.
The size of each mistake is measured by a number called the loss. If the model gave the right answer a high probability, the loss is small. If it was confidently wrong, the loss is big. A method called gradient descent then works out, for every one of the billions of parameters, which tiny nudge (up or down) would have made the loss smaller, and applies it. Repeat this trillions of times.
Loss here is âlog(probability of the correct answer). A perfect guess scores 0.
Nobody teaches the model grammar, geography or how to write code. To predict text well, it has to soak those up, because knowing that Paris is the capital of France makes âThe capital of France isâ easier to complete. Thatâs the surprising core idea: getting very good at predicting the next word forces a model to learn a lot about the world.
How big is âlargeâ? Here are some publicly reported figures:
| Model | Year | Parameters | For scale |
|---|---|---|---|
| GPT-1 | 2018 | 117 million | Could finish a sentence, sort of |
| GPT-2 | 2019 | 1.5 billion | Wrote convincing paragraphs |
| GPT-3 | 2020 | 175 billion | Storing it takes ~350 GB |
| Todayâs frontier models | 2023+ | not disclosed | Widely believed to be larger still |
Training a frontier model takes thousands of GPUs running for months and is fed trillions of tokens of text. If you read nonstop, it would take you tens of thousands of years to read that much.
7. From text-predictor to helpful assistant
A model straight out of pretraining is called a base model. Itâs a brilliant mimic but not a helpful one. Ask it âWhatâs the capital of France?â and it might continue with âWhatâs the capital of Germany? Whatâs the capital of Spain?â because it looks like a quiz list it saw online. Turning it into an assistant takes more training, in three broad stages:
- Pretraining. Predict the next token across trillions of tokens of text. The model learns language and knowledge.
- Fine-tuning. Extra training on thousands of example conversations written by people, showing good answers. The model learns the assistant format: question in, helpful answer out.
- RLHF. The model writes several answers; people (or another model) rate them; the model is nudged toward the better ones.
RLHF stands for Reinforcement Learning from Human Feedback. Reinforcement learning means learning from rewards rather than from exact right answers, a bit like training a dog with treats. This stage is also where models learn to be honest about uncertainty, to refuse harmful requests and to have a consistent personality.
Hereâs the part most people donât realise: even a chat is just one long piece of text to the model. Your conversation gets stitched together with special marker tokens, and the model predicts what the assistant says next. It looks something like this (the marker names vary by model):
<system> You are a helpful assistant. Today's date is 19 November 2026.
<user> Why is the sky blue?
<assistant> Sunlight contains every colour. When it hits the air, tiny gas
molecules scatter short blue wavelengths much more than red ones, so blue
light reaches your eyes from every direction in the sky.<end>
The assistantâs reply is generated one token at a time using the loop from section 5. The hidden instructions at the top are the system prompt, written by whoever built the app. The model itself has no memory between chats: each time, the whole conversation is sent again from the start.
Modern assistants add more on top. Some are trained to âthinkâ first, writing out step-by-step working (called reasoning or chain of thought) before giving a final answer, which helps a lot with maths and logic. Many can use tools: the model writes a special message like âsearch the web for Xâ or ârun this codeâ, the app does it, and the result is pasted back into the context. A model that uses tools in a loop to complete a multi-step task is often called an agent.
8. Why LLMs get things wrong
Once you know an LLM is predicting likely text, a lot of its weird behaviour starts to make sense.
- Hallucination. The model produces text that sounds right, which isnât always the same as text that is right. When it doesnât know, it can confidently invent a fact, a quote or a source. Always check important facts.
- Knowledge cutoff. Pretraining data stops at some date. The model knows nothing after it unless the app gives it search tools or pastes the information into the context.
- Seeing tokens, not letters. Because words arrive as chunks (section 1), counting letters, spelling backwards or rhyming can trip a model up. Newer models are much better at this, often by reasoning step by step.
- Learned from people. The training data is written by humans, so it contains human mistakes, opinions and biases. The model can repeat them. Fine-tuning reduces this but canât remove it completely.
- Limited window. Very long conversations can push early details out of the context window, or the model can lose track of things buried in the middle of a long document.
- Does it understand? Scientists genuinely disagree. LLMs clearly build rich internal models of language and the world, but they work very differently from human brains. âJust predicting the next wordâ is true and still undersells what that takes.
Glossary
No term matches that search.
- Agent: an AI system that uses tools in a loop (searching, running code, clicking around) to finish a multi-step task with less hand-holding.
- Artificial intelligence (AI): the broad field of making computers do things that normally need human intelligence. LLMs are one kind of AI.
- Attention: the mechanism that lets each token pull in information from other (earlier) tokens, so meaning depends on context.
- Attention head: one attention pattern. Each layer runs many in parallel, each tracking a different kind of relationship.
- Autoregressive: generating output one piece at a time, feeding each new piece back in as input for the next.
- Base model: a model after pretraining only. Great at continuing text, not yet trained to follow instructions or chat.
- Context / context window: the text the model can see right now, and the maximum amount of it (in tokens) it can handle at once.
- Deep learning: machine learning with neural networks that have many layers stacked on top of each other.
- Dimension: one coordinate in a vector. Embeddings in big models have thousands of dimensions.
- Embedding: the vector (list of numbers) that represents a token. Similar meanings end up with similar embeddings.
- Feed-forward layer: the part of each transformer layer that processes each tokenâs vector individually. Much factual knowledge seems to be stored here.
- Fine-tuning: extra training on a smaller, focused dataset to adjust a pretrained model, for example to make it a helpful assistant.
- GPT: Generative Pre-trained Transformer. A model that generates text, is pretrained on lots of data and uses the transformer design.
- GPU: Graphics Processing Unit. A chip that does many calculations at once, used to train and run neural networks.
- Gradient descent: the method for improving a model: work out which direction to nudge each parameter to reduce the loss, then nudge it a little.
- Hallucination: when a model states something false or made-up with confidence, because it sounded like likely text.
- Inference: using a trained model to produce outputs (as opposed to training it).
- Knowledge cutoff: the date the training data ends. The model doesnât know about later events unless told.
- Layer: one block in the modelâs stack. Each layer refines the token vectors a bit more.
- Large Language Model (LLM): a very large neural network trained on huge amounts of text to predict the next token.
- Logits: the raw scores the model gives every token in its vocabulary before they are turned into probabilities.
- Loss: a number measuring how wrong the modelâs prediction was. Training aims to make it as small as possible.
- Machine learning: getting computers to learn patterns from examples instead of being programmed with explicit rules.
- Neural network: a system of many simple connected maths units whose behaviour is set by adjustable numbers (parameters).
- Parameters (weights): the billions of adjustable numbers inside a model. Training sets their values; they hold everything the model has learned.
- Pretraining: the first, biggest phase of training: predicting the next token across a vast amount of text.
- Probability distribution: a list of possible outcomes with a percentage for each, adding up to 100%.
- Prompt: the text you give the model. Everything it writes is a continuation of the prompt.
- Reasoning (chain of thought): when a model writes out step-by-step working before its final answer, which improves results on hard problems.
- RLHF: Reinforcement Learning from Human Feedback. Training a model toward answers people rate as better.
- Sampling: picking the next token at random, weighted by the modelâs probabilities.
- Softmax: the maths function that turns logits into probabilities that add up to 100%.
- System prompt: hidden instructions placed at the start of a conversation by the appâs makers to set the modelâs role and rules.
- Temperature: a setting that controls randomness in sampling. Low means predictable, high means creative or chaotic.
- Token: a chunk of text (word, part of a word, or symbol) that the model reads and writes. About ž of an English word on average.
- Tokenizer: the program that splits text into tokens and converts them to ID numbers, and back again.
- Tool use: when a model asks an outside program to do something (search, calculate, run code) and gets the result back in its context.
- Training data: all the text a model learns from: websites, books, code, articles and more.
- Transformer: the neural network design behind modern LLMs, built from stacked layers of attention and feed-forward steps. Introduced in 2017.
- Vector: an ordered list of numbers. Can be thought of as a point or arrow in space.
- Vocabulary: the fixed set of all tokens a tokenizer knows, each with its own ID.
The figures in this post use small hand-made numbers to show how each step behaves. A real model does the same steps with billions of learned numbers.