Part 10 of 10

Large Language Models

How modern machine learning and large language models actually work — from a single neuron to the transformer — and how to build real applications and agents on top of them.

Ch. 61

What Is Machine Learning

For most of this guide, a computer did exactly what it was told: a programmer wrote the rules, and the machine followed them. Machine learning inverts that relationship. Instead of writing the rules, you show the machine examples and let it infer the rules itself. You don't program the answer — you program a process that finds the answer in data.

This matters because some problems are easy to demonstrate but nearly impossible to specify. Nobody can write down the exact rules that distinguish a cat from a dog in a photograph, but anyone can label ten thousand photos. Machine learning is the trade of turning examples into a program.

Learning From Data, Not Rules

A traditional program is rules + input → output. Machine learning flips two of those terms: it takes input + output → rules. You feed in examples (inputs paired with their correct outputs) and the learning algorithm produces a model — a function with adjustable internal numbers, called parameters (or weights), that approximates the relationship.

Analogy: Teaching by rules is writing someone a recipe. Machine learning is letting them taste a hundred finished dishes until they can cook the dish themselves — they never see your recipe, they reconstruct one that produces the same result.

The Three Kinds of Learning

Almost every ML system falls into one of three families, distinguished by what signal the model learns from:

  • Supervised learning is the workhorse: every training example comes with the correct answer (a label). The model's job is to predict the label for inputs it has never seen. Spam filters, medical-image classifiers, and price predictors are all supervised.
  • Unsupervised learning has no labels at all — only raw data. The model finds structure on its own: clusters of similar customers, anomalies in a stream of transactions, or compressed representations. The embeddings that power modern search (Chapter 66) are learned this way.
  • Reinforcement learning has no fixed answers, only rewards. An agent takes actions, receives feedback, and learns a strategy that maximizes reward over time. It powers game-playing systems and, as we'll see in Chapter 64, the final polishing step of modern chatbots.

Features, Model, Loss

Three ideas recur in every supervised system:

Term What it is
Features The input variables the model sees — square footage, pixel values, the words in an email
Model A function with tunable parameters that maps features to a prediction
Loss A single number measuring how wrong the predictions are on the training data

Training is then a search: adjust the parameters to make the loss as small as possible. The loss is the compass — it points the entire process toward "less wrong."

The Training Loop

Learning happens by repetition. The model makes predictions, measures its error, and nudges its parameters to do slightly better — over and over, sometimes millions of times:

This loop is the beating heart of nearly all machine learning, and the next chapter shows exactly how the "nudge each parameter" step works for neural networks.

Generalization — The Whole Point

Here is the subtle part: doing well on the training data is not the goal. A model that simply memorizes its examples is useless — like a student who memorizes the practice exam but can't answer a new question. The real goal is generalization: performing well on data the model has never seen.

To measure it, you split your data: train on one portion, then evaluate on a held-out test set the model never trained on. The gap between training and test performance reveals the central failure mode of ML:

Analogy: Underfitting is a student who didn't study enough to grasp the material. Overfitting is a student who memorized the textbook word-for-word but can't apply any of it. A good fit is the student who understood the concepts — and can answer questions they've never seen.

Everything that follows — neural networks, transformers, the models behind ChatGPT and Claude — is an elaboration of this one idea: adjust millions of parameters to minimize a loss, in a way that generalizes beyond the data you trained on.

Ch. 62

Neural Networks & Deep Learning

A neural network is the model architecture behind essentially all of modern AI. The name evokes the brain, but you don't need any neuroscience to understand it. A neural network is a stack of simple mathematical operations — multiply, add, and bend — repeated enough times to approximate astonishingly complex functions.

The Neuron

The atom of a neural network is the neuron. It does three things: it multiplies each input by a weight, adds the results together (plus a bias), and passes the sum through a simple nonlinear function called an activation.

That's it. A neuron computes output = activation(w₁x₁ + w₂x₂ + … + bias). The weights are the knobs the network learns; the activation is what lets the network bend, rather than just scale, its inputs.

Analogy: A neuron is a tiny voting machine. Each input gets a weight — how much its vote counts — the votes are tallied, and the activation decides whether the result is strong enough to "fire" and pass forward.

Layers and Depth

