LLM basics for software engineers
~8 min read
Interview occasional
Why it matters
You don’t need to derive backpropagation to build good LLM-powered systems, but you do need an accurate mental model of what an LLM actually is: a next-token predictor with no persistent state, no built-in notion of “true,” and behavior that’s shaped entirely by training data and the text you feed it. Almost every weird failure mode engineers hit in production — hallucination, inconsistent formatting, sensitivity to prompt phrasing — traces back to this basic model. Get it right and a lot of “AI magic” becomes debuggable engineering.
Core concepts
- Autoregressive next-token prediction. An LLM is a function from a sequence of tokens to a probability distribution over the next token. Text is generated by repeatedly sampling from that distribution and appending the result. There is no separate “thinking” step unless the model or a wrapper explicitly creates one (see Agents & planning loops).
- Transformer architecture, at a high level. Input tokens are embedded into vectors, then passed through stacked layers of self-attention (lets each token look at every other token in context) and feed-forward blocks. Attention is what gives the model its ability to relate distant words in a prompt; it’s also why cost and latency scale with context length.
- Training has (roughly) two phases. Pretraining teaches the model to predict text at internet scale (this is where most “knowledge” and language ability come from). Post-training (SFT, RLHF/RLAIF, preference tuning — see Post-training) shapes the model into something that follows instructions and behaves like an assistant.
- Statelessness. A model has no memory between API calls. Every call is a fresh forward pass over whatever tokens you send it. “Conversation history” is an illusion maintained by the caller resending prior turns — this is the seed of everything in Memory and Context management.
- Sampling parameters matter. Temperature, top-p, and top-k control how the next-token distribution is turned into a choice. Temperature 0 (or near it) gives more deterministic, repeatable output — important for anything you want to test or eval.
- Hallucination isn’t a bug to patch, it’s the default. The model is optimized to produce plausible continuations, not verified ones. Grounding techniques (RAG, tool use, citations) exist because the base mechanism has no built-in fact-checker.
- Model families and sizes. “Bigger” generally buys more capability but costs more latency and money per token. In production you’re almost always picking the smallest/cheapest model that clears your quality bar, not the biggest one available.
Mental model
prompt tokens -> [ embed ] -> [ attention + FFN layers, x N ] -> distribution over next token
|
sample one token
|
append to sequence, repeat
Think of an LLM as a very expensive, very well-read autocomplete: everything downstream (agents, RAG, tools) is scaffolding built around this one core loop to make the autocomplete more useful and less wrong.
Interview questions
-
What does it mean that an LLM is “autoregressive,” and why does that explain why models can’t easily “take back” a token they’ve already generated? Answer: Each token is generated conditioned on all previous tokens, including the model’s own prior outputs. Once a token is emitted it becomes part of the context for every subsequent token, so an early mistake (e.g., committing to a wrong approach in the first sentence) tends to compound rather than self-correct within a single generation pass.
-
Why do LLMs hallucinate, and why can’t you just “fix” a model to stop doing it entirely? Answer: The model is trained to predict plausible next tokens, not to verify facts against an external source of truth. It has no built-in mechanism to distinguish “I recall this precisely” from “this pattern completes plausibly.” Mitigations (grounding via RAG, citations, lower temperature, verification steps) reduce the rate but don’t eliminate the underlying mechanism.
-
What’s the difference between pretraining and post-training, and why does it matter for how you prompt a model? Answer: Pretraining produces a raw completion engine over internet-scale text; post-training (instruction tuning, RLHF/RLAIF) shapes it to follow instructions, refuse certain requests, and format responses like an assistant. It matters because prompting a base (non-instruction-tuned) model behaves very differently — it will continue text rather than “answer” it.
-
Why does self-attention make longer context more expensive, and what’s the practical implication? Answer: Standard self-attention computes pairwise interactions between tokens, so compute/memory scales roughly quadratically with sequence length (mitigated in practice by optimized/sparse/windowed attention implementations, but cost still grows with context). Practically: stuffing huge amounts of irrelevant context into a prompt isn’t free — it costs latency and money, which is why context management and RAG exist.
Watch
- [1hr Talk] Intro to Large Language Models — Andrej Karpathy. A code-free, high-level walkthrough of what an LLM is and how it’s built, aimed squarely at engineers who want the real mental model.
-
[Transformers, the tech behind LLMs Deep Learning Chapter 5](https://www.youtube.com/watch?v=wjZofJX0v4M) — 3Blue1Brown. Visual, intuition-first walkthrough of embeddings, attention, and the transformer block.
Further reading
- Attention Is All You Need — the original transformer paper.
- The Illustrated Transformer — Jay Alammar’s diagram-heavy walkthrough of attention and the transformer block.