AI Engineering Reference/LLM Foundations

How LLMs Work (Inference, Not Training)

Autoregressive transformers, tokenization, context windows, temperature, and why "next-token prediction" explains both brilliance and brittleness.

2/5Overview: 30m

What you're actually talking to

An LLM at inference time is not a database, search engine, or reasoning engine. It is an autoregressive transformer that predicts the next token given everything before it. Every response — code, prose, JSON — is token-by-token sampling from a learned distribution.

Understanding this single fact explains most production surprises.

The inference stack (what matters to engineers)

Your prompt → Tokenizer → Context window → Transformer layers → Logits → Sampler → Output tokens
ComponentWhat it doesWhy you care
TokenizerSplits text into subword tokens (BPE/SentencePiece)Code tokenizes differently than English; costs scale with tokens, not characters
Context windowFixed-size attention over input + outputEverything must fit; long files get truncated silently
AttentionEach token attends to prior tokensOrder matters; information in the middle gets lost (lost-in-the-middle)
SamplerPicks next token from probability distributionTemperature, top-p control creativity vs determinism

You don't need to derive attention math. You need to know context is finite, tokens cost money, and sampling is probabilistic.

Autoregressive generation

The model generates left-to-right. It cannot "go back and fix" an earlier token. This is why:

  • It commits to wrong API names early and doubles down
  • Multi-step reasoning chains can derail at step 3
  • Structured output (JSON) breaks if one brace is wrong — it keeps generating plausible continuation

Mitigation: constrain output format (JSON mode, tool schemas), break tasks into smaller calls, verify outputs programmatically.

Key inference parameters

ParameterEffectProduction default
TemperatureHigher = more random0–0.3 for code; 0.7+ for brainstorming
Top-p (nucleus)Limits token pool to cumulative probability mass0.9–1.0 typical
Max tokensHard cap on output lengthSet explicitly; unbounded = runaway cost
Stop sequencesHalt generation on delimiterUseful for multi-turn tool loops

For code generation and agent workflows, low temperature + schema constraints beats clever prompting.

Context window economics

Mid-2026 frontier models offer 128k–1M token windows, but:

  • Latency scales with context length (not just output)
  • Cost scales linearly with input tokens on most APIs
  • Quality degrades in the middle of very long contexts

Rule: treat the context window as a budget, not a dump truck. Put the most important material at the beginning and end; summarize the middle.

What training gave you (without training yourself)

Pre-training teaches broad language patterns from internet-scale text. Post-training (RLHF, constitutional AI, instruction tuning) teaches helpfulness and format compliance. Neither teaches:

  • Your private codebase
  • Today's API version
  • Your team's conventions

That's why RAG, rules, and MCP exist — covered in later topics.

Interview framing

When asked "how do LLMs work?":

  1. Autoregressive next-token prediction over a fixed context window
  2. Tokenization determines cost and effective capacity
  3. Sampling parameters control determinism
  4. No built-in fact-checking — outputs are plausible, not guaranteed true

Senior signal: Say "I'd check token count before sending the whole monorepo" instead of "I'd give it all the context." Name the inference parameter you'd tune for a CI code-review bot vs a brainstorming session.

Further Reading

Hands-On Tasks (Optional)

Practical exercises — prompt drills, local MCP servers, or workflow design on paper. The goal is professional fluency, not model training.

  • Estimate a real prompt's token budget

    Take a 500-line source file from your repo. Paste it into an online tokenizer. Record token count, estimate cost at your team's model tier, and note which sections you'd trim first to fit a 128k context window.

    20m