Awesome Testing

Models · reviewed · reviewed Aug 30, 2026 · 3 min

How does a transformer work?

A transformer repeatedly lets token representations gather information from other positions through attention, transforms each position through a small neural network, and preserves a residual path across many layers.

Attention moves information between token positions; repeated residual updates build the representation used for prediction.

Interactive note 05

Follow one attention query

Choose the position that is reading. A causal mask hides every token to its right.

test mixes information from:

  1. The4%
  2. agent12%
  3. reads30%
  4. the10%
  5. failing36%
  6. test8%

These hand-authored weights illustrate one head in one causal layer. Real models learn many heads across many layers; attention weights alone do not explain the model’s decision.

The shortest useful picture

The tokenizer produces token IDs and the model looks up an initial embedding for each position. A transformer then applies a stack of blocks. Each block has two main transformations: attention lets positions read selected information from other positions, and a feed-forward network transforms each position independently. Residual connections add each result back to the running representation instead of replacing it. Normalization keeps activations at workable scales; architectures differ in whether it appears before or after each transformation.

Several token streams moving through repeated context-mixing, per-position transformation, and residual stages, with one blue path traced through the stack.

A Transformer block updates a running representation through repeated, structured transformations; it is not one undifferentiated “AI operation.”

flowchart LR
  T[Token embeddings + position] --> A[Self-attention]
  A --> R1[Add to residual stream]
  R1 --> F[Feed-forward network]
  F --> R2[Add to residual stream]
  R2 --> N[Next transformer block]

After many blocks, the representation at the prediction position is converted into a score for every vocabulary token. A decoding rule selects the next token, appends it, and the model runs again.

Attention as addressed reading

For each position, the layer computes a query describing what information that position is looking for. Every position also offers a key describing what it contains and a value carrying information that can be copied. Query–key compatibility becomes a set of weights; the weighted values are combined into an update.

One selected token tile connecting to earlier tiles with different strengths, emphasizing two contextual links in blue before producing an updated representation.

The selected position mixes information from permitted positions with different learned weights; strong connections are selective, not proof of a human-readable explanation.

This is easier to remember as three questions:

  1. What am I looking for? — query.
  2. What does each position advertise? — keys.
  3. What information should I take from matching positions? — values.

A head can learn a useful pattern, but engineers should not assume every attention weight has one clean human interpretation. Models use several heads per layer and many layers, while feed-forward networks and residual paths also carry computation.

Order, masking, and context

Attention alone does not know token order, so the model receives positional information. In a causal language model, a mask prevents a position from reading future tokens during next-token training. The representation of the same token can therefore change with its surrounding tokens, position, and the layers already applied.

The model does not search its training data when it answers. Training has shaped the parameters that control these transformations. At inference time, the transformer runs those learned operations on the current token context.

Attention weights are not explanations

Attention is not a database lookup, a citation system, or proof that a model “looked at” a passage in a human sense. A transformer is not the whole chat product: tokenization, sampling, prompts, tools, retrieval, safety controls, and the agent harness live around it.

The architecture also does not guarantee factuality or long-range reasoning. A larger context window permits more tokens to enter the computation, but it does not guarantee that every relevant detail will influence the answer correctly.

Evaluate the model beyond a diagram

Most product teams test the model's observable behaviour rather than individual attention heads. Freeze the tokenizer, prompt format, weights, decoding settings, and runtime; then evaluate exact contracts, semantic outcomes, important slices, latency, memory, and cost.

For architecture work, compare outputs against a small trusted implementation, test mask and positional boundaries, verify tensor shapes, and check numerical behaviour across devices and precisions. Use controlled sequences where the expected dependency is known. A visualization can help form a hypothesis, but an attention plot alone is not evidence that the model used information correctly.

For applications, include long contexts with distractors, reordered evidence, conflicting instructions, negation, and facts near context boundaries. Evaluate the final answer and any resulting effects, not only token probabilities.

Sources and further reading

  1. 01
    Attention Is All You NeedVaswani et al. · research · published Jun 12, 2017 · source checked Aug 30, 2026

    Primary architecture source for transformer attention, feed-forward layers, residual connections, and positional information.

  2. 02
    The Annotated TransformerHarvard NLP · guide · source checked Aug 30, 2026

    A code-oriented walkthrough that implements and explains the architecture from Attention Is All You Need.

  3. 03
    Language Modeling from ScratchStanford University · guide · source checked Aug 30, 2026

    A current engineering map from tokenizer and transformer construction through training, scaling, and inference.