Large Language Models (LLMs) like GPT and Claude feel like magic. You type a sentence, and out comes a coherent, often surprisingly intelligent response. But under the hood, there’s no magic — just a well-defined pipeline of mathematical steps.
In this article, I’ll break down exactly how an LLM turns your text input into a text output, step by step.
Step 1: Tokenization
The first thing to understand is that an LLM doesn’t read letters or words the way we do. It reads tokens.
For example, take the sentence:
“Today the weather is good.”
Tokenization breaks it down into pieces like:
[Today] [the] [weather] [is] [good]
This process is a core part of Natural Language Processing (NLP), and it’s the very first step in any LLM architecture.
During training, the model learns to identify efficient, recurring patterns in language. Extremely common words — like “is,” “the,” or “will” — usually get their own single token, since they appear so frequently that it’s efficient to represent them as one unit. Less common or longer words may get split into multiple sub-word tokens, so the model can still understand and generalize the meaning even if it has never seen that exact word before.
Every token is assigned a unique token ID — essentially a number that represents it. This ID is what gets passed on to the next stage: embedding.
Step 2: Embedding
Once we have token IDs, the model needs to represent the meaning of each token in a way it can do math with. This is where embeddings come in.
An embedding is a vector — simply a list of numbers — that represents a token’s meaning in a high-dimensional space.
The number of dimensions in these vectors varies widely between models. Common sizes are 768, 1024, or 1536 dimensions, though larger models can use much bigger vectors — GPT-3, for instance, uses embeddings with 12,288 dimensions.
These are called semantic embeddings, because they are dense vector representations that capture meaning, not just identity. Words with similar meanings end up positioned closer together in this vector space.
For example, the embedding for “king” ends up mathematically close to embeddings for “queen,” “prince,” and “monarch” — because the model has learned that these words tend to appear in similar contexts.
Step 3: The Transformer Attention Layer
Next comes the part that makes modern LLMs so powerful: the attention mechanism, inside what’s called the Transformer architecture.
Think of attention as the model’s way of figuring out what each word is actually referring to in context.
Take this classic example:
“The cat sat on the mat because it was tired.”
What does “it” refer to — the cat, or the mat? A human instantly knows it’s the cat. Attention is the mechanism that lets the model figure this out too, by weighing how strongly each word in the sentence relates to every other word.
Through this process, the model captures:
- Grammar — how words relate structurally
- Dependency — which words depend on or modify others
- Meaning — the actual semantic relationships in the sentence
Importantly, the model doesn’t “decide” on a single correct answer at this stage. Instead, it produces a set of probabilities for what should come next.
Step 4: Probabilities
This is a crucial point to understand: an LLM never definitively “knows” the answer. At every step, it calculates a probability distribution over its entire vocabulary — essentially asking, “given everything so far, how likely is each possible next token?”
The final word you see chosen isn’t a certainty — it’s a sampled outcome from this probability distribution.
Step 5: Sampling
How does the model pick one token out of thousands of possibilities? This is controlled by a setting called temperature, along with a technique called top-p sampling.
Temperature controls how “safe” or how “creative” the model’s choices are:
- Code / technical / terminal tasks: 0.2–0.4 (low randomness, more deterministic and precise)
- General tasks: 0.7–1.0 (balanced)
- Creative writing: 1.0+ (more randomness, more unexpected/creative output)
Top-p sampling (also called nucleus sampling) works a bit differently. Instead of considering every possible token, it only considers the smallest set of top tokens whose combined probability adds up to p (for example, the top tokens that together make up 90% of the probability mass). This keeps the choices sensible while still leaving room for variety.
Together, temperature and top-p give you control over the balance between predictability and creativity in the model’s output.
Step 6: The Generation Loop
Once a token is sampled, that’s not the end — it’s just one word. The model takes that new token, appends it to the input, and runs the entire process again — attention, probabilities, sampling — to predict the next token. And then the next. And the next.
This is why LLMs generate text one token at a time, in a loop, rather than producing the whole response instantly.
Why are LLMs relatively slow?
This loop is also the reason LLMs can feel slow, especially with longer inputs. The attention mechanism compares every token in the sequence to every other token, which gives it a time complexity of roughly O(n²) with respect to sequence length. In simple terms: the longer your input and output get, the more computation is required — and that cost grows faster than the length itself.
Wrapping Up
To summarize the full pipeline:
- Tokenization — breaking text into tokens
- Embedding — converting tokens into meaningful numeric vectors
- Attention (Transformer layer) — understanding relationships and context between tokens
- Probability distribution — calculating likelihoods for the next token
- Sampling (temperature / top-p) — choosing the next token
- The loop — repeating the process to generate text one token at a time
Understanding these fundamentals demystifies a lot of what feels like “AI magic.” It’s not one giant leap of reasoning — it’s millions of small, probability-driven steps, repeated very quickly, guided by patterns learned from massive amounts of text.
If you found this useful, follow me for more breakdowns of AI and LLM fundamentals.