Part 3 · Deploy and apply Chapter 8
Applications
Prompt engineering, structured outputs and function calling, RAG, agents, and MCP and the tool ecosystem: the engineering methods that turn a model that can talk into a product that is reliable, measurable and easy to iterate on.
After this chapter you will be able to
- Write a task as a checkable spec, layer the messages, and measure every prompt change on a small evaluation set.
- Make a program use model output reliably: JSON mode, constrained decoding and the message round trips of function calling, and know that format constraints can hurt reasoning.
- Build a RAG pipeline with evaluation: chunking, hybrid retrieval, reranking and citations, measured with Hit@k and MRR.
- Design a controllable tool-calling loop that handles failures, write confirmation and prompt injection, and measure its stability with pass^k.
- Read MCP messages and error semantics, measure how many tokens tool definitions cost, and choose between listing every tool, retrieval and progressive disclosure.
For a first application, start with Your first agent to learn tool dispatch and stopping with a read-only function.
A strong model handed straight to users is still only a demo. Application engineering is about making the model do the right thing reliably in real scenarios. This chapter starts with the cheapest levers, prompts and structured output; then RAG, which makes the model answer from your own material; then agents, which call tools and finish multi-step tasks, and agent evaluation, which grades the system of model, tasks and protocol together; and finally MCP and the tool ecosystem, for when there are too many tools to fit in the context. The further along, the more complex the system, and the more it needs evaluation to hold it up.
Every page comes with experiments you can run: on a small reproducible corpus or task, they measure the gain of each step instead of tuning by feel. After building an application, go back to the Evaluation chapter and set a regression baseline for your system; and before launch, read Safety and Alignment Risks: untrusted content gets treated as instructions by the model, and answers it is unsure of get treated as facts.
Related paper explainers: Chain-of-Thought, ReAct, Toolformer and RAG survey.
Agent context and memory covers token budgets, atomic tool turns, and versioned cross-session preferences.
Chapter roadmap
- 8.1
Your first agent: a read-only tool and a bounded loop
Use a tiny library lookup to distinguish a function call, a fixed workflow, and an agent loop. Run action, observation, and stopping cases in standard Python.
Prerequisites7.1Your first inference service
- 8.2
Prompt Engineering: A Systematic Guide to Specs, Layers, Examples and Evaluation
Design the prompt as a task interface: write a task spec, layer system, examples and context, separate format errors from wrong judgments with an eval set, and use data to decide when few-shot, chain-of-thought or structured output pays off. Includes a runnable ticket-triage experiment.
Prerequisites2.2From Language Modeling to Next-Token Prediction7.2Decoding and Sampling
- 8.3
Structured Outputs and Function Calling: From JSON Mode to Constrained Decoding
A program can use model output reliably through prompt conventions, JSON mode, JSON Schema constrained decoding, and validate-and-retry. This page explains how token masks guarantee a format, how function-call messages flow, and why format limits can hurt reasoning.
Prerequisites8.2Prompt Engineering7.2Decoding and Sampling
- 8.4
RAG in Practice: Chunking, Hybrid Retrieval, Reranking and Evaluation
How to build a retrieval-augmented generation system that works: clean and chunk documents, combine dense and BM25 retrieval with RRF, rerank with a cross-encoder, assemble the context, and measure every step with Hit@k and MRR on a small runnable corpus.
Prerequisites2.5Tokenizers and Embeddings8.2Prompt Engineering
- 8.5
Agents and Tool Calling in Practice: From Function Calling to ReAct Workflows
An agent lets the model decide the next step in a loop, and its reliability comes from the code around the loop: validation, write confirmation, step limits and evaluation. Build one with Qwen3-4B, measure on 12 support cases what guardrails catch, and learn prompt injection, MCP and pass^k.
Prerequisites8.1Your first agent8.2Prompt Engineering8.3Structured Outputs and Function Calling
- 8.6
Agent context and memory: budgets, versions, and state
Context engineering fits instructions, tool history, and retrieved evidence into a model call. Long-term memory keeps confirmed preferences across sessions. Test whole tool turns, version precedence, deletion, and replay.
Prerequisites8.5Agents and Tool Calling in Practice8.4RAG in Practice
- 8.7
Agent Evaluation: You Are Grading the System, Not the Model
Agent evaluation grades the whole system — model, prompts, tools, loop, environment. An episode is tasks, trials, trajectories, and outcomes; pass^k measures reliability, stateful graders check final states, and cost is accounted per successful task.
Prerequisites8.5Agents and Tool Calling in Practice
- 8.8
MCP and the Tool Ecosystem: How It Works and What to Do When Tools Pile Up
MCP, the Model Context Protocol, is the open protocol that connects AI applications to external tools and data; its 2026-07-28 revision is stateless. This page shows the protocol in real messages, measures what 96 tool definitions cost in tokens, and compares three ways to pick a tool.
Prerequisites8.5Agents and Tool Calling in Practice8.3Structured Outputs and Function Calling