by Kondasamy Jayaraman

Module 7 · Transformers

Advanced Tokens & embeddings recommended · by Kondasamy Jayaraman

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 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.

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, with no recurrence or convolutions.

    Source, checked 21 October 2026

  2. 2 Attention

    Each token weighs the tokens before it

    Scaled dot-product attention is softmax(QKᵀ / √d_k) V: each query is scored against every key, and the scores weight the values.

    Source, checked 21 October 2026

  3. 3 The causal mask

    Token 4 looks only back, never ahead

    The original Transformer's decoder masks attention so a position cannot look at the positions after it, which keeps generation one token at a time.

    Source, checked 21 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
numbers in Larkspur mini’s weights
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 something the model can compute with, and how does it know where the token 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 does a model have many heads and many layers, and how do their results come together?

  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.