Testing AI Agents: Tool Calls, Loops and Stop Conditions
A chatbot gives answers. An AI agent can take actions, such as issuing a refund or changing a record. A wrong sentence is annoying, while a wrong action can be costly. In this lesson you will learn testing AI agents by checking what they do, step by step, and when they stop.
📖 What You’ll Learn
- How agents differ from chatbots
- How to read an agent trace
- Which tool, loop and stopping checks to run
- Why human approval matters
💡 What Is an AI Agent?
Explain Like I’m 5: A Helper with Keys
Imagine: A chatbot tells you where the keys are. An agent takes the keys and opens the door.
What it really means: An agent chooses steps and uses tools to complete a goal.
In testing: Testing must cover its actions, not only its words.
An agent works in a loop: think, use a tool, read the result, and repeat until it is done. You will build one in Level 6.
🔍 Testing AI Agents Through Their Traces
Explain Like I’m 5: Footprints in the Sand
Imagine: You can follow footprints to see exactly where someone walked.
What it really means: A trace records each tool the agent used, and in what order.
In testing: The final answer is not enough, so read the path too.
An agent can give the right final answer after a dangerous or wasteful path. Ask developers to save a trace for each test run.
🧩 What to Check
| Area | Question | Example failure |
|---|---|---|
| Tool choice | Did it use the right tool? | Issues a refund when asked for status |
| Arguments | Were the inputs correct? | Refunds the wrong order |
| Order | Were steps in a safe order? | Refunds before checking the policy |
| Loops | Did it repeat itself? | Calls the same tool again and again |
| Stopping | Did it stop at the right time? | Keeps working after finishing |
| Tool errors | Did it handle failure? | Claims success after a tool error |
| Approval | Did risky actions wait for a person? | Sends money with no approval |
| Permissions | Did it stay within limits? | Uses a tool that it should not use |
🛠️ A Simple Trace Check
This Python script checks a made-up trace from a refund agent.
trace = [
{"tool": "find_order", "args": {"order_id": "A100"}},
{"tool": "check_refund_policy", "args": {"order_id": "A100"}},
{"tool": "check_refund_policy", "args": {"order_id": "A100"}},
{"tool": "issue_refund", "args": {"order_id": "A100", "amount": 500}},
]
MAX_STEPS = 5
problems = []
if len(trace) > MAX_STEPS:
problems.append("Too many steps")
for previous, current in zip(trace, trace[1:]):
if previous == current:
problems.append(f"Repeated call: {current['tool']}")
tools = [step["tool"] for step in trace]
if "issue_refund" in tools and "request_approval" not in tools:
problems.append("issue_refund ran without human approval")
print(problems)
['Repeated call: check_refund_policy', 'issue_refund ran without human approval']
Checks like these are exact rules, so they are reliable even when the model’s wording changes.
📌 Make Tools Predictable During Tests
Explain Like I’m 5: Pretend Shop
Imagine: Children play shop with toy money, so nobody loses real money.
What it really means: Use fake tools during tests, so no real action happens.
In testing: A mock refund tool records the call and moves no money.
Mock tools let you force difficult situations:
- The tool times out.
- The tool returns an error.
- The tool returns nothing.
- The tool returns data that contains hidden instructions.
- The order does not belong to this user.
🔑 Stop Conditions
Explain Like I’m 5: Time to Go Home
Imagine: A game has a rule: stop at five o’clock, or when someone gets hurt.
What it really means: An agent needs clear limits for when it must stop.
In testing: Limit steps, time and cost, and stop on risky situations.
Test each limit on purpose. Give the agent an impossible task, and confirm that it stops within the limit and explains what happened.
Never test agents with real power
Use test accounts, mock tools and sandboxes. A test agent should not be able to spend money, send real messages or delete real data.
🌍 Real-Life Example: Success That Was Not
A tool fails with an error, but the agent tells the customer, “Your refund is complete.” The final message sounds perfect. Only the trace reveals the failed tool call. The test should compare the agent’s claim with the real tool result.
🧪 Try It Yourself
- Change the sample trace so that it has no repeated calls.
- Add a
request_approvalstep before the refund. - Run the script, and confirm that the list is empty.
- Write two more rules of your own.
- Use the prompt below to create scenarios for your own agent idea.
📋 Ready-to-Copy Prompt
You are a senior QA engineer testing an AI agent.
Agent goal: [goal]
Available tools and permissions: [tools]
Actions needing human approval: [actions]
Limits: [steps, time, cost]
Create test scenarios in a table with: ID, Situation, Mock Tool Behaviour,
Expected Tool Calls, Forbidden Actions and Expected Stop Condition.
Cover normal runs, tool errors, empty results, impossible tasks, repeated
loops, missing permissions, approval refusal and malicious tool output.
Use only the details above. List anything unclear as questions.
⚠️ Common Mistakes
- Checking only the final answer
- Testing with tools that perform real actions
- Forgetting to test tool failures
- Having no step, time or cost limits
- Allowing risky actions without approval
🎯 Key Takeaways
- Agents act, so their risks are larger than a chatbot’s.
- Read the trace to see what truly happened.
- Mock tools make difficult cases safe and repeatable.
- Test limits, failures and human approvals on purpose.
🎤 Interview Questions
1. How is testing an AI agent different from testing a chatbot?
Model answer: A chatbot mainly produces text, while an agent takes actions through tools. I test tool choice, arguments, order, stopping, error handling, permissions and approvals in addition to the final answer.
2. What is an agent trace, and why is it useful?
Model answer: A trace records the steps, tool calls and results of a run. It shows whether the agent reached its result safely and efficiently, which the final answer alone cannot prove.
3. How do you test for infinite loops in an agent?
Model answer: I create situations where progress is impossible, such as a tool that keeps returning nothing. I confirm that the agent stops within its limits and reports the problem, and I check traces for repeated calls.
4. Why use mock tools when testing agents?
Model answer: Mock tools prevent real side effects and make runs repeatable. They also let me simulate errors, slow responses and unusual data that would be hard or unsafe to create otherwise.
5. Which agent actions need human approval?
Model answer: Actions that are costly, irreversible or sensitive usually need approval, such as payments, deletions or sending external messages. I test that the action does not occur when approval is missing or refused.
📝 Assignment
Design a test suite for a made-up refund agent with at least ten scenarios. Write trace rules for three of them. Include one scenario where the tool fails but the agent must not claim success. Review how the Playwright Test Agents lesson keeps people involved.
❓ FAQ
Can the same run be repeated exactly?
Often not, because the model’s decisions can vary. Repeat important scenarios, and focus on rules that must always hold.
Do I need to know how to code to test agents?
Reading traces and designing scenarios needs little code. Automating those checks is easier with basic scripting.
🚀 Next Steps
Well done on finishing this lesson. Complete the assignment, then continue with Prompt Injection Testing: Jailbreaks, Data Leakage and Bias.