AI Engineering

Chapter 35

How LLMs work

Start with a small scene. You type: “The cat sat on the.” A large language model does not “know cats” the way a person does. It has seen billions of similar fragments during training, and it answers a narrower question: given everything so far, what token is most likely next?

It might put high probability on “mat”, lower on “rug”, and a little on “roof”. You sample one token, append it, and ask again. That loop — predict, sample, append — is the entire runtime story. Essays, code, and multi-step plans are long chains of that same move.

How an LLM answers

Tokens, not words

Models do not read characters or whole English words by default. Text is cut into tokens: common words stay whole; rarer ones split into pieces. Limits and pricing are in tokens, not characters. Code with dense punctuation burns tokens differently than prose. Truncating the wrong end of a prompt can delete the instruction that mattered.

The forward pass in plain English

Modern LLMs are transformers. You do not need every matrix name, but you do need the plot:

  1. Tokens become vectors (embeddings) in a high-dimensional space.
  2. Attention lets each position look at other positions and decide what matters — “it” finds its noun; a function call finds its arguments.
  3. Layers of attention and feed-forward networks refine those vectors.
  4. The final layer scores every vocabulary token; softmax turns scores into probabilities.

Training taught those weights by predicting the next token on huge corpora, then usually instruction and preference tuning so the model follows requests instead of only continuing internet prose.

Sampling is a design choice

Generation controls you should explain
KnobWhat it changesWhen to turn it
TemperatureHow peaky vs flat the next-token distribution isLow for code/JSON; higher for brainstorming
Top-pSample only from a probable nucleusStabilize creative tasks
Max tokensHard stop on generation lengthAlways set in production
Stop sequencesEnd early on a markerTool protocols and field delimiters

Context window: the whole stage

Everything the model can see for this call — system instructions, tools, retrieved docs, chat history, the user message — shares one context window. There is no hidden long-term mind unless you build it. When the window fills, something must go. That constraint is why RAG, memory, and agents exist: they decide what earns a seat on stage.

What “reasoning” actually is

When a model works a problem step by step, it is not running a separate logic engine. It is generating intermediate tokens that make the eventual answer more likely — a scratchpad in the same stream. Asking for chain of thought often helps because those tokens reshape later predictions.

PYTHON
from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4.1-mini",
    temperature=0.2,
    messages=[
        {"role": "system", "content": "You are a precise interview coach. Prefer short, correct answers."},
        {"role": "user", "content": "In one paragraph, what is an LLM doing at inference time?"},
    ],
)
print(response.choices[0].message.content)

Failure modes interviewers love

  • Hallucination — fluent next tokens that are not grounded in evidence.
  • Context overflow — silently dropping early instructions when you stuff the window.
  • Prompt injection — untrusted retrieved text that tries to override system policy.
  • Distribution shift — the model never saw your jargon; without RAG it will guess.

Once you see the model as a conditioned token engine with a finite stage, the rest of this part becomes inevitable engineering: put the right tokens on stage, constrain the outputs, and verify against tools and documents.

Pretraining, then alignment

Pretraining is reading the internet (and more) to learn statistics of language. Alignment stages afterward — supervised instruction data, preference tuning, safety policies — bend the same engine toward being helpful and constrained. In system design talks, separate “what the weights know” from “what we put in the prompt today.” One changes rarely and costs a fortune; the other changes every deploy.

Multimodal note

Some models also take images or audio by mapping those inputs into the same kind of token stream. The story does not change: everything becomes conditioning for the next token. When you design products, ask whether vision is required or whether a captioning tool plus text RAG is enough.

Interview drill — LLMs in production

Production LLM questions: context, decoding, structure, cost, eval.

More drills in the Interview Lab.

Q1. Context window overflow

Conversation exceeds model context.

Asked at: All AI product interviews · Difficulty: Medium · Pattern: Context management

Answer
  1. Summarize older turns; pin entities into working state.
  2. Retrieve long-term memory instead of full history.
  3. Truncate least relevant; larger windows are only one lever.

See also Lab Q10 memory.

Q2. Structured output you can trust

Pipeline needs reliable JSON.

Asked at: Platform interviews · Difficulty: Medium · Pattern: JSON / schema

Answer

Constrained decoding / JSON schema; validate; retry with repair; for critical paths prefer deterministic code over free-form LLM.

Q3. Eval before ship

How do you know a prompt change is safe?

Asked at: OpenAI-adjacent / Google · Difficulty: Medium · Pattern: Offline gates

Answer
Eval pipeline

Gold sets, rubrics, regression gates, online A/B. Lab Q3.

Q4. Cost / latency routing

Biggest model on every query is too expensive.

Asked at: All companies · Difficulty: Medium · Pattern: Model routing

Answer

Router: small model for easy intents; large for hard; cache; trim context; stream for UX; hard max tokens.

Q5. Temperature & determinism

When temperature 0 vs higher?

Asked at: Applied ML · Difficulty: Easy · Pattern: Decoding

Answer

Low/0 for extraction and tool args; higher for brainstorming. Even temp 0 is not perfectly deterministic across infra — say so.