A single neuron can only draw a straight line. The power comes from arranging neurons into layers and stacking those layers, so the output of one becomes the input of the next:

Each layer transforms its input into a more useful representation. In an image network, early layers detect edges, middle layers assemble edges into shapes, and later layers recognize whole objects. "Deep learning" simply means a network with many layers — depth is what lets the network build complex concepts out of simple ones.

The activation function is essential here. Without it, stacking layers would be pointless: a chain of pure multiply-add steps collapses into a single multiply-add. The nonlinearity — most commonly ReLU (max(0, x), keep positives, zero out negatives) — is what gives depth its expressive power.

How a Network Learns

The network starts with random weights and produces nonsense. Learning fixes this through the loop from the last chapter, made concrete:

  1. Forward pass — run the input through every layer to produce a prediction.
  2. Loss — measure how far the prediction is from the correct answer.
  3. Backward pass — work out how much each weight contributed to the error.
  4. Update — nudge every weight to reduce the error.

The third step is backpropagation: using the chain rule from calculus to push the error backward through the network, assigning each weight its share of the blame. You don't need the calculus to hold the intuition — backprop answers the question "if I wiggle this weight a little, does the loss go up or down, and by how much?" for every weight at once.

Gradient Descent

That collection of answers — the direction and steepness of the loss with respect to every weight — is the gradient. Learning is then just walking downhill on the loss:

Each step moves every weight a small distance in the direction that reduces the loss fastest. The size of the step is the learning rate: too large and you overshoot the valley; too small and training takes forever. Repeat for millions of steps and the network settles near a minimum — a setting of the weights that makes good predictions.

Analogy: Imagine standing on a foggy hillside trying to reach the valley floor. You can't see far, but you can feel which way the ground slopes under your feet. Take a step downhill, feel again, step again. Gradient descent is exactly that, in a space of millions of dimensions.

Why GPUs

Every step of this process is dominated by matrix multiplication — thousands of independent multiply-adds with no dependencies between them. That is precisely the workload a GPU's thousands of small cores devour in parallel (Chapter 31's CPU-vs-GPU story). This single fact — that neural networks reduce to matrix multiplies — is why deep learning and GPUs rose together, and why a modern model trains on racks of GPUs rather than CPUs. The systems side of that story is Chapter 65.

Ch. 63

The Transformer Architecture

The transformer is the architecture behind every modern large language model — GPT, Claude, Gemini, Llama. Introduced in 2017 in a paper titled "Attention Is All You Need," it didn't just improve on what came before; it removed the bottleneck that had limited language models for a decade. Understanding the transformer is understanding why this era of AI happened now.

The Problem With Reading One Word at a Time

Language is sequential, so the natural approach is to process it sequentially. Earlier models — RNNs and LSTMs — did exactly that: they read a sentence one word at a time, maintaining a running "hidden state" that summarized everything seen so far.

This has two fatal flaws. First, that hidden state is a bottleneck: by the time the model reaches the end of a long paragraph, the beginning has faded into a blur. Second, and just as important for the systems engineer, reading strictly in order is inherently sequential — you can't compute word 50 until you've computed word 49 — so it can't exploit a GPU's parallelism.

Attention — Look At Everything At Once

The transformer's key move is to drop recurrence entirely and let every word look directly at every other word in one shot. This mechanism is self-attention.

The intuition: to understand a word, you need context, and the relevant context might be anywhere in the sentence. In "The animal didn't cross the street because it was tired," what does "it" refer to? A human glances back at "animal." Self-attention lets the model do the same — for every word, it computes how much to "pay attention" to every other word, and blends in their information accordingly:

Mechanically, each token produces three vectors: a Query ("what am I looking for?"), a Key ("what do I offer?"), and a Value ("what information do I carry?"). A token attends to others by matching its Query against their Keys; strong matches pull in more of those tokens' Values. Crucially, all of these comparisons happen simultaneously as big matrix multiplications — perfect for a GPU.

Analogy: Self-attention is a room full of people each asking a question (Query) and wearing a name tag describing what they know (Key). Everyone looks around, finds the people whose tags best answer their question, and listens mostly to them. It all happens at once, not one conversation at a time.

