• Uncategorised
  • 0

How an LLM Works: From Text to the Next Word

How an LLM Works — From Input to Output

How an LLM Works: From Input to Output

A practical, numerical journey through tokenization, embeddings, attention, Q/K/V, multi-head attention, feed-forward networks, and billions of parameters.

Introduction

Large Language Models (LLMs) such as ChatGPT can answer questions, write code, explain concepts and generate text. But what actually happens inside an LLM when we enter a sentence?

Let’s follow this input through the model:

“The cat sat”

We will use an illustrative Transformer with the following dimensions. These are example architecture choices, not specifications of a particular commercial model.

PropertyValue
Vocabulary size50,000 tokens
Hidden dimension4,096
Feed-forward intermediate dimension14,336
Transformer layers32
Attention heads per layer32
Dimension per attention head128

1. Tokenization: Text Becomes Token IDs

The first step is tokenization. The text is split into tokens, and each token is represented by an ID from the tokenizer’s vocabulary.

The
Token ID: illustrative
cat
Token ID: illustrative
sat
Token ID: illustrative

A token is not always a complete word. Depending on the tokenizer, it may be a word, subword, punctuation mark or another text fragment.

The model operates on token IDs, not directly on the original text.

2. Embedding: Turning Token IDs into Vectors

A token ID is just an identifier. The model looks up a learned vector for that ID in its input embedding table.

Our example has 50,000 tokens, each represented by 4,096 numbers.

50,000 × 4,096 = 204,800,000 parameters

That is approximately 204.8 million embedding parameters.

For illustration, a tiny embedding could look like this:

sat → [0.2, −0.5, 0.8]

In our example model, the real vector has 4,096 elements. Positional information is also incorporated so the model can represent token order. Different architectures handle position differently.

These initial embeddings are learned during training. Their individual dimensions are not necessarily simple, human-readable features such as “animal” or “movement.”

3. Transformer Layers: Processing Context

The embedding provides an initial representation, but the model must interpret a token in the context of the sentence.

Consider the word “bank”:

  • I deposited money at the bank.
  • We sat beside the river bank.

The initial embedding for the same token can be identical, while the representation after contextual processing differs.

Our example contains 32 Transformer layers. Each layer contains two major components:

Multi-head attention Gather relevant information from other token positions.
↓
Feed-forward network Transform each token’s representation.

Residual connections and normalization also operate within Transformer layers.

4. Attention: Understanding Relationships

Attention allows a token to gather relevant information from other tokens in the input. In a causal language model, a token can generally attend to itself and earlier tokens, but not future tokens.

4.1 What are Q, K and V?

Q — Query

Used to determine which other tokens are relevant.

K — Key

Compared against queries to calculate relevance scores.

V — Value

Carries the information that can be combined from tokens.

Learned matrices

Q, K and V are calculated from current token representations using learned weight matrices.

Q = XWQ   K = XWK   V = XWV

The matrices are fixed during ordinary inference, but the Q, K and V vectors are calculated dynamically for each token and layer.

4.2 Numerical example

To keep the arithmetic simple, imagine the query for “sat” is:

Qsat = [2, 6]

Suppose the keys are:

KThe = [1, 1]   Kcat = [2, 1]   Ksat = [2, 5]

Compare Q for “sat” against each key using a dot product.

ComparisonCalculationScore
Against The2×1 + 6×18
Against cat2×2 + 6×110
Against sat2×2 + 6×534

These scores are not probabilities. Real scaled dot-product attention divides the scores by the square root of the key dimension before applying softmax, and applies a causal mask where appropriate.

4.3 From scores to attention weights

Softmax converts the scores into weights that sum to one. For illustration, suppose the weights are:

TokenIllustrative weight
The0.2
cat0.5
sat0.3

These weights are illustrative and are not the softmax result of the scores calculated above.

4.4 Use the weights to combine V

Suppose the value vectors are:

VThe = [2, 0]
Vcat = [0, 4]
Vsat = [7, 3]

The attention output for “sat” is the weighted sum:

Asat = 0.2[2,0] + 0.5[0,4] + 0.3[7,3]
= [0.4,0] + [0,2] + [2.1,0.9]
Attention output:
Asat = [2.5, 2.9]

Q helped determine the weights. The V vectors supplied the information being combined. The result is an intermediate attention output, not yet the final vector used to predict the next token.

5. Multi-Head Attention

A model can run multiple attention calculations in parallel. These are called attention heads. Different heads can learn different patterns of relationships between tokens.

In our example, the hidden dimension is 4,096 and there are 32 heads.

4,096 ÷ 32 = 128 numbers per head

The Q projection produces a 4,096-element vector, which can be viewed as 32 chunks of 128 elements. The same dimensional arrangement applies to K and V.

Each head uses its own learned projections and performs attention. The 32 head outputs are then concatenated:

32 × 128 = 4,096 numbers

What is WO?

The concatenated vector is multiplied by the output projection matrix WO.

[1 × 4,096] × [4,096 × 4,096] = [1 × 4,096]

The output is still a 4,096-element vector, but its numerical values have changed. WO learns how to combine information from the different attention heads.

A weight matrix is a two-dimensional table of learned numbers. The result of multiplying a token vector by that matrix is another vector.

6. The Feed-Forward Neural Network

Attention gathers information across token positions. The feed-forward network transforms the information in each token’s representation.

The same feed-forward weights are reused for every token position within a layer. Each token nevertheless has its own input vector and its own resulting activations.

6.1 Expand the representation

The first matrix has dimensions 4,096 × 14,336.

[1 × 4,096] × [4,096 × 14,336] = [1 × 14,336]

