Lección 4 · Módulo 1: System Prompts That Control Agents and Cut Cost
Structured outputs: making agents return clean JSON every time
- Ask for one fixed shape
- Check every reply before use
- Recover from broken replies
Why this matters
A warm, correct reply can still break your business. Calendars, customer lists and invoicing From the lesson: How an AI agent works: model, instructions, tools and memoryan action the agent can take outside its own text, such as writing to a calendarReview that lesson need exact values in exact places. A reply that reads well may hold a date the program cannot place, and then the booking goes missing without any error.
Key ideas
- A fixed shape. Name every field in the prompt, list its allowed values and say how an unknown value is written, for example that budget is a number or null. Keep this in the From the lesson: Anatomy of a system prompt: role, goal, rules, output formatthe standing instructions an agent reads before every message, with its role, rules and output formatReview that lesson, along with a rule to reply with JSON only.
- Labelled boxes. A program can parse JSON, turning the text into named boxes it opens by name. A calendar accepts
"date": "2026-11-05"because it is one exact value, but not"date": "Thursday", which the program would have to guess at. - Null means unknown. Write
"time": nullwhen no time was given. An empty text value such as""looks like an answer, so a checker may pass it by mistake. - Validation. Before any tool uses a reply, a checker tests that required fields exist, that the intent is on the allowed list and that each value has the right type. In no-code builders such as n8n or Make, this is an a no-code step that sends data down one path or another depending on a test that routes failing replies away.
- Retry once, then hand over. Send the exact error back to the agent for one retry. If the second reply still fails, the case goes to a person, an From the lesson: Guardrails in the prompt: refusals, escalation and staying on taskpassing a case to a person when the agent cannot safely handle itReview that lesson, with the original message attached.
Try it before you watch
A reply contains "budget": "around 500". Will the checker accept it, and why?
Show answerHide answer
No. The budget must be a number or null, and "around 500" is words, not a number. A check tests the form of each value, not only whether the field is there.
Words to know
- JSON: a plain text format that writes data as labelled boxes, each with a name, a colon and a value.
- Schema: another name for the fixed shape, the agreed field names, types and allowed values that every reply follows.
- Parse: to read text and turn it into a structure a program can open, which fails when the text is not valid JSON.
- Validation: checking a parsed reply for required fields and allowed values before any tool uses it.
- Null: JSON's word for an unknown or missing value, written without quotation marks.
- Handoff: passing a case to a person, with the original message, after automated retries have failed.
What to watch for
- Notice how the same field names appear in the prompt and in the Python check, so the model and the program agree.
- Watch the retry arrow: the exact error goes back to the agent, and only a second failure reaches a person.
- Listen for why the chatty reply fails: the program stops at its first character, so the meaning of the words never matters.
Apréndelo con tu tutor en la pizarra en vivo
Puntos clave del video
A booking that reads well but books nothing
- A parent messages you at night
- The agent replies in warm sentences
- The calendar needs exact boxes
layout: side
direction: LR
components:
parent: {kind: user, label: Parent}
agent: {kind: llm, label: AI agent}
cal: {kind: external, label: Calendar}
links: [parent->agent, agent->cal]
steps:
- at: "a parent messages"
show: [parent]
- at: "AI agent answers"
show: [agent]
flow: ["parent>agent: Thursday at four?", "agent>parent: Lovely, see you!"]
- at: "cannot read a sentence"
show: [cal]
flow: ["agent>cal: Thursday at four"]
note: "free text, no date"
- at: "until a lesson is missed"
note: "booking lost quietly" What you will learn
- Ask for one fixed shape
- Check every reply before use
- Recover from broken replies
layout: side
tree:
Clean JSON: [Fixed shape, Check every reply, Recover]
direction: right
steps:
- at: "fixed shape"
show: "Fixed shape"
- at: "check every reply"
show: "Check every reply"
- at: "recover"
show: Recover
- at: "these three steps"
show: all What a fixed shape looks like
- A name, a colon, a value
- Boxes can sit inside boxes
- Free text has no boxes
layout: side
tree:
Booking:
customer: [name, phone]
appointment: [date, time, subject]
direction: right
steps:
- at: "labelled form"
grow: Booking
- at: "customer section"
grow: customer
- at: "appointment section"
grow: appointment
- at: "never change"
note: "same names every time" Put the shape in the prompt
You read customer messages for a small business.
Reply with JSON only, no greeting, no explanation.
Use exactly these keys, every time:
"intent": "book" | "reschedule" | "cancel" | "lead" | "invoice" | "sales" | "other"
"date": "YYYY-MM-DD", or null if not stated
"time": "HH:MM" (24-hour clock), or null
"name": the person's name, or null
"budget": a number, or null
"needs_human": true or false The program reads the reply
import json
reply = '{"intent": "book", "date": "2026-11-05", "time": "16:00", "name": null, "budget": null, "needs_human": false}'
data = json.loads(reply)
print(type(data))
print(data["date"], data["time"]) Check the reply before anything uses it
ALLOWED = ["book", "reschedule", "cancel", "lead", "invoice", "sales", "other"]
REQUIRED = ["intent", "date", "time", "needs_human"]
def check(data):
problems = [k for k in REQUIRED if k not in data]
if data.get("intent") not in ALLOWED:
problems.append("intent")
return problems
print(check({"intent": "book", "date": "2026-11-05", "needs_human": False}))
print(check({"intent": "party", "date": "2026-11-05", "time": "16:00", "needs_human": False})) Every field has a type and a rule
- A date in one format
- Times on the 24-hour clock
- Unknown means null
cells:
A1: Field
B1: "Good value"
C1: "Bad value"
A2: date
B2: "2026-11-05"
C2: "Thursday"
A3: time
B3: "16:00"
C3: "4 pm"
A4: needs_human
B4: "true"
C4: "maybe"
A5: unknown date
B5: "null"
C5: "blank"
steps:
- at: "the good date"
highlight: B2
note: "good: one format"
- at: "Thursday"
highlight: C2
note: "bad: not a date"
- at: "the good time"
highlight: B3
- at: "four p m"
highlight: C3
- at: "true or false"
highlight: B4
- at: "never maybe"
highlight: C4
- at: "unknown is null"
highlight: B5
note: "empty is not the same as unknown" Retry once, then hand over
- One retry with the error
- A person after two failures
- Nothing unchecked reaches the calendar
layout: side
direction: LR
components:
agent: {kind: llm, label: AI agent}
check: {kind: service, label: Checker}
cal: {kind: external, label: Calendar}
staff: {kind: user, label: Staff member}
links: [agent->check, check->cal, check->staff]
steps:
- at: "goes back to the agent"
show: all
flow: ["check>agent: missing time"]
note: "retry with the error"
- at: "fixed reply passes"
flow: ["agent>check: fixed reply", "check>cal: valid"]
note: "passes, booked"
- at: "still broken"
flow: ["agent>check: still broken"]
note: "second failure"
- at: "goes to a person"
flow: ["check>staff: escalate"]
note: "a person takes over"
- at: "reaches the calendar unchecked"
note: "nothing unchecked" One checked reply, four jobs
- Bookings go to the calendar
- Leads go to the customer list
- Invoices go to invoicing
- Sales questions go to the report
layout: side
direction: LR
components:
json: {kind: file, label: Checked JSON}
router: {kind: service, label: Intent switch}
cal: {kind: external, label: Calendar}
crm: {kind: storage, label: Customer list}
inv: {kind: external, label: Invoicing tool}
rep: {kind: analytics, label: Sales report}
links: [json->router, router->cal, router->crm, router->inv, router->rep]
steps:
- at: "the same shape feeds"
show: [json, router]
note: "one checked reply"
- at: "A booking with a date"
show: [cal]
flow: ["router>cal: 2026-11-05 16:00"]
- at: "A lead, with a name"
show: [crm]
flow: ["router>crm: lead"]
- at: "An invoice request"
show: [inv]
flow: ["router>inv: invoice"]
- at: "a sales question"
show: [rep]
flow: ["router>rep: sales"]
- at: "intent box decides"
highlight: router
- at: "all four jobs"
note: "one shape, four jobs" Common mistake: the chatty reply
import json
reply = 'Sure! Here is the booking: {"intent": "book"}'
try:
data = json.loads(reply)
print("Parsed:", data)
except json.JSONDecodeError as e:
print("Error:", e.msg, "at character", e.pos) Recap: three habits
- A fixed shape in the prompt
- Check before any tool uses it
- One retry, then a person
layout: side
tree:
Reply:
Shape: [exact names, typed values]
Check: [before any tool]
Recover: [one retry, then a person]
direction: right
steps:
- at: "bring it together"
show: "Reply"
- at: "free text date"
note: "one free text date broke a booking"
- at: "three habits"
show: ["Shape", "Check", "Recover"]
- at: "fixed shape in the prompt"
show: "Shape/**"
- at: "check every reply before any tool uses it"
show: "Check/**"
- at: "one retry with the exact error"
show: "Recover/**"
- at: "safe for bookings"
path: Recover
note: "bookings, leads, invoices, reports" Exercise: your own booking agent
Exercise: a booking agent for your business
1. Pick one service you sell
2. Write a prompt with intent, date and time
3. Add the rule: reply with JSON only
4. Send five real customer messages
5. In n8n or Make, parse each reply as JSON, then
add an IF node that checks the intent is allowed
and that the required fields are present
6. Write down every failure and its fix Transcripción
A booking that reads well but books nothing. Picture this. It is eleven at night, and a parent messages your tutoring business: can my son come on Thursday at four? Your AI agent answers in warm, correct sentences, and the parent is happy. But your calendar cannot read a sentence. It needs a date and a time in exact boxes. When the agent sends Thursday at four as free text, the booking fails quietly. Nothing crashed and nothing looked wrong, so nobody notices until a lesson is missed.
What you will learn. By the end of this lesson you will be able to do three things. First, you ask the model for one fixed shape, so every reply has the same boxes in the same order. Second, you check every reply before any tool uses it, so a bad date never reaches your calendar. Third, you recover from a broken reply, with one retry and a clear handoff to a person. You will use these three steps for bookings, leads, invoices and sales reports.
What a fixed shape looks like. JSON is a plain text format for a labelled form. Think of a paper form with labelled blanks, filled in the same way every time. Each box has a name, a colon and a value. Boxes can sit inside other boxes, like a booking with a customer section and an appointment section. The names never change, so your program always knows exactly where to look. Free text has no boxes at all, so the program has to guess.
Put the shape in the prompt. Now the important part. The shape goes into the prompt, the standing instructions you send with every message. Name each field exactly as your program expects it. The first field, intent, means what the customer wants to do, and null is the JSON word for unknown. Give the allowed values for any field with a fixed list, and say how a missing value is written. Then add the rule people forget: reply with JSON only, with no greeting and no explanation around it. Keep the instructions short, and let the shape carry the weight.
The program reads the reply. Once the reply has the right shape, the program can read it. The standard json library turns the text into a dictionary, which is a set of named boxes the program can open. The first print line confirms it is a dictionary. This reply holds a date and a time, and the program asks for the date by its name. It gets exactly what the agent wrote. Compare that with free text, where the program would have to hunt for the date inside a sentence.
Check the reply before anything uses it. This is the step most people skip. Before any value goes near the calendar, the program checks it. Required fields must be present, and the intent must be one of the words you allowed. The first test is missing its time, so the check returns time on its list. The second test has the intent party, which is not allowed, so it comes back flagged. A reply with problems goes back for a fix, and it never reaches the calendar. If you build with no-code tools, the same check is an IF node in n8n or Make, and this code shows the logic it runs.
Every field has a type and a rule. A date is always text in one format: year, month, then day. So the good date is right, and Thursday is the bad one, because only the first can be placed on a calendar. A time uses the twenty four hour clock. The good time is right, and four p m is wrong. A yes or no field, like needs_human, must be true or false, never maybe. A value that is unknown is null, not blank, so the check can tell an empty field from a real answer.
Retry once, then hand over. So what happens when a check fails? The reply goes back to the agent with the exact error, such as missing time, and the agent gets one retry. If the fixed reply passes, it goes to the calendar. If the second reply is still broken, then after two failures the program stops trusting the model, and the case goes to a person who sees the original message. Nothing reaches the calendar unchecked, and the customer still gets a human answer, not silence.
One checked reply, four jobs. Here is the payoff. Once the reply is clean, the same shape feeds every job. A booking with a date and a time goes to the calendar. A lead, with a name and a budget, goes to your customer list. An invoice request goes to the invoicing tool, and a sales question goes to the sales report. The intent box decides the route, and the prompt lists every intent it may return, so one reliable shape serves all four jobs.
Common mistake: the chatty reply. Here is the mistake that breaks most agents. You ask for JSON, but you never forbid chat, so the model writes Sure! Here is the booking, and then the object. The program reads the first letter, S, and stops. A person reading that reply would see nothing wrong, which is why this slips through testing. The fix is the rule from earlier: reply with JSON only, and let the checker reject anything else, including a chatty reply.
Recap: three habits. Let us bring it together. The hook was one free text date that quietly broke a booking. Today you learned three habits. First, put one fixed shape in the prompt, with exact names, typed values and a rule to reply with JSON only. Second, check every reply before any tool uses it, and reject anything missing or invented. Third, recover in order: one retry with the exact error, then a person. Those habits make your agent safe for bookings, leads, invoices and reports.
Exercise: your own booking agent. Your exercise is short and practical. Choose one service you sell, and write a prompt with three fields: the intent, the date and the time. Add the rule to reply with JSON only. Then test the prompt on five real customer messages, from your inbox or your notes. Build the check in n8n or Make: a JSON parse step, then an IF node that tests the intent and the required fields. Run each reply through it, and write down every failure next to the fix you think it needs. Bring that list to the next lesson.
Explora las animaciones
Las animaciones del video, exactas y en vivo: reprodúcelas, pausa, arrastra el control deslizante o salta a un paso a tu propio ritmo.
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Responde todas las preguntas y luego comprueba. Apruebas con 70%; puedes intentarlo las veces que quieras.
Resumen de la lección
- Lectura
- Video visto
- Cuestionario aprobado (70%)
- A fixed shape in the prompt
- Check before any tool uses it
- One retry, then a person