Multi-head attention runs several of these attention operations in parallel, each free to focus on a different kind of relationship — one head might track grammatical subjects, another might track which adjectives modify which nouns.

Positional Encoding

Because attention looks at all tokens simultaneously, it has no inherent sense of order — "dog bites man" and "man bites dog" would look identical. The fix is positional encoding: a signal added to each token's representation that encodes where it sits in the sequence. Order is injected as data rather than baked into the processing.

The Transformer Block

Stack these pieces and you get a transformer block: self-attention to mix in context, followed by a small feed-forward network applied to each token, wrapped with residual connections and normalization that keep training stable. A full model is just this block repeated dozens to hundreds of times:

Each block refines the representation a little more. Early blocks resolve grammar and references; deeper blocks capture meaning, tone, and reasoning. Stack enough of them, train on enough text, and the result is a model that can continue any passage of language plausibly — which, as the next chapter shows, turns out to be almost everything.

Ch. 64

How Large Language Models Work

A large language model does exactly one thing: given some text, it predicts the next word. Everything else — answering questions, writing code, translating, reasoning through a problem — is an emergent consequence of doing that one task extraordinarily well, at enormous scale. The "large" refers to the scale: billions of parameters (the weights from Chapter 61), trained on trillions of words.

Tokens, Not Words

The model doesn't actually see words or letters. Text is first broken into tokens — chunks roughly the size of a syllable or a short word — and each token is mapped to an integer ID from a fixed vocabulary:

Tokenization is a compromise: whole words would need an impossibly large vocabulary, while individual characters would make sequences too long. Subword tokens hit the sweet spot — common words become single tokens, rare words split into pieces. This is also why models sometimes stumble on spelling or character counting: they never see the letters, only the chunks. (It's worth knowing tokens are the unit you're billed in and the unit the context window is measured in.)

Next-Token Prediction

At its core, the model takes a sequence of tokens and outputs a probability for every token in its vocabulary being the next one. It then picks one, appends it, and repeats — generating text one token at a time:

That single objective, "predict the next token," is deceptively powerful. To predict the next token well across the entire internet, the model is forced to learn grammar, facts, reasoning patterns, the structure of code, the rules of arithmetic, and the conventions of dialogue — because all of those help it guess better.

Analogy: Imagine the world's most well-read autocomplete. To finish "The capital of France is ___" it must know geography. To finish "def factorial(n): return ___" it must understand recursion. Pushed to the limit, "predict the next word" becomes "model how the world is described in language."

Sampling — Why the Same Prompt Varies

The model outputs probabilities, not a single answer, so the final step is to sample from that distribution. Two knobs control this:

  • Temperature scales how much randomness is allowed. At 0, the model always takes the most likely token — deterministic and repetitive. Higher values flatten the distribution, producing more varied and creative (and riskier) output.
  • Top-p (nucleus sampling) restricts the choice to the smallest set of tokens whose probabilities add up to p, trimming the long tail of unlikely tokens.

This is why an LLM can give different answers to the same prompt — and why temperature: 0 is the right choice when you need consistency, such as data extraction or tool calling.

The Context Window

The model has no memory between calls. Everything it "knows" in the moment must fit inside its context window — the fixed budget of tokens it can read at once:

The system prompt, the conversation history, any retrieved documents, and the current question all compete for that same space. Modern windows are large — hundreds of thousands of tokens — but in a long conversation or a document-heavy task they fill up faster than you'd expect, and the oldest content must be dropped or summarized. Managing this budget is one of the real engineering problems of building with LLMs (Chapter 67).

Analogy: The context window is a whiteboard in a meeting room. The model can reason about anything written on it right now — but nothing it can't see. When the board fills up, something gets erased to make room.

Scale and Emergence

Why are these models so capable? Scaling laws observed across the field show that performance improves predictably as you increase three things together: parameters, training data, and compute. More striking is emergence: certain abilities — multi-step arithmetic, following instructions, basic reasoning — appear fairly suddenly once a model crosses a size threshold, having been essentially absent in smaller models.

Why Models Hallucinate

