Skip to content
Friendly disclaimer: flozi00 TechHub is a solo side-project next to a full-time job — personal learning notes, no official statements. Verify critical steps yourself.

How LLMs Actually Work — An Animated Walkthrough

From token to probability distribution: embeddings, attention, training and sampling — explained without the equation jump, with animation.

6 min readflozi00
aillmtransformerattentiondeep-learningguide
Skip to the full article
Watch · explore · understand

Follow one token through the generation loop

An LLM turns token IDs into contextual vectors, scores possible next tokens and repeats with the selected token.

AI-assisted learning visualization · schematic article explanation

Step 1 / 4Represent the text as numbers

Tokenization produces vocabulary IDs. An embedding is a learned vector for an ID; it gives the network numerical features to process, rather than a hand-written definition of the word.

ArchitectureCurrent stepSelect a block to explain it
Input text
A tokenizer splits text into pieces, not necessarily whole words.
Token IDs
Each piece receives an index in the model’s vocabulary.
Learned embeddings
A lookup table turns IDs into numerical vectors.
  1. Input text Token IDs
    tokenize
  2. Token IDs Learned embeddings
    look up
What to remember

Generation repeatedly predicts one next token from numerical context; training is the separate process that learns the weights.

This is a causal Transformer overview. Exact tokenizers, positional methods, layer layouts and decoding rules vary by model.

Terms and the complete walkthrough
Embedding
A learned numerical vector used to represent a token.
Logit
An unnormalized score for a possible output token.
Autoregressive
Each next prediction is conditioned on the tokens already available.
  1. Represent the text as numbers

    Tokenization produces vocabulary IDs. An embedding is a learned vector for an ID; it gives the network numerical features to process, rather than a hand-written definition of the word.

  2. Build a context-dependent state

    Transformer layers combine attention, feed-forward networks, residual paths and normalization. Attention weights decide how to mix information from visible positions; they are not the probabilities of the next vocabulary token.

  3. Choose from the vocabulary

    The final hidden state produces scores called logits. A decoding rule chooses a token from these scores; sampling may first turn them into probabilities and adjust that distribution.

  4. Repeat with fixed model weights

    Append the selected ID and process the next position. A KV cache reuses earlier attention state, while the learned model weights remain fixed during ordinary generation.

All connections

  • Input text → Token IDs: tokenize
  • Token IDs → Learned embeddings: look up
  • Learned embeddings → Transformer layers: process vectors
  • Transformer layers → Next-token scores: output projection
  • Next-token scores → Select and append a token: choose
  • Select and append a token → Token IDs: append new ID
  • Transformer layers → Retained attention state: retain new KV
  • Retained attention state → Transformer layers: reuse past KV
Set your own pace. Pause at any time or select a step directly.

Start with the building blocks: Explore attention step by step, then follow KV-cache reuse. The visual learning path connects all three beginner lessons. The animation follows the prompt "The cat sat on the mat because it was" to an illustrative next-token choice. Its word chunks, attention links and probabilities are invented for teaching; they are not output from a measured model.

„The cat sat on the mat because it was“▍
LLM
tired62 %sleepy9 %

illustrative next-token probabilities

1 · One job: predict the next token

An autoregressive language model maps a token context to next-token scores, which can be turned into probabilities. Repeating this step generates text. The diagrams and percentages here are illustrative, not measurements from a model.

The one-sentence version

An autoregressive language model maps a sequence of tokens to next-token scores. Softmax can turn those scores into a probability distribution; repeated selection generates text. This explains the generation loop, though actual assistants also involve tokenization, multimodal inputs, post-training, tools and runtime orchestration.

If you internalize that sentence, the rest of this page is just mechanical detail about how the function is built.

Stage by stage

From letters to IDs

The model consumes token IDs produced by a tokenizer. Many LLM tokenizers use BPE variants, but vocabulary and token boundaries vary by model; a word may be one or several tokens. The animation colors whole words only to make the stages readable. Token counts for the same text can differ substantially across tokenizers and languages. See the Hugging Face tokenizer documentation.

One consequence that surprises people: the model has no built-in concept of a "word". Spelling tricks, homoglyphs, or unusual spacing can change the token stream — and therefore the computation — even when the meaning for a human is identical.

From IDs to geometry

Each token ID indexes a learned embedding vector. Contextual representations across later layers can capture semantic relationships; the particular nearest neighbors of raw input embeddings are model-dependent and are not measured in this animation.

