How Large Language Models WorkHow Large Language Models WorkStage 1 of 8 · 8 stages · ~6 min
LLM · TRANSFORMER CORE

Follow one sentence through the model

Watch text become tokens, vectors, attention weights, learned updates, and a response built one prediction at a time.

8 stages~6 min
  1. ENCODE TEXT
  2. MIX CONTEXT
  3. PREDICT AGAIN
Read mode · answer first

How large language models work inside

Follow text through tokenization, vectors, causal attention, transformer blocks, training, and the repeated next token generation loop.

Cheat sheet · 6 essential ideas

The whole story in 6 lines

An LLM repeatedly turns token context into probability distributions using learned transformer weights, then feeds each chosen token...

  1. A tokenizer maps raw text into reusable subword pieces from a fixed vocabulary.
  2. Token and position vectors add together so content and order enter the model in one stream.
  3. Causal attention mixes earlier information while giving every future position exactly zero weight.
  4. Residual paths preserve the stream while attention and an MLP add learned refinements.
  5. Training compares every next token prediction with the known continuation and sends error back into the weights.
  6. Generation samples one token, appends it to context, and repeats the same prediction loop.
What does tokenization produce?
It produces a sequence of vocabulary pieces and their integer IDs, which become the model input.
Why add a position vector to each token vector?
Attention alone does not know sequence order, so the added position signal distinguishes the same token at different locations.
What does a causal mask do before softmax?
It blocks scores for future positions, which makes their attention weights exactly zero.
Why do residual paths matter inside a transformer block?
They preserve the incoming stream while attention and the MLP add refinements, which also gives gradients a direct path through deep stacks.
Where does the training target come from?
The same text supplies it by shifting the token sequence one position, so each context must predict the token that truly follows.
How does an LLM continue beyond one generated token?
It appends the chosen token to context and runs the same next token computation again.
Download PDF cheat sheet
Stage 1 of 8

Setup

Setup

A chat response can feel instant and fluent, but the model builds it from repeated numerical predictions. We will follow one short sentence through that machinery.

A token is a reusable text piece, while a vector is a row of numbers that carries information about that piece.

Attention decides which earlier positions can contribute. A logit is the raw score produced for one possible next token.

Our journey now connects six jobs: split text, build vectors, mix context, refine the stream, learn from errors, and generate repeatedly.

Keep those four terms nearby as the same token pieces and vectors return across stages. Let us begin with how raw text becomes model input.

Stage 2 of 8

Text Becomes Tokens

Text Becomes Tokens

The model begins with a character string, not with ready made words or meanings. This stage follows that string into the discrete pieces the network can receive.

A subword tokenizer matches reusable pieces from a fixed vocabulary. Common fragments may stay together, while a less common word can split across several pieces.

The word curious has crossed two vocabulary boundaries in our illustrative fixture. What must replace those text pieces before matrix operations can begin?

Pause and predict
What replaces each token piece before the neural network starts?

Every shard now lands in one numbered vocabulary slot. The sequence length comes from the tokenizer, while the actual IDs come from the fixed vocabulary it learned earlier.

Tokenization preserves text as an ordered ID sequence, but an ID alone carries no useful geometry. Those token boundaries stay available as the sequence moves through every later layer. Their order stays fixed throughout that journey. Next, each ID becomes a vector and gains a signal for position.

Stage 3 of 8

Meaning Meets Position

Meaning Meets Position

We now have ordered vocabulary IDs from tokenization. This stage asks how one small integer becomes a numerical state that can carry both content and location.

Each ID selects one row from a learned embedding table. Tokens used in similar contexts can develop related patterns across these learned vector dimensions.

The same token vector could appear at any location, so content alone cannot distinguish subject order from object order. What signal must enter before attention compares positions?

Pause and predict
What information must be added before attention?

The semantic strip and position strip add cell by cell. Their combined vector now carries token content and sequence location through the same fixed width stream. This sum does not attach a human definition to the token. It creates a learned state whose coordinates matter only through later computations.

A position aware vector is still only one local state. Next, causal attention will compare these states and mix useful information from earlier tokens.

Stage 4 of 8

Causal Attention

★ If you remember one thing · Causal masking removes every future path before attention mixes information.
Causal Attention

The vectors now know content and position, but each one is still isolated. Attention lets every query compare with keys and gather a weighted mixture of value vectors.

A query and key dot product produces one compatibility score. Softmax then turns each completed row into weights that sum to one before values are mixed.

During next token training, a position must not copy information from text that comes later. Which part of the score matrix must disappear before softmax?

Pause and predict
Which attention scores must be blocked?

