Note / Essay

Talking with AI, Part 8: Following a Token Through a Transformer

From tokenization and embeddings through attention, MLPs and residual streams to logits, softmax and next-token selection.

Part 8 of 11 in Talking with AI: From LLM Training to AGI

A conversation between Dr. Suttisak Denduangchai and ChatGPT-5.6 Reasoning Instant, translated from the original Thai chat. ChatGPT’s explanations and examples are preserved, with headings, tables and mathematical notation adjusted for reading on the web.

Read the complete series

Me: Tell me more.

ChatGPT-5.6 Reasoning Instant:

Sure. This time we will dissect an LLM from input until a token appears on the screen 🔬🧠

Suppose you type:

เมืองหลวงของญี่ปุ่นคือ — “The capital of Japan is”

and the model answers:

โตเกียว — “Tokyo”

What happens inside is not search database → find Tokyo; it is a pipeline roughly like this:

Text
 ↓
Tokenizer
 ↓
Token IDs
 ↓
Embeddings
 ↓
Transformer blocks × N
 ↓
Logits
 ↓
Softmax
 ↓
choose next token
 ↓
put the new token back in
 ↓
repeat

1. Tokenizer: split text first

An LLM does not read characters directly as humans do.

The text:

The cat is sleeping.

might be split roughly as:

["The", " cat", " is", " sleeping", "."]

But long or unusual words may be split:

"unbelievable"

→ ["un", "believ", "able"]

Thai is interesting too because we do not put spaces between every word. A Tokenizer therefore absolutely does not mean:

1 token = 1 word

A token may be:

a word
part of a word
a symbol
a space
a code fragment
a byte sequence
etc.

Each token is then mapped to an integer:

"The"   → 791
" cat"  → 8415
" is"   → 374

These numbers are called Token IDs.


2. But 8415 has no “meaning”

If:

cat = 8415
dog = 5672

that does not mean:

8415 is close to 5672
therefore cat is similar to dog

An ID is only an index.

So we need an Embedding.

Roughly:

Token ID 8415
     ↓
Embedding table
     ↓
[0.18, -0.42, 0.73, 0.11, ...]

One token becomes a high-dimensional vector.

This is called:

Embedding vector


3. Embeddings turn meaning into geometry

This is a beautiful idea in neural networks.

Things are not represented by descriptions:

CAT:
animal=true
legs=4
cute=maybe

They are represented in vector space.

Conceptually:

             animals
                 ↑
        cat •    │    • dog
                 │
            • tiger
                 │
─────────────────┼────────→ vehicles
                 │
                 │      • car
                 │
                 │    • truck

In reality there are thousands of dimensions, and representations change by layer, so this 2D picture is only intuition.


4. Position has to be included too

Look at these two sentences:

Dog bites man.

and

Man bites dog.

The tokens are similar, but the meanings are very different 🐕😱

So the model must know position/order too.

Modern architectures have several ways to encode positional information, such as positional encodings or relative/rotary position techniques.

Conceptually:

dog + position 1
bites + position 2
man + position 3

So it knows where each item is in the sequence.


5. Then it enters the Transformer Block

Now we have:

Token embeddings
       ↓
┌──────────────────┐
│ Transformer      │
│ Block 1          │
└────────┬─────────┘
         ↓
┌──────────────────┐
│ Transformer      │
│ Block 2          │
└────────┬─────────┘
         ↓
       ...
         ↓
┌──────────────────┐
│ Transformer      │
│ Block N          │
└──────────────────┘

At a high level, each block has important components such as:

Attention
+
MLP / Feed-forward network
+
Normalization
+
Residual connections

Actual architectures can differ, but this structure is a useful mental model.


6. Attention: “What should I look at now?”

Suppose:

John gave Peter his book because he trusted him.

When processing the words, the model must capture relationships such as:

he
↓
John?

him
↓
Peter?

Attention helps each position use information from other positions.

We have already discussed Q K V:

Q = Query
K = Key
V = Value

The formula:

Attention(Q,K,V) = softmax((QKᵀ) / √dₖ) V

In human language:

Query:
What information do I need?

Key:
How relevant is the information at each position?

Value:
If it is relevant, what information should I use?

7. Multi-Head Attention

There is not just one attention mechanism.