Understanding next-token prediction also explains the technology's defining flaw. The model is trained to produce plausible continuations, not true ones. It has no built-in notion of a fact it can look up — only statistical patterns over text. When it doesn't "know" something, it doesn't fall silent; it generates the most plausible-sounding tokens, which can be confidently wrong. This is hallucination, and it's structural, not a bug to be fully patched. It's the central reason techniques like retrieval (Chapter 66) and evaluation (Chapter 67) exist — to ground and check a system whose nature is to sound right rather than to be right.

Ch. 65

Training, Fine-Tuning & Alignment

A raw model fresh out of next-token training is brilliant but feral. It can continue any text, but it doesn't want to be helpful, doesn't know it's in a conversation, and has no sense of what it should or shouldn't say. Turning that raw capability into the polite, useful assistant you actually talk to takes several distinct stages. Understanding them demystifies what "training a model" really means — and clarifies the constant practical question of when to fine-tune versus when not to.

The Stages, End to End

Pretraining

The first and by far most expensive stage is pretraining: next-token prediction (Chapter 63) over a vast corpus — much of the public internet, books, and code, often trillions of tokens. This is where the model learns language, facts, and reasoning patterns. It's self-supervised: the data needs no human labels, because the "answer" for each position is simply the word that actually came next. Pretraining can cost millions of dollars and run for weeks on thousands of GPUs.

The result is a base model: enormously knowledgeable, but it only knows how to continue text. Ask it a question and it might reply with a list of more questions — because on the internet, questions are often followed by more questions.

Supervised Fine-Tuning

Next comes supervised fine-tuning (SFT): continuing to train the base model, now on a curated set of high-quality example conversations written or vetted by humans — prompts paired with ideal responses. The model learns the format and behavior of being a helpful assistant: when asked a question, give a direct, well-structured answer. This is far cheaper than pretraining because it needs only thousands to millions of examples, not trillions of tokens.

Analogy: Pretraining is a lifetime of reading everything ever written. SFT is a short apprenticeship where a mentor shows you, "when someone asks you this, here's how a good assistant responds."

Preference Tuning (RLHF and DPO)

The final polish aligns the model with subtle human preferences that are hard to write down — being more helpful, less verbose, refusing harmful requests gracefully. The classic method is RLHF (Reinforcement Learning from Human Feedback):

Humans rank several model responses from best to worst. Those rankings train a reward model that predicts what humans prefer. The LLM is then tuned (using reinforcement learning, Chapter 60) to produce responses the reward model scores highly. A newer, simpler method, DPO (Direct Preference Optimization), skips the separate reward model and optimizes the preference data directly — cheaper and more stable, and increasingly the default.

Alignment

These last stages are collectively about alignment: making the model's behavior match human intentions and values. The informal target is often summarized as helpful, harmless, and honest — it should do what you actually want, avoid causing harm, and not deceive. Alignment is what stands between a raw capability and a system safe enough to hand to millions of users, and it remains one of the most active research areas in the field.

When to Fine-Tune (and When Not To)

You can also fine-tune an already-aligned model on your own data. But fine-tuning is the right tool less often than people expect, because it competes with a cheaper alternative — retrieval (Chapter 66):

The rule of thumb worth memorizing: fine-tuning changes behavior; retrieval supplies knowledge. Reach for fine-tuning when you need the model to adopt a consistent tone, output a strict format, or master a narrow style. Reach for retrieval — not fine-tuning — when you need it to know facts that are private, fresh, or frequently changing. Most production systems use a fine-tuned model with retrieval on top, getting both at once.

Ch. 66

Machine Learning Infrastructure

Machine learning is, from a systems perspective, just another workload — but one with an unusual shape: a slow, data-hungry training phase that produces a model, and a fast, repeated inference phase that uses it. Building reliable ML systems is mostly about the infrastructure around the model, not the math inside it.

Where ML Fits in the System

The model itself is one box in a much larger machine: a data pipeline turns raw events into features, a training pipeline consumes them on GPUs to produce model weights, a registry versions the resulting models, a serving layer answers requests, and monitoring watches for the thing that makes ML uniquely fragile — data drift, where the live data slowly diverges from what the model was trained on.

Training vs Inference

These two phases have opposite hardware and latency profiles, and conflating them is a common early mistake:

This is why a company might train on a cluster of eight A100 GPUs costing hundreds of thousands of dollars, yet serve the resulting model on a single modest GPU — or even a CPU.

