04Chapter 4 of 7
Free course4 min read

Build the agent.

The logic layer of your automation. This chapter is where the prompt, the decision tree, and the human handoffs come together into something that can actually do the work.


Once the trigger has fired, something has to decide what to do with the thing that just arrived. That something is the agent. It reads the data, asks the AI the right question or two, interprets the answer, and chooses the next move. This chapter is about building one that actually behaves.

04.0

What an agent actually is

The word agent gets thrown around. The simplest way to hold it: an agent is an AI that can take action on its own, call tools, read results, decide what to do next, rather than just answering a question and going quiet. If you ask a chatbot “is this email urgent?” and it says yes, that is not an agent. If something reads the email, decides it is urgent, drafts a reply, and books a follow-up in your calendar, that is.

The running example for this chapter is Avery. Avery runs a 3-person home-services company , small renovations, handyman work, the occasional kitchen refit. Leads come in through a contact form on the website. Each one is a few sentences: someone’s sink is leaking, someone wants a quote on a deck, someone wants to know if Avery does tiling. The loop Avery wants to automate is: read each lead, decide what kind of job it is, write a reply that matches, and either book a site visit or hand the lead off so Avery can call them personally.

The trigger in chapter 03 was the easy part, a new form submission, one webhook, done. Everything after that is the agent. It is also where the difference between a sturdy automation and a sloppy one shows up. A sloppy agent asks the AI a vague question and then tries to guess what the answer means. A sturdy one asks a specific question in a constrained format, validates the answer in code, and has a clear path for the cases where the answer is not good enough to act on.

04.1

The prompt that makes the agent useful

The prompt is the instructions you give the AI. It is the single most important thing you write in the whole automation. The model is going to do exactly what you ask it, no more, no less, and if you ask vaguely, you get vaguely back. The prompt has to do 4 things at once: tell the AI what role it is playing, what information it has, what to produce, and what shape that output needs to be in so the next step can use it.

Here is the prompt Avery’s agent uses to read a new lead. Read it once before the walk-through, it is shorter than it looks.

The lead-reading prompt
read-lead.promptprompt
1You are reading a new lead from Avery's Home Services
2contact form. Avery does small renovations, handyman work,
3tiling, and the occasional kitchen refit. Avery does NOT do
4roofing, plumbing emergencies, or commercial work.
5 
6Here is the message the customer left:
7"""
8{{lead.message}}
9"""
10 
11Return a small structured summary so the next step can act
12on it. Use these fields exactly:
13 
14 job_type: one of [handyman, renovation, kitchen, tiling,
15 out_of_scope, unclear]
16 urgency: one of [today, this_week, flexible, unclear]
17 budget_hint: a short phrase from the message if budget is
18 mentioned, otherwise "none"
19 confidence: one of [high, medium, low]
20 short_reply: 2 to 3 sentences Avery could send as-is,
21 warm but not gushing, no emoji, no exclamation
22 points, signed "— Avery"
23 
24If the job is clearly outside Avery's scope (roofing,
25emergency plumbing, commercial), set job_type to
26"out_of_scope" and write a short_reply that politely
27declines and suggests calling a specialist.
Role + scope
Tells the AI who it is reading for and what Avery does. The explicit NOT list stops the model from accepting roofing or commercial work.
The input
The customer message itself, dropped in via a template variable. Triple-quoted so the model can tell where it starts and ends.
Output framing
Sets the expectation that this is a small structured form, not a free-form opinion.
Output fields
Each field is constrained to a list or a short phrase so the next step can switch on it cleanly. Tone constraints on short_reply stop the AI from sounding generic.
Edge case
Explicit instruction for out-of-scope leads so the agent has a defined path instead of guessing.

Walk it from the top. The first paragraph gives the AI a role, it is reading a lead for Avery, and it has the context of what Avery actually does. That sentence about what Avery does not do is doing a lot of work; without it, the AI will cheerfully accept a roofing job because it does not know any better. The triple-quoted block in the middle is the lead message itself, what the AI can see in one go is just this prompt, so anything you want it to consider has to be in here.

The bottom half is the shape of the answer. The agent is not asking the AI “what do you think?”, it is asking for a small filled-in form. job_type is constrained to a list, so the next step can switch on it cleanly. urgency is the same. confidence is the agent’s honest read of how sure it is, and the next step uses that to decide whether a human looks at this one. Theshort_reply is the draft Avery might actually send. Constraints on the reply (no emoji, no exclamation points, warm but not gushing) keep the AI from sounding like a used-car ad. If you leave tone unspecified, the model defaults to enthusiastic and generic — the exact opposite of how a tradesperson actually writes to a potential job.