There are multiple heads:

             ┌→ Head 1
             │
Input ───────┼→ Head 2
             │
             ├→ Head 3
             │
             └→ Head ...

Each head can learn different relationship patterns.

Intuitively, some heads may be useful for:

pronoun → noun

function call → definition

opening bracket → closing bracket

country → capital

subject → verb

But be careful: we cannot always neatly label every head as “this is the Country Head.”

Reality is more distributed and messier than that.


8. Attention is not all of the Transformer

People often forget this.

There is another very large component:

MLP / Feed-forward network

Roughly:

Representation
     ↓
Linear
     ↓
Activation
     ↓
Linear
     ↓
New representation

If Attention is like:

bringing together information from other positions

then MLP can be viewed roughly as:

processing/transforming features at that position

Both work together through dozens or hundreds of layers.


9. The Residual Stream is the main road

This is a very interesting concept.

Rather than having each layer destroy the original representation and build a completely new one:

Layer1 → Layer2 → Layer3

there is a residual connection roughly:

x_new = x + f(x)

That is:

original information ───────────┐
                                 +
the layer computes something new ┘
                                 ↓
                         new representation

Each layer is therefore like adding information to a stream of representation.

It is like a whiteboard where many teams add annotations, rather than erase the board and rewrite it each time.


10. From an ordinary word, the representation becomes contextualized

Suppose the word:

bank

in:

I deposited money at the bank.

and:

We sat on the river bank.

At the start, the token may have similar embeddings.

But after passing through Transformer layers:

bank + money + deposit
        ↓
financial institution

In the other case:

bank + river + water
        ↓
river edge

So a token’s representation is not fixed.

It is contextualized.

This is one of the important capabilities of Transformers.


11. Now return to the Japan question

We give:

เมืองหลวงของญี่ปุ่นคือ

It passes through the layers.

The representation at the final position may encode information related to:

question
capital
Japan
geography
expected answer
Thai language

It then passes through the output projection.

We get numbers for every token in the vocabulary.

These are called:

Logits

Roughly:

The Thai labels in the following examples mean: โตเกียว (Tokyo), โอซาก้า (Osaka), เกียวโต (Kyoto), กรุงเทพฯ (Bangkok), แมว (cat), and อื่น ๆ (other).

Token             Logit

โตเกียว            15.8
โอซาก้า             9.1
เกียวโต             8.7
กรุงเทพฯ            2.1
แมว                -3.4
banana             -5.7
...

12. A Logit is not yet a probability

We pass logits through Softmax:

P_i = exp(z_i) / Σ_j exp(z_j)

Then we get roughly:

โตเกียว     94.2%
โอซาก้า      2.8%
เกียวโต      1.9%
อื่น ๆ       1.1%

This is:

P(next token | context)

The heart of what we began discussing in the first episode is back again 🔄


13. Then which token is chosen?

The simplest method:

Choose the token with the highest probability.

This is roughly called greedy decoding.

โตเกียว 94%
      ↓
choose "โตเกียว"

But if we do this all the time, output may be rigid and highly predictable.

So there is sampling.

For example:

A    50%
B    30%
C    15%
D     5%

We sample from the distribution.

A has the greatest chance, but B/C can also be selected.


14. Temperature 🔥

Temperature adjusts the sharpness of the probability distribution.

Low temperature:

A █████████████████ 90%
B ██                 7%
C █                  2%
D                    1%

Output tends to be more deterministic.

High temperature:

A ███████ 40%
B █████   28%
C ████    20%
D ██      12%

There is more diversity.

So there is a trade-off:

Low temperature
→ stable
→ predictable

High temperature
→ diverse
→ creative
→ but more likely to go off track

However, modern inference stacks may be more complex than turning temperature alone.


15. Once a token is chosen... it is not over

Suppose it chooses:

"โต"

If the tokenizer splits the Thai spelling โตเกียว (Tokyo) into pieces such as โต and เกียว, the model must continue:

เมืองหลวงของญี่ปุ่นคือ โต
                     ↓
                  Model
                     ↓
                    เกียว

Then:

เมืองหลวงของญี่ปุ่นคือ โตเกียว
                           ↓
                         Model
                           ↓
                           .

Then it may choose end-of-sequence.

So generation is a loop:

Context
   ↓
