Chapter 1 Reading Notes: Tactics — The Emerging LLM Stack

Notes on Chapter 1 of What We Learned from a Year of Building with LLMs: what the four building blocks of prompting, retrieval (RAG), workflow tuning, and evaluation and monitoring each say, and what needs revising more than two years later.

Published
Reading time
59 min read
Category
Reading notes
Tags
Translated from
the Chinese original
Contents33 sections

Book: What We Learned from a Year of Building with LLMs (O’Reilly, July 2024) Authors: Eugene Yan, Bryan Bischof, Charles Frye, Hamel Husain, Jason Liu, Shreya Shankar Scope of these notes: Chapter 1 (Prompting → Information retrieval/RAG → Workflow tuning → Evaluation and monitoring) Note: [Book] marks my summary of the book’s points; [Extension] marks background, examples, or changes since the book that I added. Numbers from the book are cited; for numbers in the extensions, refer to the original papers.


The book has three layers: Tactical → Operational → Strategic. Chapter 1 is the bottom layer and the most hands-on one. The question it answers is: once you have an LLM, what exactly do you do to turn a demo into a reliable product?

The authors’ answer is four building blocks, and they come in a specific order:

OrderBuilding blockMain point in one sentenceCore actions
①PromptingStart with prompting; don’t jump to fine-tuningThree basic skills + structured input/output + breaking prompts into smaller ones + crafting the context
②Retrieval / RAGFeed the model knowledge through retrieval rather than changing the modelWatch three metrics: relevance/density/detail; keyword search as the baseline; hybrid search
③Workflow TuningGo beyond a single prompt and break the task into a multi-step flowMulti-step flows, deterministic first, diversity, caching, when to fine-tune
④Evaluation & MonitoringWithout evaluation, none of the other three can be iterated onAssertion-based unit tests, LLM-as-Judge, the intern test, guarding against Goodhart’s law

A way of thinking that runs through the whole chapter: an LLM application = one probabilistic component + a surrounding shell of deterministic engineering. The model itself is random, unreliable, and given to lying; every technique (structured output, deterministic workflows, caching, guardrails, evals) is essentially a deterministic means of “boxing in” this probabilistic component. Once you grasp this thread, all the sections of the chapter connect.


Prompting

Start with prompting: neither underestimate nor overestimate it

[Book] The authors recommend starting with prompting when you develop a new application. It is easy to underestimate — with the right techniques it can take you far; and easy to overestimate — even with a great prompt, making it work reliably in the real world still requires a lot of engineering around the prompt.

[Extension] The economics behind this: the cost of iterating on a prompt is nearly zero (change a few lines, run an eval once), while fine-tuning, self-hosting, and switching models are each orders of magnitude more expensive. So the right approach is to climb the “cost ladder”:

Text
Change the prompt → add examples (n-shot) → add structural constraints → break into a multi-step flow → add retrieval (RAG) → caching → only then fine-tuning
Low cost ────────────────────────────────────────────────────▶ High cost

At each rung, first ask: “Has the previous rung really been exhausted?”

Three basic skills: n-shot, chain-of-thought, providing relevant resources

n-shot prompting + in-context learning

[Book] Give the model a few examples to demonstrate the task and align the output format. Four lessons:

  1. Don’t make n too small: with too few examples the model over-”anchors” on those examples and generalizes worse. A rule of thumb is n ≥ 5, and going up to dozens is fine.
  2. Examples should represent the real input distribution: for a movie summarizer, put in the different kinds of movies in the proportions you actually get.
  3. Complete “input–output” pairs are not required: often it is enough to show “what the expected output looks like”.
  4. If the model supports tool calling, the examples should also demonstrate tool use.

[Extension] Why does a small n cause “over-anchoring”? To the model, examples are not just “task instructions”; they also convey label distribution, format, tone, length, and even answer tendencies. Few examples means each one carries a lot of “weight”, so the model treats accidental features as rules. Related academic findings (not mentioned in the book):

  • Majority label bias / recency bias: Zhao et al. found that if most example answers share a label, or the last example has a certain label, the model leans noticeably toward that label (Zhao et al., 2021). → Takeaway: balance the labels in your examples and shuffle their order.
  • Order sensitivity: Lu et al. found that merely reordering the examples can swing the same model’s accuracy on the same task anywhere from near-random to SOTA (Lu et al., 2021). → Takeaway: treat example order as a hyperparameter that your evals have to cover.

Supplementary example (customer service ticket classification): suppose there are four categories — “refund/logistics/quality/other” — with n=4 and one example per category. The problem is that the catch-all “other” category is extremely hard to learn from a single example, so the model crams ambiguous tickets into the first three categories. Change to n=12, three per category, and deliberately include a few hard examples in “other” that “look like refunds but aren’t”; generalization improves noticeably. This is exactly what “examples should represent the real distribution + cover the boundaries” means.

Chain-of-thought (CoT)

[Book] Letting the model explain its reasoning before giving the final answer is like giving it a piece of “scratch paper”, so it doesn’t have to rely entirely on “memory”. The earliest zero-shot approach was to add “Let’s think step by step”, but the authors found that writing the CoT concretely is often more effective — adding one or two sentences of explicit step instructions significantly reduces hallucination.

Example (meeting minutes summarization): ① first list key decisions, action items, and owners in a scratch area; ② check the scratch area against the source for factual consistency; ③ finally synthesize a concise summary.

The authors are also candid: recent research questions whether CoT is as powerful as it is made out to be (the book links to Stechly et al.’s 2024 Chain of Thoughtlessness?), and what actually happens at inference time is also debated — but it is still a technique worth experimenting with.

[Extension] This is worth expanding on, because it is one of the places that has changed the most since the book came out.

TimeUnderstandingImpact on this chapter
2022Wei et al. proposed few-shot CoT (Wei et al., 2022); Kojima et al. proposed zero-shot CoT (Kojima et al., 2022)The source of this chapter’s techniques
2023Self-Consistency: sample multiple reasoning chains for the same question and vote, more stable than a single CoT (Wang et al., 2022)Not mentioned in the chapter; very practical
2023CoT is unfaithful: the reasons a model writes down are not necessarily the real reasons for its answer; CoT may be post-hoc rationalization (Turpin et al., 2023)The book says what happens at inference time is “debated” without citing a source; CoT unfaithfulness is one such debate
After Sep 2024Reasoning models (OpenAI o1, DeepSeek-R1, etc.) train “think before answering” into the model itself; the model decides how long to thinkThe marginal value of hand-written CoT drops, but “writing steps concretely” still applies to non-reasoning models and flow design

Practical conclusions (my judgment):

  • With ordinary instruction models, concretized CoT is still the most cost-effective technique, especially for tasks with clearly decomposable steps (extract → verify → synthesize).
  • With reasoning models, hand-holding “step one… step two…” is no longer the default: research has found it hurts performance on some tasks (Liu et al., 2024) and has mixed effects on others. State the goal and acceptance criteria clearly first; whether to add steps should be verified per task with evals.
  • CoT’s greatest engineering value is not “thinking more correctly” but exposing the intermediate artifacts — observable, assertable, and separately evaluable. This is fully consistent with 1.4, “breaking prompts into smaller ones”.

Supplementary example (CoT for Text-to-SQL): a naive prompt asks for SQL directly. A concretized CoT asks: first list the tables and columns involved in the question and explain why; then write out the JOIN relationships needed; then write the SQL; finally self-check “does every SELECT column have a clear source”. The intermediate artifact (the tables and columns involved) is exactly what can be used for assertion tests.

Providing relevant resources

[Book] Giving the model text snippets to use directly can expand its knowledge, reduce hallucination, and increase user trust; this is usually done through RAG. The key point: just stuffing the materials in is not enough — you also need to explicitly tell the model to prioritize these materials, cite them directly, and say so when the materials are insufficient.

[Extension] This is the most common “one sentence short” problem in RAG. Much poor RAG performance is not poor retrieval but instructions missing three things:

Text
1. Answer only based on <materials>; do not use your own knowledge.          ← constrain the knowledge source
2. State which material (by number) your answer is based on.                  ← force citation
3. If <materials> are insufficient to answer, explicitly say "the materials contain no relevant information".   ← allow "I don't know"

Point 3 corresponds directly to section 4.7, “LLMs return output even when they shouldn’t” — without an exit for “I don’t know”, the model will make things up.

Structure your inputs and outputs

