Interview prep

Crack the AI-engineer loop

Loops in 2025 and 2026 test five things: LLM system design, RAG debugging, evals, practical trade-offs and a working take-home with evaluation. This page is the revision layer over the book: the fast-track order, the 25 concepts you must explain cold, twelve design drills with a rubric, and every chapter's questions with a self-check.

Self-check: 0 of 163 questions marked as answerable without notes.

Fast track

If time is short, read in this order. Chapters 05, 07, 08 and 09 are worth a two-minute explanation each; read them after.

  1. 103Embeddings and Vector Space★★★written
  2. 210Inference Mechanics★★★written
  3. 311Serving, Model Selection and Economics★★★written
  4. 412Prompting Fundamentals★★★written
  5. 513Structured Output★★★written
  6. 615Information Retrieval Foundations★★★written
  7. 717RAG Pipelines★★★written
  8. 818Advanced and Agentic RAG★★★written
  9. 919Evals I: Methodology and Error Analysis★★★written
  10. 1020Evals II: Judges, RAG/Agent Evals and Continuous Evaluation★★★written
  11. 1121Tool Use / Function Calling★★★written
  12. 1222The Agent Loop and Architectures★★★written
  13. 1323Memory★★★written
  14. 1427Context Engineering★★★written
  15. 1531Cost and Latency Engineering★★★written
  16. 1633Security: Prompt Injection and Agent Security★★★written
  17. 1730Observability★★★written
  18. 1832Deployment and LLMOps★★★written

The core 25

