How Large Language Models Work
A practical mental model for tokens, attention, training, inference, context, and the limits of generated answers.
How large language models work
A large language model turns a sequence of tokens into a probability distribution for the next token. It selects a continuation, adds that token to the sequence, and repeats. The result can look planned and conversational, but the basic operation is repeated prediction conditioned on the text currently available. This distinction matters: fluent wording is evidence that the model can produce a plausible sequence, not proof that every statement in that sequence is true.
Most modern LLMs use the Transformer architecture introduced in Attention Is All You Need. The important practical idea is attention: while processing a token, the network can assign different relevance to other positions in the sequence. Layers of attention and learned transformations build contextual representations that help the model continue text.
How text becomes an answer
Imagine asking: “Why does my sourdough rise more slowly in winter?” The system does not look up a complete sentence and paste it back. A simplified pipeline looks like this:
- Tokenize the input. The text is split into tokens. A token may be a word, part of a word, punctuation, or another learned text fragment.
- Represent the tokens. Each token becomes a vector of numbers. Position information helps distinguish identical tokens appearing in different places.
- Build context through layers. Attention layers combine information from relevant positions. In this example, “rise,” “slowly,” and “winter” help shape the representation of the question.
- Score possible next tokens. The final representation produces probabilities for possible continuations.
- Choose and repeat. A decoding strategy selects a token. The new token becomes part of the next step’s context until the response ends.
Temperature and related decoding settings affect selection, not what the model learned during training. A lower temperature usually concentrates selection around higher-probability tokens; a higher value allows more variation. Neither setting turns uncertainty into knowledge.
Training is not the same as answering
During pretraining, a model processes a large collection of examples and adjusts numerical parameters to reduce prediction error. It does not normally preserve the collection as a searchable library of pages. It learns distributed statistical structure: spelling, syntax, associations, recurring formats, styles, and many patterns that support useful generalization.
Additional training can shape behavior. Instruction tuning uses examples of desired responses. Preference-oriented methods can encourage helpful formats and discourage unwanted behavior. Product systems may also add a system instruction, safety filters, retrieval, tools, or memory around the underlying model. That means “the model” and “the application” are not always the same thing. When comparing products, ask which layer produced the behavior you observed.
Inference is the answering phase. The model parameters are mostly fixed, and the system applies them to the current context. A request does not retrain the model. A conversation may appear to teach it because earlier messages remain in the context, but that temporary influence is different from updating model weights.
What attention does and does not mean
Attention is best understood as a learned information-routing mechanism. Different heads can learn different relationships, and successive layers transform the result. An attention score is not a reliable explanation of intention, consciousness, or factual certainty. It is one internal component in a large numerical process.
The mechanism also has practical limits. More input consumes more of the context budget and adds material the model must distinguish. Research such as Lost in the Middle shows that relevant information may be used differently depending on where it appears. Therefore, a large advertised context window should be treated as capacity to test, not a guarantee that every detail will influence the answer equally.
For important work, place the task and essential constraints clearly, remove irrelevant history, label source material, and test whether the model can recover decisive facts from realistic inputs. The context window definition includes a compact position-sensitivity test.
Why an LLM can be useful and wrong
The model is optimized to produce a fitting continuation. It may reproduce a supported fact, combine patterns into a new solution, or generate a convincing error. Several failure types look similar on the surface:
- Missing knowledge: the training process did not establish enough signal for the question.
- Stale knowledge: the world changed after the relevant training or retrieval source.
- Context failure: the needed fact was present but ignored, diluted, or misread.
- Reasoning failure: intermediate steps were inconsistent even though the ingredients were available.
- Instruction conflict: competing instructions or untrusted content redirected the response.
- Tool failure: a surrounding search, database, calculator, or code tool returned poor data.
Calling all of these “hallucination” hides the remedy. Missing knowledge may call for retrieval. A context failure calls for better input design and testing. A tool failure requires observability around the tool call. A high-impact claim still requires verification against an appropriate source.
The TRACE mental model
Use TRACE to inspect any LLM answer without pretending you can see inside the model:
- T, Task: What exact transformation did you request: classify, extract, explain, draft, or decide?
- R, Relevant context: Which input facts should control the answer, and can you point to them?
- A, Assumptions: What did the response add that was not supplied or sourced?
- C, Checks: Which parts can be validated with a schema, calculation, source, test, or second method?
- E, Escalation: What happens when confidence is low or the cost of error is high?
Consider the sourdough answer. The task is explanation. Relevant context might include room temperature, starter activity, hydration, and timing. An assumption would be claiming an exact temperature without the baker providing one. Checks include a trusted baking reference and direct temperature measurement. Escalation is modest because the likely cost is a disappointing loaf. The same workflow applied to a medical or legal decision should have a much stricter boundary and qualified review.
A better way to learn a model’s behavior
Do not begin with a benchmark headline. Build a small set of inputs from your actual task. Include ordinary examples, ambiguous requests, long inputs, missing data, adversarial instructions, and cases where the correct action is to abstain. Record the model version and settings. Score observable properties such as factual support, completeness, format validity, latency, and cost.
Change one variable at a time. If adding a source fixes an answer, you learned something about grounding. If moving the same source changes the answer, you found position sensitivity. If a schema catches malformed output, you found a control worth keeping. This empirical loop is more useful than arguing about whether a model “understands” in the abstract.
Practical implications
An LLM is a learned sequence model inside a larger system. Tokens enter, contextual representations are computed, and tokens are generated one step at a time. Training gives the model broad patterns; the current context steers a particular response. Retrieval and tools can add current evidence or deterministic operations. Verification remains an application responsibility.
Keeping those layers separate (model, context, decoding, tools, and controls) makes failures easier to diagnose and systems easier to design without mystifying the technology or dismissing what it can do.