Inside a Transformer: From Tokens to Predictions
Understand tokens, embeddings, attention, training and inference without confusing fluent generation with knowledge.
- Trace a token through a decoder
- Understand context and KV caches
- Design a useful evaluation
Tokenization is the interface
A language model usually receives token IDs, not whole words. A tokenizer maps text to a vocabulary of pieces. The embedding table converts each ID into a learned vector. Position information distinguishes order; the same words in a different sequence may change the answer. Token counts depend on language, tokenizer and formatting, so character counts are only a rough proxy.
Attention builds context
A transformer layer uses query, key and value projections of its input representations. Dot products between queries and keys determine attention weights after scaling, masking and normalization. Weighted combinations of values are transformed and passed through further layers, along with residual connections and feed-forward operations. In a causal decoder the mask prevents access to future tokens.
Multiple attention heads let a layer process different relationships in parallel. Attention weights alone are not a reliable explanation of a model’s reasoning. The output projection converts final representations into logits over the vocabulary. Sampling or decoding selects a token, then repeats the process.
Training versus generation
Pretraining optimizes predictions across large datasets. Post-training may improve instruction following and other behaviors using additional objectives. At inference, model weights generally remain fixed while the context changes. A convincing response can be wrong because token prediction is not a database lookup. External retrieval and verification remain valuable.
Latency and memory
Prefill processes the supplied context. Decode produces subsequent tokens one at a time. A key/value cache avoids recomputing earlier attention projections during ordinary autoregressive decoding, but consumes memory that generally grows with sequence length. Quantization can reduce weight memory; actual quality and speed depend on hardware and implementation. Measure time to first token, output rate, total latency and quality with your workload.
Test the application, not the demo
Build a small benchmark with ordinary questions, ambiguous instructions, unsupported claims and long inputs. Preserve the prompt and model identifier for each result. Record factual errors, refusal behavior, task completion and latency. Do not treat the context window as a guarantee that the model uses every detail correctly.
Practice & self-check
Compare the same question with a concise prompt, an irrelevant long context and an evidence-rich context. Record answer quality and response time.
What does a causal mask do?
It prevents a token representation from attending to later tokens during causal modeling.
Does more context always improve accuracy?
No; irrelevant or contradictory context may degrade results.
