Jonas Hansen

How modern language models work

The whole article as a progression of ideas, one line each. Every step links to its full section.

From text to vectors

  1. TextA language model never reads letters directly: before it can do anything, text has to become a sequence of integers.
  2. TokenizationText is split into units from a fixed vocabulary: whole words, pieces of words, or raw bytes.
  3. Token IDsEach token is replaced by its position in the vocabulary. The number is an index, and carries no meaning of its own.
  4. EmbeddingsToken IDs select rows of a learned table of vectors: the model's starting point for each token, not its final word on meaning.
  5. Positional informationAttention on its own ignores word order, so position has to be injected. Most current models do it by rotating queries and keys (RoPE).

Inside the transformer

  1. Context-dependent representationsAs vectors pass through the network, each token's representation is rewritten using the tokens around it.
  2. Transformer layersA transformer is a stack of blocks. Each one lets tokens exchange information (attention), then transforms every token on its own (an MLP).
  3. Self-attentionEach token's representation gathers information from other positions in the context, weighted by how relevant the model computes them to be.
  4. Queries, keys and valuesEach token is projected into a query (what it looks for), a key (what it can be matched on) and a value (what it passes along).
  5. Dense attentionIn full (dense) attention every token can attend to every earlier token, so the work grows with the square of the sequence length.
  6. Sparse and sliding-window attentionSome attention layers restrict each token to a window or pattern of positions, trading direct reach for lower cost. It all happens inside the model.
  7. Context windowsThe context window is the most tokens a model can process at once: prompt, documents, tool results and its own output together.
  8. KV cacheDuring generation, keys and values already computed for earlier tokens are kept and reused, so each new token only computes its own.

Choosing the next token

  1. LogitsThe final representation at the last position is turned into one score per vocabulary entry: the logits.
  2. Probabilities and softmaxSoftmax turns the logits into a probability distribution over the entire vocabulary.
  3. Decoding and samplingGeneration is a loop: choose one token from the distribution, append it to the input, run the model again.
  4. Temperature, top-k and top-pTemperature reshapes the distribution; top-k and top-p cut off its unlikely tail before a token is drawn.
  5. What happens to the tokens not chosenNext-token candidates that were not chosen are normally discarded. The model does not keep them as alternative lines of reasoning.
  6. Multiple samples, search and verificationSystems can explore alternatives deliberately: generate several candidates, search over partial solutions, and check the results. It is an outer loop around generation.

Running a model yourself

  1. QuantizationQuantization stores a model's weights in fewer bits, such as 8 or 4 instead of 16, trading a little accuracy for a model that is smaller and faster to read.
  2. Running a model on your own hardwareWhether a model runs locally is mostly a question of memory: it must hold the weights and the KV cache, and generation speed is set by how fast those weights can be read.

Bringing in outside information

  1. Retrieval-augmented generationRAG searches an external collection at request time and places the most relevant passages in the context before the model answers.
  2. Retrieval versus attentionAttention mixes information among tokens already in the context. Retrieval decides which outside information gets into the context at all.
  3. External memoryAnything a system should remember beyond one context window has to be stored outside the model and read back in as tokens.
  4. Tool useA model can emit a structured request to call a tool. Software outside the model runs it and feeds the result back into the context.
  5. Model Context Protocol (MCP)MCP is an open protocol for exposing tools, data and prompts to AI applications. It standardises the connection, not the retrieval.
  6. Codebase-aware retrievalCoding agents find the relevant parts of a repository with search tools, such as text search, symbol indexes, embeddings or code graphs, and read them into context.

Learning, and what the words mean

  1. Inference versus trainingTraining changes a model's weights. Inference, meaning ordinary use, runs fixed weights and changes nothing in the model.
  2. HallucinationA model can state something false as fluently as something true, because it generates likely text and has no store of facts to check against.
  3. Self-training and synthetic dataModel outputs that have been filtered, checked or ranked can become training data for a later model: a learning loop outside generation.
  4. Recursive self-improvementThe idea that an AI system could improve its own capabilities, including its ability to improve itself. A research concept, not a current mechanism.
  5. AGI, and why definitions varyArtificial general intelligence has no single agreed definition, so any claim about it depends on which definition is being used.