✱ Field note
A good prompt tells the AI 4 things: what role you are playing, what you can see, what to produce, and what shape it should come back in. If any of the 4 is missing, the AI fills the gap with its own taste. You will not like its taste.
04.2

One call or many?

A common reflex is to write 1 enormous prompt that does the whole job, classify, draft, decide, write it all to the CRM, send the email. It feels efficient. It is also fragile. The more you cram into a single call, the more places the model has to be right at once, and the harder it is to figure out which part went wrong when something does.

The cleaner pattern is to break the agent into 2 or 3 smaller calls, each with one job. For Avery’s lead-handling agent, that looks like: 1 call to read and classify the lead, then if the agent has high confidence, a second call to draft a slightly longer version of the reply for the customer. If confidence is low, no second call, Avery sees it and handles it. Two simple calls, each easy to test, each easy to fix.

Single call vs split calls
TriggerAgentModelActionSingle big call, everything in one shotnew leadclassify + draft + decideone big answerdo it (somehow)Split calls, one decision at a timenew leadclassify onlytype + confidenceif confident: draft replydraft textroute by confidence
The split version lets the agent skip the draft step entirely on low-confidence leads, and stops on its own when it should.

Splitting is also how you control cost. Classification is cheap. Drafting is more expensive. If 1 lead in 4 is out of scope or unclear, you skip the drafting call for those and you have just cut the model bill by 25 percent without losing anything. The agent is making a decision about which calls are even worth making. For a high-volume agent — say, Avery's business grows and the form gets 50 leads a day — that 25 percent saving compounds into a meaningful monthly number without any sacrifice in quality.

04.3

Handling the answer reliably

Before we drill into validation, look at the loop end-to-end. Here is what the agent actually does once it is running. Watch the pulse, that is a single lead moving from the contact form through the classify call, through the judge step, and out to whichever action the agent picks. Every lead Avery gets makes this trip.

One lead, one pass through the agent
Inbound leadwebsite formwebsite formRead + class…GPT-5 miniGPT-5 miniJudgeconfidence checkconfidence checkActionauto-reply or handoffauto-reply or handoffmessage + emailjob_type + confidenceroute the lead
The pulse is the lead. It moves through the same 4 stations every time, what changes is which action the judge step picks at the end.

Four stations, in order. The lead lands. The classify call reads it and returns a small structured answer, job type, urgency, confidence, draft reply. The judge step looks at that answer and decides whether to act on its own or hand the lead off. The action step does the one thing that actually touches the customer. Notice that only the first station is driven by what the customer sent; every station after that is driven by what the previous station returned. The chain moves one step at a time, and each step only needs to know about its immediate inputs and outputs.

The AI’s answer is text. Even when you ask for a structured form, you are getting text back that looks like a form. The agent’s job is to read that answer and decide what to do with it, which means the agent has to handle two questions every time: is this answer in the shape I asked for, and is the AI confident enough that I should act on it?

The first question is the boring one. You check that the answer has the fields you asked for, that job_type is one of the values from your list, thatshort_reply is not empty. If it fails, you retry the call once and then, if it fails again, the lead goes to Avery untouched. Do not get clever with parsing, the model will occasionally hand you something malformed, and the right response is a clean handoff to a human, not heroic recovery. Trying to salvage a malformed answer tends to produce subtly wrong behavior that is harder to debug than a clean failure.

The second question is the interesting one. The AI itself can tell you how confident it is, if you ask. That is what the confidence field in the prompt was for. The agent uses it as the main fork in the road.

What the agent does with the AI's answer
YesNo
Confidence = high?
Auto-send + bookreply goes, calendar holds slotHand to Averydraft attached, Avery decides

Two outcomes only. The agent is either confident enough to act, or it is not. There is no middle path where the agent does 80 percent of the work and quietly skips the rest, that is the path where bad things happen quietly. The lead that did not fit any category, the lead where the AI guessed the urgency wrong, the lead where the customer asked for something Avery does not do but the model decided to be helpful anyway, all of those go to Avery, with the draft attached so Avery can see what the agent would havedone. That last part is how Avery learns to trust the agent over time.