The concepts that come up in almost every loop. Say each answer out loud in under a minute, then open the section to check yourself.

  1. 01Tokenization (BPE) and why token count drives cost and latency02.0302.09

    Models read tokens, not characters. BPE starts from bytes and repeatedly merges the most frequent adjacent pair in its training text until the vocabulary reaches a target size, so common words become one token and rare strings split into several. Everything is billed and computed per token: cost = input tokens x input price + output tokens x output price. Latency grows with prompt length (prefill) and output length (decode, one token per step). English runs roughly four characters per token; Hindi and code often need noticeably more tokens for the same content, so the same feature costs more. Count with the provider's tokenizer or usage fields, never len(text).

  2. 02Self-attention and its O(n²) cost05.0205.09

    Each token produces a query, key and value vector. Attention scores every query against every key with a scaled dot product, applies softmax and takes a weighted sum of the values, so any token can pull information from any earlier token in one step. The cost is the n by n score matrix: work grows with n squared, so doubling the context roughly quadruples attention compute. FlashAttention removes the n-squared memory by tiling, not the n-squared compute. In practice this is why long prompts slow prefill, why some providers charge more above a context threshold, and why you retrieve a few relevant passages instead of stuffing the whole corpus.

  3. 03Prefill vs decode10.0110.04

    Prefill runs the whole prompt through the model in one parallel pass and builds the KV cache. It is compute-bound and sets time to first token, roughly in proportion to prompt length. Decode then produces one token per forward pass, re-reading the weights and the growing KV cache each step, so it is memory-bandwidth-bound and sets time per output token. Total latency is roughly TTFT + output tokens x TPOT. The levers differ: shorter prompts and prefix caching cut prefill; fewer output tokens, smaller or quantized models and speculative decoding cut decode. Batching raises throughput but can raise each user's latency.

  4. 04KV cache and its memory math10.0210.13

    Each decode step needs the keys and values of every earlier token at every layer, so the KV cache stores them instead of recomputing. Size per token = 2 (K and V) x layers x KV heads x head dim x bytes per value. With 32 layers, 8 KV heads, head dim 128 and fp16, that is 2 x 32 x 8 x 128 x 2 = 128 KiB per token, so one 32K-token sequence needs about 4 GiB. That memory, not compute, caps how many concurrent requests fit on a GPU. Levers: grouped-query attention, KV-cache quantization, PagedAttention to avoid fragmentation, and sharing cached prefixes across requests.

  5. 05Context windows and lost in the middle12.1027.03

    The context window is the most tokens one call can hold, prompt plus output. A large window is not the same as a usable one. Liu et al. 2023, Lost in the Middle, found multi-document QA accuracy was best when the relevant passage sat at the start or end of the context and dropped when it sat in the middle, and later work on context rot shows quality falls as irrelevant tokens pile up. Every token is also paid for. So put instructions first, keep retrieval to a few reranked passages, place the strongest evidence near the edges, and compact long histories rather than trusting the window size.

  6. 06Sampling: temperature and top-p04.04

    The model outputs logits over the vocabulary, and softmax(logits / T) turns them into probabilities. A temperature T below 1 sharpens the distribution toward the top token, above 1 flattens it, and T = 0 is effectively greedy. Top-p keeps the smallest set of tokens whose cumulative probability reaches p, for example 0.9, renormalises and samples from that set, which cuts the long tail of unlikely tokens; top-k keeps a fixed count instead. Use low temperature for extraction, classification and judges, higher for brainstorming. Even T = 0 is not perfectly repeatable, and some newer models reject these parameters, so put decisions that must be repeatable in code.

  7. 07Embeddings and vector similarity03.0303.05

    An embedding model maps text to a fixed-length vector, trained contrastively so texts with similar meaning land close together. Similarity is usually cosine, dot(a, b) / (|a| |b|); for unit-normalised vectors cosine equals the dot product and gives the same ranking as L2 distance. A bi-encoder embeds queries and documents separately, so documents are embedded once offline and search becomes a nearest-neighbour lookup. Trade-offs: dimensions drive memory (1,024 float32 dimensions is 4 KB per vector), and vectors are only comparable within one model and version, so store the model id with every vector and re-embed the whole corpus when you switch.

  8. 08Chunking strategies17.0417.05

    Chunking splits documents into retrievable units, because one vector for a long document blurs its topics and the prompt can only hold a few passages. The baseline is fixed-size chunks of a few hundred tokens with some overlap. Structure-aware chunking splits on headings, sections or clauses and keeps tables and lists whole. Small-to-big indexes small chunks but hands the model the parent section. The trade-off: small chunks match precisely but lose context, large chunks keep context but dilute the vector and cost prompt tokens. Chunk count is about document tokens / (size - overlap). Pick the size by measuring recall on your own questions.

  9. 09Hybrid BM25 + dense retrieval15.0415.09

    BM25 scores exact term matches, weighted by how rare the term is (IDF), with term frequency saturating and long documents normalised; it is strong on identifiers, codes, names and rare words. Dense retrieval matches meaning and paraphrase but can miss an exact section number or product code. Hybrid search runs both legs in parallel and fuses the lists. Reciprocal rank fusion scores each document as the sum over legs of 1 / (k + rank), with k around 60, which avoids comparing incompatible score scales. It usually helps on mixed queries, but measure it: on a question-to-question corpus a BM25 leg can lower nDCG.

  10. 10Reranking15.1017.08

    First-stage retrievers, BM25 or a bi-encoder, score query and document separately, which is fast but coarse. A cross-encoder reranker reads the query and one candidate together through a transformer and outputs a relevance score. That is much more precise, but it costs one forward pass per pair, so it cannot scan the corpus. The pattern: retrieve a wide set of 50 to 100 candidates, rerank them, keep 5 to 10 for the prompt. Compute per query is roughly 2 x reranker parameters x candidates x pair tokens, which sets your GPU bill and adds tens of milliseconds or more. LLM listwise reranking trades more latency for quality.

  11. 11Recall@k, MRR and nDCG15.11

    Recall@k is the share of relevant documents found in the top k. It is the metric for the first stage, because a reranker cannot recover what was never retrieved. Precision@k is the share of the top k that is relevant. MRR averages 1 / rank of the first relevant result over queries, which suits tasks with one right answer. nDCG@k uses graded relevance with a log discount: DCG is the sum of (2^rel - 1) / log2(rank + 1), divided by the DCG of the ideal ordering. All of them need labelled judgments, ideally pooled blind from several systems, on a few hundred real queries.

  12. 12RAG vs fine-tuning vs prompting17.1309.01

    Start with prompting: cheapest and instant to change. Add RAG when the model lacks knowledge, especially private, changing or citable facts; the knowledge lives in an index you can update in minutes and trace to sources. Fine-tune when the problem is behaviour rather than knowledge: a strict format or style, a narrow classifier, or distilling a large model's outputs into a small cheap one. Fine-tuning is a poor way to add facts: they are slow to update, cannot be cited and can still be hallucinated. Long context suits small, stable corpora but is paid on every call. The options combine; let evals decide.

  13. 13LoRA and QLoRA09.05

    Full fine-tuning updates every weight and needs memory for weights, gradients and optimizer state, roughly 16 bytes per parameter with Adam in mixed precision. LoRA freezes the base model and learns a low-rank update for chosen weight matrices, W + BA, where B is d x r and A is r x d with rank r around 8 to 64, so trainable parameters per matrix fall from d squared to 2dr, often well under 1% of the model. QLoRA also stores the frozen base in 4-bit NF4 while training the adapters in higher precision, so a 7B model fits on one consumer-class GPU. Adapters are small and swappable.

  14. 14Quantization: INT8/INT4 trade-offs10.0809.03

    Quantization stores weights, and sometimes activations or the KV cache, in fewer bits: FP16 is 2 bytes per parameter, INT8 is 1 and INT4 is 0.5. A 70B model drops from about 140 GB to about 35 GB of weights in 4-bit, which changes how many GPUs you need. Because decode is memory-bandwidth-bound, fewer bytes also mean faster tokens. GPTQ and AWQ use calibration data to protect the weights that matter; GGUF targets CPUs and laptops. The cost is quality: 8-bit is usually close to lossless, 4-bit often acceptable, lower degrades, and the loss is uneven across tasks. Rerun your own evals on the quantized model.

  15. 15RLHF vs DPO07.0507.06

    Both teach a model human preferences from pairs of responses where a person preferred one. RLHF first trains a reward model on those pairs, then optimises the policy with reinforcement learning, usually PPO, to maximise reward minus a KL penalty that keeps it near the reference model. It works but is heavy: policy, reference, reward and value models in memory, and touchy training. DPO (Rafailov et al. 2023) shows the same objective can be optimised directly with a classification-style loss on the log-probability ratios of chosen versus rejected answers, with no reward model or RL loop. DPO is simpler and cheaper; RL still wins with verifiable rewards.

  16. 16Hallucination: causes and mitigations04.1029.06

    A model is trained to produce plausible text, not true text, so when it lacks knowledge it still writes fluently; post-training can reward confident answers and sampling adds variance. In applications the causes are missing or stale knowledge, retrieval that brought the wrong passage, long noisy context, and prompts that demand an answer. Mitigations stack: ground answers in retrieved sources with citations, give the model a legal way to abstain, verify in code that cited ids exist and numbers appear in the source, add a second checking call for named facts, keep temperature low for facts, and track faithfulness on an eval set. You reduce it; you never eliminate it.

  17. 17Structured output and tool calling13.0213.0921.02

    Structured output makes the model return data that matches a schema. JSON mode only guarantees valid JSON; schema-enforced or constrained decoding masks tokens that would break the schema, so fields and types hold, but you still validate meaning in code with Pydantic or Zod. Tool calling is the same mechanism aimed at actions: you describe functions with JSON schemas, the model returns a call with arguments, your code runs it and sends back the result. Use structured output when one call should produce data; use tools when the model must choose actions or fetch information mid-turn. Every tool definition costs input tokens on every call.

  18. 18Agent vs workflow22.01

    In a workflow your code owns the control flow: fixed steps such as classify, retrieve, generate and verify, with LLM calls inside them. In an agent the model decides the next step in a loop, choosing tools until it judges the task done. Workflows are cheaper, faster, easier to test and easy to bound. Agents handle open-ended tasks where the number and order of steps cannot be known in advance, at the price of variable latency and cost and new failure modes. Anthropic's Building Effective Agents (2024) gives the rule: use the simplest design that works, and add autonomy only where evals show a fixed pipeline failing.

  19. 19Agent loops and their failure modes22.0522.13

    The loop: call the model with the conversation and the tool definitions; if it returns tool calls, run them, append the results and call again; stop on a final answer or when a budget runs out. Failure modes: repeating the same call, swallowed or misread tool errors, context growing until quality and cost degrade, wrong tool or arguments, declaring success too early, and side effects repeated on retry. Defences: hard caps on steps, tokens, cost and wall time; errors returned to the model as readable results; idempotency keys; trimmed tool output; checkpoints for resuming; human approval before irreversible actions; and evals that grade the trajectory, not only the final answer.

  20. 20Agent memory23.0323.04

    Models are stateless, so memory is whatever you put back into the context. Short-term memory is the conversation buffer, cut to a window or compacted into a running summary as it grows. Long-term memory keeps facts or episodes outside the model, in a database, search index, vector store or files, and retrieves the relevant ones each turn. A common split is episodic (what happened, with dates), semantic (facts about the user or world) and procedural (how to do things, often the prompt). The hard parts are the write policy, conflicts and staleness, privacy and deletion, and proving with evals that recall improves answers.

  21. 21MCP26.0226.04

    The Model Context Protocol, published by Anthropic in late 2024, standardises how AI applications connect to tools and data, turning N apps times M integrations into N plus M. A host runs one MCP client per server; servers expose tools (actions), resources (readable data) and prompts (templates) over JSON-RPC, through stdio locally or streamable HTTP remotely, with OAuth for remote auth. The model still only sees tool definitions; MCP standardises discovery and transport. Trade-offs: each connected server's tool descriptions cost context tokens, and third-party servers add supply-chain and injection risks such as tool poisoning. For one internal integration, a direct API call is simpler.

  22. 22Eval design: golden sets, judge calibration, pass@k vs pass^k19.0720.0320.09

    Build a golden set from real traces: a few hundred cases stratified by type, including edge cases and questions with no valid answer, each with a reference or pass criteria. Prefer code checks (exact match, schema, citations exist) and use an LLM judge only for what code cannot check, with a binary or small rubric. Calibrate the judge against human labels and report true positive and true negative rates, not raw agreement. With per-attempt success p, pass@k = 1 - (1 - p)^k is the chance at least one of k tries passes, which suits generate-and-verify; pass^k = p^k is the chance all k pass, which measures agent reliability.

  23. 23Prompt injection, direct and indirect33.0233.0333.06

    Prompt injection is untrusted text being followed as instructions, because a model cannot reliably separate data from commands in one token stream. Direct injection is typed by the user, jailbreak-style. Indirect injection hides in content the system reads, such as web pages, emails, documents and tool results, and is worse because the user is the victim. Risk peaks with Simon Willison's lethal trifecta: access to private data, exposure to untrusted content, and a channel to send data out. Classifiers help but are not sufficient. Architectural defences: least-privilege tools, no exfiltration channels such as auto-loaded markdown images, human approval for side effects, and dual-LLM or plan-then-execute patterns.

  24. 24Guardrails29.0129.05

    Guardrails are checks around the model. On input: scope classification, moderation, PII redaction, injection screening and rate limits. On output: schema validation, citation and number checks against sources, policy filters, and length and format rules. Each guard costs latency and has false positives, so run cheap deterministic code first, run model-based checks in parallel with generation where you can, and decide per rule whether a failure is fixed in place, retried once with the violation stated, replaced by a safe template, or escalated to a human. Measure guards like classifiers, with precision and recall on real traffic, not only unit tests.

  25. 25Cost and latency levers: caching, batching, routing, streaming31.0331.0531.0731.08

    Cost per request = input tokens x input price + output tokens x output price, less cache discounts; multiply by volume. Cost levers: prompt caching for a stable prefix placed first, billed at a fraction of normal input; exact or semantic response caches for repeated questions; routing easy requests to a cheap model and escalating hard ones; batch APIs at a discount for anything not interactive; and trimmed context and capped output. Latency levers: streaming cuts perceived wait to time to first token, parallel calls overlap independent steps, and smaller models or lower reasoning effort cut decode time. Track cost per successful task, not per token.

