Sub topic 14
Testing and Tracing Why an Agent Chose an Action
This chapter assumes you know what topics, instructions, and actions are, and how the reasoning loop moves from a request to a topic to an action. You should be able to open Setup and an agent in the builder. The tool names here are the ones Salesforce uses at the time of writing. Menu labels shift between releases, so match the ideas rather than the exact wording.
Why agent behavior needs tracing
An agent's answer is the end of a chain: topic classification, instruction reading, action selection, input filling, action execution, and response writing. A wrong answer can come from any link. If you only read the final reply, you are guessing which link failed.
Two kinds of tools help:
- Interactive tracing shows the decisions made for one conversation turn. Use it to diagnose a single bad result.
- Batch testing runs many saved utterances and compares the outcomes to expectations. Use it to confirm a fix and to catch regressions.
Use tracing to find the cause, then batch testing to prove the fix and keep it fixed.
Interactive testing in the builder
The agent builder has a conversation preview panel. You type as a customer would and read the replies. Next to it is the plan tracer, which records what the reasoning engine did for each turn.
For each turn, the trace typically lets you inspect:
- The utterance as received.
- The topic the engine selected.
- The instructions and actions that were available in that topic.
- The action it chose, if any, and the input values it passed.
- The action's output.
- The final response, and the prompts the engine built along the way.
Read the trace in order, from the top. The first step that looks wrong is usually the real cause. Later steps often look wrong only because they inherited a bad earlier choice.
What to look for at each stage
Topic selection
Check whether the selected topic is the one you expected. If it is not, compare the utterance with the classification descriptions and scopes of the topics involved. Typical causes:
- Two topics have overlapping classification descriptions, so a request fits both.
- The correct topic's description is written in internal vocabulary, while customers use different words.
- The request is out of scope for every topic, and the engine picked the closest one anyway.
Action selection
Once the topic is right, check which actions were available and which one ran. If the right action was never called, read its label and description as the model sees them. If the wrong action ran, look for another action whose description also matches the request.
Instructions matter here too. A line such as "Use the order status action before answering" pushes the engine toward the action. A missing line leaves the engine free to answer from the conversation alone.
Inputs
Look at the values passed to the action. Three problems are common:
- The engine invented a value because the customer had not supplied one.
- The engine passed the right value in the wrong field, because input descriptions were vague.
- A required input was missing, so the engine asked a follow-up question instead of acting.
The @InvocableVariable descriptions in the Apex example from the topic plan are what the engine reads when deciding what each input means. A description like "The order number the customer provided" tells it where to look and not to make one up.
Output and response
If the action returned the right data but the reply is wrong, the problem is in how the response was composed. Check whether the instructions say how to present results, for example "If the status is Not Found, say so and do not guess."
A fixed debugging order
Work from the top of the chain downward. Changing several things at once makes it impossible to tell which change helped.
Common symptoms and likely causes
| Symptom | Likely cause | First thing to change |
|---|---|---|
| Request lands in the wrong topic | Overlapping classification descriptions | Narrow each description and state what the topic excludes |
| Agent answers without calling an action | No instruction requires the lookup | Add an instruction naming the action |
| Wrong action called | Two actions have similar descriptions | Rewrite descriptions to say when each one applies |
| Action called with a made-up value | Input is optional or vaguely described | Mark it required and describe where it comes from |
| Agent refuses a valid request | Scope text excludes it | Reword the scope |
| Same utterance gives different results | Wording is ambiguous to the model | Make descriptions more specific, then test with several phrasings |
Test the action separately
If the trace shows the right action and the right inputs but the output is wrong, the agent is not the problem. Test the action on its own, with no model involved. For the Apex action from this tutorial, an ordinary unit test does that.
@IsTest
private class OrderStatusActionTest {
@IsTest
static void returnsStatusForKnownOrder() {
Account acct = new Account(Name = 'Test Account');
insert acct;
Order ord = new Order(
AccountId = acct.Id,
EffectiveDate = Date.today(),
Status = 'Draft'
);
insert ord;
String orderNumber = [SELECT OrderNumber FROM Order WHERE Id = :ord.Id].OrderNumber;
OrderStatusAction.Request req = new OrderStatusAction.Request();
req.orderNumber = orderNumber;
Test.startTest();
List<OrderStatusAction.Response> results =
OrderStatusAction.getStatus(new List<OrderStatusAction.Request>{ req });
Test.stopTest();
System.assertEquals(1, results.size());
System.assertEquals('Draft', results[0].status);
}
@IsTest
static void returnsNotFoundForUnknownOrder() {
OrderStatusAction.Request req = new OrderStatusAction.Request();
req.orderNumber = '99999999';
List<OrderStatusAction.Response> results =
OrderStatusAction.getStatus(new List<OrderStatusAction.Request>{ req });
System.assertEquals('Not Found', results[0].status);
}
}The second test covers the path that the topic instruction relies on: "If the order cannot be found, say so and do not guess." The action must return a clear Not Found value, or the agent has nothing reliable to report. The tests also call the method with more than one request in a list in real use, since invocable methods are bulk-called, so add a test with two requests if your action does anything beyond a simple lookup.
Batch testing with the Testing Center
A single conversation proves little. The Agentforce Testing Center lets you save test cases and run them together. Each test case pairs an utterance with the outcome you expect:
| Field | Meaning |
|---|---|
| Utterance | What the customer says |
| Expected topic | The topic that should be selected |
| Expected actions | The actions that should run |
| Expected response | What the reply should convey |
After a run, you see which cases passed on topic, action, and response. A topic failure and an action failure point at different fixes, which is why the debugging order above starts at the top.
Build the test set deliberately:
- Add typical requests for each topic, phrased in at least three different ways.
- Add boundary requests, such as "I want a refund for my order," which sits next to Order Support but is out of its scope. Expect the agent to decline or hand off, not to call the status action.
- Add requests with missing inputs, such as "Where is my order?" with no number. Expect a follow-up question, not an action call.
- Add requests that should not trigger any action.
Keeping results trustworthy
- Re-run the full set after every change. A fix to one topic description can pull requests away from another topic.
- Treat results as probabilistic. Run ambiguous utterances more than once. A case that passes once and fails once means your descriptions leave room for interpretation.
- Change one thing at a time. Edit one description or one instruction, then re-run.
- Test with realistic data. An action that returns nothing in an empty sandbox looks like an agent failure.
- Check permissions. An action that runs as a user without access to the records returns empty results, and the trace will show a correct call with an unhelpful output.
Exercise
A customer says "Where is order 10045?" The trace shows the Order Support topic was selected, the Get Order Status action was never called, and the agent replied "Your order is on its way." Which stage failed, and what is the most likely fix?
Show answer
Action selection failed, since the topic was correct and no action ran. The agent answered without looking anything up. Add an instruction to the topic such as "Use the Get Order Status action to look up the order before answering," and check that the action's description says it looks up an order's status by order number. Then save the utterance as a test case with Get Order Status as the expected action and re-run the set.
Exercise
Your test set passes for "Where is my order?" but fails for "I need my money back for order 10045," which was meant to be declined. The agent calls Get Order Status and reports the status. Name two changes you could make, and say how you would confirm them.
Show answer
First, make the topic scope explicit that returns and refunds are not handled, so the request is not treated as an order status question. Second, add an instruction for what to do when a customer asks for something outside the scope, such as telling them it cannot be done here. Change one at a time, re-run the failing utterance with the plan tracer to confirm the action is no longer called, then re-run the whole test set to confirm other topics were not affected.