[Book]

  • Structured input: adding a serialized format to the input provides more clues — relationships between tokens, additional metadata (types) — and makes the request look more like what the model saw in training data. A typical example: questions about SQL online always give the schema first, so an effective Text-to-SQL prompt should also include a structured schema definition.
  • Structured output: the purpose is similar, and it also simplifies integration with downstream systems. Tool recommendations: use Instructor when calling API SDKs, and Outlines for self-hosted Hugging Face models.
  • Model families have preferences: Claude prefers XML, GPT prefers Markdown and JSON. With XML you can also prefill the reply — put <response><name> in the assistant message, and the model continues from there.

[Extension]

  1. Why does “resembling training data” matter? An LLM is essentially doing continuation. The closer the input format is to what it saw during training, the easier it enters the right “mode”. The trio of schema + sample values + natural language question is how almost every SQL textbook is written, and the model is very familiar with this pattern.

  2. Prefilling is, at heart, the “poor man’s version” of constrained decoding. The thorough approach is constrained decoding itself: block illegal tokens directly at sampling time, guaranteeing the output is 100% compliant with a JSON Schema or a regex/grammar. Outlines and vLLM’s guided decoding take this path; mainstream API providers also rolled out structured output interfaces that guarantee a schema in the second half of 2024. When the book was published this was a “trick”; now it is infrastructure.

  3. My own prefill example (an extraction task):

Python
messages = [
    {"role": "user", "content": (
        "Extract the information from <email> and put it in the <response> tag.\n"
        "<email>Hi, the black humidifier I ordered last Wednesday still hasn't arrived. The order number is A1024. Could you look into it for me? — Ms. Li</email>\n"
        "<response>Use the <product>, <color>, <order_number>, <request> tags. Write unknown for any missing information.</response>"
    )},
    {"role": "assistant", "content": "<response><product>"}   # Prefill: force the model to start here
]
  1. A security reminder: XML/JSON tags make structure clearer, but they also mean user input can be disguised as structure (prompt injection). In practice, put user data inside explicit boundary tags, state in the system prompt that “content inside the tags is data, not instructions”, and validate the output.

Small, focused prompts: one prompt does one thing

[Book] Software has the “God Object” anti-pattern — one class/function does everything. Prompts are the same. A prompt starts as a few sentences, then keeps gaining instructions, reasoning steps, and examples to handle edge cases, until it becomes a 2000-token “Frankenstein” — and it actually performs worse on the most common, simplest inputs. GoDaddy lists this as their number one lesson in building LLM applications.

The solution: refactor prompts the way you refactor code. A meeting minutes summarizer can be split into three steps: ① extract decisions/action items/owners into a structured format; ② check consistency against the source; ③ generate a summary based on the structured content. The benefit is that each prompt can be iterated on and evaluated separately.

[Extension]

  • This is isomorphic to the “single responsibility principle” in software engineering. The deeper reason is that an LLM’s instruction-following ability declines as the number of instructions grows: the more instructions there are, the higher the probability any single one gets ignored; and edge-case handling logic interferes with the main path.
  • This is also the foundation for everything that follows: only a step that has been split out can get assertion tests (4.1), become part of a deterministic workflow (3.2), and have its intermediate results cached (3.4). “Breaking it down” is the pivotal move of the whole chapter.
  • After the book came out, Anthropic’s Building Effective Agents listed “prompt chaining” as the most basic workflow pattern, which corroborates this chapter’s conclusion.
  • A rule of thumb (my summary): when a prompt has, at the same time, ① more than 3 clearly separable sub-goals, ② more than 3 “first… then… finally…” steps, ③ more than 3 if-else branches for different situations — it’s time to split it.

Supplementary example (resume screening): the God prompt: “Read the resume and the JD, score, write strengths and weaknesses, give interview advice, and if the candidate has less than 3 years of experience then… if it’s a technical role then…”. After splitting: ① extract structured information from the resume (years, skills, projects); ② extract hard requirements and plus points from the JD; ③ compare item by item and output a table; ④ generate a conclusion based on the table. Each step is testable on its own, and the output of step ③ can be shown directly to HR as an explanation.

Craft your context tokens

[Book] Take a fresh look at how much context the model really needs from you. Be like Michelangelo — don’t pile things on; chisel away the excess. RAG’s job is to haul over every “block of marble that might be relevant”, but what have you done to pull out the part that is needed?

One very practical method: put the complete final prompt (with all concatenations, meta-prompts, and RAG results) on a blank page and read it through as an article. This is how the authors have caught redundancy, wording that contradicts itself, and bad formatting.

Another key is the structure of the context: a bag-of-docs is no help to a person, and there is no reason to think it helps an agent. Design the structure with care, bring out how the parts relate to each other, and make “extraction” as easy as you can.

[Extension]

  1. “Read the final prompt once” is the highest-value suggestion in this chapter, yet very few teams actually do it. The final prompt of a RAG application is often a blob of system prompt + conversation history + 10 chunks + user question, and because there are so many paths, no one has ever read the whole thing. I suggest making it a fixed step in the development process: before every release, randomly sample 20 real requests, export the complete prompts, and have a human read them.

  2. “Lost in the Middle”: Liu et al. found that models make good use of the beginning and end of long contexts, but information utilization drops noticeably in the middle (Liu et al., 2023). This directly supports “structure matters”: put the most important constraints and materials at the beginning and end, and add brief title/source/date metadata before each chunk rather than concatenating raw text.

  3. New evidence after the book: Chroma’s 2025 “Context Rot” research points out that even when a task fits entirely within the window capacity, model performance on simple tasks degrades systematically as the input grows longer. This provides firmer grounds than 2024 for “chiseling away the excess”: extra context is not free; it actively hurts performance.

  4. An example of structured chunks (my format):

Text
[Doc 3 | Source: product manual v2 | Updated: 2024-05]
Title: Return policy
Body: Unopened items can be returned within 30 days of receipt...

[Doc 7 | Source: online support chat logs | Updated: 2024-06]
Title: Return inquiry
Body: Customer asked whether an opened item can be returned; support replied...

Contrast: “Unopened items can be returned within 30 days of receipt… customer asked whether an opened item can be returned…” — the latter mixes two contradictory sources together, and the model has a hard time choosing correctly.


Information retrieval / RAG

RAG quality depends on three dimensions: relevance, information density, level of detail

[Book] RAG lets the model do in-context learning based on provided context, which is much cheaper and easier than fine-tuning. But the ceiling of RAG is the quality of the retrieved documents.

DimensionMeaningMetrics/examples in the book
RelevanceWhether what’s retrieved is on-topicRanking metrics like MRR and NDCG; a “shuffle the retrieval results” experiment to test the impact of ranking on downstream tasks
DensityGiven equal relevance, the more concise one with less filler is betterMovie reviews: highly upvoted reviews and editorial reviews are denser than the full set of reviews
DetailWhether the information given is enough for the model to understand the semanticsText-to-SQL: column names only → add column descriptions → add representative values; SQL accuracy improves step by step

[Extension]

① What do MRR / NDCG actually measure?

  • MRR (Mean Reciprocal Rank): for each query, take “the reciprocal of the rank of the first relevant result”, then average over all queries.

    Example: 3 queries, first relevant result at ranks 1, 2, 4 → MRR = (1/1 + 1/2 + 1/4) / 3 ≈ 0.58. The meaning is “how many results a user has to skip on average before hitting a relevant one”.

  • NDCG (Normalized Discounted Cumulative Gain): considers the relevance grades of all results, discounts by position (the further down, the smaller the contribution), then normalizes by the score of the ideal ranking; range 0–1. It comes from the 2002 work of Järvelin and Kekäläinen.

  • When to use which: only one correct answer needed (FAQ, exact fact queries) → MRR; multiple useful results and order matters (summarization, synthesis) → NDCG.

② The “shuffle experiment” is a clever causal test: use the same documents, only shuffle the order, and rerun the downstream task. If results get noticeably worse → ranking matters, and investing in reranking is worthwhile; if almost unchanged → either the model can pick on its own, or your retrieval results aren’t actually doing anything — the latter is a warning sign.

③ The cost and limits of adding detail: the more detail, the longer the context and the higher the cost, and “Context Rot” means more noise as well. The order I’ve found works: first add column/field descriptions (cheap and almost always effective), then add a few sample values (especially effective for enum-like and coded fields), and only last consider adding full documents.