System-design drills

Set a 45-minute timer, talk through the design out loud or on paper, then open the drill to compare. Every drill is graded on the same rubric.

  1. aScope: users, traffic, SLOs (p95 latency, cost per task, quality bar), what is out of scope.
  2. bBaseline first: the simplest architecture that works, drawn end to end, before anything clever.
  3. cChoices with trade-offs: retrieval, model per step, context, tools; say what you rejected and why.
  4. dEvals: offline set, metrics per stage, judge validation, how a change is accepted.
  5. eGuardrails and fallbacks: abstain paths, validation, injection, human escalation, provider failover.
  6. fObservability and cost: traces, per-task cost, dashboards, alerts, budgets.
  7. gNumbers: QPS, tokens per request, cost per 1K requests, latency budget per stage, index size.
  8. hCommunication: clarify first, narrate trade-offs, check in, time-box.
D01

Production RAG over 10M documents at 1K QPS

Design a question-answering system over 10 million internal documents that serves 1,000 queries per second at peak, with cited answers. Walk me through the architecture, the numbers and how you know it works.

Revise 15.09 · 15.10 · 16.03 · 16.09 · 17.04 · 17.11 · 31.03

D02

Customer-support agent with escalation

Design an AI agent that handles customer support chats for an e-commerce company: it answers policy questions, looks up orders, can issue small refunds, and hands off to a human when it should. How do you build it and how do you know it is safe to ship?

