Language becomes a sequence of tokens and vectors
A transformer language model does not read words or sentences the way a human reader experiences meaning. Before any neural computation occurs, an algorithm called a tokenizer—typically based on Byte-Pair Encoding (BPE) or unigram subword vocabularies—splits raw text into discrete chunks called tokens. A common English word like “science” may be a single token, whereas a rare compound term, a line of computer code, or an equation may be split into several subword fragments.
Each token ID in the vocabulary (often 50,000 to 250,000 entries) is mapped to a high-dimensional numeric vector called an embedding. Because a transformer processes all tokens in a context window in parallel rather than strictly left-to-right one step at a time like older recurrent neural networks (RNNs), the model injects positional encodings—such as Rotary Position Embeddings (RoPE)—so the network can distinguish “the dog chased the cat” from “the cat chased the dog.”
During pre-training on trillions of tokens of text and code, the network adjusts billions of internal matrix weights using stochastic gradient descent and backpropagation to minimize cross-entropy loss on next-token prediction. Compressing the statistical regularities of vast text corpora forces the internal layers to build rich geometric representations of syntax, coreference, domain terminology, and procedural patterns.
How scaled dot-product attention builds context
Introduced by Vaswani and colleagues in the 2017 paper *Attention Is All You Need*, the self-attention mechanism allows every token representation in a sequence to gather relevant information directly from earlier tokens regardless of how many words sit between them.
At each layer and within each attention head, the model projects every token vector into three distinct learned vectors:
- **A Query vector (Q):** representing what information the current position is looking for.
- **A Key vector (K):** representing an index label describing what information each earlier position holds.
- **A Value vector (V):** representing the content payload to be passed forward when a query matches a key.
The model computes dot products between the query of token i and the keys of all valid preceding tokens j, scales them by the square root of the head dimension (√dₖ) to keep gradients numerically stable, and applies a softmax function to produce attention weights that sum to 1. Multiplying those weights by the value vectors mixes context across the sequence.
Multi-head attention runs dozens of these projections in parallel at every layer: one head may track pronoun-noun agreement across a paragraph, another may track indentation and variable scope in Python code, and another may associate clinical symptoms with pharmacological terms. Alternating behind the attention blocks are position-wise feed-forward multilayer perceptrons (MLPs), residual skip connections, and layer normalizations that transform and store factual associations across dozens of stacked layers.
Autoregressive generation and why hallucinations happen
At inference time, an autoregressive transformer generates responses one token at a time. Given the user’s prompt tokens, a forward pass through the layers produces a probability distribution (logits) over the entire vocabulary for the very next token. Decoding algorithms—controlled by parameters such as temperature, top-p (nucleus sampling), or greedy selection—pick a token, append it to the prompt, and feed the extended sequence back in to predict the following token.
This architecture explains both why transformers write with extraordinary fluency and why they hallucinate:
- **No built-in truth oracle:** During pre-training and generation, the core mathematical objective rewards continuing a sequence in a way that is statistically plausible given the prompt and training distribution. A fabricated academic citation with a realistic-sounding author, journal name, and year often has high token-by-token probability even when the paper does not exist.
- **Exposure bias and snowballing errors:** Once the model commits to an incorrect intermediate claim or arithmetic slip early in a response, every subsequent token is conditioned on that mistake, often causing the model to rationalize rather than retract the error.
- **Context dilution in long windows:** Even when a model supports a 100,000- or 1,000,000-token context window, attention scores distributed across massive documents can suffer from “lost-in-the-middle” degradation or conflicting instructions injected inside untrusted retrieved text (prompt injection).
Post-training, retrieval, and tool verification
Modern production systems rarely deploy a raw pre-trained base model directly. Supervised fine-tuning (SFT), reinforcement learning from human feedback (RLHF), and reinforcement learning with verifiable rewards (RLVR) on mathematics and code train models to follow instructions, refuse harmful requests, and expend extra test-time “thinking” tokens checking intermediate steps before emitting a final answer.
Even with deliberate reasoning chains, a transformer remains a probabilistic estimator rather than a deterministic database. High-reliability applications therefore wrap the model in an engineering harness: Retrieval-Augmented Generation (RAG) supplies verbatim source passages for grounding; external calculators, Python sandboxes, and compilers execute and verify code or math; and human domain experts audit outputs whenever medical, legal, financial, or scientific accuracy is at stake.