Supplementary example (formula assistant for a low-code platform): a user asks “how do I calculate overdue days”. Given only a list of function names, the model invents a nonexistent function DATEDIFF; given each function’s signature, a one-sentence description, and an example, plus 3 real formulas from the platform as samples, accuracy improves significantly. This is a direct illustration of “level of detail”.

[Book] Demos of RAG on embeddings have become so popular that people forget what the field of information retrieval has built up over decades. Embeddings capture high-level semantic similarity, but they fail on specific keyword-style queries — names (like Ilya), abbreviations (like RAG), IDs (like claude-3-sonnet). BM25-style keyword search is built exactly for this. Users are already accustomed to keyword search behavior, and it frustrates them when the document they expect isn’t retrieved. Perplexity’s CEO and Sourcegraph’s CTO hold similar views: vectors don’t magically solve search, and making real improvements on top of BM25 is hard.

Two more advantages of keyword search: explainable (you can see which words matched); computationally efficient (Lucene and OpenSearch have been optimized for decades).

In most cases hybrid search is best: keywords handle exact matching, embeddings handle synonyms, hypernyms and hyponyms, typos, and multimodality. Shortwave has shared their RAG pipeline: query rewriting → keyword + embedding retrieval → ranking.

[Extension]

  1. Why do embeddings fail on names and IDs? Embeddings compress tokens into a dense vector whose goal is “semantic proximity”; rare proper nouns and IDs seldom appear in training data, so their vectors aren’t distinctive enough, and claude-3-sonnet and gpt-4-turbo may be very close in semantic space (both are “model names”). BM25 is exact term matching, and rare words get high IDF weights — it is exactly good where embeddings are weakest, hence complementary.

  2. A brief review of BM25 (Robertson & Zaragoza, 2009): for a query, each document’s score is the sum of the scores of each query term; each term’s score depends on ① the term’s frequency in the document (saturating growth, parameter k₁), ② the term’s rarity (IDF), and ③ document length normalization (parameter b). No training needed; tune two parameters and you’re live.

  3. How do you fuse the results of hybrid search? The most common way is RRF (Reciprocal Rank Fusion): for each document, take the reciprocal of its rank in each retrieval method and add them up (Cormack et al., 2009). No score normalization needed; simple and robust. A more advanced approach: after a coarse retrieval pass, add a cross-encoder reranker to finely rank the top 50 — this is what “reranking” means in the book, and it is now standard in RAG.

  4. A special note for Chinese (not in the book): Chinese has no spaces, so BM25 needs tokenization first (e.g. jieba) or bigram tokenization; proper nouns, brand names, and model numbers are often segmented incorrectly. In practice, you can run several retrievers, “tokenized BM25 + literal n-grams”, as a safety net. Also, the choice of Chinese embedding model (whether it was trained on Chinese corpora, whether it supports an instruction prefix) greatly affects recall; be sure to run evals on your own data rather than just looking at leaderboards.

  5. Query rewriting: the Shortwave case in the book mentions “query rewriting”. Common techniques: coreference resolution (replace “it” with the specific thing), multi-query expansion (rewrite one query from 3 angles), HyDE — first have the model write a hypothetical answer, then use that answer for embedding retrieval (Gao et al., 2022), because “answer-to-answer” embeddings are often closer than “question-to-answer”.

Supplementary example (enterprise IT knowledge base): a user asks “how do I fix error code ERR-4021”. Pure vector retrieval may return a pile of generic “error code handling guide” articles; BM25 hits the one containing “ERR-4021” in one shot. Conversely, for “my computer won’t turn on”, BM25 can’t find the article on “failure to power up”, but vector search can. Hybrid search wins both ways.

Injecting new knowledge: prefer RAG over fine-tuning

[Book] Both can give an LLM new knowledge and improve its performance on specific tasks, so which do you try first? Recent research favors RAG. In one study, RAG was pitted against unsupervised fine-tuning (continued pretraining): on MMLU subsets and current-events questions, RAG came out ahead more consistently, whether the knowledge had appeared in training or was entirely new. A second paper set RAG against supervised fine-tuning, using agricultural data, and again RAG brought the bigger gain, most clearly with GPT-4.

Beyond effectiveness, RAG’s engineering advantages:

  1. Cheap updates: maintaining a retrieval index is far cheaper than continued pretraining or fine-tuning.
  2. Deletable: if the index contains harmful content, just delete or edit the document; after fine-tuning, it’s hard to make the model “forget”.
  3. Controllable permissions: partitioned indexes ensure each tenant can only retrieve their own documents, with no leakage to other organizations.

[Extension]

  • Why is fine-tuning bad at adding knowledge? Once knowledge is “written into” the parameters, it is distributed, uneditable, and untraceable, and the model easily confuses “having learned related content” with “actually knowing this fact” — making it more likely to confidently make things up. RAG externalizes knowledge, and answers can come back with sources, which pairs well with the “force citation” point in 1.2.3.
  • A mnemonic: knowledge via RAG, behavior via fine-tuning. Need the model to “know new facts” → RAG; need the model to “speak in a specific way / output a specific DSL” → fine-tuning (see 3.5).
  • The two are not mutually exclusive: a common combination is “fine-tune a small model specifically for query rewriting/reranking/summarization, while RAG provides the facts”.
  • An often-overlooked risk: RAG quality depends on document quality. Documents that are outdated, contradict each other, or carry the wrong permissions get passed to users as-is. So operating RAG is essentially a document governance problem — far more important than swapping in another embedding model.

Long context won’t make RAG obsolete

[Book] Gemini 1.5 offers a context window of up to 10 million tokens, and some people declared that “big windows kill RAG” — just dump all your data in and chat. The authors’ view is that exaggerated reports of RAG’s death have been around for a long time, and they give three reasons:

  1. Selection is still needed: however big the window, it can never hold all your data; you still need to decide what to put in.
  2. Reasoning ability is unproven: apart from narrow tests like “needle in a haystack”, nothing convincing yet shows that a model can reason well across a context that large; without good retrieval and ranking, you bury the model in distractors, or even stuff the window with content that has nothing to do with the task.
  3. Cost: a Transformer’s inference cost grows with the square of the context length (or linearly in each of time and space). A model being able to read through your company’s whole Google Drive before each answer does not make that a good idea. The analogy is memory vs. disk: even with machines that have tens of terabytes of RAM, we still read and write disks.

Conclusion: the RAG pattern remains useful as windows grow.

[Extension] Looking back more than two years after the book, this section is the most “time-tested” part of the chapter.

The book’s predictionEvidence from 2025–2026
“Needle in a haystack ≠ long-text reasoning”NoLiMa (2025) shows that once the “literal overlap” shortcut is removed and semantic association is required, most models lose about half of their short-text baseline at 32K; RULER (Hsieh et al., 2024) shows models with a nominal 1M window may have an effective length far below the nominal value
“Models get drowned in distractors”The “Context Rot” research shows performance degrades non-uniformly with input length
“Cost is a problem”Dumping everything in vs. precisely retrieving 10K tokens differs greatly in per-request cost and time-to-first-token (blogs give different multiples, but the order-of-magnitude conclusion is consistent)

Today’s engineering consensus has converged on: not either/or, but routing by scenario, and combining the two.

ScenarioFirst choiceReason
Close reading of a single long document, contract/code reviewLong context, dump it inNeeds a “global view”; chunking breaks cross-clause relationships
Small, stable corpus (product FAQ)Long context + prompt cachingNo index, no maintenance
Enterprise knowledge base with tens of thousands of documentsRAGWon’t fit; needs permissions, auditing, citation tracing
Time-sensitive dataRAG / tool callingIncrementally update the index; no need to reread everything
Multi-hop relationships, corpus-wide summarizationGraphRAG / Agentic RAGPure vector chunking cuts the implicit relationships between documents

And the most popular combination pattern is exactly the book’s memory/disk analogy: first use RAG to narrow from a sea of documents to the Top-N, then put those N documents whole into the long window for cross-document reasoning — “retrieval finds, the window reads”.

A point the book doesn’t emphasize but is now critical: long context eats your cache. If every request carries 500K tokens of material, even with prefix caching the price and latency are far higher than 10K tokens of precise retrieval; and once the material updates, the cached prefix is invalidated.


Tuning and optimizing workflows

Multi-step “flows” bring huge improvements