Revise 21.12 · 21.13 · 22.06 · 22.08 · 29.09 · 20.10 · 34.06

D03

AI coding agent or PR reviewer

Design an AI reviewer that comments on every pull request in a large company's repositories. Then tell me what changes if it is also allowed to push fixes.

Revise 28.03 · 28.05 · 18.08 · 21.10 · 20.11 · 28.11 · 33.03

D04

Voice assistant

Design a voice assistant that customers can call or talk to in the app, in Hindi, English or a mix, to check account status and do simple actions. It has to feel like a conversation, not an IVR menu.

Revise 14.03 · 14.04 · 14.05 · 31.08 · 10.04 · 29.07 · 29.03

D05

Deep-research agent with citations

Design an agent that takes a research question, searches the web for several minutes, and returns a report where every factual claim is cited. How do you make the citations trustworthy, and how do you keep cost under control?

Revise 18.06 · 18.07 · 25.02 · 25.04 · 17.09 · 20.07 · 31.12

D06

Text-to-SQL analyst assistant

Design an assistant that lets business users ask questions in plain English about the company's data warehouse and get back correct numbers, a chart and the SQL it ran. How do you make it correct and safe?

Revise 18.03 · 13.04 · 21.13 · 33.09 · 19.09 · 20.08 · 15.14

