# Foundations

> Language modeling, neural networks, information theory, tokens and embeddings, and optimizers: the lowest-level primitives of an LLM, and the reason behind almost every design decision that follows.

- URL: https://dmxsf.com/en/wiki/foundations/
- [DMXSF](https://dmxsf.com/en/about/)
- Updated: 2026-11-18

## After this chapter you will be able to

- Say what language modeling estimates, split a sentence's probability into per-token predictions with the chain rule, and write the cross-entropy loss and perplexity.
- Read the forward and backward pass of a neural network, and see how linear layers, nonlinearities and gradients make up the one machine used to train LLMs.
- Put loss, perplexity and the KL penalty into the language of information theory and convert between them.
- Explain how text is cut into tokens, how tokens become vectors, and how vocabulary size affects cost and quality.
- Choose an optimizer and a learning-rate schedule for training, and work through a checklist when the loss spikes or turns into NaN.

If Python or matrix dimensions are new to you, start with [Python and tensor shapes](https://dmxsf.com/en/wiki/foundations/tensors-and-shapes/). Calculate one entry by hand before running the layer.

LLMs look all-powerful, but their underlying task is plain: **given what came before, predict the next token**. This chapter takes that plain task apart and explains the primitives it uses: what the training objective is and why "predict the next word" can produce such a rich set of abilities; how the neural network that fits the objective is trained; the information theory behind loss, perplexity and KL divergence; how text is cut into tokens and tokens become vectors; and which optimizer and learning rate to train with.

The pages in this chapter are the shared language of every later chapter. The [Model Architecture](https://dmxsf.com/en/wiki/architecture/) chapter shows how these vectors are processed inside a Transformer, and the [Pre-training](https://dmxsf.com/en/wiki/pretraining/) chapter puts the loss and optimizers here into a full training pipeline. Readers who already know this material can check themselves against the "After this page you will be able to" box of each page and skip what they do not need.

[Chapter labs on GitHub](https://github.com/dmxsf/dmxsf/tree/main/labs/foundations)