[Book] Breaking one big prompt into several small ones gets better results. A typical example is AlphaCodium (competitive programming): moving from one prompt to a multi-step workflow took GPT-4’s pass@5 on CodeContests from 19% to 44%. The flow includes: ① reflect on the problem; ② reason about public tests; ③ generate multiple candidate solutions; ④ rank the solutions; ⑤ generate synthetic tests; ⑥ iterate on public and synthetic tests.

Small tasks + clear goals = the best agent/flow prompts. Not every step needs structured output, but structured output makes integration with orchestration systems much easier.

The authors give approaches worth trying:

  • Explicit planning steps, with as precise a spec as possible;
  • Predefine several plans to choose from;
  • Rewrite the user’s raw prompt into an agent prompt — note this step is lossy;
  • Organize agent behavior with linear chains, DAGs, or state machines; different scales suit different structures;
  • Plan validation: the plan includes instructions on “how to evaluate other agents’ answers”, ensuring the final assembly is done properly;
  • Prompt engineering with fixed upstream state: an agent’s prompt should be evaluated separately against each of the variants that may come from upstream.

[Extension]

  • AlphaCodium comes from Ridnik et al.’s 2024 work (Ridnik et al., 2024). Its flow essentially makes explicit how a software engineer solves a problem: understand → work backward from tests → multiple approaches → filter → create your own tests → iterate.
  • Why is multi-step better? Three reasons: ① each step’s prompt is shorter and more focused (back to 1.4); ② intermediate artifacts can be checked, rejected, and redone (self-correction); ③ multiple candidates + ranking introduces “search” rather than “one-shot generation”.
  • “Rewriting the user’s prompt is lossy” is an important point. The rewriter (model or human) drops details of the user’s intent and adds its own understanding. Suggestion: keep the original request after rewriting and pass it downstream; and in evals, specifically test “intent fidelity after rewriting”.
  • Related academic lineage: Plan-and-Solve (Wang et al., 2023) is an early representative of “write a plan first, then execute”; Tree-of-Thoughts goes further on “multiple candidates + evaluation + backtracking”. They share their roots with AlphaCodium’s approach.

Supplementary example (research report generation): single prompt: “write a research report on market X”. Multi-step flow: ① break the question into 5–7 sub-questions; ② retrieve + answer each sub-question; ③ check whether each sub-answer is supported by citations; ④ find and resolve contradictions between sub-answers; ⑤ generate an outline; ⑥ write the report according to the outline; ⑦ check every paragraph’s facts against the sub-answers. Steps ③ and ⑦ are “plan validation”.

Prefer deterministic workflows for now

[Book] Agents can respond dynamically to users and the environment, but their nondeterminism makes deployment hard. Every step can fail, and error recovery is poor, so the success rate of a multi-step task drops exponentially with the number of steps. This is why agent teams find a reliable agent so hard to ship.

A promising approach: have the agent generate a deterministic plan and then carry it out in a structured, repeatable way. Step one turns a high-level goal into a plan; after that, execution is deterministic. The benefits:

  1. The plans it produces can be reused as few-shot examples, or to fine-tune the agent;
  2. Executing deterministically makes the system more dependable and easier to test and debug, and a failure can be pinned to a particular step of the plan;
  3. A plan can be expressed as a DAG; compared with a static prompt, that is easier to follow and easier to adjust when new situations come up.

The authors also have a nice line: the people best at building agents may well be those who have mentored junior engineers, because producing a plan resembles guiding a newcomer: you hand a newcomer a clear goal and a specific plan, not a fuzzy, open-ended instruction, and agents should get the same treatment.

Conclusion: reliable agents come from more structured, more deterministic approaches, and from gathering data to improve the prompts and fine-tune the models. Without that, you get an agent that is “brilliant now and then, but a letdown for users on average”, and retention suffers.

[Extension]

① Working out the “exponential drop” makes it more tangible. Let each step’s success rate be p; the probability all n steps succeed is pⁿ:

Per-step success rate5 steps10 steps20 steps
95%77%60%36%
90%59%35%12%
80%33%11%1%

If the product requires end-to-end success ≥ 90%, then at 95% per step you can afford at most about 2 steps. This table is the mathematical root of all the “conservative” advice in this chapter.

② Why do deterministic workflows work? They replace part of the n in pⁿ from “model steps” with “code steps” (code’s success rate ≈ 100%). For example, if 6 of 10 steps become code, the model has only 4 steps left: 0.95⁴ ≈ 81% instead of 60%.

③ An extra benefit of “plan → execute”: the plan itself is readable, so you can let the user approve it before execution (human-in-the-loop), which is almost mandatory for high-risk operations (sending email, transferring money, deleting data).

④ Changes since the book (parts that need revising): from late 2024 into 2025, reasoning models and tool-use capabilities improved greatly, and end-to-end autonomous agents (writing code on their own, operating a computer on their own) are much stronger than when the book was written; protocols like MCP (released November 2024) have also standardized tool integration. So the “for now” in “prefer deterministic for now” is loosening. But my judgment is that the core principle hasn’t changed; only its applicable boundary has expanded:

  • Exploratory, low-risk tasks with a verifier (write code and run tests, search and research) → autonomous agents are now practical;
  • High-risk, irreversible, strongly regulated tasks → still deterministic workflows + approval.
  • And an autonomous agent’s “self-correction” ability depends precisely on external verifiers (compilers, tests, retrieval results), which is the same idea as AlphaCodium’s “generate synthetic tests and iterate” — the verifier is the source of reliability, not a smarter model itself.

⑤ A practical intermediate form: constrain the agent to a “finite state machine” — a fixed set of states, transitions decided by code, and the model only “fills in blanks” within each state. This is far more controllable than a fully open ReAct loop, and more flexible than a purely linear flow.

Beyond temperature: getting more diverse outputs

[Book] Suppose you’re doing product recommendations: recommend new products based on what a user has bought. After several runs you find the results too similar, so you raise the temperature. Temperature flattens the probability distribution over the next token, making less likely tokens more likely to be chosen. But raising temperature brings some failure modes:

  • Products in the catalog that would fit well never show up;
  • A few products in the training data highly correlated with the prompt are overrepresented;
  • With temperature too high, nonexistent products (or gibberish) appear.

That is, turning up the temperature gives no assurance that the model will sample from the distribution you hope for (uniform random, say). The authors give other ways to increase diversity:

  1. Adjust elements in the prompt: say the prompt template contains the purchase history; shuffle the order every time it is inserted, and the effect is marked.
  2. Maintain a list of recent outputs: tell the model explicitly not to recommend anything on the list, or, when an output resembles a recent recommendation, throw it out and sample again.
  3. Change the angle of the question: e.g. “pick a product the user will use often”, “pick a product the user would recommend to a friend” — the focus moves, and diversity moves with it.

[Extension]

  • Why isn’t temperature enough? Temperature only redistributes the probability mass of the distribution the model already has. If the model assigns most of the probability to “a few products”, raising temperature first introduces noise into the “tail” rather than moving probability mass evenly onto other good products. A problem with the shape of the distribution can’t be fixed by temperature. Also, the quality of long outputs collapses quickly as temperature rises, because every step is “flying without a net”.
  • Other sampling parameters: top-p (nucleus sampling) limits the candidate set; min-p (proposed in 2024) truncates dynamically by a proportion of the highest probability, and is reported to be more stable than top-p at high temperatures. But these all still operate within “the distribution the model already has”.
  • Diversity actually has three levels, with different solutions:
LevelProblemSolution
Sampling levelSame prompt, similar outputstemperature / top-p / min-p
Prompt levelOutputs stuck in one patternThe book’s shuffling, changing angles, exclusion lists
System levelNo diversity constraintMMR (maximal marginal relevance) at retrieval time, take one per cluster after clustering; deduplicate with embeddings
  • Engineering details of dedup/exclusion lists: string matching isn’t enough; “genuine leather sofa” and “leather three-seater sofa” are semantic duplicates. In practice, use embedding cosine similarity (threshold around 0.85–0.9, tune per data) for semantic dedup against “recent outputs”; it’s far more effective than exact matching.
  • The diversity vs. relevance tradeoff: diversity should aim for “diversity under a relevance constraint” — first ensure every candidate is relevant, then seek difference among them. The book’s “different question angles” is exactly moving the distribution while staying relevant.

Caching is underrated

