Agents and workflows
From answering to acting
So far the model answers. Booking a counselling call takes more: find free slots, ask which one suits, book it, confirm.
A system that repeats a loop of decide → use a tool → look at the result until the job is done is called an agent.
- them"I'm Tom, Python course. Can I book a call Saturday morning?"
- decideI need free slots. Use
find_slots. - resultAnand 10:00 and 11:00, Divya 10:00 and 11:00, Meera 10:00.
- reply"Saturday at 10:00 or 11:00?" Tom: "10:00 please."
- decideBook it. Use
book_call(Anand, Sat 21 Nov 10:00, Tom). - resultBooked.
- reply"Done! Anand will see you Saturday at 10:00."
Does everything need an agent?
Workflow or agent?
When the steps are always the same, write them in code, in order. That's a workflow. It's predictable, cheap and easy to test.
Use an agent only when the next step depends on what the person says.
Good FDEs use the simplest thing that works.
An agent also has to remember things.
Remember the conversation
People give details bit by bit, across many messages. The agent keeps what it has learned so far in a small record, and updates it each turn.
That record is the conversation's state. It's also what gets saved to the lead tracker at the end.
State
Now, booking safely.
Book without double booking
In discovery you found Rohan and Kavya both booked into Divya's 11:00 slot on 17 November. People were booking from an old view of the calendar.
An agent can make the same mistake: it finds a free slot, chats for a minute, then books, and meanwhile someone else took it.
Let's read the calendar.
✏️ Practice 6.1
Only a slot marked Free can be booked. Booked, Blocked and Leave slots can't.
Counselling slots, Tuesday 24 November
| Counsellor | 11:00 | 15:00 | 18:00 |
|---|---|---|---|
| Anand | Free | Free | Free |
| Divya | Free | Free | Booked (Abdul) |
| Meera | Free | Free | Free |
Hand over to a person
Some chats need a person: money trouble, grief, refunds, complaints, or when the agent isn't sure. Passing the chat to a person is a handover.
A good handover includes a short note, so the counsellor knows the story and the family doesn't have to repeat it.
What if the agent itself goes wrong?
Stop it going wrong
Tools sometimes fail or time out. An agent that simply tries again can book the same call twice.
An action that's safe to repeat, because doing it twice has the same effect as doing it once, is called idempotent. Make booking idempotent, and cap how many steps one message may take.
How many steps can it afford?
✏️ Practice 6.2
reply time ≈ steps × time per step
Do it for real
Now build the agent that books calls. In the notebook it remembers the conversation, finds the real double booking in Northstar's calendar, and books without stealing anyone's slot.
You'll also make retries safe, hand a worried parent to a counsellor with a note, and add a step limit for when a tool breaks.
Colab runs Python in your browser for free. Replay mode needs no key. About 45 minutes.
Then check what stuck.
Lesson test
Five questions, all on new examples.