Testing an agent: tools, trajectory, outcome
An agent is tested on three levels: a single tool, the model's decision (tool and arguments) and the trajectory with its outcome in the world. The outcome is database state, a ticket or an email, not only the final text. Assertions cover required steps, forbidden actions and the end state. Tool failures are reproduced with stubs, and step and cost limits are checked separately.
AI for QA · Testing AI
The agent answered "Done, the money is refunded", yet there is no refund in the database. What did the test check?
A test for a support agent checked that the final answer contains "money refunded". It was green, and there were no refunds in the database: the agent wrote a pleasant phrase and never called the tool. For a regular chat bot the text is the result, for an agent the result is a change in the world. The lesson covers how to test an agent at the level of tools, decisions and trajectory.
How it works and why. An agent is more complex than a chat: it has steps and side effects (for the structure of the loop see the lesson “Tool calling, agent and MCP”). So three levels are tested.
- A single tool. Ordinary tests: contract, argument validation, errors, idempotency. No model is needed here, the tests are deterministic and fast;
- Model decisions. Whether the right tool with the right arguments was chosen for this request (tool selection accuracy) and whether there are extra calls;
- Trajectory and outcome. The whole sequence of steps and the result in the world: database state, a created ticket, a sent email, not only the final text.
Three levels of testing an agent
The main technique: record the trajectory (a trace of all messages and calls) and assert on it and on the system state.
Text or the state of the world
Checking the final text
An assert on the phrase "Done, the money is refunded". The agent could write that without calling the tool, and the test is green.
Trajectory and world state
An assert on the call sequence and on the database state: exactly one refund. The text only complements.
test('refund request creates exactly one refund', async () => {
const run = await agent.run('Refund order 1042', { seed: fixtureDb });
// path: the needed tools, no extra ones
expect(run.toolCalls.map((c) => c.name)).toEqual(['get_order', 'create_refund']);
expect(run.toolCalls[1].args).toMatchObject({ orderId: 1042 });
// the outcome in the world, not just text
expect(await db.refunds.count({ orderId: 1042 })).toBe(1);
// limits: steps and budget
expect(run.steps).toBeLessThanOrEqual(6);
expect(run.usage.totalTokens).toBeLessThan(8000);
});
The test has three layers. The path: the needed tools, no extra ones. The outcome: exactly one refund in the database. The limits: steps and tokens within bounds. It does not require an exact phrase, because the phrase may change.
Example: a tool failure. The most interesting agent defects show up on failures. The tool performed the action, but the response was lost to a timeout, and the model, not seeing the result, may repeat the call (the duplicate email story is analysed in the lesson on tool calling). For the check we replace the tool with a stub that performs the action and then throws a timeout:
test('a lost tool response does not create a second refund', async () => {
// the tool runs, but its response is lost to a timeout
const tools = fakeTools({ create_refund: timeoutAfterSuccess() });
const run = await agent.run('Refund order 1042', { tools, seed: fixtureDb });
expect(await db.refunds.count({ orderId: 1042 })).toBe(1);
expect(run.status).not.toBe('step_limit');
});
The test checks not texts but invariants: exactly one refund and the agent did not loop. This is how resilience to failures is checked: a tool returned an error or a timeout, an empty response, data in an unexpected format. The agent must neither loop, nor invent a result, nor quietly ignore the error.
Practices.
- Mock the outside world (tools, APIs, time) for fast stable runs, and keep a small separate set with real integrations in a sandbox;
- Do not require a single path if several are correct: check the mandatory steps, forbidden actions and the final state;
- Record / replay: recorded model answers give deterministic regressions of the agent logic with no model cost;
- Side effects: idempotency, human confirmation of dangerous actions, tool rights by the principle of least privilege;
- Multi-step dialogues: loss of context, contradictory clarifications, a change of intent;
- Limits: max steps, tokens, time, cost and behaviour when they are reached (the agent stops honestly and does not pretend it finished).
Limits and alternatives. Rigid asserts on the call sequence are brittle if the agent has several correct paths: a test "it must be exactly these two tools" is overly strict. Then check the set of mandatory steps and prohibitions. Tests with a real model are non-deterministic, so they run over repeats and measure the success rate (see the lesson on evals). To judge the quality of reasoning and the wording of the result, add a judge with a rubric, and keep the action check deterministic.
Common mistakes.
- Asserting only the answer text. Consequence: an agent that did nothing passes.
- Requiring a single sequence of tools. Consequence: the test fails on a correct alternative path.
- Not testing tool failures. Consequence: loops and invented results are found by users.
- Real integrations in every test. Consequence: slow, expensive, side effects in real systems.
- Not checking limits. Consequence: an infinite loop is noticed on the bill.
Check yourself
A test expects the call order get_order, create_refund. After an update the agent starts by calling get_customer, and the result is the same. What do you do with the test?
Answer
This is a correct alternative path, not a regression. Rewrite the test: the mandatory steps (create_refund after get_order), forbidden actions and the final state in the database, without a rigid order of extra helper calls. Otherwise the test will cry wolf on every legal change.
Why does the stub in the tool failure test perform the action and only then throw a timeout, instead of throwing the error at once?
Answer
Because the real risk is that the action happened and the response got lost. Then a retry creates a duplicate. If the error is thrown before the action, this case is not reproduced and the test does not check idempotency.
The agent reached the step limit and answered "Done". What should the test check?
Answer
The completion status (limit, not success) and the absence or rollback of partial side effects. The agent must stop honestly: say that it did not finish, not pretend success.