[Book] By not generating again for an input it has already seen, caching saves money and takes generation latency out of the picture. And when a reply has already been through the guardrail checks, you can hand out that checked reply as it is, which lowers the risk of serving harmful or inappropriate content.

  • The most direct approach: give each item being processed (an article, a product review) a unique ID. When a request arrives, look in the cache for its summary first: if there is one, send it back right away; otherwise generate → run the guardrails → return → write it to the cache.
  • Open-ended queries can be cached too, borrowing from what the search field does. Autocomplete and spelling correction and similar features can also normalize what users type, which raises the cache hit rate.

[Extension] There are actually four kinds of “cache” in LLM applications; don’t conflate them:

TypeWhat is cachedHit conditionTypical tools/mechanisms
Exact cacheThe complete responseInput exactly matches (ID or hash)Redis etc.; this is mainly what the book describes
Semantic cacheThe responseThe input’s embedding is close enough to a past questionGPTCache etc.
Prompt/prefix cacheKV cache (attention computation for the prefix)The request prefix matches a previous oneProvider prompt caching (mainstream providers added support from late 2024)
Embedding cacheVectors of documents/queriesText matchesSelf-built; common in RAG systems
  • Be careful with semantic caching: similar questions ≠ same answers. “How much is the iPhone 15” and “How much is the iPhone 16” are close in embedding space but have completely different answers. Set the threshold loose and you return wrong answers; set it strict and the hit rate is near zero. Suggestion: use it only in scenarios where answers are stable and question phrasing varies (FAQ), and add a hard constraint that “key entities match”.
  • Prefix caching changes how prompts are written: it requires stable content first, dynamic content last (system prompt → tool definitions → knowledge materials → conversation history → current question), and the materials should be as stable as possible, or the cache is invalidated. This is a new constraint on “context structure” that appeared after the book, and it complements 1.5.
  • The interplay of caching and guardrails is a clever point in the book: what’s cached is not just the “answer” but the “already-vetted state”. In compliance-sensitive scenarios this is worth more than the cost savings.
  • Cache invalidation strategy: knowledge updates (prices changed) require proactive invalidation — generating the cache key from the source document’s version number is the simplest reliable approach.

When to fine-tune

[Book] Some tasks can’t be done no matter how cleverly you design the prompt. If after extensive prompt engineering the system is still far from “reliable, high-quality”, you may need to fine-tune a model for the task. Two success stories:

  • Honeycomb’s natural language query assistant: initially they put the “programming manual” and n-shot examples into the prompt, and it worked passably; after fine-tuning, the model did better at following the domain-specific language’s syntax and rules.
  • ReChat’s Lucy: the model had to produce a particular format that mixes structured and unstructured data for frontend rendering; fine-tuning was the key to consistent behavior.

But fine-tuning has significant costs: labeling data, training and evaluating the model, and ultimately self-hosting. So think carefully about whether the upfront investment is worth it. When a prompt alone takes you to 90%, fine-tuning may not pay off. If you do decide to fine-tune, you can cut the cost of manual labeling with synthetic data, or by bootstrapping from open-source data.

[Extension]

  • When to fine-tune (behavior problems): strict output format/DSL, brand voice, a fixed reasoning pattern, distilling a large model’s capability into a small model to reduce latency and cost, shortening the prompt (saving tokens).
  • When not to fine-tune: adding new knowledge (→ RAG, see 2.3), tasks that change frequently, lacking hundreds to thousands of high-quality samples, lacking the ability to maintain self-hosted infrastructure.
  • A pre-fine-tuning checklist (my summary):
    1. Have prompting and RAG both been tried and reached their limits?
    2. Do you already have at least several hundred (ideally a thousand or more) real, high-quality input–output samples?
    3. Is the eval set in place (otherwise you can’t tell whether fine-tuning is actually better)?
    4. Is the failure mode “doesn’t know how” rather than “doesn’t know”?
    5. Can you afford the ongoing model operations (versioning, rollback, monitoring)?
  • Technical choices: PEFT methods (like LoRA / QLoRA) have greatly lowered the bar for fine-tuning; a single consumer GPU or a single workstation suffices. Distilling a 7B-class small model for a single task is often faster and cheaper than calling a flagship API.
  • The synthetic data route: use a flagship model to generate outputs from real inputs, manually spot-check them, and use the passing ones as training data; or “prompt first, fine-tune later” — distill the behavior of a long prompt into the model (prompt brevity).

Evaluation and monitoring

[Book] LLM inputs and outputs are arbitrary text and the tasks vary widely, so evaluation is a minefield. But careful, thoughtful evaluation is key: OpenAI’s technical leads take part in evaluation work themselves and comment on individual evals, and that is no accident.

There are several perspectives on evaluating LLM applications: it’s like unit testing, or more like observability, or just data science. The authors find every one of them useful.

[Extension] These three perspectives correspond to three time scales of evaluation, and in practice you should have all three:

PerspectiveTime scaleExamplesConsequence of failure
Unit testingEvery prompt/model changeAssertions, regression testsA change introduces a regression and nobody notices
Observability (monitoring)Continuous, in productionUser feedback, refusal rate, guardrail trigger rateData drift, model providers quietly changing things
Data analysisPeriodicError analysis, metrics by sliceNot knowing what to improve first

Build a small set of assertion-based unit tests from real input/output samples

[Book] Take input/output samples from production and turn them into unit tests (that is, assertions), with what you expect of an output resting on at least three criteria. Three may look like an arbitrary number, but it is a practical place to start; if you have fewer than three, the task may not be clearly defined, or it may be too open (a general chatbot, say). Every change to the pipeline (editing a prompt, bringing in new context through RAG, and so on) should set off these tests.

Suggestions for getting started:

  • Start with assertions that “all replies must contain / must not contain certain phrases or viewpoints”;
  • Check that word counts, item counts, or sentence counts fall within a range;
  • For other generation tasks, the assertions take other forms. Execution-evaluation is a strong tool for judging generated code: execute it and check whether the resulting runtime state meets what the user asked for. If the user asks for a function foo, for example, foo ought to be callable once the code has run. The hard part is that code written by an agent often leaves the runtime state a little different from what the target code would; what works is to “relax” the assertion down to the weakest assumption that every workable answer has to meet;
  • Dogfooding: use the product the way customers do. It turns up failure modes on real data, and it also gives you samples from production to turn into evals.

[Extension] An example of assertion-based tests (meeting minutes, my format):

Python
def test_summary_assertions(sample):
    out = run_pipeline(sample.transcript)
    # 1. Must include every owner name that appears in the source
    for owner in sample.expected_owners:
        assert owner in out
    # 2. Must not contain person names not in the source (hallucination guard)
    assert not has_unknown_person(out, sample.transcript)
    # 3. Structural constraint: exactly N todos, each with an owner
    todos = parse_todos(out)
    assert len(todos) == sample.expected_todo_count
    assert all(t.owner for t in todos)
    # 4. Length constraint
    assert 80 <= count_words_cn(out) <= 250

Supplementary points:

  • The logic behind “at least three criteria”: if you can only write one assertion for a task, chances are you haven’t yet worked out what it actually requires. This is really a requirements-clarification tool, not just a testing tool.
  • An example of “relaxing to the weakest assumption”: the task “write a function that returns the second largest number in a list”. Don’t assert the function must be named second_largest; only assert “there exists a function that, when called, returns the correct value”.
  • Execution-evaluation and HumanEval: OpenAI’s HumanEval (Chen et al., 2021) uses pass@k — generate k candidates per problem, and it counts as a pass if any one passes the unit tests. This is the same metric as AlphaCodium’s pass@5. Note that pass@1 (average performance) and pass@k (best case) are two completely different metrics; don’t mix them up when reporting.
  • Where do samples come from: dogfooding is the best source for cold start; after launch, it’s user negative feedback (downvotes), records of human takeover, and guardrail interception records. I suggest closing the loop: production issue → label → add to eval set → regression protection.
  • Limits of unit tests: they can only guard “known failure modes” and can’t tell you about average quality. So they need to be paired with the pairwise comparison in 4.2 — one guards the floor, the other tracks the trend.

LLM-as-Judge is somewhat useful, but not a silver bullet

[Book] Some people are skeptical of using a strong LLM to grade other LLMs’ outputs (several of the authors started out as firm skeptics). Done well, though, LLM-as-Judge tracks human judgment reasonably closely, and at the very least it gives you a prior on “how a new prompt or technique is likely to do”. In a pairwise comparison (control vs. treatment), LLM-as-Judge usually points the right way, even if the size of the win or loss is noisy.

