by Samy (Kondasamy Jayaraman)

Module 7 · Transformers

Advanced

What happens inside the model before its next word

A language model writes one token at a time. Each time, every token of the prompt becomes a list of numbers (a vector), attention lets each one weigh the tokens before it, and the last one ends up as a score for every token the model knows. Here you open a tiny one up and watch.

Read as

The whole idea in one example

The model scores every next token

The prompt

7 tokens

“We reply within five business …”

The start of a sentence from Larkspur’s guides.

What comes next

768 scored
  1. ␣days0.65
  2. ␣day0.15
  3. ␣business0.02
  4. ␣hours0.02
  5. ␣m0.01

Larkspur mini’s five likeliest, of all 768.

Pick one, add it to the prompt, run again, and a reply grows one token at a time. Larkspur mini is tiny: 51,072 numbers, trained only on Larkspur’s own documents. It runs for real in your browser. It shows how the machinery works; it is no provider’s model. For scale: Meta’s Llama 3 report (2024): its largest model has 405 billion learned numbers, 126 layers of 128 heads, vectors of 16,384 numbers, a vocabulary of 128,000 tokens and a window of up to 128K tokens.

How it works

Four ideas carry the module

  1. 1 One token at a time

    The model reads the prompt’s tokens and scores the next one

    Vaswani and colleagues introduced the Transformer in 2017, a network built on attention alone, dropping the building blocks earlier language networks relied on (recurrence and convolutions).

    Source, checked 30 October 2026

  2. 2 Attention

    Each token weighs the tokens before it

    Vaswani and colleagues (2017) compute attention in three moves: each token’s query is scored against every key, the scores become weights that add up to 1, and the weights mix the values.

    Source, checked 30 October 2026

  3. 3 The causal mask

    Token 4 looks only back, never ahead

    Vaswani and colleagues (2017) mask attention in the part of the Transformer that writes, so a position cannot look at the positions after it and each prediction depends only on what came before.

    Source, checked 30 October 2026

  4. 4 Heads and layers

    2 layers of 2 heads, each looking its own way

    Multi-head attention runs several heads side by side, so the model can attend to different kinds of information at once.

    Source, checked 21 October 2026

The story

Larkspur opens up a model

Larkspur Supply Co.’s team opens up “Larkspur mini”, a tiny model trained only on Larkspur’s own documents, and watches it work in the browser. You’re on the team that wants to know why it says what it says, and what a long prompt costs.

Played Tokens? Larkspur mini reads the same tokens as this site’s tokenizer there. RAG chapter 2 showed why you can’t send everything; chapter 6 here shows what a long prompt costs inside.

51,072
learned numbers in Larkspur mini
2 × 2
layers × heads
32
tokens in its window
44%
of unseen Larkspur text: the real next token is in its top 5

Your path

The chapters

  1. Chapter 1

    One token at a time

    What does a language model actually do with a prompt?

  2. Chapter 2

    Tokens become vectors

    How does a token id become numbers, and how does the model know where it sits?

  3. Chapter 3

    Attention

    How does each token gather what it needs from the tokens before it?

  4. Chapter 4

    Heads and layers

    Why many heads and layers, and how do their results combine?

  5. Chapter 5

    Choosing the next token

    How do temperature and top-p change which token gets picked?

  6. Chapter 6

    Why long prompts cost more

    Why does the work grow faster than the prompt, and what does the KV cache save?

  7. Chapter 7

    Trace a prompt

    Can you follow any prompt through the model, step by step?

Who it’s for

  • Engineers
  • Product managers
  • Analysts
  • No code needed

Engineers, product managers and analysts who use language models and want to see what happens inside one. It builds on Tokens & embeddings; any maths is optional.