The contrast is now complete. The unrestricted matrix keeps every cell, while the causal matrix crosses out its future triangle and gives those paths exactly zero weight. Each allowed weight now tells the model how strongly one earlier value should influence the current evolving token state.

Each allowed row now mixes only information available at that position. Next, the transformer block will add that attention result back into the continuing vector stream.

Stage 5 of 8

Inside One Transformer Block

Inside One Transformer Block

Causal attention has produced a context update for every position. One transformer block must fold that update into the stream without discarding the state that arrived.

Layer normalization prepares the stream, then attention computes a context shaped delta. The original stream also travels around that operation on a residual path.

The first addition joins original state and attention delta. A second normalization then prepares each position for an independent multilayer perceptron, often called the MLP.

The MLP expands and transforms each position before projecting it back to the stream width. What should happen to the state that entered this second branch?

Pause and predict
How should the MLP result rejoin the stream?

The second addition preserves the incoming state and adds the MLP refinement. Attention mixes information across positions, while the MLP transforms each position through learned nonlinear features.

One block makes two refinements without replacing the stream. Deep models repeat this structure many times. Next, we will see how training teaches all those weights.

Stage 6 of 8

Learning From the Next Token

Learning From the Next Token

The transformer block contains learned weights, but architecture alone does not choose useful values. Training turns ordinary text into many next token exercises for those weights.

The training target is the same sequence shifted one position. After The, the target is the first curious piece. After that piece, the target is its continuation.

Every position produces logits over possible vocabulary tokens. Once softmax assigns probability to the true continuation, what signal tells the model how wrong it was?

Pause and predict
What converts the true token probability into an error signal?

The computed losses now flow backward through every operation. An optimizer uses those gradients to update shared weights, so future passes can assign more probability to the actual continuation. Those shared weights affect many positions, so each update combines evidence from many prediction examples.

Training repeats this prediction and update process across vast text collections. At runtime the updates stop. Next, fixed weights will generate by choosing and appending one token at a time.

Stage 7 of 8

One Token, Then Another

One Token, Then Another

Training has finished and the weights are fixed. Generation begins with the current token context and runs one forward pass through the trained transformer stack.

The final hidden state projects into one logit for every vocabulary entry. Softmax converts those raw scores into a probability distribution for the next position.

Temperature rescales the logits before softmax. If Temperature rises, how will the probability bars change while their ordering stays the same?

Pause and predict
What happens when Temperature rises?

One token is selected from the computed distribution. The bars show uncertainty before selection, while the green shard records the concrete continuation chosen for this run.

Switch Temperature through every setting and compare how strongly the longest bar dominates. Your goal is to make one candidate clearly outweigh the others.

The selected shard appends to context and becomes input for the next pass. Repeating this same loop builds a response one token at a time. Now let us connect the entire system.

Stage 8 of 8

The Whole LLM Loop

The Whole LLM Loop

We started with tokenization, which turned raw characters into an ordered sequence of reusable vocabulary pieces and IDs.

Then embeddings and position signals turned those IDs into vectors that carry both content and sequence order.

Causal attention compared positions, removed every future path, and mixed only information available at each prediction point.

Transformer blocks preserved the stream through residual paths while attention and MLP branches added learned refinements.

Training shifted real text into next token targets, measured prediction loss, and pushed gradients backward into shared weights.

Generation reused those fixed weights, sampled one token from computed probabilities, and appended it to the continuing context.

The complete system now meets at one repeated task: turn the available token context into a probability distribution for what comes next.

Cheat sheet · 6 essential ideas

The whole story in 6 lines

An LLM repeatedly turns token context into probability distributions using learned transformer weights, then feeds each chosen token...

  1. A tokenizer maps raw text into reusable subword pieces from a fixed vocabulary.
  2. Token and position vectors add together so content and order enter the model in one stream.
  3. Causal attention mixes earlier information while giving every future position exactly zero weight.
  4. Residual paths preserve the stream while attention and an MLP add learned refinements.
  5. Training compares every next token prediction with the known continuation and sends error back into the weights.
  6. Generation samples one token, appends it to context, and repeats the same prediction loop.
What does tokenization produce?
It produces a sequence of vocabulary pieces and their integer IDs, which become the model input.
Why add a position vector to each token vector?
Attention alone does not know sequence order, so the added position signal distinguishes the same token at different locations.
What does a causal mask do before softmax?
It blocks scores for future positions, which makes their attention weights exactly zero.
Why do residual paths matter inside a transformer block?
They preserve the incoming stream while attention and the MLP add refinements, which also gives gradients a direct path through deep stacks.
Where does the training target come from?
The same text supplies it by shifting the token sequence one position, so each context must predict the token that truly follows.
How does an LLM continue beyond one generated token?
It appends the chosen token to context and runs the same next token computation again.