B.12 Pass 10: Agents
With a served model and tool calls in the engine, the system can act as well as answer. This pass builds an agent SDK in Go on the engine’s tool-call API: provider types, a tool registry with schema validation, the agent loop, a tool gate with a prompt-injection suite, and durable agent runs. It adds retrieval over the system’s own documentation, so the agent can answer questions about the system and cite its sources, and an evaluation runner with scorers, an LLM judge, and paired A/B experiments. The gateway gains a usage policy enforced once, at the edge, for every model and client.
Concepts from other sections (17)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- agent loop: repeated model calls, each followed by running the tool calls it returned and appending their results, until a reply has no calls. Read more: 28.3 The agent loop
- LLM judge: a language model used as a scorer, treated as a noisy instrument whose malformed replies are missing readings. Read more: 29.3 LLM judge with position control
- usage policy: an ordered list of rules that allow or deny inference requests by model, tenant, token cap, or classifier score. Read more: 25.6 Usage policy enforcement
- DPO: minimizing over preference pairs, equivalent to fitting the KL-regularized reward objective. Read more: 18.2 Direct preference optimization and IPO
- GRPO: a PPO-style RL method that replaces the critic with the group-normalized reward of several samples of the same prompt. Read more: 19.7 The training step and validation
- verifiable reward: a reward computed by a deterministic checker rather than a learned model. Read more: 18.3 Group relative policy optimization with verifiable rewards
- Retrieval-augmented generation (RAG): retrieving relevant evidence at query time and placing it in the context window, so a model answers from fresh, private, citable facts instead of only its parametric memory. Read more: Chapter 27: Retrieval and RAG
- SSRF (server-side request forgery): an attack in which input chooses the address a server connects to. Read more: 27.1 RAG Ingest: Crawler, Chunker, Embedder, and Store
- BM25: a lexical ranking function with saturating term frequency (
k1), length normalization (b), and an idf that is never negative. Read more: 27.2 Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR - IVF (inverted file index): a vector index that clusters rows around centroids and scans only the
nprobenearest clusters. Read more: 27.2 Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR - RRF (reciprocal rank fusion): combining ranked lists by summing over the lists containing each item. Read more: 27.2 Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR
- MMR (maximal marginal relevance): greedy selection that trades relevance against similarity to items already picked. Read more: 27.2 Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR
- position bias: a judge’s tendency to prefer whichever answer occupies a particular slot. Read more: 29.3 LLM judge with position control
- Confidence interval: a procedure that, over repeated samples, produces intervals covering the fixed true parameter at the stated rate. Read more: 13.11 Probability problem set, part c: limit theorems and confidence intervals in Mathematics
- IPO: regressing the margin to the target with a squared loss. Read more: 18.2 Direct preference optimization and IPO
- forward KL: , weighted by the teacher; mass-covering. Read more: 18.4 Distillation with forward and reverse KL
- reverse KL: , weighted by the student; mode-seeking. Read more: 18.4 Distillation with forward and reverse KL
An optional second half post-trains the capstone model: supervised fine-tuning, DPO, GRPO with verifiable rewards, and distillation.
Learning objectives
- Explain how the agent loop calls tools through the engine and why the tool gate blocks injected instructions.
- Explain how retrieval, context assembly, and citations make an answer checkable.
- Explain how the eval runner, scorers, judge, and A/B experiments compare two models with an interval.
- Explain how the gateway enforces a usage policy for every request.
Before you begin
Section titled “Before you begin”This pass needs Pass 9: the served capstone and the engine’s tool calls from Pass 7.
In this pass
Section titled “In this pass”- Types, an OpenAI-compatible provider, and a retry wrapper. Afterwards you can explain the agent SDK’s types, the OpenAI-compatible provider, and which errors the retry wrapper retries.
- Tools, a registry, and JSON Schema argument validation. Afterwards you can explain how tools register and how arguments are validated against JSON Schema.
- The agent loop. Afterwards you can trace the agent loop from model call to tool execution and explain when it stops.
- The tool gate, the SQL safety gate, and the prompt-injection suite. Afterwards you can explain how the tool gate and SQL safety gate block unsafe calls and what the prompt-injection suite tests.
- Durable agent runs. Afterwards you can explain how AgentRun makes an agent run durable and replayable.
- RAG Ingest: Crawler, Chunker, Embedder, and Store. Afterwards you can explain how RAG ingest crawls behind an SSRF allowlist, chunks, embeds, and stores documents.
- Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR. Afterwards you can explain how BM25, flat and IVF vector search, RRF, and MMR retrieve and rerank.
- Context Assembly, Citations, and the search_docs Tool. Afterwards you can explain how context assembly fits retrieved chunks into budget, attaches citations, and exposes search_docs.
- Eval runner. Afterwards you can explain how the eval runner executes a suite and records results.
- Agent evaluation scorers. Afterwards you can explain what each agent evaluation scorer measures.
- LLM judge with position control. Afterwards you can explain how the LLM judge controls for position bias.
- Paired A/B experiments. Afterwards you can explain how a paired A/B experiment estimates a difference with a confidence interval.
- Usage policy at the gateway. Afterwards you can explain the usage policy and why it is enforced at the gateway.
- Usage policy enforcement. Afterwards you can explain how the gateway enforces the usage policy and returns 451.
- The agent image and chart. Afterwards you can explain how the agent image and chart are built and deployed.
- Agent evals as tests. Afterwards you can explain how agent evals are written as tests and what they assert.
- What the System Does After Pass 10. Afterwards you can explain how the durable agent answers with citations and passes evals, policy checks, and prompt-injection cases.
Going deeper: post-training
Section titled “Going deeper: post-training”These optional sections post-train the TinyStories model and end with a second capstone:
- Optimization problem set, part b: constrained optimization, duality, and DPO. Afterwards you can work every problem in the set and explain each answer.
- Supervised fine-tuning: chat rendering, assistant-only loss, packing. Afterwards you can explain how SFT renders chats, masks the loss to assistant tokens, and packs sequences.
- Direct preference optimization and IPO. Afterwards you can derive the DPO and IPO losses from the preference objective.
- Group relative policy optimization with verifiable rewards. Afterwards you can explain how GRPO computes group-relative advantages from verifiable rewards.
- Distillation with forward and reverse KL. Afterwards you can explain how distillation with forward KL differs from reverse KL.
- Capstone 2: post-trained TinyStories model. Afterwards you can explain how the post-trained TinyStories model is built and why it beats the base model on held-out preference tasks.
The stages are also in this pass’s manifest, path.tsv. Pass 11 releases the system as v1.0.0 and operates it.
Stages
Section titled “Stages”Generated from path.tsv.
| Stage | Read | Done when |
|---|---|---|
| 1 | Types, OpenAI-compatible provider, retry wrapper 28.1 Types, an OpenAI-compatible provider, and a retry wrapper | You can explain the agent SDK’s types, the OpenAI-compatible provider, and which errors the retry wrapper retries (ag.01). |
| 2 | Tools, registry, JSON Schema argument validation 28.2 Tools, a registry, and JSON Schema argument validation | You can explain how tools register and how arguments are validated against JSON Schema (ag.02). |
| 3 | Agent loop 28.3 The agent loop | You can trace the agent loop from model call to tool execution and explain when it stops (ag.03). |
| 4 | Tool gate, SQL safety gate, prompt-injection suite 28.4 The tool gate, the SQL safety gate, and the prompt-injection suite | You can explain how the tool gate and SQL safety gate block unsafe calls and what the prompt-injection suite tests (ag.04). |
| 5 | Durable agent runs (AgentRun) 28.5 Durable agent runs | You can explain how AgentRun makes an agent run durable and replayable (ag.05). |
| 6 | RAG ingest: crawler with SSRF allowlist, chunker, embedder, store 27.1 RAG Ingest: Crawler, Chunker, Embedder, and Store | You can explain how RAG ingest crawls behind an SSRF allowlist, chunks, embeds, and stores documents (ag.06). |
| 7 | Retrieval: BM25, flat and IVF vectors, RRF, MMR 27.2 Retrieval: BM25, Flat and IVF Vectors, RRF, and MMR | You can explain how BM25, flat and IVF vector search, RRF, and MMR retrieve and rerank (ag.07). |
| 8 | Context assembly, citations, search_docs tool 27.3 Context Assembly, Citations, and the search_docs Tool | You can explain how context assembly fits retrieved chunks into budget, attaches citations, and exposes search_docs (ag.08). |
| 9 | Eval runner 29.1 Eval runner | You can explain how the eval runner executes a suite and records results (ag.09). |
| 10 | Agent evaluation scorers 29.2 Agent evaluation scorers | You can explain what each agent evaluation scorer measures (ag.10). |
| 11 | LLM judge with position control 29.3 LLM judge with position control | You can explain how the LLM judge controls for position bias (ag.11). |
| 12 | Paired A/B experiments 29.4 Paired A/B experiments | You can explain how a paired A/B experiment estimates a difference with a confidence interval (ag.12). |
| 13 | Usage policy at the gateway 37.1 Usage policy at the gateway | You can explain the usage policy and why it is enforced at the gateway (ethics.05). |
| 14 | Usage policy enforcement 25.6 Usage policy enforcement | You can explain how the gateway enforces the usage policy and returns 451 (gw.08). |
| 15 | Agent image and chart 10.8 The agent image and chart | You can explain how the agent image and chart are built and deployed (dep.07). |
| 16 | Agent evals as tests (R9) 3.9 Agent evals as tests | You can explain how agent evals are written as tests and what they assert (craft.23). |
| 17 | What the system does after Pass 10 B.12.1 What the System Does After Pass 10 | You can explain how the durable agent answers with citations and passes evals, policy checks, and prompt-injection cases. |
| 18 | Optimization problem set, part b: constrained optimization, duality, and DPO 16.6 Optimization problem set, part b: constrained optimization, duality, and DPO | You can work every S-M10b problem and explain each answer. |
| 19 | Supervised fine-tuning: chat rendering, assistant-only loss, packing 18.1 Supervised fine-tuning: chat rendering, assistant-only loss, packing | You can explain how SFT renders chats, masks the loss to assistant tokens, and packs sequences (L12.1). |
| 20 | Direct preference optimization and IPO 18.2 Direct preference optimization and IPO | You can derive the DPO and IPO losses from the preference objective (L12.2). |
| 21 | Group relative policy optimization with verifiable rewards 18.3 Group relative policy optimization with verifiable rewards | You can explain how GRPO computes group-relative advantages from verifiable rewards (L12.3). |
| 22 | Distillation with forward and reverse KL 18.4 Distillation with forward and reverse KL | You can explain how distillation with forward KL differs from reverse KL (L12.4). |
| 23 | Capstone 2: post-trained TinyStories model A.2 Capstone 2: post-trained TinyStories model | You can explain how the post-trained TinyStories model is built and why it beats the base model on held-out preference tasks (C2). |