How Large Language Models Work Under the Hood
Large language models (LLMs) like GPT, LLaMA, and Gemini feel almost “intelligent” because their internal architecture is designed to capture rich patterns in text. This article explains how they work from tokens to full responses and includes diagrams to help you visualize the key ideas.
1. What an LLM Actually Does
An LLM is, at heart, a next‑token predictor: given your prompt, it estimates the most likely next token, then repeats this step to build a full response. It learns these patterns by training on vast amounts of text, so its outputs resemble human‑like language even though it has no conscious understanding.
Figure: How an LLM takes a sequence, predicts probabilities for the next token, and generates the next word or subword.
2. Turning Text into Numbers
Before math can happen, text is converted into numbers:
- Tokenization: The input is split into tokens (words, word‑parts, or punctuation).
- Embedding: Each token is mapped to a vector by an embedding layer, turning discrete symbols into continuous numerical representations.
These vectors become the input to the model’s neural‑network layers.
3. The Transformer Architecture
Most modern LLMs use a decoder‑only Transformer architecture, in which:
- Each layer has multi‑head self‑attention and feed‑forward MLP blocks.
- Positional encodings or RoPE‑style embeddings are added so the model knows token order.
The architecture is “decoder‑only” because it only needs to read the prefix and generate the continuation, not encode a separate source sequence.
Figure: A decoder‑only Transformer block with embedding, normalization, self‑attention, and feed‑forward layers.
4. Self‑Attention in Action
Self‑attention lets every token “attend” to all previous tokens in the sequence, weighting how much each one matters for the current prediction. This is why LLMs can handle long‑range dependencies and context: every word in the prompt can influence later tokens.
Figure: Key components of a transformer decoder block, including multi‑head self‑attention and feed‑forward modules.
5. Training: From Raw Text to Language Skill
LLMs are usually trained in several stages:
- Pre‑training (self‑supervised): The model learns general language statistics by predicting masked or next tokens on huge corpora.
- Fine‑tuning (supervised): The model is adjusted on labeled examples (instruction–response pairs, QA, etc.) to follow tasks.
- Alignment (RLHF or DPO): Human‑rated responses are used to tune the model so outputs are helpful, honest, and safe.
These stages can be thought of as a pipeline that gradually shapes raw language capability into a usable assistant.
Figure: Three stages of LLM training: pre‑training, supervised fine‑tuning, and reinforcement learning / alignment.
6. How Inference Works: From Prompt to Output
During inference:
- Your prompt is tokenized and embedded.
- The embeddings pass through the transformer layers, building contextual representations.
- The model outputs a probability distribution over possible next tokens, and a sampling strategy (e.g., top‑k, nucleus sampling) picks the next token.
This repeats until a stop‑token is generated or the context window is full.
Figure: Visualization of next‑token probabilities, showing how the model chooses among candidate tokens.
7. Why They “Feel” Intelligent
LLMs are effectively pattern‑matching machines: their internal parameters encode statistical correlations between words and contexts learned from training data. When the data is broad and diverse, these patterns can approximate reasoning steps, summarization, and coding, even though the model has no symbolic world model.
8. Common Limitations
Important limitations to keep in mind:
- No persistent memory: The model only “remembers” what is in the current context window.
- Hallucination: It can invent false facts that sound plausible.
- Context window limits: There is a fixed maximum length (e.g., 8k, 32k, or 128k tokens) beyond which older text is dropped.
9. Putting It All Together
In summary, an LLM:
- Converts text into token vectors and embeddings.
- Processes these through a deep decoder‑only Transformer using self‑attention and MLPs.
- Learns from massive text corpora, then is fine‑tuned and aligned with human feedback.
- Generates responses by repeatedly predicting and sampling the next token.