How Large Language Models Work
Follow a prompt through tokens, the context window and the predict-next-token loop, and see why a model needs a system around it.
The short answer
A large language model is a program trained on a large body of text to predict the next token, a word or part of a word. It writes an answer one token at a time, reading everything in its context window on each step. It has no memory between calls and no live knowledge unless a surrounding system supplies them.
What is a large language model?
A large language model (LLM) is a program trained on a large body of text to predict the next token, a word or part of a word, from the tokens before it. ChatGPT, Claude and Gemini are products built on LLMs, and each one is a form of generative AI.
Training works by repetition. The training process shows the model a passage with the next token hidden, and the model guesses. The process then adjusts the model's parameters, the numbers that hold what it has learned, so the correct token scores higher next time. After enough passages, the parameters encode the grammar, facts and styles found in the text. Today's models use the transformer design that Google researchers published in 2017.
Training then stops, and the provider releases the model with its parameters fixed. Your conversations do not change them.
How does a model write an answer?
A model writes one token at a time. It reads the text so far and scores the candidates for the next token, and the software running it picks one. That token joins the text, and the cycle runs again.
On each pass the model reads the whole text again, including the tokens it has written itself. That re-reading keeps a reply on topic from its first sentence to its last.
What is a token?
A token is the unit of text a model reads and writes: a common word, a piece of a longer word, or a punctuation mark. A program called a tokeniser splits your prompt into tokens before the model sees it.
In English, a token averages a little less than one word. Providers measure a model's limits and their bills in tokens, so a long document in the prompt costs more than a short question. What AI agents cost to run shows the arithmetic.
What is the context window?
The context window is the maximum number of tokens a model can handle in one call, counting what goes in and what it writes. The model can read anything inside the window and nothing outside it.
Your instructions, any documents you attach, the conversation so far and the reply all draw on that one budget. Once a long conversation fills the window, the application has to drop or summarise the oldest messages. If a chatbot loses track of an instruction you gave an hour ago, the application may have trimmed it out of the window.
Why does the same prompt give different answers?
The software picks each token by sampling, a weighted random draw from the model's scores. A token with a high score wins most draws, and a lower-scored token wins some. One different token early in a reply changes the tokens that follow it.
A setting called temperature controls how much randomness the draw allows. A low temperature makes replies more repeatable, and a high one makes them more varied. Run a prompt several times before you rely on it and judge the spread of answers, since a single good reply proves little.
Why do models state false things with confidence?
A model produces the tokens that fit the text so far, and nothing in the loop checks them against the facts. A true sentence and a false one come out in the same fluent voice. Researchers call a false statement of this kind a hallucination.
Two measures reduce it:
- Grounding. Put the source material in the prompt, tell the model to answer from it, and give it permission to say that the source does not contain the answer. Retrieval-augmented generation does this at scale by fetching the relevant passages for each question.
- Checking. Ask the model to quote the passage that supports each claim, then have a person verify the claims that carry consequences.
Anthropic's documentation describes both techniques and states that they reduce hallucinations without removing them.
What does a model lack unless a system supplies it?
A model on its own has no memory between calls and no knowledge of anything after its training ended. Each call starts from the fixed parameters plus whatever sits in the context window.
A chat application creates the feeling of memory by sending the earlier conversation back to the model with each new message. Longer-term memory needs a store outside the model, covered in AI agent memory. Live knowledge works the same way: a search tool or a document store fetches current information, and the application places it in the prompt.
Use that split to question any product built on an LLM. The model supplies the language skill. Ask the vendor which part of their system supplies the memory and the current facts, and how they tested it.
Common questions
Does a large language model understand language?
Researchers disagree. A model predicts tokens from patterns in its training text, and that process produces replies that read as understanding. For practical work, test the output on your own cases and verify the claims that matter.
Does the model learn from my conversations?
Not during the conversation. The parameters stay fixed after training. Some providers use conversations to train later versions, depending on your plan and settings, so read the data policy for the product you use.
Why does a chatbot forget what I said earlier?
The context window holds a fixed number of tokens. Once a conversation outgrows it, the application drops or summarises older messages, and the model can no longer read them.
Sources and references
- Vaswani et al., arXiv, Attention Is All You Need
- OpenAI Help Center, Understanding and counting tokens
- Anthropic, Context windows
- Anthropic, Reduce hallucinations
Concepts covered: large language model, token, context window, sampling, temperature, hallucination, grounding, transformer.