This article is free to read. Creating a lesson uses your daily or stored credits; review the language and settings before starting.
How do large language models work?
Quick answer
Large language models work by predicting the next word in a sequence. They break text into tokens, process them through many layers of a transformer neural network, and use attention to weigh context. Trained on vast text, they learn patterns and generate responses one token at a time.
Step by step
- First, the input text is split into tokens, which are common words or word pieces, and each token is mapped to a numerical ID.
- These IDs become vectors, and positional information is added so the model knows the order of tokens in the sequence.
- The vectors pass through dozens of transformer layers, where self-attention lets each token gather relevant information from all other tokens.
- After the layers, the model produces a probability distribution over the entire vocabulary for the next token.
- During training, the model's predicted distribution is compared to the actual next token, and backpropagation adjusts the weights to reduce error.
- At generation time, the model picks a token from the distribution, appends it to the input, and repeats the process to build a full response.
Key things to understand
- Text is converted into tokens (numbers); the model processes them through billions of learned parameters.
- The transformer's 'attention' mechanism weighs how each word relates to the others.
- Training adjusts the parameters to predict the next token accurately across huge datasets.
- Because they predict plausible text rather than verify facts, they can 'hallucinate' — sound right but be wrong.
Labelled diagram
- Input Text
Your prompt is received and prepared for processing.
- Tokenization
Text is split into tokens and converted to numerical IDs.
- Transformer Layers
Each token's vector is refined using self-attention and feed-forward networks.
- Next-Token Probabilities
The model outputs a probability for every possible next token.
- Token Selection
One token is chosen based on the probabilities.
- Repeat
The chosen token is appended and the process repeats.
Real-world examples
- When you type a question into ChatGPT, the model tokenizes your words and generates a reply one token at a time, often within seconds.
- Google Search uses a large language model to understand your query and provide direct answers in the search results page.
- GitHub Copilot suggests code completions in real time by predicting the next tokens based on the code you have already written.
- A customer service chatbot on a banking website uses an LLM to interpret your request and generate a helpful response.
Common mistake
✗ Myth: Large language models understand language and know facts like a human.
✓ Fact: They do not understand; they predict likely sequences of tokens based on statistical patterns in training data. They can generate fluent text without any grasp of meaning or truth.
People also ask
How are large language models trained?
What is a token in an LLM?
Why do LLMs hallucinate?
How does an LLM generate an answer?
What is attention in a transformer?
Part of the guide: How AI Works →

