Series

How Language Models Generate Text

A nine-part series on tokenization, embeddings, attention, training, inference, and the limits of what the architecture explains.

9 installments

Start the series →

The first models I used for coding felt like a very good autocomplete. I already knew the one in VS Code: I would start a for loop and the IDE would complete names, parameters, and the occasional variable. The difference was that I could now write a comment such as “C function that packs parameters into Modbus” and get a complete implementation back. It often had problems, but it saved me a lot of work. I checked the logic, tested that it did what I needed, and moved on.

A few months later, I no longer needed to request each function. I could write the headers and definitions, and the model would generate the implementations. My mental model had not changed: it was still autocomplete, except it had moved from names to templates and then to functions.

A few days ago, I had a backlog waiting in a personal project. I opened an agent, pointed it at the project, and asked it to do all the tasks. Then I went to sleep. The next morning they were done; I reviewed the result and it worked. What really got me was looking at the design decisions: many were like the ones I would have made, and others covered things that had not even crossed my mind.

I could not understand how a vague instruction, a task list, and several hours of computation could produce all that. My label of “glorified text predictor” was no longer useful. Technically, the model was still predicting tokens. The problem was that the phrase did nothing to explain the gap between predicting the next token and building a coherent program inside an existing project.

I could still fit the code-writing itself into my idea of mechanical work. What I could not get my head around was the architectural decisions. Maybe it had simply repeated choices I had already made in the code and project history. Or maybe the existing architecture left few options, and those were the natural decisions for those tasks. Both explanations were possible. But did it matter in the end? The result could well have been the same whether I had been there or not.

It was a reality check. There was something I was not seeing. How much of what I did was mechanical enough for a machine to do it? I needed to understand what was happening, why it worked, and where it stopped working. I started researching and taking modern LLMs apart, piece by piece. The series starts with the smallest piece: a single prediction.

This series follows Hi! from its arrival as text to the model’s token-by-token response. Each installment examines one component: the problem it solves, how its design emerged, which operations it performs, and where its limits lie.

The order builds each idea on the previous one, but every installment also has its own question. You can read the complete series or return to a specific piece when you need to review it.

Part 1

How text enters the model

From characters to ids and from ids to vectors the network can operate on.

  1. 01
    How an LLM Generates Text Token by Token

    What next-token prediction means and what happens between submitting a message and receiving a response.

  2. 02
    How a Tokenizer Turns Text into Tokens

    Why tokenizers use subwords, how they learn a vocabulary, and what that means for errors, languages, characters, and context length.

  3. 03
    How Embedding Turns Each Token into a Vector

    From one-hot to distributed representations: how the table is learned, what its geometry means, and why it does not yet contain context.

Part 2

How context is constructed

How Attention exchanges information between positions and how the MLP transforms each one.

  1. 04
    How Attention Builds Context

    From RNN machine translation to Q, K, and V: why Attention emerged and how it compares positions, mixes information, and uses multiple heads.

    Not published
  2. 05
    What the MLP Does inside a Transformer

    Expansion, nonlinear activations, and blocks with independent weights: how every position is transformed after Attention.

    Not published
Part 3

How predictions are produced and learned

From the final vector to a chosen token and from prediction error to jointly adjusted weights.

  1. 06
    From the Final Vector to the Next Token

    How the LM head scores the entire vocabulary and how softmax, temperature, and sampling rules choose a continuation.

    Not published
  2. 07
    How a Language Model Learns Its Weights

    Next-token prediction, cross-entropy, and backpropagation: how embeddings, projections, MLPs, and the LM head are adjusted together.

    Not published
Part 4

How it works in practice

Optimizations that make inference viable and limits no optimization removes.

  1. 08
    How LLM Inference Is Optimized

    Prefill, decode, KV cache, memory, and speculative decoding: which optimizations make generation practical and what tradeoffs they introduce.

    Not published
  2. 09
    What Language Model Architecture Does Not Explain

    Hallucinations, multimodality, and interpretability: why knowing the operations does not mean understanding all the learned behavior.

    Not published

← All posts