# Chapter 25. Prompts and Structured Output for Applications (/building-ai-workers/prompts-and-structured-output/overview)

---
type: Document
title: Chapter 25. Prompts and Structured Output for Applications
description: 'Give code the seven jobs a person did without thinking when they read the worker''s answer: a versioned prompt with the suite behind it, instructions in the right place, a schema the API holds, the stop reason read first, checks against the record, and a needs_review status for what code will not accept. Built on Claude first, then ported to OpenAI and to open source.'
status: stable
order: 325
ksor:
  owner: team:panaversity
  audience:
    - public
  approval:
    by: process:panaversity
    at: 2026-11-30T00:02:23Z
chapter: '25'
part: III
expert_status: co-build
objectives:
  - id: CCDV-F.D6.S2
    label: primary
  - id: CCDV-F.D6.S3
    label: primary
  - id: CCDV-F.D2.S1
    label: supporting
word_budget: 4000
prerequisites:
  - '01'
  - '02'
  - '03'
  - '04'
  - '05'
  - '06'
  - '07'
  - 8
  - 9
  - '10'
  - '11'
  - '12'
  - '13'
  - '14'
  - '15'
  - '16'
  - '17'
  - '18'
  - '19'
  - '21'
  - '22'
  - '23'
  - '24'
build_step: The invoice extraction as a versioned prompt with a schema the API enforces, value checks against the record, stop-reason gates with fixtures, needs_review for what code will not accept, and a port to OpenAI Structured Outputs
artifact: brightline-ap with a versioned extraction prompt, a schema on the response, value checks, tests/fixtures/output/, tag ch25
lab_data: https://github.com/panaversity/agentfactory-v2-resources/releases/latest/download/brightline-lab-ch25.zip
delta_entries:
  - D-012
  - D-013
  - D-022
concepts:
  - '25.1'
  - '25.2'
  - '25.3'
  - '25.4'
  - '25.5'
  - '25.6'
  - '25.7'
  - '25.8'
  - '25.9'
  - '25.10'
  - '25.11'
last_verified: '2026-11-29'
generated:
  at: 2026-11-30T00:02:23Z
  by: esl-rewrite/1.2.0+ksor.1
trust_tier: unverified
build_id: sha256:cc6fd9ac5f72207d4365ecbda63d87d1b64f99e05519b358a45b9f5d9bfd6f8f
dirty: true
ksor_version: 0.0.60
---

## Start here if you skipped Chapter 23 or 24

*If you read [Chapter 23](../hosted-agents/overview.md) and Chapter 24, skip this. These are the words from them that this chapter builds on.*

| Word | What it means | Where this chapter uses it |
| --- | --- | --- |
| **The portal** | Brightline's web application, which Ana signs in to. Its code runs the worker | Everywhere |
| **The morning triage** | The worker's run every weekday at 7:10, which sorts the open invoices and writes the morning report | Everywhere |
| **Case, label, golden set** | A case is one input with the answer Ana Morales expects, written down once. Her expected answer is the label. The golden set is all of Ana's cases. She wrote 40 cases: 25 AP questions and 15 invoice extractions. She added one question at the end of Lab 24, which makes 41 | Everywhere |
| **Suite** | The program that runs the worker on every case and compares each answer with its label. It runs on a pull request. It passes when at least a set number of cases pass, which is its threshold | 25.1, 25.6 |
| **Check by code, judge** | A check by code compares an answer with a label by plain code. A judge is a second model that reads what only reading can check. A check by code stays the gate | 25.1, 25.9 |
| **The extraction step** | Code in the portal that sends each new invoice to a model before the 7:10 run, and gets the invoice's fields back as JSON. Lab 24 asks for the JSON in the prompt and checks it in code, with no retry | Everywhere |
| **The red case** | The one extraction case Lab 24 leaves failing: valid JSON, every field in place, and a total that is the invoice's subtotal | 25.6, 25.9 |
| **`needs_review`** | The status of an answer the code will not accept. It goes to Ana, never onward as done | 25.9, 25.10 |
| **Format drift** | One of six names for a failure: the answer is right in meaning and wrong in form | 25.6 |

## The point