Position enters the computation too. In models using rotary position embeddings (RoPE), position rotates query and key vectors within attention; it is not simply a vector added to each input embedding.

Attention: the actual idea

Attention is the load-bearing concept. For every token, the model computes three projections of its vector — a query, a key, and a value — and then:

  1. the query of token i is dotted against the keys it may see (in full causal self-attention, itself and earlier tokens),
  2. the scores are scaled and softmaxed into weights that sum to 1,
  3. the output is the weighted mixture of the values.

This is a weighted mixture of value vectors, as in the original Transformer paper. The thick line from “it” to “cat” is a possible teaching example; those weights were not measured, and a single attention weight is not a reliable explanation of a model's reasoning.

Full self-attention performs O(N²) query-key comparisons over an N-token prefill. Sliding-window attention changes which pairs are compared. IO-aware implementations such as FlashAttention reduce memory traffic without removing all of that arithmetic. During generation, KV-cache storage grows with the number of cached positions; grouped-query attention and KV quantization can reduce bytes per position.

Why depth beats width

Transformer blocks combine attention, feed-forward layers, normalization and residual connections; block count and order differ across models. Some mechanistic studies find features such as induction heads, but these findings do not assign a fixed role to every layer of every LLM.

This is also where the parameters live: the block's matrices scale with the square of the hidden size, so doubling a model's width roughly quadruples its parameters — which is why a 70B model needs ~140 GB of weight storage at BF16 where a 4B model needs ~8 GB (2 bytes per parameter, linear in parameter count). The LLM inference math guide covers the resulting memory and compute budgets, and the Qwen3-Next notes show how sparse MoE activation bends the rule.

Sampling: where "creativity" hides

The final hidden vector of the last position is projected to vocabulary scores, and softmax can convert them to probabilities. Sampling introduces variation; a fixed seed can reproduce outputs only when the rest of the numerical execution is deterministic:

  • Temperature rescales logits before softmax. Lower temperatures sharpen the distribution; higher temperatures flatten it. Neither setting guarantees safety or correctness.
  • Top-p / top-k truncate the distribution before sampling, cutting the long tail of nonsense tokens.

The animation's stage 6 shows exactly this: the same context, a distribution over candidate next tokens, and one sampled winner.

Training: predict, correct, repeat

Pre-training is embarrassingly simple at heart: show the model a chunk of text, ask it to predict each next token, measure how wrong it was (cross-entropy), and nudge all the matrices a tiny step in the direction that reduces the error (backpropagation + an optimizer). Repeat over trillions of tokens. There is no teacher that explains why — only the raw pressure of the next token.

Post-training may include supervised instruction fine-tuning and preference optimization such as RLHF or DPO. These methods shape behavior, but neither helpfulness nor factual accuracy follows automatically from them.

What the animation leaves out

Honesty pass — the parts the pretty version skips:

  • KV cache & decode mechanics. Real generation caches keys and values so each new token doesn't reprocess the whole prompt; that cache dominates serving memory at long context. The inference calculator lets you compute it.
  • MoE routing. Many current models (including Qwen3-Next) route each token through a subset of experts; the six stages stay identical, only the feed-forward step becomes sparse.
  • Multimodality. Images, audio and video may be encoded into model-specific token or embedding sequences; the encoders and fusion paths differ by architecture.
  • Reasoning models. "Thinking" tokens are still the same next-token loop — just run longer before answering. The machinery in this article doesn't change.

A released example is Mellum2.1: each token uses eight of 64 experts, while 21 local and seven global attention layers handle different distance ranges. Only the global layers apply YaRN position scaling. The architecture guide follows its actual tensor shapes and explains why active parameters do not equal resident weight memory.

A pretrained language-model backbone can also feed a decision head. Cloudflare Clef Omni encodes text, audio and visual inputs, then scores declared schema options after one prefill. Its wrapper replaces the vocabulary projection with an identity module and retains the output-embedding weights for option lookups, so no next-token sampling or decode loop follows.

Liquid d1-3B takes a different route: it keeps the tied vocabulary head, packs one shared state as a trunk with independent question branches, and merges branch attention exactly from partial log-sum-exp statistics. The guide derives which work is actually shared and why its returned confidence is not calibrated by default.

If you want the performance side — how all this maps to FLOPS, bandwidth, and euros — continue with the inference economics page, which builds on the same pipeline from the hardware's point of view.