Learning SequenceVol. 18 · Part XVIII
0 / 0

Trails / Learning Sequence

LS·209

Phase 13 — AI Application Engineering

The thirteenth phase: embeddings, vector search, retrieval-augmented generation, evaluation, structured output, tool calling, agents and multimodal AI, ending with a production RAG application with a tool-using workflow; the order and why, an entry check, step-by-step exit checks mapped to Volume 14B, the project with milestones and definitions of done, traps, a time budget, and a lab measuring retrieval depth against context cost and two ways of routing questions to a tool.

17 topics · ~7 min read · 16-question quiz
Updated Suggest a correction

Start here: Phase 13 in one page

Phase 12 explained what models are. Phase 13 is about building applications on top of them: finding the right information for a model to use, getting reliable structured answers out, letting models call your code, and measuring whether any of it works. The project is a RAG application that answers questions from your own documents and uses tools for things a model shouldn't do in its head, such as arithmetic.

The sequence

Text
embeddings → vector search → RAG → evaluation → structured output → tool calling → agents → multimodal

Representations first (embeddings), then how to search them at scale, then retrieval-augmented generation, which combines search with a model. Evaluation comes immediately after RAG, before anything more complex, because everything after it is only as good as your ability to measure it. Then the interfaces between models and code (structured output, tool calling), then agents that chain them, then other modalities.

Measured trade-offs

The lab used this textbook's own chapters (5,346 chunks of about 200 words) and 879 glossary definitions as questions:

Chunks retrieved (k) Right chapter found Context sent to the model
1 45.7% ~200 tokens
3 65.0% ~600 tokens
5 72.5% ~1,000 tokens
10 81.9% ~2,000 tokens

More context finds more answers but costs tokens, latency and, past a point, model attention. And routing questions to a calculator tool:

Router Correct (of 100) Text questions wrongly sent to the calculator
"contains a digit" 92 8 (e.g. "What is a 404 error?")
intent words and at least two numbers 100 0

The calculator answered all 40 arithmetic questions correctly. The second router was written while looking at the test questions, so its 100% is optimistic: a fresh test set is the honest check.

How deep to go in Phase 13

Volume 17 defines six mastery levels; Phase 13's exit targets are (L5 on the AI track):

Topic Exit level What that means here
embeddings and vector search L3 choose models and indexes; measure recall
RAG L4 chunking, hybrid search, reranking, debug failures
evaluation L4 golden sets, retrieval and answer metrics, regression tests
structured output and tool calling L3–L4 schemas, validation, retries, tool design
agents L3 loops, memory, limits, when not to use them
multimodal L2 images and documents as inputs

The sequence, step by step

Step Learn Read Exit check
1 embeddings Vol. 14b, Start Here, ch. 159 compare two embedding models on your own queries
2 vector databases Vol. 14b, ch. 160 an ANN index with measured recall and latency
3 RAG Vol. 14b, ch. 161 a hybrid (BM25 + vector) RAG over your documents
4 RAG and AI evaluation Vol. 14b, ch. 162, 175 a golden set of 100+ questions and a dashboard of metrics
5 structured outputs Vol. 14b, ch. 167 validated JSON with retries on failure
6 tool calling and protocols Vol. 14b, ch. 168, 172 two tools, including one exposed over MCP
7 agents, memory, orchestration Vol. 14b, ch. 169–171 an agent with step limits and a trace of every step
8 multimodal and generative media Vol. 14b, ch. 173–174 answer questions about an uploaded PDF with images
9 fine-tuning, alignment, quantisation, inference (background) Vol. 14b, ch. 163–166 explain when fine-tuning beats RAG

Entry check

Phase 12, plus a working backend (Phases 3–4) to host the application.

The phase project: a production RAG application with a tool-using workflow

