LLM Foundations / How LLMs Work
From input to next token.
Reviewed by Yuvaraj
You have met the pieces, tokens, embeddings, attention. Now let us trace a single request end to end and watch a large language model turn your text into a response.
Suppose you type: "AI learns from" and ask the model to continue.
Answer from memory before revealing, retrieval practice is what builds durable recall.
At its core, a large language model is trained to:
The "context window" of an LLM is:
An LLM's weights change every time it answers a user during inference.
Ask about this lesson, or about anything in AI. Answers cite the lessons they draw on.
Finished this lesson?
Mark it complete to earn XP, keep your streak, and schedule a review.
Stacking many transformer blocks is what builds an increasingly rich representation: early layers capture surface patterns, later layers capture meaning and long-range structure.
Step 4 produces one logit per vocabulary token, a raw, unbounded score. Suppose, after "AI learns from," the top three logits are:
data: 4.0water: 2.0rocks: 1.0Softmax exponentiates and normalizes them into probabilities:
The model now assigns data an 84% chance of being next, not because it looked up a fact, but because that continuation fit the patterns it learned. How one token gets picked from this distribution is the next lesson.
Here is the key move: the chosen token is appended to the input, and the whole process runs again to produce the next token.
AI learns from → data → AI learns from data → by → AI learns from data by → ...
The model generates one token at a time, each step conditioned on everything so far. This is called autoregressive generation. A long answer is just this loop run many times.
Prediction, not retrieval
An LLM is not looking up an answer in a database. At each step it is predicting a plausible next token from patterns learned in training. That is why it can be fluent and wrong at the same time, and why "next-token prediction" is the single most important idea about how these models work.
Everything the model considers, your prompt plus the tokens generated so far, must fit inside its context window, measured in tokens.
Why length has a limit
Attention compares tokens against each other, so its cost grows quadratically as the sequence gets longer: double the tokens, roughly quadruple the attention work. The context window is the model's hard limit on how much text it can attend to at once. Exceed it and the earliest tokens must be dropped or summarized.