Neural Networks from the Systems Side

You don't need the calculus to understand why ML reshaped hardware. A neural network is a stack of layers, and each layer is fundamentally a matrix multiplication followed by a simple nonlinear function:

Training repeats a four-step loop millions of times: a forward pass computes an output, a loss measures how wrong it is, a backward pass computes how to nudge each weight, and an update applies the nudge. Because every step is dominated by matrix multiplication — thousands of independent multiply-adds — it maps perfectly onto a GPU's thousands of cores. That single fact is why deep learning and GPUs rose together.

Analogy: Training is tuning millions of knobs on a giant mixing board. You play the song (forward pass), judge how it sounds (loss), work out which knobs to turn and how far (backpropagation), adjust them (update), and repeat until it sounds right.

Serving Patterns

How you deploy a trained model is its own design decision:

The choice mirrors the monolith-vs-microservice trade-off: embed for simplicity and latency, separate for independent scaling, or precompute in batch when predictions aren't time-sensitive (recommendations, say).

Ch. 67

Prompting, Embeddings & RAG

With a trained, aligned model in hand, the question becomes practical: how do you actually get good, grounded answers out of it? This chapter covers the three techniques that do most of the work in real applications — shaping the input (prompting), turning meaning into math (embeddings), and feeding the model your own knowledge at query time (retrieval-augmented generation).

Prompting — Programming in English

The prompt is your interface to the model, and small changes to it can dramatically change the output. A well-built prompt usually has a few parts: a system prompt that sets the model's role and rules, optional examples, and the user's request. Beyond structure, a few techniques recur:

The most important of these is chain-of-thought: simply asking the model to "think step by step" before answering. Because the model generates one token at a time with no scratchpad, forcing it to lay out its reasoning in the output gives it room to work through multi-step problems — and reliably improves accuracy on math and logic, at the cost of more tokens.

Analogy: Few-shot prompting is showing a new employee two or three completed reports before asking for theirs. They infer the expected format and tone without being given an explicit spec.

Embeddings — Meaning as Geometry

Prompting handles how you ask. But how does a system find the right information to put in the prompt? The answer is embeddings: a learned mapping from text to a vector of numbers (the unsupervised representations from Chapter 60) whose defining property is that similar meanings produce nearby vectors.

The magic is that similarity is semantic, not textual. "What were our Q1 sales?" lands close to a document titled "First-Quarter Revenue Report" even though they share almost no words. Meaning becomes distance, and distance is something a computer can measure instantly.

Vector Databases

A vector database (Pinecone, Weaviate, pgvector, FAISS) stores millions of these vectors and answers one question very fast: "which stored vectors are nearest to this query vector?" This nearest-neighbour search is the retrieval engine underneath semantic search and the RAG pipeline below. It's the same instinct as the database indexes of Chapter 19 — precompute a structure that turns a slow scan into a fast lookup — applied to meaning instead of exact keys.

Retrieval-Augmented Generation

Put prompting and embeddings together and you get RAG, the dominant pattern for grounding a model in knowledge it wasn't trained on:

The user's question is embedded, the vector database returns the most relevant chunks of your documents, those chunks are pasted into the prompt, and the model answers using the supplied text. The model effectively takes an open-book exam instead of relying on memory.

RAG directly attacks the hallucination problem of Chapter 63: instead of inventing plausible facts, the model is handed the real ones and asked to synthesize an answer — ideally with citations back to the source. And because the knowledge lives in the database rather than the weights, you update it by editing documents, not by retraining.

Analogy: Asking a bare LLM a question about your company is a closed-book exam — it answers from memory and may misremember. RAG slides the relevant pages onto the desk first. Same student, far better answers, and you can swap the pages anytime.

This is the practical resolution of the RAG-vs-fine-tuning choice from Chapter 64: when the problem is knowledge, retrieval almost always wins.

Ch. 68

Building Applications with LLMs

Calling a model in a notebook is easy. Building a product on top of one — reliable, fast enough, affordable, and safe — is a real engineering discipline. This chapter is the application layer: how you actually wire an LLM into software, and the production concerns that separate a demo from a system you'd put in front of customers.

The Stack

A serious LLM application is a layered system, not a single API call:

