Every LLM — GPT-4, Claude, Gemini, Llama — runs on this. Know attention cold. Know why hallucination happens. These are the most asked topics in 2026 SDE AI interviews.
Attention answers one question: "For each token, how much should I pay attention to every other token?"
"What information am I looking for?"
Each token asks a question about what context it needs
"What do I represent / offer?"
Each token broadcasts what information it contains
"What information do I actually contribute?"
The actual content passed to tokens that attend to me
Each token's output is a weighted combination of all tokens' values, where the weights reflect how relevant each token is to the current token.
Instead of one attention computation, run it h times in parallel (h = number of heads, typically 8–32).
Each head learns a different type of relationship between tokens:
The results from all heads are concatenated and projected back to the original dimension.
| Component | What It Does | Why It's Needed |
|---|---|---|
| Token Embedding | Converts token IDs (integers) to dense vectors | Gives the model a learnable numeric representation for each vocabulary item |
| Positional Encoding | Adds position information to each embedding | Attention has no built-in sense of order — without this, "dog bites man" = "man bites dog" |
| Multi-Head Self-Attention | Each token attends to all other tokens (decoder: only past) | Captures relationships between any two tokens, regardless of distance |
| Feed-Forward Network (FFN) | Two linear layers with ReLU/GELU in between, applied per token independently | Where "factual knowledge" is stored — acts as a key-value memory for facts |
| Layer Normalization | Normalize activations to mean=0, std=1 within each layer | Stabilizes training — prevents activations from exploding or vanishing |
| Residual Connections | Add the input directly to the output: output = f(x) + x | Creates gradient highways — gradients can flow directly to early layers, enabling very deep networks |
Bidirectional — each token attends to ALL tokens (past and future)
Training task: masked language modeling (predict masked tokens)
Best for:
Causal / Autoregressive — each token attends ONLY to past tokens
Training task: next-token prediction on enormous corpora
Best for:
A very large transformer decoder trained on a single task: predict the next token given all previous tokens. Trained on trillions of tokens from the internet, books, code, and more.
At certain model sizes, capabilities appear that were not present in smaller models: few-shot learning, chain-of-thought reasoning, arithmetic, code generation. These "emerge" — they are not explicitly trained for.
Too many steps for long sequences. Model can't learn long-range patterns efficiently.
Vocabulary explosion (millions of words). Can't handle rare words, new words, or different languages.
Start with individual characters. Iteratively merge the most frequently co-occurring pair into a new token. Repeat until vocabulary size is reached (typically 32K–100K tokens).
| Setting | Behavior | Best For |
|---|---|---|
| T = 0 (Greedy) | Always pick highest probability token. Deterministic. | Code generation, factual Q&A where consistency matters |
| T = 0.3–0.7 | Conservative sampling. High-prob tokens dominate. Coherent & focused. | Customer support, factual summaries |
| T = 1.0 | Sample from natural model distribution. Balanced. | General chat, default for most APIs |
| T = 1.5–2.0 | More random. Flatter distribution. Creative but may ramble. | Creative writing, brainstorming, poetry |
| Top-p = 0.9 | Sample from smallest set of tokens accounting for 90% of probability. Ignores long tail. | Most production systems — better than pure temperature |
Full fine-tuning a 7B model: ~28GB GPU memory, hours of training, expensive.
Keep original weights frozen. Add small trainable low-rank matrices to each attention layer:
Only A and B are trained. r=8 or r=16 is typical.
Result: 99%+ reduction in trainable parameters. A 7B model can be fine-tuned on a single consumer GPU (≈6GB VRAM with QLoRA).
Ground responses in retrieved documents. System prompt: "Answer ONLY using the provided context." If the fact isn't in the context, the model can't make it up from memory.
"Let's think step by step" forces reasoning before answering. Each step can be checked. Significantly reduces hallucination on factual and reasoning tasks.
System prompt: "If you're not certain, say 'I don't have information on that.' Never fabricate." This helps but doesn't eliminate hallucination.
Ask the model to cite sources. Then verify those sources actually exist in retrieved chunks. If a cited source doesn't exist → flag the response.