QA AND TEST AUTOMATION · PART 10 · TESTING AI FEATURES · AGENTS · 92 OF 112

Testing AI Agents: Tool Calls, Steps, Multi-Turn

Agents decide which tools to call. Testing ShopRung's chat and its get_order tool on a real local model: assert on the trace, not just the answer. Real findings: malformed tool arguments in 1 of 3 runs, an answer that claimed "delivered" for a SHIPPED order, internal tool names shown to customers, a follow-up question that lost its order, and access control that held only because it lives in the tool code.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 11

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Test the trace
  • malformed args 1 in 3
  • answer vs tool result
  • leaked tool names
  • lost follow-up
  • access in code

Notes

The loop

The loop: the model reads the question and a description of the tool, then asks for a tool call, get order with order id two. ShopRung's code runs the tool, checks who is asking and returns JSON. The model reads that and writes the answer. A step limit, three model calls here, stops runaway loops. Every step is recorded in the response as tool calls, which is what makes it testable.

One order question
1 model reads the question and
  the tool description
2 model: call get_order
    {"order_id": 2}
3 code runs the tool, checks
  the user, returns JSON
4 model writes the answer from
  the tool result
max 3 model calls per question

Test the trace

So test the trace, not only the text. Assert the tool name, the arguments, and that the tool returned data rather than an error. Convert before comparing: the real model sent the order id as the text two, not the number. Then check the answer agrees with the tool result: order two is shipped. These checks are exact, because the tool side is code, even though the answer wording varies.

Trace assertions
r = chat("What is the status of
  order 2?", token=alice)
assert r.toolCalls[0].name ==
  "get_order"
args = r.toolCalls[0].arguments
assert int(args["order_id"]) == 2
assert "SHIPPED" in r.answer
  .upper()
assert "error" not in
  r.toolCalls[0].result

Real finding 1

Here is what really happened when Alice asked about order two, three times. Twice the model sent order id two and got the order. Once it invented arguments, status, total and a key with brackets around order id, so the tool found nothing. Small models do this. The tool should validate arguments against its schema and return a clear error, and your tests should count how often calls are malformed.

Alice, llama3.2:3b, 3 runs
runs 2, 3: get_order
  {"order_id": "2"}  ✓ data
run 1:     get_order
  {"status": "status",
   "total": "total",
   "[order_id]": "2"}  ✗
  → "I could not find order
     undefined in your account."
answer: "...try calling the
  get_order tool again..."

Real finding 2

Even with the right data, the answer can drift from it. The tool said shipped. One answer said the order had been shipped and delivered. Delivered is a different status, not in the result. So check answers against the tool output, for example that the status word in the answer equals the status in the trace, or with a judge given the tool result.

Tool said vs model said
tool result:
  {"id": 2, "status": "SHIPPED",
   "total": "$75.00", ...}
answer (run 3):
  "Order 2 is currently shipped
   ... It cannot be cancelled as
   it has already been shipped
   and delivered."
DELIVERED: not in the result

Real findings 3, 4

Two more. A logged out customer was told to call the get order tool, an internal name that means nothing to them. And in a conversation, after order two is shipped, the follow up and when will it be delivered got I don't know, twice. ShopRung only offers the tool when the latest message mentions an order number, and the follow up does not. Multi turn tests catch exactly this kind of lost context.

Customer-facing problems
not logged in:
  "...you can call the get_order
   tool with the order number."
  → internal name shown to a
    customer
multi-turn:
  "Order 2 is SHIPPED."
  "And when will it be delivered?"
  → no tool offered: the router
    looked only at the last
    message; answer "I don't know"

Permissions

Permissions. When Bob asked for Alice's order, the tool code checked ownership and returned not found, three times out of three. With ShopRung's chat order leak bug, the check is gone, and Bob got Alice's order and its keyboard five times out of five. The model never refused on its own. Access control must live in the tool code, and every agent test suite needs a second user.

Bob asks for Alice's order 2
normal:       tool → not found
              refused 3/3
chat-order-leak bug:
              tool → Alice's
              order, 5/5:
  "Order 2 is currently SHIPPED
   ... the Quiet Mechanical
   Keyboard."
rule: never trust the model to
  enforce permissions

What to test

A checklist. The right tool is chosen, and no tool when none is needed. Arguments match the schema. Tool errors are handled gracefully. Permissions hold for a second user. The answer agrees with the tool output. Follow ups keep their context across turns. Step limits prevent loops and keep latency in bounds. And no internal names or raw data reach the customer.

Checklist
  • right tool for the task, no tool when none is needed
  • arguments valid (schema)
  • tool errors handled gracefully
  • permissions: a second user
  • answer agrees with tool output
  • multi-turn: follow-ups keep context
  • step limit, no loops, latency
  • no internal names or data shown

Common mistakes

Mistakes. Checking only the final text. Writing permissions in the prompt, such as only show the user's own orders, instead of enforcing them in code. Testing single turns only. Not validating tool arguments. One run per scenario, when malformed calls happen one time in three. And no step limit, so a confused agent can loop and burn tokens.

Avoid these
  • checking only the final text
  • permissions in the prompt ("only show the user's orders") instead of code
  • single-turn tests only
  • no argument validation
  • one run per scenario
  • no step limit

Say it like this

In the interview: I assert on the trace: which tool was called, with which arguments, what it returned, and whether the answer agrees with that result. Permissions are enforced in the tool code and tested with a second user. I add multi turn scenarios, repeat each scenario to measure how often calls are malformed, and check step limits and that internal names and data never reach the user.

In the interview
  • "I assert on the trace: which tool, which arguments, what it
  • returned, and that the answer
  • agrees with it. Permissions are enforced in tool code and tested
  • with a second user. I add multi- turn scenarios, repeat runs to measure malformed-call rates, and check step limits and that internals never leak."

Interview questions

Q1What is an AI agent?

A model that can take actions: it decides to call tools, reads their results and may take several steps before answering.

Q2What is a trace and why test it?

The record of tool calls, arguments and results; asserting on it checks what the agent did, which is exact even when wording varies.

Q3Where must permissions be enforced?

In the tool code. With ShopRung's order-leak bug, the model returned another customer's order 5 of 5 times; it never refused on its own.

Q4What is a malformed tool call?

A call with arguments that do not match the tool's schema, like {"[order_id]": "2"}; validate arguments and measure how often it happens.

Q5Why test multi-turn conversations?

Follow-ups depend on context; ShopRung's "And when will it be delivered?" lost the order because routing looked only at the last message.

Q6How do you check the answer uses the tool result correctly?

Compare facts in the answer with the trace (status, totals) or give a judge the tool result; one answer claimed DELIVERED for a SHIPPED order.

Q7Why a step limit?

To stop loops of repeated tool calls that waste tokens and time; test that the agent stops and answers within it.

Q8What should never reach the customer?

Internal tool names, raw JSON, other users' data and system instructions.