Overview
Curated: · Written: · Reviewed:
LLM Fundamentals
In one line
A decoder-only LLM maps a token sequence to a next-token distribution, and generation is a sampling loop around it; reach for this model when you need to explain what sets cost, latency, context limits and what determinism you can actually promise. Everything outside the loop (validation, authorization, retrieval, tools) carries the guarantees the model cannot.
Key definitions
- Token: an integer ID from a fixed subword vocabulary; cost, latency and context limits are denominated in tokens.
- Logits and softmax: raw scores over the vocabulary, converted to a probability distribution for the next token.
- Causal mask: position i attends only to positions at or before i, so one forward pass trains every next-token prediction at once.
- KV cache: stored keys and values for prior positions, so each decode step computes only its own q/k/v.
- Nucleus (top-p) sampling: keep the smallest set of tokens whose cumulative probability reaches p, then renormalise and sample.
- Constrained decoding: restricts sampled tokens so output stays valid against a schema; guarantees syntax, not semantics.
Formulas and complexity
KV cache per sequence:
GPT-3-class geometry, full multi-head cache, 4k context, fp16:
Attention score matrix is n×n, so doubling context from 4k to 8k quadruples score computation. Temperature divides logits:
How it works
Prefill sets TTFT and grows with input length. Each decode step attends over the cache, which sets inter-token latency. Seeds fix only the sampling stream; batching, floating-point order and model revision still vary output.
When to use and when not to
| situation | use an LLM step | prefer deterministic code or a tool |
|---|---|---|
| open-ended synthesis or summarising text | yes, with schema-checked output | no |
| arithmetic, totals, prices | no, unless verified | yes |
| permissions, refunds, side effects | proposes only | authorises and executes |
| facts that change weekly | only with retrieval and citations | lookup against source of truth |
| stable format or tone | prompt first, tune only if prompts fail | n/a |
Traps interviewers probe
- Trusting temperature for accuracy. Lowering temperature narrows the sampled set; it does not make the top token correct. Fix: pin decoding for extraction, then validate against ground truth.
- Treating a seed as determinism. Batching and floating-point order still vary output. Fix: pin the revision and decoding, replay fixed inputs, and make the downstream contract deterministic.
- Validating syntax and calling it correct. JSON mode forces parseable output, not the right total. Fix: server-side range, enum and cross-field checks against the authoritative record.
- Estimating cost from English word counts. Code, CJK and emoji cost 1.5 to 4 times more tokens per unit of meaning. Fix: count with the model's exact tokenizer and chat template.
- Putting authorization in the prompt. Retrieved text can carry instructions. Fix: bind identity and limits outside the model and check every tool argument against them.
Interview tips
Open by naming the loop: tokenise, prefill, sample one token at a time, validate outside the model. Then say which component owns each guarantee.
Expect these follow-ups: why long context is expensive (quadratic scores in prefill, linear KV memory per sequence, the 19.3 GB arithmetic); how you make output deterministic (you don't; you make the contract deterministic); fine-tune, prompt or retrieve (facts outside weights, behaviour in prompts, stable format in tuning); and whether the product hallucinates (yes, by mechanism; here is grounding, abstention and review).
30-second outline: "Text becomes BPE tokens, which set cost and context. Prefill builds the KV cache in one parallel pass, which sets TTFT. Each step samples one token from temperature-scaled logits, using top-p for variety or greedy for extraction. The loop stops, and the output is schema-validated, checked against authoritative state, then billed by tokens. The model proposes; deterministic code decides."
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.