The resulting intermediate vector contains 14,336 numbers.

6.2 Apply a nonlinear operation

The model applies an activation function or a gated operation. For a simple ReLU example:

[2, 3, −1] → [2, 3, 0]

Modern LLMs commonly use other activation functions or gated feed-forward designs.

6.3 Project back to 4,096

The second matrix has dimensions 14,336 × 4,096.

[1 × 14,336] × [14,336 × 4,096] = [1 × 4,096]

The representation returns to 4,096 dimensions, but its values have been transformed by learned weights and nonlinear processing.

The feed-forward network expands the representation, transforms it, and projects it back to the original hidden dimension.

7. Why Are There 32 Transformer Layers?

Each of our 32 layers has its own attention weights and feed-forward weights.

  1. Layer 1 processes the initial representations.
  2. Layer 2 uses the updated representations and its own weights.
  3. The same process repeats through the remaining layers.

Consequently, Q, K and V are recalculated from the latest representations at every layer. The learned weights are normally fixed during inference.

Embedding vectors
↓
Layer 1 Attention → feed-forward network
↓
Layer 2 Attention → feed-forward network
↓
… Layers 3–31 …
↓
Layer 32 Attention → feed-forward network
↓
Final hidden representation

8. Calculating Billions of Parameters

A parameter is a learned numerical value stored in the model. Let’s count the major matrices in our illustrative architecture.

8.1 Input embeddings

50,000 × 4,096 = 204.8 million

8.2 Attention matrices

Each layer has four major matrices: WQ, WK, WV, and WO.

4 × 4,096 × 4,096 = 67.11 million per layer
67.11 million × 32 ≈ 2.147 billion

8.3 Feed-forward matrices

Each layer has two major matrices:

2 × 4,096 × 14,336 = 117.44 million per layer
117.44 million × 32 ≈ 3.758 billion

8.4 Output projection

Assuming the output weights are separate from the input embedding table:

4,096 × 50,000 = 204.8 million

8.5 Total

ComponentParameters
Input embedding table204.8 million
Attention matrices across 32 layers2.147 billion
Feed-forward matrices across 32 layers3.758 billion
Output projection204.8 million
Total6.315 billion

This estimate omits biases, normalization parameters and some architecture-specific components. If input embeddings and output weights are tied, the shared matrix is counted only once.

Modern gated feed-forward networks and other attention designs can have different parameter counts.

9. Parameters Versus Calculations

Parameters and calculations are not the same thing.

Parameters

The learned numbers stored in the model.

Calculations

The operations performed using those numbers.

For the input “The cat sat on the mat,” the same feed-forward weights are applied to each token’s representation. The model does not create a separate feed-forward network for every token.

  • More parameters mean more learned numbers.
  • More input tokens generally require more computation.
  • The same parameters can be reused across token positions and across requests.

A billion-parameter model does not have a billion parameters for each token.

10. The Output Layer: Predicting the Next Token

After all Transformer layers, the model uses the final hidden vector at the last input position to predict the next token.

For illustration, suppose the final vector is:

h = [4, 2]

In our real-sized example, this vector would have 4,096 elements.

10.1 What is an output vector?

The output projection has a learned vector associated with each possible output token. These vectors are parameters.

Some architectures share input embedding weights with the output projection; others use separate weights. Therefore, an output vector is not necessarily the same as the token’s input embedding.

For a tiny example, suppose the output vectors are:

CandidateOutput vector
on[2, 1]
down[1, 2]
quietly[0, 1]

10.2 Calculate the scores

Score(on) = [4,2] · [2,1] = 10
Score(down) = [4,2] · [1,2] = 8
Score(quietly) = [4,2] · [0,1] = 2
CandidateScore
on10
down8
quietly2

These scores are called logits. The model calculates one for every candidate token in its vocabulary. The scores are not distances between words.

10.3 Convert scores into probabilities

P(i) = exp(score(i)) / sum of exp(all scores)

Softmax converts the logits into probabilities. For our three illustrative candidates, the approximate probabilities are:

CandidateProbability
on88.0%
down11.9%
quietly0.1%

These probabilities apply only to the tiny example, not to a real LLM vocabulary.

With greedy decoding, the model selects the highest-scoring token: “on.” The text becomes:

The cat sat on

If more text is needed, the model predicts another token using the expanded sequence.

11. How Does Training Teach the Model?

During pretraining, the model sees large amounts of text and learns to predict the next token.

For example:

Input

The cat sat

Correct next token

on

The model calculates its predictions, measures the error using a loss function, and uses backpropagation and an optimizer to update the learned parameters.

These updates affect embeddings, attention matrices, feed-forward matrices and output weights. Over many training examples, the model learns patterns that improve its predictions.

Later training stages can further improve instruction following and response quality. Training does not guarantee that every answer will be correct.

Conclusion: The Complete Journey

1. TokenizationText → token IDs
↓
2. EmbeddingToken IDs → initial vectors
↓
3. AttentionQ/K comparisons → attention weights → weighted V
↓
4. Multi-head attentionParallel heads → concatenate → WO
↓
5. Feed-forward network4,096 → 14,336 → 4,096
↓
6. RepeatProcess through all Transformer layers
↓
7. Output projectionFinal hidden vector → vocabulary scores
↓
8. Softmax and decodingScores → probabilities → next token
The big picture: Attention gathers contextual information. Feed-forward networks transform token representations. The output projection scores candidate next tokens. Billions of learned parameters make these transformations possible.

An LLM is not simply a dictionary of words or a system that selects the nearest embedding. It is a learned numerical system that processes context and calculates probabilities for possible next tokens.

An introductory, numerical guide to Transformer-based language models.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *