/the study roomby harrycodecraft

AI ENGINEERING / LLMS

How LLMs Actually Work

From your words to an AI-generated response.

Follow the complete journey of a prompt as it becomes tokens, numerical vectors, contextual representations, and finally a generated answer.

Interactive lab

Trace a prompt through the model.

Change the input, select a token, and follow each transformation.

01 / Tokenization

Text becomes discrete pieces.

Each outlined chip is one illustrative token. Real token boundaries and IDs depend on the model's tokenizer.

02 / Embeddings

Token IDs look up learned vectors.

How

ID 125

d11.17
d2-0.32
d3-0.45
d4-0.72
d5-0.67
d60.99
[1.17, -0.32, -0.45, -0.72, -0.67, 0.99]

Vector space · first two dimensions

d1d2HowdoAImodelslearn?

A tiny projection of a high-dimensional vector; select any token above to inspect it.

03 / Positional encoding

Position adds order to each vector.

Token embedding1.17 · -0.32 · -0.45
Position signal · 00.84 · 0.05 · 0.00
Position-aware vector2.01 · -0.27 · -0.45

Position signals here are illustrative; real models may use learned positions or methods such as RoPE.

04 / Self-attention

Queries match keys to weight values.

Choose a query token

Q · query1.17 · -0.32 · -0.45What this token seeks
K · key-0.32 · -0.45 · -0.72What each token offers to match
V · value-0.45 · -0.72 · -0.67Information passed forward

Attention weights · softmax(QKᵀ / √dₖ)

How
0.40
do
0.07
AI
0.12
models
0.24
learn
0.07
?
0.10

Toy vectors and scores make the calculation visible; they are not outputs from a trained model.

05 / Multi-head attention

Parallel heads learn different relationships.

Head 01Nearby context

How do

Illustrative focus pattern

Head 02Long-range link

How models

Illustrative focus pattern

Head 03Token pattern

How AI

Illustrative focus pattern

06 / Transformer block

Attention and feed-forward layers repeat.

InputAttentionFFNOutputresidual connection

Token embeddings combine with position signals.

07 / Next-token prediction

Logits become a probability distribution.

1.0
learn2.4
46.6%
reason1.8
25.6%
predict1.3
15.5%
adapt0.6
7.7%
generalize0.1
4.7%

Logit → softmax(logit / temperature) → next-token probability. Higher temperature flattens the distribution.

08 / Autoregressive generation

The model appends one token, then predicts again.

Context How do AI models learn?

Ready for the first token…

Each step adds a token to the context used for the next prediction.