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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
- Read the trigger’s data and figure out what kind of thing it is.
- Ask the AI in a constrained, structured way, never open-ended.
- Check the answer fits the shape you asked for, retry once if not.
- Use confidence to decide between acting and handing off.
- For anything destructive, send it to a person before it ships.
- An agent is an AI that takes action, reads data, calls tools, decides what is next, not just an AI that answers a question.
- 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.
- Prefer 2 or 3 small calls over 1 enormous one. Each call should do one thing you can test in isolation.
- Ask the AI for its confidence and use that as the main fork between auto-act and hand-off.
- Never let the agent take a destructive action on a single AI call without a human in the loop.
- Validate the AI’s answer in code, check the fields, check the values, retry once, then escalate.
- 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.