Suggestions:

  1. Use pairwise comparison: rather than having the model rate one output on a Likert scale, show it two options and let it pick the better one; the results are steadier.
  2. Control for position bias: the order of the options sways the verdict. Run every comparison twice, flipping the order the second time. After the swap, make sure each win is credited to the option that actually earned it!
  3. Allow ties: the two options may be equally good; allow declaring a tie rather than forcing a choice.
  4. Use chain-of-thought: explaining before concluding makes the judge more reliable. A side benefit is that a weaker, faster model can then get close to the same results, and because this stage usually runs in batches, the latency CoT adds is not an issue.
  5. Control for response length: LLMs favor longer responses, so keep the two responses in each pair about the same length.

A particularly strong use is regression checking: if production results have been logged, replay those samples under a new prompt strategy and let LLM-as-Judge show you quickly where the new strategy may come off worse.

The authors show a simple, effective method: log each model reply together with the judge’s critique (its CoT) and its final verdict, then go through them with stakeholders to find what to improve. Three rounds of iteration later, agreement between humans and the LLM judge had gone from 68% to 94%.

But LLM-as-Judge is not a silver bullet: some subtleties of language are beyond reliable judgment even for the strongest models. Traditional classifiers and reward models can be more accurate, and they cost less and respond faster. And for generated code, LLM-as-Judge loses out to more direct strategies such as execution-evaluation.

[Extension]

① There are more biases than the two the book mentions. Zheng et al.’s work on MT-Bench / Chatbot Arena (Zheng et al., 2023) systematically documents:

BiasManifestationMitigation
Position biasPrefers the option in a certain positionSwap order and do it twice
Verbosity biasPrefers longer answersPair by length; state in the rubric that “conciseness is a virtue”
Self-enhancement biasPrefers text generated by itself (or its own family)Judge and judged come from different model families
Limited ability to grade reasoningStruggles with math/programming problems requiring complex reasoningUse execution-evaluation or rule checks for such tasks

Self-enhancement bias is not mentioned in the book but is very important: don’t use GPT to grade GPT, or Claude to grade Claude.

② “Switching families” isn’t enough: the closed-loop risk of synthetic data. If the eval set itself was generated by model A, and then scored by model A (or a close relative), what you measure is closer to “the system’s consistency with itself”. The generator and the judge should be separated as much as possible, and periodically calibrated with human labels.

③ A debiased pairwise comparison procedure (ready to copy):

Python
def pairwise_debiased(question, a, b):
    r1 = judge(question, a, b)   # A first
    r2 = judge(question, b, a)   # Swapped
    winner_r1 = r1.winner                   # "A" or "B" or "tie"
    winner_r2 = "A" if r2.winner == "B" else "B" if r2.winner == "A" else "tie"
    return winner_r1 if winner_r1 == winner_r2 else "inconsistent"

The proportion of inconsistent results is itself a useful metric — it reflects the judge’s noise level.

④ The methodology behind 68% → 94% is the point: the specific number doesn’t matter; the method does — record the judge’s reasoning → review with humans → revise the rubric → retest. LLM-as-Judge is a system that needs “training”, and the rubric is its training data.

⑤ What goes to rules, what goes to the LLM? Don’t give the LLM what can be written as code: format validation (JSON Schema), whether citations exist, whether numbers are in range, whether code runs. Leave LLM-as-Judge for genuinely subjective dimensions: relevance, tone, helpfulness. This is the same point as the book’s “traditional classifiers are more accurate and cheaper”.

⑥ A new trend after the book: Agent-as-a-Judge (from late 2024). Let the judge not just “read” but also “do” — execute code, search to verify facts, open citation links — then synthesize a judgment. This essentially combines execution-evaluation with LLM-as-Judge, addressing the shortcoming that “LLM judges lack external verification”.

⑦ The practical meaning of “right direction, noisy magnitude”: pairwise comparison suits A/B decisions (which is better), not fine-grained score comparisons (improved by 3.2 points or 3.5 points). Report win rates, not score differences.

The “intern test” for evaluating generated content

[Book] Give the model’s complete input (context included), unchanged, as an assignment to an ordinary undergraduate studying the relevant subject. Would they manage it? And how long would they need?

AnswerDiagnosisResponse
No, because they lack necessary knowledgeInsufficient contextFind ways to enrich the context (RAG, add materials)
No, and enriching the context won’t fix itThe task exceeds current LLM capabilitiesNarrow the task scope or give up
Yes, but it takes a long timeThe task is too complexReduce complexity: can it be split? Can some parts be templated?
Yes, and quicklyThe problem is the modelDig into the data: where does the model go wrong? Do the failures follow a pattern? Have the model explain itself before and after answering, and build a “mental model” of it

[Extension]

  • This is the tool I recommend most in this chapter, because it requires no ML knowledge and no experiments, just a person willing to read your prompt carefully. It turns the vague question “is the model good enough” into the actionable question “is the task well-defined”.
  • It is isomorphic to the “new-hire test” in software engineering: if a new hire can’t get started from this “requirements document”, it’s not the new hire’s problem; it’s the document’s.
  • Practical advice: find a colleague who knows the business but wasn’t involved in the project, give them the complete prompt without any spoken explanation, and ask them to (1) restate the task, (2) try one example, (3) say where they got stuck. Their answers will tell you directly what the context is missing and where the structure is confusing.
  • Connections to earlier sections:
    • “Enrich the context” → 1.5 crafting the context, section 2 RAG;
    • “Can the task be split” → 1.4 breaking prompts into smaller ones, 3.1 multi-step flows;
    • “Templated” → 3.2 deterministic workflows;
    • “Where does the model go wrong” → 4.1 assertion tests, and the error analysis in Chapter 2 section 1.2.

Supplementary example: the prompt asks to “judge, based on the policy document, whether this customer email needs escalation”. The intern reads it and says: “I can judge, but the document doesn’t say whether ‘threatening to expose this on social media’ counts as needing escalation.” — That pinpoints the gap in the context; go back and add one rule and two examples.

Overemphasizing certain evals hurts overall performance

[Book]

When a measure becomes a target, it ceases to be a good measure. — Goodhart’s law

A cautionary example is the needle in a haystack (NIAH) eval. It inserts a specific sentence into a long document of repeated Paul Graham essays and asks the model to repeat that “magic number”. It did help measure how well models recall things as contexts got longer, but it was overemphasized — it became Figure 1 of the Gemini 1.5 report. Some models recall almost perfectly, but it is doubtful that NIAH reflects the kind of reasoning and recall real applications need.

A more realistic scenario: give the model the transcript of an hour-long meeting. Can it sum up the main decisions and what happens next, and assign every item to the person it belongs to? That takes more than rote memory: the model has to follow a complex discussion, pick out what matters, and pull it together into a summary.

The authors give an example of a “practical NIAH”: using the transcript of a doctor–patient video call, ask about the patient’s medication — recall is about 80%; also insert a random sentence about “the secret ingredient to the perfect pizza” — recall is only 30%.

Overemphasizing NIAH can also degrade performance on extraction and summarization tasks: the model has been fine-tuned to attend closely to every sentence, so it starts counting irrelevant details and distractors as important content and puts them into its final output.

The same holds for other evals and use cases. Take summarization: push on factual consistency → summaries turn vaguer (which leaves less room for inconsistency) but may also be less relevant; push on style and fluency → more ornate, marketing-style language, which can let factual inconsistencies in.

[Extension]

  • What exactly is wrong with NIAH? It tests “copying after exact string matching”, which requires no understanding. And that is precisely one of the pattern-matching tasks LLMs are best at. RULER (Hsieh et al., 2024), contemporary with the book, and NoLiMa (2025) after it both confirm: once the literal shortcut is removed, models’ long-context scores drop sharply.
  • The 30% for the pizza ingredient is a good example: same model, same long document, and just because “this sentence is semantically irrelevant to the task”, recall drops by a large margin. This shows that distractors in real scenarios are not random; they are things that look like answers. When designing evals, deliberately include “similar distractors”.
  • The mechanism of “overattention → distractors enter the summary”: this is a mismatch between the training objective and the eval objective. Optimize recall → the model learns “include as much as possible” → in summarization this becomes “cram everything in”. This is the same as Goodhart’s law: you get what you reward, including its side effects.
  • Practical advice for multi-objective evals: summarization should at least simultaneously look at (1) factual consistency, (2) coverage/relevance, (3) conciseness. Any single one can be gamed. Make them three separate metrics with minimum thresholds (fail if any one fails), rather than a weighted sum — a weighted sum lets an extreme value in one dimension mask a collapse in another.

