Tokenwerk · The LLM textbook

A free, hands-on textbook on LLMs

Understand how language models do the math. Then build your own.

From a single neuron to a Transformer that writes text. No machine learning background needed: every formula comes after a worked example with real numbers, and every chapter has an experiment you can run in your browser.

Start with chapter 1

What you’ll be able to do

  • Explain it: What happens inside a Transformer, step by step – in your own words, with small numbers.
  • Work it out: Neurons, softmax, attention and backpropagation by hand, until the formulas feel obvious.
  • Build it: Your own GPT in PyTorch, written step by step: tokenizer, model, training, text generation and chat, on your own machine.

The path in seven parts

I · Understanding neural networks

  1. Your path to your own language model
  2. What does it mean for a program to learn?
  3. An artificial neuron you can compute by hand
  4. Why neurons need an activation function
  5. From neurons to a small network
  6. How do we measure a wrong answer?
  7. How numbers actually learn
  8. Learning through several layers

II · From numbers to language

  1. Vectors, matrices and tensor shapes
  2. From text to token IDs
  3. Language as a probability problem
  4. Your first real language model
  5. Embeddings and shared representations
  6. The learning goal: cross-entropy

III · Building the transformer

  1. Self-attention without the mystery
  2. Causality and multiple heads
  3. The complete transformer block

IV · Training and evaluating

  1. Data is part of the model
  2. Your first training loop
  3. Reading the loss and finding bugs
  4. From scores to text

V · Modern language models

  1. RMSNorm, RoPE and SwiGLU
  2. GQA, FlashAttention and the KV cache
  3. Precision and training memory
  4. From a text model to an answer model
  5. Evaluating answers honestly

VI · Thinking bigger

  1. Scaling up without calculating blindly
  2. What today's large systems need on top

VII · Build your LLM

  1. Your blueprint: a GPT in eight steps
  2. Step 1: The tokenizer
  3. Step 2: Preparing the data
  4. Step 3: Your first neural language model
  5. Step 4: The GPT model
  6. Step 5: Training
  7. Step 6: Generating text
  8. Step 7: From storyteller to chat
  9. Step 8: Modernizing
  10. Your own project: building further

Appendix · Reference

  1. Formula cheat sheet: math symbols in everyday language
  2. Glossary, formulas and sources

Frequently asked questions

Do I need a maths background?

Adding and multiplying is enough to get started. Every new piece of notation is explained before you need it, and always worked through with numbers first.

Do I need a GPU?

No. All the experiments run in your browser, and the Python project trains small models on an ordinary laptop. A GPU just makes bigger runs faster.

Will I be able to build ChatGPT afterwards?

You’ll understand the same building blocks and train a small model yourself. Large systems differ mainly in the amount of data, compute and polish – the last part of the book explains how.

Does it cost anything?

No. No account, no cookies, no tracking. Your reading progress stays in your browser.