Skip to content
The AI Agent Factory

Chapter 25. Prompts and Structured Output for Applications

Approved by
Panaversity ·
22 min read

Start here if you skipped Chapter 23 or 24

If you read Chapter 23 and Chapter 24, skip this. These are the words from them that this chapter builds on.

WordWhat it meansWhere this chapter uses it
The portalBrightline's web application, which Ana signs in to. Its code runs the workerEverywhere
The morning triageThe worker's run every weekday at 7:10, which sorts the open invoices and writes the morning reportEverywhere
Case, label, golden setA 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 41Everywhere
SuiteThe 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 threshold25.1, 25.6
Check by code, judgeA 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 gate25.1, 25.9
The extraction stepCode 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 retryEverywhere
The red caseThe one extraction case Lab 24 leaves failing: valid JSON, every field in place, and a total that is the invoice's subtotal25.6, 25.9
needs_reviewThe status of an answer the code will not accept. It goes to Ana, never onward as done25.9, 25.10
Format driftOne of six names for a failure: the answer is right in meaning and wrong in form25.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.

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 you wrote a schema that refuses terms outside the five. In 21.3 your loop read the stop reason, the field that says why the model stopped, before it read the content. In Lab 23, 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.

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

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, 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.

In this chapter

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