Simplify labeling: binary tasks or pairwise comparison

[Book] Giving open-ended feedback or Likert-scale scores on model outputs is cognitively heavy, produces noisier data (large inter-rater variance), and is less useful. What works better is to simplify the task and lighten the cognitive load. Two forms that work:

  • Binary classification: have labelers make “yes/no” judgments — is the summary factually consistent with the source? Is the reply relevant? Does it contain toxicity? Next to a Likert scale, a binary judgment is more precise, raters agree with each other more often, and throughput is higher. This is how DoorDash labeled its menu items: with a tree of “yes/no” questions.
  • Pairwise comparison: show a pair of replies and ask which is better. Saying “A is better than B” is easier than giving A or B a score of its own, so labeling is faster and more reliable. Thomas Scialom, one of the Llama 2 authors, confirmed at a meetup that pairwise comparison beats collecting supervised fine-tuning data (hand-written replies, for example) on both speed and cost: $3.5 per unit for the former, $25 for the latter.

[Extension]

  • Why are Likert scales noisy? “What’s the difference between a 4 and a 5” means completely different things to different people; even the same person’s standard drifts across time and sample order (anchoring effect). “Which of A and B is better” is a relative judgment that doesn’t require maintaining an absolute standard.
  • Measuring agreement: use chance-corrected agreement metrics like Cohen’s Kappa (κ) to assess labeling quality, not simple percent agreement. For binary tasks, also check whether the labeling guide is clear — low κ is usually the guide’s problem, not the labelers’.
  • The practical meaning of the cost conversion: if pairwise comparison costs 1/7 of writing replies, then producing “preference data” (used for DPO/RLHF) is much cheaper than producing “SFT data”. But remember they serve different purposes: preference data trains “which is better”, SFT data trains “what it should look like”.
  • A compromise beyond binary/ternary: sometimes you do need grades (e.g. “severe/mild/none”). Use decision-tree-style binary questions (DoorDash’s approach): first ask “is there a problem?” → if yes → “is it severe?”. Each node is still a binary judgment, but the whole gives multi-level labels.
  • A note for Chinese: for Chinese labeling, pay special attention to judging “factual consistency” — Chinese expression is flexible (synonym substitution, word order changes, subject omission), so the labeling guide needs plenty of positive and negative examples, or κ will be dragged down by cases that are “different in wording but the same in meaning”.

(Reference-free) evals and guardrails can be used interchangeably

[Book] Guardrails are for stopping inappropriate or harmful content; evals are for measuring the quality and accuracy of outputs. With reference-free evals, the two can be seen as two sides of the same coin. A reference-free eval needs no “golden” reference answer (one written by a person, for instance): the input prompt and the model’s reply are enough to judge quality.

Example: a summarization eval needs only the input document to judge factual consistency and relevance. If a summary scores badly, you can simply not show it to the user, and the eval is now doing a guardrail’s job. In the same way, a reference-free translation eval needs no human reference translation and can serve as a guardrail too.

[Extension]

  • The engineering value of the “two sides of the same coin” insight: write a reference-free evaluator once and use it in two places — offline batch evals (producing reports) and online real-time interception (making decisions). Don’t build it twice.
  • Ready-made tools: for summarization factual consistency there are NLI-based evaluators like SummaC (Laban et al., 2022); for hallucination detection there is SelfCheckGPT (Manakul et al., 2023) — sample multiple times for the same input and see whether the answers contradict each other; for reference-free machine translation quality estimation there are models like COMET-QE.
  • Deterministic hallucination detection: the book mentions in 4.8 that when RAG output is structured and indicates which sources it cites, you can mechanically verify that “the cited content really comes from that source” (string containment, numeric comparison, NLI models). This is the cheapest, most reliable class of guardrails and should be implemented first.
  • Three ways to handle a “fail”: block and don’t show / show with a downgrade notice (“the following information may be inaccurate”) / trigger regeneration. Set different policies for different failure types rather than one size fits all.

LLMs will return output even when they shouldn’t

[Book] A key challenge: LLMs often produce output when they shouldn’t. The consequence may be a harmless but meaningless answer, or toxic or dangerous content. Example: asked to extract specific attributes from a document, the model confidently returns values that don’t exist at all; or because the context contains a non-English document, the model answers in another language.

You can have the prompt ask the model to return “not applicable” or “unknown”, but that isn’t foolproof. Log probabilities (log probs), even when you have them, are a bad gauge of output quality: a log prob tells you how likely a token is, not whether the generated text is right; instruction-tuned models are trained to “give a coherent answer to a query”, so their log probs may be badly calibrated. A high log prob says the text is fluent and coherent, not that it is accurate or relevant.

So careful prompt engineering goes part of the way, but it has to be backed up by robust guardrails that catch unwanted outputs and either filter them out or generate again. OpenAI, for example, offers a moderation API that flags unsafe replies involving hate, self-harm, sexual content and the like, and plenty of packages detect personally identifiable information (PII). One advantage of guardrails is that they don’t depend on the use case: they can cover every output in a language. Also, if retrieval is precise, a system that finds no relevant document can say “I don’t know” deterministically.

A corollary: LLMs can also produce nothing when they should produce output. The causes vary, from long-tail latency at the API provider to a moderation filter blocking the output. So keep logging the inputs and the (possibly missing) outputs, for debugging and monitoring.

[Extension]

  • This is the most important “worldview” of the whole chapter: an LLM is a system that “always has something to say”; its default behavior is not “I don’t know”. All extraction, structuring, and RAG applications must explicitly handle the “empty” case.
  • Why is extraction especially bad? Extraction prompts usually imply “please fill in this schema”, and every field in the schema syntactically expects a value. The model tends to “complete the task” rather than “leave blank”. Mitigation: make fields nullable in the schema and give null examples; state in the instructions “fill null if not mentioned”; and deliberately include “field missing → null” examples in the few-shot examples (otherwise the examples themselves imply “every field has a value”).
  • Three layers of defense for declining to answer:
    1. Retrieval layer (most deterministic): RAG recall score below threshold → go directly to the “not found” branch without calling the model at all;
    2. Instruction layer: explicitly allow “I don’t know”, with examples;
    3. Output layer: verify that citations exist and field values actually appear in the source.
  • On calibration: the book says log probs are poorly calibrated, and research supports this. For instruction models, a more reliable approach is to probe uncertainty through behavior rather than probability — sample multiple times and check consistency (the SelfCheckGPT idea), have the model give citations and then verify, or train a small “correctness classifier”.
  • “Failing to output when it should” is often overlooked: a sudden flood of empty replies online may be caused by: the provider’s filter falsely blocking, JSON parsing failure (the model output extra text), or timeouts. Monitoring should distinguish three kinds of emptiness: “model refused”, “guardrail blocked”, “technical failure”.
  • Changes after the book: from late 2024, providers began treating “knowing when to say I don’t know” as an explicit training objective (e.g. OpenAI’s SimpleQA test set counts “correct refusal” as one of the metrics), and models’ refusal behavior improved overall. But on extraction and structuring tasks, the tendency to “fill in the schema” still exists; don’t skip the defenses.

Hallucination is a stubborn problem

[Book] Content safety and PII get a great deal of attention, so those defects rarely happen. Factual inconsistency is different: it is stubborn and harder to detect. It is also more common, with a baseline rate of 5%–10%, and from what the authors have heard from LLM providers, getting it under 2% is hard even on a task as simple as summarization.

The response combines upstream and downstream:

  • Upstream (prompt engineering): CoT and similar techniques, which have the model lay out its reasoning before the result, cut down on hallucination;
  • Downstream (factual inconsistency guardrails): check how factual each summary is, and filter out or regenerate the hallucinations.
  • Hallucination can sometimes be detected deterministically: when an answer draws on resources retrieved by RAG and is structured so that it names the resources it cites, checking by hand that they really come from the input context ought to be possible.

