Understanding LLMs from Scratch with School Mathematics
A visual, no-calculus tour of neural networks, tokens, embeddings, attention and the transformer idea behind modern language models.
1. The whole pipeline in one table
| Stage | Input | Output | Question answered |
|---|---|---|---|
| Tokenizer | Text | Token IDs | What pieces do we compute on? |
| Embedding | Token ID | Vector | How do we represent each token numerically? |
| Attention | Vectors | Context-aware vectors | Which earlier information matters here? |
| Feed-forward layers | Vectors | Transformed vectors | What features should each position compute? |
| Language-model head | Final vector | Scores/logits | How plausible is each next token? |
| Softmax | Scores | Probabilities | How do scores become shares? |
2. Tokenization: text becomes pieces
A model does not multiply letters directly. A tokenizer converts text into a sequence of token IDs. A token may be a whole word, punctuation mark or reusable piece of a longer word depending on the tokenizer.
"students learn fast"
→ ["students", " learn", " fast"]
→ [8142, 2231, 991]3. Embeddings: IDs become coordinates
Token ID 8142 is only a label. An embedding table maps it to a vector. Imagine a tiny three-number vector even though real models use many more dimensions.
student → [0.8, 0.2, -0.4]
teacher → [0.7, 0.3, -0.2]
banana → [-0.1, 0.9, 0.5]4. Dot products are multiplication plus addition
Attention compares learned Query and Key vectors. A dot product multiplies matching positions and adds them. For Q=[1,2] and K=[3,4], the dot product is 1×3 + 2×4 = 11. Higher compatibility can become a higher attention score.
function dot(a, b) {
return a.reduce((sum, value, index) => sum + value * b[index], 0);
}
console.log(dot([1, 2], [3, 4])); // 115. Softmax turns arbitrary scores into percentages
function softmax(values) {
const max = Math.max(...values);
const exp = values.map((value) => Math.exp(value - max));
const total = exp.reduce((sum, value) => sum + value, 0);
return exp.map((value) => value / total);
}
console.log(softmax([2, 1, 0]));Subtracting the maximum value before exponentiation is a numerical-stability trick. The final values are positive and sum to approximately 1, so they can be interpreted as shares.
6. Query, Key and Value
| Vector | Intuition |
|---|---|
| Query | What this position is looking for |
| Key | What this position offers for matching |
| Value | Information to mix into the result |
The model learns matrices that transform each token representation into these three views. Attention scores come from Query–Key compatibility; the resulting weights mix the Values.
7. Why multiple attention heads?
One head gives one learned projection of relationships. Multiple heads let the layer model several kinds of relationships in parallel before combining them. Do not assume each head always has one clean human-readable purpose; the useful idea is parallel learned views.
8. Position still matters
The same words in a different order can mean something different. Transformer models therefore encode position, often using learned or rotary-style methods, so the representation includes order information.
9. The transformer block
- 1Attention
Positions exchange information.
- 2Residual path
Earlier representation is added back.
- 3Normalization
Keeps activations in a useful numerical regime.
- 4Feed-forward network
Transforms each position independently after attention.
- 5Repeat
Many blocks deepen the representation.
10. Causal masking: GPT cannot look into the future
During next-token training, the representation for position i is prevented from attending to later positions. This causal mask makes the task match generation: predict the next token using only earlier context.
11. The final logits and next-token probabilities
The final representation is projected to one score for every token in the vocabulary. Softmax converts those logits into a probability distribution. Decoding chooses a next token, appends it, and repeats.
12. Training
- 1Input
Feed token sequences.
- 2Predict
Produce next-token probabilities.
- 3Compare
Measure error against the actual next token.
- 4Backpropagate
Compute how parameters influenced the error.
- 5Update
Adjust parameters a small amount.
- 6Repeat
Do this across huge numbers of examples.
13. What this simplified lesson leaves out
| Topic | Why it matters |
|---|---|
| Tokenizer design | Changes sequence length and vocabulary behaviour |
| RoPE/position methods | Represent relative order efficiently |
| KV caching | Speeds autoregressive generation |
| Grouped-query attention | Reduces memory/computation in some architectures |
| Mixture of experts | Routes tokens through subsets of parameters |
| Quantization | Reduces memory and can speed inference |
| Instruction/preference tuning | Changes how pretrained models follow user intent |
14. A good mental model
An LLM is a learned numerical system that repeatedly transforms token representations and estimates which token is likely to come next. The scale is huge; the building blocks are understandable.
Primary references
Sources and further reading
Clarvo articles are independently written. These references let you inspect the original inspiration and primary or official sources used to deepen the guide.