**Friday, April 16, 2027, 4:50 in the afternoon.** Midwest Packaging Co., one of Brightline's vendors, sends invoice INV-8340 in a new layout. You are the builder, and you want to know whether the worker's extraction prompt can read it. So you send the invoice to the model once, by hand, with that prompt alone, and you read the raw JSON that comes back. The terms say "Net 30 days". You read that as Net 30, which is right for this vendor. The answer looks fine. You go home.

**Monday, April 19, 7:10 in the morning.** The morning triage runs, and nobody is reading. The portal's code sends INV-8340 to the model, and the same answer comes back. This time the reader is a check in code. "Net 30 days" is not one of Brightline's five payment terms, so the check refuses the answer. At 8:40 Ana Morales, Brightline's AP analyst, finds INV-8340 in the morning report, marked `needs_review`, with the reason beside it. AP is accounts payable, the team that checks and pays a company's bills.

<!-- OPEN: the spec asks that Friday's and Monday's failures be recorded as real runs. No run was made for this draft. Record both before the page is approved, and change the scene to what the run really did. -->

The same answer was fine on Friday and refused on Monday. Nothing changed in the model, the prompt or the invoice. Only the reader changed.

**You have already given part of that reading to code.** In [14.5](../structuring-programs/schema-rules.md) you wrote a schema that refuses terms outside the five. In [21.3](../agent-loop-by-hand/one-turn-request-response-and-stop-reason.md) your loop read the stop reason, the field that says why the model stopped, before it read the content. In [Lab 23](../hosted-agents/lab.md), Task 18, code checked the morning report against the tool results, sent a failure back at most twice, and stored what still failed as `needs_review`. Each was added for its own reason, one at a time. Nobody has yet written the whole list of what a person does when they read an answer. A job that is on nobody's list stays undone, and no error says so.

So before you fix INV-8340, answer one question. **On Friday you read one answer and knew it was fine. What did you do, without thinking, that the code must now do on purpose?**

## Why it matters

Monday's failure was the lucky kind. The answer had the wrong form, a check for form already existed, and the invoice stopped at a person.

Chapter 24's suite holds the unlucky kind. One of its 41 cases is still red. The model returns valid JSON with every field in place, and the total it gives is the invoice's subtotal, the amount before tax. A check of form passes that answer. In the suite, Ana's label catches it, because she wrote down the true total. A live invoice at 7:10 has no label. Nothing in the code today would stop that answer, and the triage would use it.

A person reading that invoice would have looked at the total, looked at the tax line, and seen the gap. That is one of seven things a reader does without noticing. This chapter lists all seven, and gives each one to something in your own code that does it on purpose.

1. They knew the form they wanted.
2. They knew who was speaking, and which text was the invoice.
3. They read the answers again after they changed the prompt.
4. They coped with any layout.
5. They saw at once that a decline (the model says it will not do the task), or half an answer, was no answer, and they knew who had answered.
6. They knew a wrong value.
7. They asked someone when they were unsure.

<!-- EDITOR: the seven jobs are now a numbered list in the overview, because the spec says the overview names them and 25.11 refers to them by number. Figure 25.1 stays as the spec fixes it, with both columns. The other option: cut the seven labels from the figure's left column, so the list is told once. -->

Figure 25.1 puts the seven beside the seven steps in code.

<!-- FIGURE 25.1: "One answer, two readers." One sheet of JSON in the middle at the top, labeled "the worker's answer". Two columns below it. LEFT column, headed "Friday: a person reads", shows a person and a list of seven things they do, numbered 1 to 7, with these labels: 1 knows the form they want. 2 knows who is speaking, and which text is the invoice. 3 reads again after a change. 4 copes with any layout. 5 sees that a decline or half an answer is no answer. 6 knows a wrong value. 7 asks someone when unsure. RIGHT column, headed "Monday, 7:10: code reads", shows seven named steps in the same order, with these labels: 1 the prompt says the form, and an example shows it. 2 rules in the system prompt, the invoice in a tool result. 3 a versioned prompt file, and the suite. 4 a schema the API holds. 5 the stop reason, read first. 6 a value check against the record. 7 needs_review, to Ana Morales. Seven arrows, one per row, each running left to right, from a job on the left to the step on the right that takes it over. No arrow runs right to left, and no arrow crosses rows. Under both columns, one line: "A job that is on nobody's list stays undone." The alt text must give all seven pairs, word for word. -->

*Figure 25.1. One answer, two readers. Each thing the person did without noticing becomes a named step in your code.*

## How this chapter works