Critical
Never let the agent take a destructive action, sending money, deleting records, calling a customer’s phone, based on a single AI call without a confirmation step. The AI will be wrong sometimes. Destructive actions need a human in the loop, every time, until the agent has earned the trust with a long stretch of being right.
04.4

Where the human stays in the loop

Almost every working agent in a small business has a person reading what it did before anything irreversible happens. Sometimes the person reads every single output for the first month. Sometimes they read a daily digest of just the low-confidence ones. Sometimes they only see the agent’s work when it flags itself as unsure. But the person is always there. The agents that try to remove the person entirely are the ones that quietly burn customer trust until someone notices.

✱ Field note
Someone always reads the result before money moves, before a customer is told something binding, before a record is deleted. Build the handoff in from day one, retrofitting a human review step into an agent that has been firing for a month is harder than it sounds.

For Avery, the handoff is simple: anything the agent is not confident about goes to a single Slack channel with the draft reply attached. Avery scans the channel between jobs. Most days there are 1 or 2 leads to look at. Avery either approves the draft, edits it, or writes a fresh reply, and the agent learns nothing from this in the technical sense, but Avery learns a lot about where the prompt needs sharpening, which is how the automation gets better over the next month. After 30 days of that feedback loop, the low-confidence rate usually drops by half — not because the model changed, but because the prompt got clearer about the edge cases it kept getting wrong.

04.5

The full loop, one lead at a time

Put it together. A lead from Avery’s contact form arrives. The agent reads the message, runs the classification call, looks at the confidence, drafts a reply if it can, and either fires off the auto-response or hands the whole thing to Avery. From the outside it looks like one smooth step. From the inside it is 4 small ones in a row. The customer who gets the auto-response has no idea an AI was involved — they got a warm, specific reply in under a minute, which is better than most of what they'd get from a busy tradesperson checking messages at the end of the day.

Avery's lead-handling agent, one full pass
New lead form arrives
Agent reads the message
Agent drafts a reply
Agent picks an action
Confidence = high?
Yes
Auto-reply + book site visit
No
Send to Avery with draft attached

Run that loop on a real morning. 6 leads come in between 7 AM and 11 AM. 2 are easy handyman jobs with clear addresses and a flexible timeline, the agent classifies them with high confidence, sends a warm short reply, and books site visits for later that week. 1 is a kitchen refit with a real budget mentioned, high confidence, but the budget is much bigger than Avery’s usual job, so the agent flags it for Avery to call personally. 2 are out of scope (a roof leak and a commercial fit-out), the agent sends the polite decline. 1 is hard to read: the customer wrote 3 sentences that could mean tiling or could mean a full bathroom renovation. The agent marks it low confidence and hands it to Avery with the draft attached. Avery reads the message at lunch, decides it is a bathroom renovation, sends a follow-up, and notes that the prompt should probably ask the AI to flag “bathroom” as its own category next month.

What the agent is doing, in 5 lines
  1. Read the trigger’s data and figure out what kind of thing it is.
  2. Ask the AI in a constrained, structured way, never open-ended.
  3. Check the answer fits the shape you asked for, retry once if not.
  4. Use confidence to decide between acting and handing off.
  5. For anything destructive, send it to a person before it ships.
KEY TAKEAWAYS
  1. An agent is an AI that takes action, reads data, calls tools, decides what is next, not just an AI that answers a question.
  2. The prompt is the agent. Write it to tell the AI its role, what it can see, what to produce, and what shape the answer should be in.
  3. Prefer 2 or 3 small calls over 1 enormous one. Each call should do one thing you can test in isolation.
  4. Ask the AI for its confidence and use that as the main fork between auto-act and hand-off.
  5. Never let the agent take a destructive action on a single AI call without a human in the loop.
  6. Validate the AI’s answer in code, check the fields, check the values, retry once, then escalate.
  7. Build the human review step in from day one. The agent gets better because a person reads its work, not because the model gets smarter.

End · Chapter 4 of 7

Mark it complete and it’s ticked off on your route.

Rivet, the product security agent for software teams, free to start. Human-led pentests and design review when you need them. AI agents are our specialty.

or get a free Rivet scan

BASEDChicago, IL · remote

FIELD NOTES

ONE SHORT EMAIL A MONTH

© 2026 ROUTEWORK — ALL RIGHTS RESERVED

Claude and Mythos are trademarks of Anthropic. RouteWork is not affiliated with Anthropic.