Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Inside a language model

A whole LLM, layer by layer

A large language model (LLM) combines tokens, attention and feed-forward networks. How do those parts become a model that writes?

The complete network

We can now put everything we have built into one picture. Follow “the old rabbit sleeps” from the token IDs at the top to the vocabulary probabilities at the bottom.

Use the model-size picker to put real dimensions on the axes. Keep an eye on the residual stream: it connects every layer, including those left out of the drawing.

A complete language model, from tokens to next-token probabilitiesThe prompt is tokenised and its IDs select embedding vectors. The transformer layers each add a causal attention update and a gated feed-forward update to one continuous residual stream. Dots before the final layer indicate additional layers; the residual stream stays continuous. The final position is normalised, multiplied by the output weights, then passed through softmax to predict the next token.Text → tokens → IDsReview tokenisation and token IDstheID 12oldID 7rabbitID 31sleepsID 18Review the embedding lookupEmbedding table · look up each IDInput vectors · one column per tokenReview the input token vectorsEarlier context is dimmed; outlined columns are current.Current token computation.Review transformer layersFirst layer · own learned weightsReview transformer layersRepeated layers · own learned weightsReview transformer layersFinal layer · own learned weightsReview the residual streamResidual streamReview normalisation around each branchNormaliseReview causal self-attentionAttentionCausal self-attention: combine information from this token and earlier tokens, using position information.Review residual addition+Review normalisation around each branchNormaliseReview the SwiGLU gated feed-forward networkGated feed-forwardThe same gated feed-forward weights transform every token separately.Review residual addition+Review normalisation around each branchNormaliseReview causal self-attentionAttentionCausal self-attention: combine information from this token and earlier tokens, using position information.Review residual addition+Review normalisation around each branchNormaliseReview the SwiGLU gated feed-forward networkGated feed-forwardThe same gated feed-forward weights transform every token separately.Review residual addition+Review normalisation around each branchNormaliseReview causal self-attentionAttentionCausal self-attention: combine information from this token and earlier tokens, using position information.Review residual addition+Review normalisation around each branchNormaliseReview the SwiGLU gated feed-forward networkGated feed-forwardThe same gated feed-forward weights transform every token separately.Review residual addition+Review why generation reads the last columnEarlier context is dimmed; outlined columns are current.Current token computation.Review the last-column predictionLast columnReview normalisationFinal normalisationReview the output weight tableOutput weightsReview vocabulary scoresOne score per tokenReview softmax: scores to probabilitiesSoftmax · T = 1.0Review the next-token predictionIllustrative probability: 1.26%Illustrative probability: 5.62%Illustrative probability: 0.17%Illustrative probability: 4.02%Illustrative probability: 1.97%Illustrative probability: 0.22%Illustrative probability: 8.35%Illustrative probability: 0.59%Illustrative probability: 0.53%Illustrative probability: 8.64%Illustrative probability: 0.23%Illustrative probability: 1.76%Illustrative probability: 4.41%Illustrative probability: 0.17%Illustrative probability: 5.19%Illustrative probability: 1.42%Illustrative probability: 0.26%Illustrative probability: 9.08%Illustrative probability: 0.44%Illustrative probability: 0.72%Illustrative probability: 7.68%Illustrative probability: 0.20%Illustrative probability: 2.44%Illustrative probability: 3.34%Illustrative probability: 0.18%Illustrative probability: 6.44%Illustrative probability: 1.02%Illustrative probability: 0.33%Illustrative probability: 9.34%Illustrative probability: 0.33%Illustrative probability: 1.00%Illustrative probability: 6.48%Illustrative probability: 0.18%Illustrative probability: 3.31%Illustrative probability: 2.46%Illustrative probability: 0.20%One probability per vocabulary token

Largest: 9.3%

61129,2807,168CL7,168CL7,1687,168129,280129,280
Click a block to revisit its explanation. Follow the residual stream from embeddings to vocabulary probabilities. Dots mark omitted layers. Axis labels use the selected model; CL is context length. Cells and IDs are illustrative, and the architecture is generic.

One prediction becomes the next input

Temperature adjusts the scores before softmax. Try the slider beside the final array: lower values concentrate probability on the highest-scoring tokens; higher values spread it more evenly. This changes the distribution used for sampling, not the model’s weights.

Suppose we choose “at” from that final array. The next input is now “the old rabbit sleeps at”:

Choose a token, append it, and predict againThe prompt ‘the old rabbit sleeps’ enters the model. We select ‘at’ from its next-token distribution and append it. The longer prompt ‘the old rabbit sleeps at’ then goes into the same model for another prediction.the old rabbit sleepsLanguage modelNext-token probabilitiesChoose “at”the old rabbit sleeps atUse the longer contextPredict again · weights stay the same
Feed the longer context back into the same network for the next prediction. The learned weights stay fixed.

That completes the basic architecture. The later articles explore how to make this journey cheaper as the model and context grow.

Sources and assumptions

The diagram shows a generic pre-normalised decoder with gated feed-forward networks, not a named checkpoint. Axis labels use the selected model’s dimensions; the three drawn blocks stand for the full stack. Coloured cells and IDs are illustrative, not trained activations or a real tokenisation. Position handling is included in attention.

Vectors are columns. The context has shape width × context length; embeddings have shape width × vocabulary; output weights have shape vocabulary × width. Some models reuse the embedding weights in the transposed orientation for the output; others learn separate weights.

The final probability grid is schematic, with softmax calculated from illustrative scores. It is not a prediction from a trained model. The generation loop describes full-prefix evaluation; caching is a later topic.

← Browse all subjectsBack to the beginning ↑