[Extension]

  • The weight of the 5%–10% number: if a customer service bot answers 10,000 questions a day, that means 500–1,000 answers with factual errors every day. This is the quantitative basis for “why LLM products can’t do without guardrails and human-in-the-loop”. Model capabilities have improved greatly since the book, and hallucination rates have dropped noticeably on many tasks, but “subtle factual errors in summaries (numbers, dates, names, negations)” and “right citation, wrong interpretation” remain difficult.
  • Three sources of hallucination, with three corresponding responses:
SourceManifestationResponse
Missing knowledgeThe model doesn’t know, so it makes things upRAG + allow “I don’t know” (section 2, 4.7)
Context ignored/misreadThe material has it, but it’s used wronglyCrafting the context (1.5), forced citation (1.2.3), structured chunks
Reasoning/extraction errorsMismatched answers, numbers attributed to the wrong thingConcretized CoT (1.2.2), multi-step flows (3.1), deterministic checks (4.6)
  • A hallucination-detection ladder ordered by cost (my summary): ① deterministic checks (do the numbers, dates, names appear in the source) → ② NLI model judging “can the source entail this sentence” (SummaC) → ③ LLM-as-Judge with citation verification → ④ human spot checks. Start with ①; it’s the cheapest and catches most “obvious fabrication”.
  • A note on “regeneration”: after detecting hallucination, regenerating doesn’t guarantee a better second try; log the regeneration rate — if it rises, something upstream is wrong (retrieval quality dropping or documents updated).
  • Set thresholds by scenario: chat products tolerate high hallucination rates; medical/legal/financial scenarios require near zero. The latter should default to “no answer without citation”, with human review.

Where the book says too little or is debatable

More than two years after the book, the changes in each section are already written in the [Extension] parts above; a point-by-point check of where things stand now is in the fourth post. Here I only record places where the chapter doesn’t mention something, doesn’t say enough, or where I think it’s debatable:

  1. “n ≥ 5” is a rule of thumb, not a law. The number of examples matters far less than the quality and distribution of the examples. Five carefully chosen boundary examples beat 30 similar easy ones. Reasoning models depend less on examples, and examples also consume reasoning budget; but examples remain irreplaceable for “defining format and edge cases”.
  2. “At least three criteria” doesn’t apply to open-ended generation tasks (brainstorming, creative writing); such tasks suit pairwise comparison + human taste review better.
  3. “Prefer deterministic workflows” and “agent autonomy” are in real tension. The book’s leaning to one side is reasonable, but readers should know this is a risk-appetite choice, not an absolute judgment of technical superiority.
  4. The LLM-as-Judge 68% → 94% figure comes from a single case and should not be taken as an expectation. What actually transfers is the process of “record the judge’s reasoning → human review → revise the rubric”.
  5. The temperature discussion is a bit brief; it doesn’t mention temperature’s cumulative damage to long-output quality, or its interaction with top-p.
  6. “Each model family has format preferences” matters less today: models from all families handle XML and Markdown reasonably well; what matters more is isolating untrusted data with explicit boundary tags (anti-injection).
  7. The evaluation section lacks a discussion of sampling variance: the same prompt run twice gives different results, so evaluation must sample repeatedly and report means with confidence intervals; contamination of eval sets (test questions in training data) also needs vigilance.
  8. The agent section lacks a discussion of tool-calling reliability: choosing the wrong tool and filling in wrong parameters are among the main failure sources of modern agents, and need dedicated tool-calling evals.
  9. Reasoning models’ intermediate process isn’t fully visible: many reasoning models only expose a reasoning summary externally, so the approach of “expose the intermediate artifacts, then assert on them” works less well for them.
  10. Topics not covered: multimodality, systematic defense against prompt injection, regressions from model provider changes, cost/latency as first-class metrics (this part is in Chapter 2).

The underlying ideas running through the chapter (distilled)

After reading this chapter, I distill the scattered techniques into seven principles:

  1. Cheap before expensive: prompt → examples → structure → multi-step → RAG → caching → fine-tuning. Don’t move to the next rung before the current one is exhausted.
  2. Break it down: one prompt does one thing; one task splits into multiple steps; one flow splits into multiple units that can each be evaluated. Breaking things down is the precondition for evals, caching, deterministic execution, and observability.
  3. Make the implicit explicit: CoT scratch work, structured output, generated plans, the judge’s reasoning — whatever is in the model’s “head”, make it write it out, so it can be checked.
  4. Wrap the probabilistic in the deterministic: schema validation, execution-evaluation, citation verification, caching, deterministic workflows, threshold-based refusal — all use the determinism of code to box in the model’s uncertainty.
  5. Context is a scarce resource: every token has a cost (money, latency, diluted attention). Chisel away the excess, structure what remains, put the important parts at the beginning and end.
  6. No evals, no iteration: all optimization depends on being able to measure change. Evaluation is testing (guarding the floor), monitoring (watching the trend), and analysis (finding the direction).
  7. The model always has something to say: assume by default it won’t say “I don’t know”, that it will fabricate missing values, that every step can fail — so there must be an exit for “I don’t know”, guardrails, and recovery mechanisms.

There is also a “human” principle, hidden in 3.2’s line about “people who have mentored junior engineers”: treat an LLM like a smart new colleague who has no context and won’t speak up to say “I don’t understand” — give it clear goals and concrete steps, supply the materials it needs, specify the output format, allow it to say it doesn’t know, and check what it produces.


A practical checklist

The chapter’s advice, compressed into a checklist you can go through item by item:

Prompt

  • Are you starting from prompting, not from fine-tuning or an agent framework?
  • Are there ≥ 5 examples, does their distribution match real data, are labels balanced, is the order shuffled?
  • Is the CoT written with concrete steps? For reasoning models, should you instead write less?
  • Have you told the model to “prioritize the materials, cite them, and say so when they’re insufficient”?
  • Is the input structured (schema/tags)? Is the output structured and verifiable?
  • Have you exported the final prompt and read it through on a blank page?
  • Does one prompt do only one thing?

RAG

  • Is there a BM25 baseline? Is hybrid search in place?
  • Have you measured MRR/NDCG? Have you done the “shuffle experiment”?
  • Do chunks carry metadata (source, date, title)? Is there reranking?
  • Is new knowledge added via RAG rather than fine-tuning? Are permissions isolated per tenant?
  • Have you evaluated “dump everything into the long window” vs. “RAG” on cost/latency/accuracy?

Workflow

  • Is the task split into small steps? Can a failure be traced to its step?
  • How many steps are done by the model, and how many by code? Have you estimated end-to-end success (pⁿ)?
  • Do high-risk operations require approval? Can the user see the plan?
  • Is exact caching implemented? Is prefix caching used (stable content first)?
  • Before fine-tuning, have you confirmed the prompt has hit its limit, the data is sufficient, and evals are in place?

Evaluation and monitoring

  • Are there assertion-based tests from real samples (≥ 3 criteria)? Do they run automatically on every change?
  • Does your LLM-as-Judge compare in pairs, swap positions, allow ties, use CoT, control for length, and come from a different model family?
  • Have you done the “intern test”?
  • Are there multiple metrics with minimum thresholds, rather than a single metric?
  • Is labeling simplified to binary/pairwise? Have you measured κ?
  • Is there a reference-free eval doubling as an online guardrail?
  • Do extraction tasks actually return null for missing values? Is there an exit for “I don’t know”?
  • Are you monitoring hallucination rate, refusal rate, guardrail trigger rate, and regeneration rate? Are you logging cases of “should have output but didn’t”?

Further reading

Mentioned in the book

  • AlphaCodium: Ridnik, Tal, et al. “Code Generation with AlphaCodium: From Prompt Engineering to Flow Engineering.” 2024. arXiv:2401.08500
  • CoT skepticism: Stechly, et al. 2024. “Chain of Thoughtlessness? An Analysis of CoT in Planning.” arXiv:2405.04776
  • Structured output tools: Instructor, Outlines
  • Keyword search foundations: Robertson, S., & Zaragoza, H. “The Probabilistic Relevance Framework: BM25 and Beyond.” 2009.
  • NDCG: Järvelin, K., & Kekäläinen, J. “Cumulated gain-based evaluation of IR techniques.” ACM TOIS, 2002.
  • The book’s free online edition and the authors’ homepage: applied-llms.org

Added in these notes


These notes are a summary, interpretation, and extension of Chapter 1 of the book. All [Extension] parts are the note author’s additions and do not represent the book’s views. For cited numbers, refer to the original papers or the book.