Each layer is a place where engineering happens — and where things go wrong. Most of this chapter walks up that stack.

The API Call

At the bottom, you talk to the model over an HTTP API. The core request is a list of messages, each tagged with a role:

{
  "model": "claude-...",
  "messages": [
    { "role": "system", "content": "You are a concise travel assistant." },
    { "role": "user", "content": "Three days in Lisbon — what should I do?" }
  ],
  "temperature": 0.7
}

The system message sets behavior; user and assistant messages carry the conversation. Because the model is stateless (Chapter 63), you resend the relevant history on every call — the API has no memory of the last one.

Streaming

Generating a long response takes several seconds, and waiting for the whole thing feels broken. Streaming sends tokens to the client as they're produced — the familiar word-by-word typing effect. It doesn't make generation faster, but it slashes perceived latency and lets the user start reading immediately. Under the hood it's usually server-sent events (Chapter 33's persistent-connection patterns).

Structured Output

Inside a program you rarely want prose — you want data. Structured output constrains the model to emit valid JSON matching a schema you define, so its response slots directly into your code:

{ "destination": "Lisbon", "days": 3, "interests": ["history", "food"] }

This turns the model from a chatbot into a component you can compose with ordinary software.

Tool / Function Calling

The single most important capability for real applications is tool calling (also called function calling). You describe the functions your app exposes; the model, when it needs one, responds not with prose but with a structured request to call it:

The critical detail: the model never executes anything itself. It only emits a request — {"tool": "get_weather", "args": {"city": "Paris"}}. Your code runs the real function and feeds the result back as an observation. This single mechanism — model decides, your code acts — is the foundation of everything in the next two chapters. It's how an LLM reaches past its frozen training data to query a database, hit an API, or check today's date.

Context, Cost, and Latency

Three intertwined constraints shape every design decision:

  • Context-window management — the budget from Chapter 63 is finite, so long conversations and large documents must be trimmed, summarized, or retrieved on demand rather than stuffed in wholesale.
  • Cost — you pay per token, input and output. Long prompts (few-shot examples, big retrieved chunks) and chatty chain-of-thought add up fast across millions of requests. Levers: shorter prompts, smaller models for easy subtasks, and caching repeated context.
  • Latency — each model call costs hundreds of milliseconds to seconds. A chain of ten calls is slow; this is why production systems route easy work to small fast models and reserve the frontier model for the hard parts.

Guardrails

The model is capable of saying and doing things it shouldn't, so production systems wrap it in guardrails — filters on the way in and out:

  • Input guardrails screen incoming requests — blocking prompt-injection attacks, where a user (or a malicious document) tries to override your system instructions.
  • Output guardrails screen what the model produces — catching leaked secrets, unsafe content, or actions that should require human approval first.

Analogy: Guardrails are the compliance department around a brilliant but naïve new hire. The talent is real; the checks make it safe to let them act.

Evals and Observability

Two practices separate a demo that works in the room from a system that works in production:

Evals are testing for non-deterministic software. Because there's often no single correct answer, you build a dataset of representative inputs with known-good outputs (or grading rubrics), run the system against it, and track pass rates as you change prompts or models. Without evals, every "improvement" is a guess.

Observability is tracing for reasoning systems. When an answer is wrong, you need to see every prompt sent, every tool called, every response returned, and how long each took. Logs alone aren't enough; tools like LangSmith or OpenTelemetry-based tracing let you look inside a multi-step run and find where it went off the rails — indispensable once you reach the agents of the next chapter.

Analogy: Evals are the exam that tells you whether the system is good enough to ship. Observability is the X-ray that tells you why it failed when it does.

Ch. 69

AI Agents

Everything so far has described a model that responds: you ask, it answers. An agent is the next step — a model given the ability to act. The difference is the difference between an assistant who tells you how to send an email and one who actually sends it, checks for a reply, and follows up. Agency is what you get when you wrap a language model in a loop, give it tools, and let it observe the consequences of its own actions.

From Assistant to Agent

The leap is small in mechanism but profound in consequence. An assistant produces text. An agent pursues a goal — taking actions, reading the results, and deciding what to do next, over many steps, until the goal is met. It builds directly on tool calling (Chapter 67): the model proposes actions, your code executes them, and now those results feed back in to drive the next decision.

