Sub topic 14
Testing and Tracing Why an Agent Chose an Action
This chapter assumes you know what topics, instructions, and actions are, and that you have read how the reasoning loop picks a topic and then an action. Here you will test an agent and read its trace to see why it made each choice.
Why tracing matters
An agent's choice depends on text: topic descriptions, instructions, action labels, and input descriptions. When the agent picks the wrong action, you can't fix it by reading code. You need to see what the reasoning engine saw and what it decided. Testing shows what happened. Tracing shows why.
Test in the builder first
Agentforce Builder includes a conversation preview panel. Use it to chat with the draft agent before you activate it. Each time you send a message, the preview can show the reasoning steps behind the reply.
Start with the Order Support topic from earlier chapters. Send these test messages:
- "Where is order 00001234?" This should select the Order Support topic and the Get Order Status action.
- "Where is my order?" The agent should ask for the order number first, because the instructions say so.
- "I want a refund for order 00001234." The scope excludes refunds, so the agent should not call Get Order Status as if it were a refund.
Run each message in a fresh preview session. Earlier turns stay in the conversation context and can change the result.
What the trace shows
A trace follows the reasoning loop in order. Read it from top to bottom and check each step.
| Trace step | Question to ask |
|---|---|
| Topic selected | Did the request match the topic you expected? |
| Instructions applied | Were the topic instructions actually in the prompt? |
| Action chosen | Is this the action you intended, or a similar one? |
| Inputs passed | Does each value match the user's words? |
| Action output | Did the action return what the agent then said? |
| Final response | Does the reply reflect the output, with no invented details? |
Always work in this order. A wrong topic makes every later step wrong, so fixing action descriptions first wastes time.
Common failures and fixes
- Wrong topic chosen. Two topic descriptions overlap. Rewrite them so each names what it handles and what it does not.
- Right topic, wrong action. Two actions have similar labels or descriptions. Make each description say when to use that action.
- Right action, wrong input. The input description is vague. In the Apex example,
description='The order number the customer provided'tells the agent to pass the number the customer gave, not a record ID. - Action never runs. The topic instructions or scope may forbid it, or the action is not added to the topic.
- Agent guesses instead of saying "not found". Check that the instructions say what to do when the action returns
Not Found.
Test the action separately
If the trace shows the right action and right inputs but a bad result, the problem is in the action. Test the Apex method or flow on its own. For the Apex example, run it in anonymous Apex:
OrderStatusAction.Request req = new OrderStatusAction.Request();
req.orderNumber = '00001234';
List<OrderStatusAction.Response> out =
OrderStatusAction.getStatus(new List<OrderStatusAction.Request>{ req });
System.debug(out[0].status);If this prints Not Found for an order you know exists, the agent is behaving correctly. Check the order number and the sharing rules, since the class uses with sharing.
Change one thing at a time
Edit one description or instruction, then rerun the same test messages. If you change several things, you cannot tell which one fixed the problem. Keep a short list of test messages, including ones that should be refused, and rerun it after every change.
Exercise
A user asks "Where is order 00001234?" The trace shows the correct topic and the correct action, but the action received the input "my order". Which part of the configuration should you change first, and why?
Show answer
Change the input description on the action's request variable (for example the Order Number variable). The topic and action selection were correct, so the fault is in how the agent filled the input. A clearer description, such as "The order number the customer provided, for example 00001234", tells the agent which value to extract.