Goal. A question-answering application over a real document set (your team's docs, a product manual, this textbook) that cites sources and calls tools for exact work.

Milestones. Ingestion (parsing, chunking, metadata, re-ingestion on change) → hybrid retrieval with reranking → generation with citations and "I don't know" when retrieval is weak → a golden set and automated evaluation (retrieval recall, faithfulness, answer correctness) in CI → structured outputs for any machine-consumed answer → at least two tools (e.g. a calculator and a database lookup) with a router or model-chosen tool calls → a bounded agent loop for one multi-step task → tracing of every request (retrieval, prompts, tool calls, tokens, cost) → a small UI.

Done when. Evaluation runs on every change and blocks regressions; answers cite sources; tool calls are validated and logged; the system refuses when evidence is missing; a report shows recall@k, answer accuracy, cost and latency per request.

Stretch. Add image or PDF-page understanding for documents with diagrams.

Common traps

  • No evaluation set: "it looks good" on five questions.
  • Chunking by fixed size without structure.
  • Letting the model do arithmetic or lookups it should delegate.
  • Agents for fixed workflows.
  • Trusting tool arguments without validation.

Exit criteria

  • [ ] The project meets its "done when" list.
  • [ ] I can debug a wrong answer to retrieval or generation.
  • [ ] I can design tools and validate their calls.
  • [ ] I can explain when to use an agent and how to bound it.
  • [ ] I scored at least 80% on 40 questions from Volume 14B's quizzes, twice.

Time budget

Volume 14B's chapters 159–175 (about 87,000 words) are about 6.3 hours of reading; with practice, about 25 hours, plus 4 weeks for the project: about 7 weeks at 8 hours a week.

Lab: retrieval depth, context cost and tool routing

Setup. The retrieval set from Volume 14B's embeddings lab: the main chapters of Volumes 1–14 split into 5,346 chunks of about 200 words, and 879 glossary definitions from the revision sheets used as queries, where the right answer is the chapter the definition came from. Retrieval is BM25 (rank_bm25), no model needed, so the numbers are exact and repeatable. Token counts are estimated as 1.3 × words.

Experiment 1: how many chunks to retrieve

k Right chapter among the top k Mean context
1 0.457 155 words (~202 tokens)
3 0.650 463 words (~602 tokens)
5 0.725 769 words (~999 tokens)
10 0.819 1,527 words (~1,985 tokens)

Going from 1 to 10 chunks finds the right chapter 79% more often for 10 times the tokens. The usual answer is to retrieve wide (say 20–50 candidates) and rerank down to a few, getting most of the recall with a small context.

Experiment 2: routing to a calculator tool

100 questions: 40 arithmetic questions ("How much is 18 lunches at 120 rupees minus 3 skipped days?") and 60 text questions (54 glossary definitions plus 6 deliberately tricky ones containing digits, such as "What is a 404 error?" and "Explain the 3-2-1 backup rule").

Router Correct Text questions sent to the calculator
contains any digit → calculator 92/100 8 (including "What is a 404 error?" and definitions mentioning "8 bits")
intent words ("how much", "total", "cost", "price") and at least two numbers → calculator 100/100 0

The calculator itself, parsing the numbers and computing (lunches − skipped) × price, answered 40/40 correctly: exact, instant and free, which a language model doing mental arithmetic isn't.

Reading the results

  • Retrieval depth is a cost–quality dial; measure it on your own golden set rather than copying someone's k.
  • Simple routing rules fail on the edge cases you didn't think of; the digit rule looked reasonable until "404".
  • The second router's 100% is not a result. It was written knowing the test questions, the classic way to overfit an evaluation. Check it on fresh questions, or let the model choose tools and evaluate that.

Try it yourself

  1. Add dense embeddings and a hybrid score; compare recall@5 with BM25 alone.
  2. Retrieve 50 and rerank to 5 with a cross-encoder; compare recall and context size with k=5.
  3. Write 30 new arithmetic and text questions and re-test both routers.
  4. Replace the router with model tool calling and measure the same table.

Revision sheet: Phase 13, AI application engineering

Key ideas

  • Sequence: embeddings → vector search → RAG → evaluation → structured output → tool calling → agents → multimodal.
  • Evaluate retrieval and generation separately; build the golden set early.
  • Delegate exact work to tools; validate tool calls.
  • Prefer pipelines; bound agents.

Numbers to remember

  • Lab: recall 0.457 at k=1 (~200 tokens) → 0.819 at k=10 (~2,000 tokens).
  • Digit router 92/100; intent router 100/100 (on a test set it was written for); calculator 40/40.
  • Budget: ~25 study hours + 4 project weeks ≈ 7 weeks at 8 h/week.

Pitfalls

  • No golden set; overfitting the evaluation.
  • Naive chunking; too much context.
  • Model arithmetic; unvalidated tool arguments.
  • Agents where a pipeline would do.

Glossary

  • RAG: retrieval-augmented generation.
  • Recall@k: share of queries whose answer is in the top k.
  • Reranking: re-scoring retrieved candidates with a stronger model.
  • Tool calling: the model emits a structured call; your code executes it.
  • Golden set: questions with known answers for evaluation.