Appendix A — Using Other LLMs

From Part II on, the book’s code talks to two open models running on your own machine through LM Studio: Qwen3.8-27B for most chapters, gpt-oss-20b for the tool-calling ones, and nomic-embed-text for every embedding. A local model is free, private, and shows every mechanism in the book without a bill. It also needs a machine that can hold a 27B model — about 16 GB of memory — and the patience for local speeds. If that is not your machine, the same code runs against a hosted model. This appendix is how, with two worked routes: OpenAI, and the book’s own open model served by a hosting provider.
A.1 What the code assumes
Every file that talks to a model names it in the same three lines, and nowhere else — the graph code never learns which model is behind the client. The Qwen files carry one more line, the switch that turns the hybrid model’s thinking off (Chapter 12):
llm = ChatOpenAI(
base_url="http://localhost:1234/v1",
api_key="lm-studio", # the local server ignores the key
model="qwen/qwen3.8-27b",
temperature=0, # 0 = as reproducible as a real model gets
reasoning_effort="none", # Qwen3.8 thinks by default; off for now
)Embeddings are the same shape, one class up: the RAG chapters subclass OpenAIEmbeddings to put nomic’s search_document: / search_query: prefixes on, and point it at the same local server with model="text-embedding-nomic-embed-text-v1.5".
| Model | Where the book uses it | Why that one |
|---|---|---|
qwen/qwen3.8-27b |
Part II and all of Part III except two chapters: routing, structured output, memory, MCP, RAG, evaluation | a capable open model that follows instructions, thinking switched off |
openai/gpt-oss-20b |
two chapters only, Chapter 13 and Chapter 16 | a reasoning model trained for tool calls, which reaches for a tool on its own judgment — the lesson of Chapter 13, and the channel behaviour Chapter 16 diagnoses |
text-embedding-nomic-embed-text-v1.5 |
every embedding in Part III | a small open embedder with a clear retrieval recipe |
A.2 The switch
The companion carries a script that rewrites exactly those lines, in every file under code/, and touches no other line:
python tools/use_llm.py openai # gpt-4.1-mini + text-embedding-3-small; export OPENAI_API_KEY
python tools/use_llm.py open # gpt-oss-20b + an open embedder, hosted; export DEEPINFRA_API_KEY
python tools/use_llm.py local # back to LM Studio: the original lines, byte for byte
python tools/use_llm.py openai --diff # show what would change, write nothingThe mechanism is a table: one row of values per provider, swapped into those three lines in place:
PROVIDERS = {
"local": dict(base_url="http://localhost:1234/v1", api_key='"lm-studio"',
chat=None, embed="text-embedding-nomic-embed-text-v1.5"),
"openai": dict(base_url="https://api.openai.com/v1", api_key='os.environ["OPENAI_API_KEY"]',
chat="gpt-4.1-mini", embed="text-embedding-3-small"),
"open": dict(base_url="https://api.deepinfra.com/v1/openai", api_key='os.environ["DEEPINFRA_API_KEY"]',
chat="openai/gpt-oss-20b", embed="BAAI/bge-base-en-v1.5"),
}The script does not manage keys: export the provider’s key in your shell and the client reads it.
A.3 Route 1 — OpenAI
gpt-4.1-mini for chat and text-embedding-3-small for embeddings. Not gpt-5-mini: the book sets temperature=0 everywhere for reproducibility, and gpt-5-mini accepts only its default temperature — every file would fail with Unsupported value: 'temperature'. (That is this model’s limit, not the whole family’s; check the parameter support for whichever id you pick.) After the switch the block above reads:
llm = ChatOpenAI(
base_url="https://api.openai.com/v1", # use_llm.py: the provider's endpoint
api_key=os.environ["OPENAI_API_KEY"], # use_llm.py: key from the environment
model="gpt-4.1-mini", # use_llm.py: was qwen/qwen3.8-27b
temperature=0, # 0 = as reproducible as a real model gets
reasoning_effort=None, # Qwen3.8 thinks by default; off for now
)A.3.1 What a run of the whole book costs
Less than a dollar. Every program in the companion that reaches a model, chapter files and answer files alike, all but the three that are local by design (below), was run once on this route through a proxy that recorded the token usage of every request, and at gpt-4.1-mini’s published prices the whole book, answers included, came to a few cents. Your run will not match to the cent — a few programs loop until the model is satisfied, and answers differ in length from one run to the next — but it will land in the same place.
Two things I found out the hard way. First, the persisted Chroma stores under code/chroma_* were built by the local embedder, with 768-dimensional vectors; a hosted embedder writes 1,536 and Chroma refuses the mix (Collection expecting embedding with dimension of 768, got 1536). I had forgotten the stores were on disk at all. Delete them after switching and the programs rebuild them. Second, the answer files’ “is a model server up?” probe treated any HTTP error as “no server”; a hosted endpoint answers a key-less probe with 401, so every model test quietly skipped itself, and the suite stayed green, with nothing in it calling a model, until I read the skip counts. The probe now knows that a status code is a server.
A.4 Route 2 — the book’s open model, hosted
gpt-oss-20b is served by several providers behind OpenAI-compatible endpoints, so the second route keeps the book’s own chat model and changes only where it runs. Two things to check on a provider before choosing it: that the model is offered with tool calling — the tool-calling chapters need it, and providers differ (DeepInfra and Groq document it for gpt-oss-20b; Fireworks’ listing says not) — and what it offers for embeddings, since a chat-only provider such as Groq means a second key for the embedder. The switch’s open route uses DeepInfra for both, with BAAI/bge-base-en-v1.5 as the embedder; nomic’s own nomic-embed-text-v1.5 is hosted elsewhere (Fireworks), if matching the book’s retrieval numbers matters more than one key:
llm = ChatOpenAI(
base_url="https://api.deepinfra.com/v1/openai", # use_llm.py: the provider's endpoint
api_key=os.environ["DEEPINFRA_API_KEY"], # use_llm.py: key from the environment
model="openai/gpt-oss-20b", # use_llm.py: was qwen/qwen3.8-27b
temperature=0,
reasoning_effort=None,
)The script keeps one hosted chat model for the whole book, so the chapters that run on Qwen3.8 locally run on gpt-oss-20b here — the same class of open model, different wording in the transcripts. (DeepInfra also serves Qwen/Qwen3.8-27B itself; point PROVIDERS["open"]["chat"] at it if you want the book’s own model, and keep in mind that a hosted hybrid thinks unless the request says otherwise.) This route was checked against the providers’ documentation for model names, endpoints and tool-calling support; the full-book run in this appendix was done on Route 1.
A.5 What changes when you switch
The mechanisms do not; the transcripts do. One chapter runs the other way round: Chapter 23 records its timings on gpt-4.1-mini rather than the local model, because the ratios it measures are the far end’s to give, and it ends by showing what the same runs look like back on LM Studio. Everywhere else, wording differs from the book’s output blocks, and every number that comes from the embedder — the similarity scores in Chapter 27, the recall counts in Chapter 29 — is the embedder’s, so a different one gives different values around the same decisions. Three programs are local by design and stay that way: code/ch10_weak_model.py needs a deliberately weak model to contrast with a capable one, code/ch14_peek_reasoning.py compares the reasoning channels of the local models against each other, and code/ch22_langsmith.py needs a LangSmith key. The Chapter 30 deployment is local-only as shipped: its embedding factory supplies the LM Studio placeholder key. To use a hosted embedder, add an embedding API-key setting and inject it with env[].valueFrom.secretKeyRef into both the build Job and the serving pods, then pass it to the client — a ConfigMap alone does not carry a secret. Point embedding.lmBase and embedding.model (in chart/values.yaml) at the provider, choose its required query/document formatting, and rebuild the index with that same embedding configuration.
A.5.1 What survived the switch, and what did not
Every chapter program ran. Most routing and tool decisions came out as the book’s output blocks have them — with two exceptions that are worth separating from the numbers: Chapter 13’s forced-tool challenge declined the call, and Chapter 27 took the web branch where the book answers from the local store. The retrieval numbers moved too; that is the second half of this section. Chapter 12’s router took the same route on every question (search, then generate; search, web, generate); the tool-calling loops of Chapter 13 and Chapter 25 called the same tools, with their own wording for the query; the reranker in Chapter 29 reached the same three outcomes, two questions answered from the knowledge base and the decorator question sent to the web with every document scored 0; the evaluation gates in Chapter 32 passed, and the HTTP smoke test of Chapter 22 passed its fourteen checks. The deterministic parts — the sync decisions of Chapter 30, the chunk boundaries of Chapter 28 — came out identical.
What differs is every number an embedder produces, and the book’s thresholds were set for its local one. With text-embedding-3-small the similarities run lower: Chapter 28‘s comparison still ranks the topic-sized chunk above the whole document (0.683 against 0.637, where the book has 0.801 against 0.791), but Chapter 27’ second question — the one filed under a different word — scores 0.542 against the chapter’s 0.6 threshold and goes to the web instead of hitting locally, and the rewrites in Chapter 29 raise recall by less (1 of 2, where the book recovers both).
Of the 27 answer files, 20 pass every test and seven fail exactly one assertion each. Six of the seven depend on model or embedding output — a score, a ranking, or how much a rewrite recalls — and miss an expectation established on the local stack. The seventh is a different kind of change, and it is first because sorting it with the others would hide it:
- Ch11, forced tool — under the strict system prompt, gpt-4.1-mini still answers the France question from memory instead of calling search. That is the challenge’s own point (a prompt raises compliance, only structure enforces it), made on a model that happens to be less obedient on that one question.
- Ch25, prove semantic retrieval; Ch25, prompt injection; Ch26, where the size breaks; Ch29, add a golden entry — similarity scores and ranking: a phrasing that shares no content word no longer clears 0.6, the hostile document scores 0.486 where the test expects it above the threshold, a fragment outranks the answer chunk, and the new golden entry lands at 0.793 under the layer’s 0.8 bar.
- Ch27, floor and cap; Ch27, union of original and rewrites — the reranker’s scores and the rewrites’ recall, one judgement and one embedding away from the numbers the tests were written against.
These failures show which measurements depend on the model. Separate them before choosing a fix, because only the first kind is a threshold you re-sort and re-cut:
- A similarity cut — the three Ch25/Ch29 failures, where a score lands on the wrong side of a fixed bar. Section 32.7’s procedure applies directly, and it is not finished when the test goes green: the same move changes what else the cut now admits or drops.
- Ranking and recall under a different embedder — Ch26, where a fragment outranks the answer chunk, and Ch27’s union, which recovers less than the book’s run does. Moving a cut does not fix an ordering, and a bar that admits the right chunk still leaves it second; re-examine the trade on your own corpus.
- A reranker’s grades — Ch27’s floor and cap. Those are a model’s judgements of a document, not a score on the axis Section 32.7 sorts, and that chapter’s answer is the sturdier rule rather than a better number.
The Ch11 one takes a different reading, and no threshold will move it. What changed there is an execution path — a tool the model declined to call — so the thing to check is the trajectory, not a constant (Section 33.1), and the fix, if you need one, is the structural one that chapter already names.