The chapter has eleven concepts and one lab. Read the concepts in order, because the order is the order of the work.

- **25.1 to 25.6 are about what you send:** the prompt, and the invoice beside it.
- **25.7 to 25.10 are about what comes back.** They teach three gates. A gate is a check an answer must pass before it goes further. Your code applies them in this order: the stop reason, then the shape, then the values. The sections teach the shape first, because the other two gates check what a schema cannot. Whatever a gate stops goes to a person.
- **25.11 shows today's tools.** The concepts are taught on Claude first. Then the same jobs are shown on OpenAI and on an open-source model, with what stays in your own code on every one of them.

Three subjects belong to other chapters. Text that tries to give the model orders, and how to defend against it, belongs to Chapter 28. The suite itself, and the judge, belong to Chapter 24: here you use them to revise a prompt. How hard the model works on a task, and what it costs, belongs to Chapter 29.

After this chapter you can:

- **Change a prompt the way you change code,** and say which test reads the result.
- **Put each instruction in its place,** and keep an invoice's text out of all of them.
- **Write the form into the prompt,** and choose between no example, one and several.
- **Hold an answer's shape with a schema,** and say what a schema cannot check.
- **Read the stop reason first,** check each value against the record, and send every answer you will not accept to a person.

This is a Co-build chapter. The builder writes the prompt and the code. The expert supplies the criteria and the examples: which terms are allowed, what a total is, and which invoices show the form best.

## What you will build

**Who's who.** You are the builder. Ana Morales is Brightline's AP analyst. She labels the cases, reads what the code hands her, and supplies the criteria. Midwest Packaging Co. is vendor V001, on Net 30 terms, and INV-8340 is its invoice.

**Where you are.** At the end of Lab 24 the AP worker runs in Brightline's portal every weekday at 7:10. Before each run, the portal's code extracts the fields of each new invoice with one model call. The prompt asks for JSON. Code checks the answer with 14.5's schema, and a failed check goes to `needs_review`, with no retry. The suite passes 40 of its 41 cases, and names the one it allows to fail.

**What you build instead,** in [Lab 25](lab.md), in two sittings:

1. **The red case, fixed by one change.** You add the check that would have stopped it live, then change one thing in the prompt, and run the whole suite.
2. **The extraction step under a schema the API holds,** with Brightline's five terms as its allowed values.
3. **Three gates in code, each with a test:** a declined request, an answer cut short, and valid JSON with a wrong total. Each one ends at `needs_review`, with the gate that stopped it.
4. **The same extraction on OpenAI and on an open-source model,** checked against one schema.

As in Chapters 17 to 24, coding agents write every file and run every command. You specify, check and approve.

<!-- EDITOR: the overview and 25.6 add the total check before the prompt changes, as the spec's worked run says, and Lab 25's Task 1 now has a step for it. The other option: keep the check in Task 3 only, and say here that 25.9 builds it. -->

<!-- OPEN: the lab page's file name (lab.md) and its task numbers are not fixed yet. The task numbers the concept sections name are proposed until the lab is built. -->

## In this chapter

| Section | The question it answers |
| --- | --- |
| 25.1 A prompt is code: versioned, reviewed and tested | Who reads the results when I change a prompt? |
| 25.2 System or user: where an instruction goes | How does the model know who is speaking, when code writes every turn? |
| 25.3 Clear instructions and output constraints | What must the prompt say, and what should come out of it? |
| 25.4 Zero, one or many examples | When does an example help, and when does it narrow the answer too much? |
| 25.5 Sanitizing input before it reaches the prompt | How does the invoice reach the model as data, and never as instructions? |
| 25.6 Revising from a failing regression case, and keeping it closed | How do I fix one case without breaking the others? |
| 25.7 Structured outputs: a schema on the response | What can the API hold for me, and how does the model say "not on the invoice"? |
| 25.8 Strict tool use or structured output | Is the result an action or data? |
| 25.9 Validate before downstream code uses it | The shape is right. Is the value? |
| 25.10 Defensive parsing: refusals, truncation and free text | What does my code read first, and where does a bad answer go? |
| 25.11 How today's tools realize it | Which of the seven jobs does each API do, and which stay mine? |
| Lab 25 | Two fixes and three gates, from tag `ch24` to tag `ch25` |
| Check yourself | One open question for each concept, with an outline of its answer |