D07

Eval and CI pipeline for an agent

Your team ships an agent that plans, calls tools and writes answers for customers. Design the eval and CI pipeline so that a prompt, model or tool change cannot quietly make it worse.

Revise 19.07 · 19.13 · 20.03 · 20.09 · 20.13 · 20.14 · 32.11

D08

LLM gateway with routing, caching and fallbacks

Twenty internal teams call three LLM providers directly, each with its own keys and retry code. Design a shared LLM gateway that handles routing, caching, fallbacks, rate limits and cost attribution.

Revise 11.08 · 11.09 · 11.10 · 31.03 · 31.04 · 32.08 · 32.09 · 30.06

D09

Document extraction at scale

We receive about 2 million PDFs a month (invoices, contracts and scanned forms) and need structured fields out of each one, accurately enough to post into our finance system. Design the extraction pipeline.

Revise 13.02 · 13.05 · 13.07 · 13.10 · 14.02 · 19.09 · 31.07 · 32.06

D10

Cut LLM cost by 50% with no quality loss

Your LLM bill doubled last quarter. Finance wants it halved next quarter, and product says users must not notice any drop in quality. How do you do it?

Revise 11.05 · 31.01 · 31.02 · 31.03 · 31.05 · 31.06 · 31.07 · 31.10 · 31.11 · 31.14

D11

Memory for a long-lived assistant

Design memory for a personal assistant that people use every day for years. It should remember preferences, past conversations and facts about their life without becoming creepy, stale or expensive.

Revise 23.03 · 23.04 · 23.05 · 23.06 · 23.09 · 23.10 · 23.11 · 23.12 · 27.07

D12

Threat-model an email agent

We are building an agent that reads a user's inbox, drafts replies, schedules meetings and can send email on their behalf. Threat-model it and design the defences.

Revise 33.01 · 33.03 · 33.05 · 33.06 · 33.08 · 33.09 · 33.11 · 21.13 · 33.14

Every chapter's questions

Tick a question when you can answer it cold. Ticks are kept in this browser and shared with the chapter pages.

esc

Narrow with repo:jm layer:eval q:naive mine · ↑↓ to move, enter to open