What Makes an Agent

Four capabilities, working together, turn a model into an agent:

  • Perception — taking in information from its environment: tool outputs, API responses, error messages, the results of its own prior actions.
  • Reasoning — the LLM at the core deciding what to do next given everything it has perceived. This is the "brain."
  • Action — actually doing something: calling a function, running code, querying a database, writing a file, invoking another agent.
  • Memory — carrying context across steps so each action is informed by what came before.

The Agent Loop

The engine of every agent is an iterative loop, most famously the ReAct pattern (Reasoning + Acting):

The agent thinks about what it knows and needs, acts by choosing a tool, observes the result, and loops — repeating until it either completes the task, hits a step limit, or determines it can't proceed. That stopping condition matters: without a budget on iterations, a confused agent will loop forever, burning time and money.

Analogy: An agent loop is a detective working a case. Examine the evidence (observe), form a theory of what to check next (think), go knock on a door or pull a record (act), and repeat — until the case is solved or the leads run dry.

Memory

Memory is what lets an agent operate over more than a single step, and it comes in distinct flavors:

In-context memory is the working scratchpad of the current run (Chapter 63's context window). External memory (RAG, Chapter 66) lets the agent recall far more than fits in context. Episodic memory logs past runs so the agent can learn from its own history. Procedural memory is the skill baked into the model's weights by training (Chapter 64).

Tools — The Agent's Hands

Tools are how an agent affects the world, and they fall into rough categories worth keeping straight because they carry very different risk:

Category What it does Examples
Read Gather information Web search, database SELECT, read a file, API GET
Write Change state in the world Send a message, write a file, API call with side effects
Compute Perform operations Run Python, do a calculation, call another model
Agent Invoke another agent as a subroutine Delegate a subtask — the basis of multi-agent systems

The quality of a tool's description matters enormously: the model reads it to decide when and how to use the tool, so a vague description leads to misuse. Writing good tool descriptions is one of the genuine craft skills of agent development.

How Agents Plan

The simple ReAct loop is one of several planning strategies, trading adaptiveness against predictability:

ReAct is reactive and flexible. Plan-and-execute drafts the whole plan up front and is more predictable but less adaptive. Tree of Thoughts explores several reasoning branches and backtracks — better for problems with multiple viable paths, at higher cost.

Multi-Agent Architectures

For complex work, one agent is often replaced by several, each specialized:

The most common pattern is hierarchical: an orchestrator decomposes a goal and delegates subtasks to worker agents, then synthesizes their results — mirroring how a manager runs a team.

Why Agents Are Hard

Agents are far harder to make reliable than chatbots, for reasons that follow directly from the loop:

  • Error compounding — a small mistake at step 2 of a 10-step chain cascades. Per-step error rates multiply, so even 95%-reliable steps give a coin-flip over a long task.
  • Irreversible actions — a wrong chatbot reply is harmless; an agent that deletes a file or sends an email to a customer cannot easily undo it. This is why write-tools deserve guardrails and human-in-the-loop approval (Chapter 69).
  • Evaluation is about the trajectory, not just the answer — you must judge whether the agent took sensible steps, not only whether it landed on the right result (Chapter 67's evals, made harder).
  • Latency and cost — a 20-step loop is 20 model calls. Long-horizon agents get slow and expensive fast, which drives the use of smaller, faster models for routine steps.

These challenges are exactly why the surrounding machinery — orchestration frameworks, standardized tool protocols, guardrails, and observability — exists. That's the final chapter.

Ch. 70

Orchestration, MCP & the AI Engineering Landscape

The previous chapters described the pieces: models, prompts, retrieval, tools, agent loops. This final chapter is about the connective tissue — the frameworks that orchestrate those pieces, the protocol that standardizes how agents plug into tools, and the safety and operational layers that make the whole thing fit to ship. It closes by assembling everything into one mental model of the modern AI stack.

Orchestration Frameworks

Writing the agent loop by hand — parsing tool calls, managing memory, handling retries, looping until done — is repetitive plumbing. Orchestration frameworks like LangChain and LangGraph provide that machinery so you can focus on your tools and prompts.

Analogy: An orchestration framework is an operating system for an agent. The same way an OS sits between your program and the raw hardware — managing memory, scheduling, I/O — the framework sits between your application and the raw model, managing prompts, tool calls, memory, and the control loop.

The useful abstractions are worth knowing regardless of which framework (or none) you choose:

Abstraction What it gives you
Model wrapper One interface over many providers — swap models without rewriting your app
Prompt template Prompts as reusable forms with variables, not hardcoded strings
Chain Compose steps so one's output feeds the next — like piping functions
Memory Pluggable conversation history (buffer, summary, or vector-backed)
Executor The runtime loop that drives think-act-observe and enforces limits

Early frameworks modeled everything as a linear chain (step A → B → C). But real agents branch, loop, and backtrack. LangGraph models the agent as a graph — nodes are steps, edges are conditional transitions — which is the difference between a checklist and a flowchart. That graph view is also what makes human-in-the-loop clean: you mark certain nodes as requiring approval, and the graph pauses there until a human signs off — the practical answer to the irreversible-action risk from Chapter 68.

The Model Context Protocol

Every tool an agent uses needs an integration, and historically each one was bespoke: a custom wrapper for Slack, another for GitHub, another for your database — each written and maintained by you. The Model Context Protocol (MCP), introduced by Anthropic in late 2024, standardizes this.

Analogy: MCP is USB for AI tools. Before USB, every device needed its own connector and driver. USB defined one standard, and suddenly any device worked with any computer. MCP does the same for agents: tool providers publish an MCP server once, and any MCP-compatible agent can use it instantly — no custom integration code.

The model has three roles:

A host is your AI application. Inside it, an MCP client speaks the protocol — the standardized socket. An MCP server is what a tool provider publishes; it exposes a set of tools and describes them. "Server" is used loosely: locally it's often just a process on your machine communicating over stdin/stdout (like piping shell commands); remotely it's a real service reachable over HTTP that many agents across an organization can share.

Under the hood, MCP is a simple JSON-RPC conversation in three phases:

The agent initializes (agree on protocol version), discovers tools (tools/list returns a manifest of tools with their input schemas — which is fed to the LLM so it knows how to call them), and executes (tools/call with a name and arguments; the server runs the real work and returns the result). A single call looks like:

{ "method": "tools/call",
  "params": { "name": "query_db", "arguments": { "sql": "SELECT ..." } } }

The shift MCP brings is the same one that standardized database drivers (JDBC/ODBC) or USB: integration stops being bespoke plumbing you maintain and becomes a service you consume.

The Holistic Stack

Step back and the whole field assembles into one layered, swappable stack:

Read it top to bottom as a sentence: a base model provides raw intelligence; post-training makes it a usable assistant; fine-tuning specializes its behavior; RAG grounds it in your private, current knowledge; tools give it hands and MCP standardizes how those hands connect; an orchestration loop drives its reasoning; memory gives it continuity; guardrails and human-in-the-loop keep it safe; and evals plus observability tell you whether it works and why it broke. Each layer is independent enough to swap — change the model, the vector database, or the framework without rebuilding the rest. That modularity is what makes the modern AI stack so flexible.

Where It's Heading

The clear trajectory is less scaffolding, more capability in the model itself. Early agents needed elaborate prompt engineering to reason and use tools; newer frontier models are reliable tool users and planners out of the box, so the surrounding code keeps shrinking. The open frontiers are genuinely unsolved: long-horizon planning (can an agent reliably execute a hundred-step task without drifting?), verification and trust (how do you know an agent did what you intended?), and multi-agent coordination at scale. These are the live research questions defining the field right now.

The Same Ideas, One More Time

It's fitting that this part closes the guide, because the AI stack is built from the very ideas the rest of the book established. Embeddings are an index (Chapter 19) over meaning. RAG is a cache (Chapter 32) of relevant knowledge fetched on demand. The agent loop is a fetch-decode-execute cycle (Chapter 5) raised to the level of reasoning. Tool calling is a system call (Chapter 7) — a controlled boundary between a thinking core and the outside world. Multi-agent orchestration is microservices (Chapter 23) with language models as the services. Guardrails are input validation; evals are tests; observability is tracing.

The transformer is new. The engineering around it is the same field you've spent this whole guide learning — old ideas, wearing one more new hat.