Transformer
   ↓
Next-token probabilities
   ↓
Choose token
   ↓
Append token
   │
   └────────────↺

One token at a time.


16. This is Autoregressive Generation

The word autoregressive is very important.

Previous output becomes input for the next round.

P(x₁,x₂,…,xₙ) = ∏ᵢ P(xᵢ | x₍<ᵢ₎)

So it is roughly:

Token 1
 ↓
Token 1 + Token 2
 ↓
Token 1 + Token 2 + Token 3
 ↓
...

This also explains one strange behavior.

If the model starts answering incorrectly:

France's capital is Lyon...

the following tokens are conditioned on the wrong answer it just generated.

It may therefore elegantly generate reasoning that supports that error 🫠


17. This is another source of Hallucination

Suppose the early distribution is close:

Correct path    45%
Wrong path      40%
Other           15%

By chance, it chooses the wrong path.

Then:

Wrong token
    ↓
becomes context
    ↓
Next prediction conditioned
on wrong information
    ↓
Wrong path becomes stronger

This produces a snowball effect.

This is one reason verification and tool use are so important.


18. How many times does ChatGPT use a Transformer for one answer?

If an answer has 1,000 output tokens, autoregressive generation fundamentally needs about 1,000 consecutive decoding steps.

But it uses a KV cache so it does not have to recalculate the Key/Value of every old token again from scratch.

Prompt
 ↓
Prefill
 ↓
KV Cache
 ↓
Generate token 1
 ↓
update cache
 ↓
Generate token 2
 ↓
update cache
 ↓
...

The first step, which processes a large prompt, is generally called prefill.

It then enters decode.


19. Prefill and Decode have different workload characteristics

Prefill

Read a large amount of context at once:

100,000 input tokens
       ↓
Transformer

It has high parallelism.

Decode

Generate:

token
↓
token
↓
token
↓
token

It has sequential dependencies.

This is one reason generation latency has a different nature from prompt processing.


20. Now we can follow a token from birth to death

"เมืองหลวงของญี่ปุ่นคือ"
           │
           ▼
       TOKENIZER
           │
           ▼
       Token IDs
           │
           ▼
       EMBEDDINGS
           │
           ▼
    Position information
           │
           ▼
┌──────────────────────────┐
│ Transformer Block 1      │
│ Attention + MLP          │
├──────────────────────────┤
│ Transformer Block 2      │
│ Attention + MLP          │
├──────────────────────────┤
│           ...            │
├──────────────────────────┤
│ Transformer Block N      │
└────────────┬─────────────┘
             ▼
          LOGITS
             │
             ▼
          SOFTMAX
             │
             ▼
 P(next token | context)
             │
             ▼
        DECODING
             │
             ▼
          "โตเกียว"
             │
             └────────↺

This is the LLM core at the center of all the systems we have discussed.

But now there is one thing worth correcting in our mental model one more layer:

ChatGPT, Claude, Gemini, or the coding agent we use do not necessarily equal this Transformer alone.

What we call an “AI model” in everyday life may be in a system with routing, reasoning, context construction, retrieval, memory, tool execution, verification, and serving infrastructure surrounding the neural network.

So the next valuable step is to separate three terms people often mix together:

LLM vs Model vs AI System/Agent

Then we will assemble everything we have learned into one picture, from when you press Send in ChatGPT until the first text begins to stream back onto the screen 🧠 → 🏭 → 📱


← Previous: Does an LLM Memorize, Learn or Guess?

Next: From an LLM to an AI System and Agent →

Read the series

  1. Part 1: What Does Training an LLM Mean?
  2. Part 2: How Can Predicting the Next Token Lead to Coding?
  3. Part 3: Context, Attention and Memory During a Conversation
  4. Part 4: Why More Thinking Can Improve an Answer
  5. Part 5: GPUs, VRAM and the Infrastructure Behind LLM Training
  6. Part 6: From a Base Model to an Assistant
  7. Part 7: Does an LLM Memorize, Learn or Guess?
  8. Part 8: Following a Token Through a Transformer (this episode)
  9. Part 9: From an LLM to an AI System and Agent
  10. Part 10: More Compute for Training or for Answering?
  11. Part 11: From More Capable LLMs to the Question of AGI

Back to Notes