An AI agent is an LLM in a loop: it reads a task, decides which tool to call, reads the result, and repeats until it thinks it is done. Testing one is harder than testing a chatbot, because a wrong step can still end in a plausible final message, and because the agent may take a different path every time. The approach that works is to test three things separately: the steps, the outcome, and what happens when something goes wrong.
Make the agent testable first
Three engineering changes make agent testing practical, and they are worth doing before writing tests:
- Record the trajectory. Log every model call, tool call, arguments and result as structured data. Tests assert on this trace, and it is what you will debug with.
- Make tools replaceable. Tools should be injected so a test can swap the real payment API for a fake that returns fixed data, errors or timeouts.
- Control the environment. Seeded test accounts, a sandbox database and fixed dates. Agents are sensitive to data, and flaky data makes every result meaningless.
Test the tool calls
Most agent bugs are visible in the tool calls long before the final answer. For each scenario, assert on what must and must not happen:
- the right tool was called, with valid arguments (schema checks catch a lot);
- required steps happened in a sensible order, for example looking up the order before refunding it;
- forbidden actions never happened: no refund above the limit, no email to an address not in the task;
- the agent did not loop, repeating the same call with the same arguments.
Avoid asserting on one exact sequence unless the business requires it. Two valid paths to the same result should both pass. Assert on invariants instead.
def test_refund_respects_limit(agent, fake_tools):
fake_tools.orders.add(id="A-1001", total=80.00, status="delivered",
customer="customer@example.com")
trace = agent.run("Customer says order A-1001 arrived broken, refund it")
refunds = trace.calls("issue_refund")
assert len(refunds) == 1
assert refunds[0].args["order_id"] == "A-1001"
assert refunds[0].args["amount"] <= 80.00
assert trace.called_before("get_order", "issue_refund")
# only the customer on the order may be emailed
assert all(c.args["to"] == "customer@example.com" for c in trace.calls("send_email"))Score outcomes over many runs
Because agents are not deterministic, one passing run proves little. Run each scenario several times (five to ten is common) and report a task success rate per scenario. Success should be checked against the end state, not the agent's message: is the ticket actually closed, the record actually updated, the refund actually issued once?
A scenario that passes 7 times out of 10 is a real finding. It usually points at an ambiguous tool description, a missing instruction or a tool that returns confusing output. Track success rates per scenario over time; a drop after a model or prompt change is exactly the regression you want to catch.
Test failure recovery deliberately
Real tools fail. Use the fake tools to inject failures and check that the agent behaves safely:
- an API returns an error or times out: does the agent retry sensibly, or invent a result?
- a tool returns empty or unexpected data: does it ask, stop, or guess?
- the task is impossible or not allowed: does it say so instead of doing something nearby?
- it reaches the step limit: does it stop cleanly and report what it did?
The worst agent failure is not an error message, it is a confident "done" after a failed step. Make that case a test.
Track cost and latency with quality
A prompt change can raise the success rate and triple the number of model calls. Record steps per task, tokens and wall-clock time for every scenario run, and set budgets alongside quality thresholds. In production, step count is also a cheap early signal: a sudden rise usually means the agent is struggling.
Include the security cases
Agents read content they do not control: web pages, emails, documents, tool results. Any of these can contain instructions aimed at the agent. Test that text inside a tool result cannot make the agent call tools the user did not ask for. That is indirect prompt injection, covered in our prompt injection testing guide.
Where to start
- Pick the five tasks that matter most to the business and write them as scenarios with fake tools.
- Add invariants for each: required calls, forbidden calls, end state.
- Run each scenario ten times and record the success rate, steps and cost.
- Add two failure-injection variants per scenario.
- Run the suite on every prompt, model or tool change, and nightly against the current model.
AI agent testing is part of our LLM, RAG and AI agent testing service. For agents built on retrieval, see also how to test a RAG system.