Module 2 · AI Agents Foundations
How agents work
Open up an agent and see its six parts (model, instructions, tools, knowledge, memory and guardrails) and the loop that ties them together, traced step by step on real customer messages.
About 30 minutes
The problem
Mrs Okafor's nephew Tunde says: "An agent is just ChatGPT connected to WhatsApp." Her friend who runs a pharmacy says agents are "robots that do everything by themselves". Both are wrong, and if Mrs Okafor believes either, she'll buy the wrong thing or trust it too much.
Before you build an agent, you need a clear picture of what's inside one and how it decides what to do. Then you can ask any vendor the right questions, and you'll understand every line of code in the later courses.
The concept
The six parts of an agent
1. The model is the reasoning engine: a large language model (LLM) such as Claude, GPT or Gemini. It reads everything it's given and decides what to say or do next. It doesn't remember anything between calls by itself and it doesn't run anything; it only produces text, including requests to use a tool.
2. Instructions (also called the system prompt) tell the agent who it is and how to behave: "You are Ọja Electronics' WhatsApp assistant. Answer in the customer's language. Never ask for card details. Hand complaints to a person." Instructions are the biggest lever you have, and lesson 3 is all about writing them.
3. Tools are actions the agent may ask for: find_order, check_stock, create_payment_link, hand_over_to_person. A tool is a small piece of software that someone wrote and tested. The model asks to use one; the software runs it and returns the result. Tools decide what an agent can reach. An agent with no refund tool can't issue refunds, however cleverly a customer asks.
4. Knowledge is the approved information the agent can look things up in: Ọja's 24 help answers on delivery, payment, warranty and returns; the price list; the returns policy. Without knowledge, a model guesses from what it learned in training, which may be out of date or simply wrong for Ọja.
5. Memory is what the agent knows about this conversation and this customer: what was said five messages ago, the customer's name, their last order. You'll build three kinds of memory in the Python course.
6. Guardrails are the limits around the agent: actions that need a person's approval, a maximum number of steps, topics it must refuse, information it must never ask for. Guardrails are what make an agent safe enough to put in front of customers.
The loop
The parts work together in a loop.
- Think. The model reads the instructions, the conversation, any knowledge, and the list of tools, and decides what to do next.
- Act. If it needs information or needs to do something, it asks for a tool, such as find_order(OJ204811).
- Observe. The software runs the tool and gives the result back to the model.
- Decide. Is the goal met? If yes, write the reply. If not, go back to step 1 with the new information.
Some messages need no tools at all ("What time do you close?" is answered from knowledge in one step). Others need several turns of the loop: "I want the Infinix Note 50 delivered to Surulere, how much in total?" needs a stock check, a price, a delivery fee and a sum.
Why the loop needs a guard
Sometimes an agent gets stuck: the tool keeps failing, or the customer's message is confusing, and the agent tries the same thing again and again. Every turn costs money and the customer is waiting. So every real agent has a step limit and a fallback: after, say, five steps without an answer, stop and hand the conversation to a person with a note of what was tried.
Same message, different paths
| Customer message | What the agent does |
|---|---|
| "What time do you close on Saturday?" | Think → answer from knowledge (no tool) |
| "Where is my order OJ204811?" | Think → find_order → observe → reply |
| "Is the HP 15 in stock and how much to Yaba?" | Think → check_stock → delivery_fee → add up → reply |
| "Your rider insulted me, I want my money back" | Think → this is a complaint → hand_over_to_person → tell the customer a person will reply |
The last one is important: a good agent's skill is often knowing when not to act.
Example
Here is a trace of one conversation: the record of each step an agent took. Real agent platforms show traces like this, and reading them is how you understand and debug agents.
| step | what happened | detail |
|---|---|---|
| 1 | message received | "Abeg how much be Oraimo 20000mAh power bank, and una dey deliver Ajah?" |
| 2 | think | Customer wants a price and delivery to Ajah; they wrote in Pidgin. |
| 3 | tool: check_stock | Oraimo 20,000mAh power bank: ₦24,000, 75 in stock |
| 4 | knowledge: delivery | FAQ01: the Island and Ikorodu cost ₦5,000 to ₦6,000 |
| 5 | think | Ajah is on the Island. Total ₦29,000 to ₦30,000. Reply in Pidgin. |
| 6 | reply | "The Oraimo 20,000mAh na ₦24,000, e dey for stock. Delivery to Ajah na ₦5,000 to ₦6,000. You wan make I arrange am?" |
Six steps, one tool, one knowledge lookup, a reply in the customer's own language, and a question that moves towards a sale.
Walkthrough
Use the inbox sheet from lesson 1.
- Make a pivot table: select all the data, then Insert → Pivot table → New sheet.
- Add intent under Rows and message_id under Values (summarise by COUNTA). You now have the number of messages of each kind.
- Add staff_minutes under Values too (summarise by SUM). This shows which kinds of message cost the most staff time.
- Next to each intent, write which agent parts it needs: knowledge only, a tool, or a person.
- Sort the pivot table by staff minutes, largest first. Which three intents would save the most time if an agent handled them?
Practice
Practice
Order status questions need a tool (finding the order). How many order_status messages did Ọja get in October?
Practice
How many staff minutes in total did order_status messages take?
Task
8 minA customer writes at 9:15pm: "Good evening, do you have the HP 15 laptop? I'm in Yaba and I need it by Friday."
Write out the agent loop for this message as numbered steps: what the agent thinks, which tools or knowledge it uses, what it observes, and the reply. Then name one guardrail that applies.
Your work is checked for
- Numbered steps (at least four lines)
- Uses a tool or knowledge (stock, check, delivery, FAQ, knowledge…)
- Includes thinking and observing (think, observe, result, returns…)
- Includes the reply to the customer
- Names a guardrail (limit, approval, never, hand over, person, card…)
Check your understanding
Answer every question to check.