← Back to Blog

Deep Learning

How Transformers Actually Work — A Visual Guide

Jun 2, 2026·2 min read·By Rahish Saifi

How Transformers Actually Work — A Visual Guide — cover illustration for AI with Rahish blog

Every major language model you use today is a transformer. Yet many tutorials stop at “attention lets tokens talk to each other.” That is true, but not enough when you are debugging context limits, tuning inference, or reading model cards.

This guide walks through the transformer block the way you would implement it: tensors in, tensors out, with just enough math to be precise.

The problem transformers solve

Before transformers, sequence models (RNNs, LSTMs) processed tokens one step at a time. Long-range dependencies were hard to learn, and parallelization across sequence length was poor. Transformers replace recurrence with self-attention: every token can directly attend to every other token in one pass.

Self-attention in plain language

For each token, the model asks: “Which other tokens should I pay attention to when updating my representation?” It computes three vectors per token—query (Q), key (K), and value (V)—via learned linear projections.

Attention scores are roughly softmax(QK^T / sqrt(d)), then multiplied by V. High scores mean “this token’s value should influence me strongly.” Multi-head attention runs several attention patterns in parallel, then concatenates and projects the result.

Positional information

Attention alone is permutation-invariant—it does not know word order. Transformers add positional encodings (sinusoidal in the original paper; learned or rotary in many modern LLMs) so “dog bites man” differs from “man bites dog.”

One encoder block

  1. Multi-head self-attention + residual connection + layer norm
  2. Feed-forward MLP (two linear layers with activation) + residual + layer norm

Decoder blocks add masked self-attention (tokens cannot see the future) and cross-attention to encoder states when doing seq2seq tasks. GPT-style models use decoder-only stacks.

Why this matters when you build

  • Context length is attention’s quadratic cost in sequence length—why long-context models need architectural tricks (sparse attention, sliding windows, etc.).
  • KV cache during inference stores keys and values for prior tokens so you do not recompute them each step—that is the main memory bottleneck in serving.
  • Fine-tuning often touches attention projections and layer norms first; understanding blocks helps you choose PEFT targets.

For the original research framing, read our Attention Is All You Need — paper explained. For applying models in apps, pair this with fine-tuning vs RAG.