Sub topic 5
How the Reasoning Engine Picks Topics and Actions
What this chapter assumes
You should already know what a topic, an instruction, and an action are in Agentforce. You should also have a general idea of what a large language model (LLM) does: it predicts text from the text it is given. This chapter explains how those pieces fit together at run time. It covers how the reasoning engine turns a customer or employee message into a topic choice, an action call, and a reply.
The same engine runs both service agents and employee agents. What you learn here applies to both.
The agent loop
The reasoning engine is the component that decides what the agent does next. It does not follow a fixed script. For each message it runs a loop:
- Read the user's message and the conversation so far.
- Choose the topic that best matches the request.
- Read that topic's instructions and see which actions it offers.
- Decide whether to call an action, ask the user for missing information, or answer.
- If an action ran, read its result and go back to step 4.
- When nothing more is needed, write the reply.
Steps 4 and 5 can repeat. An agent may call one action, read the result, and call a second action before it replies. A topic's instructions guide this loop, but the engine still decides at each step based on the conversation.
Step 1: choosing a topic
The engine compares the user's request with the topics defined for the agent. A topic has a name, a classification description, a scope, and instructions. Selection relies mostly on the first three, because they are what the engine reads before it commits to a topic.
Here is the Order Status topic from the earlier chapters:
Topic: Order Status
Classification description: Customer asks where an order is or what state it is in.
Scope: Look up the status of a single order using the Get Order Status action.
Instructions:
- Ask for the order number if the customer has not given one.
- Call the Get Order Status action with that order number.
- Report only the status returned by the action.
- If the action returns 'Order not found', ask the customer to check the number once, then offer to connect them with a human agent.
- Do not discuss refunds or cancellations in this topic.The classification description is the main routing signal. "Customer asks where an order is or what state it is in" describes the kind of message that belongs here. The scope states what the topic does and, by implication, what it does not do.
Good classification descriptions:
- Describe the user's intent, not the internal process.
- Stay distinct from the descriptions of neighboring topics.
- Are short enough to read at a glance.
Topic selection is not a keyword match. A message like "my package never showed up" can reach Order Status even though the description does not contain the word "package". The flip side is that two topics with overlapping descriptions can both look plausible, and the engine may pick the wrong one.
Step 2: choosing an action
Once a topic is selected, the candidate actions are the ones assigned to that topic. Actions assigned to other topics are not on the list. This is why a refund action cannot fire from the Order Status topic unless you add it there.
For each candidate action, the engine reads:
- The action's name and description.
- Each input's name, description, and whether it is required.
- The topic instructions that mention the action.
The Apex example from the Actions chapter shows where this information comes from:
public with sharing class GetOrderStatusAction {
public class Request {
@InvocableVariable(label='Order Number' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Order Status')
public String status;
}
@InvocableMethod(
label='Get Order Status'
description='Returns the status of an order for a given order number'
)
public static List<Response> getStatus(List<Request> requests) {
Set<String> orderNumbers = new Set<String>();
for (Request r : requests) {
orderNumbers.add(r.orderNumber);
}
Map<String, String> statusByNumber = new Map<String, String>();
for (Order o : [SELECT OrderNumber, Status FROM Order WHERE OrderNumber IN :orderNumbers]) {
statusByNumber.put(o.OrderNumber, o.Status);
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.status = statusByNumber.containsKey(r.orderNumber)
? statusByNumber.get(r.orderNumber)
: 'Order not found';
results.add(res);
}
return results;
}
}The label and description on the method, and the label on each variable, are what the engine reads when it decides whether this action fits. The required=true flag matters too. The engine cannot call the action without an order number, so it asks the customer for one, which matches the first instruction in the topic.
The engine fills the inputs from the conversation. If the customer wrote "Where is order 00012345?", the engine can supply orderNumber itself. If the customer wrote "Where is my order?", the engine must ask a question first.
Step 3: reading the result
After an action runs, its output goes back into the loop as new information. The engine then decides again. In the Order Status example, the possible outcomes are:
- The result is
Shipped. The engine replies with that status, as the instructions say to report only the status returned. - The result is
Order not found. The instructions tell the engine to ask the customer to check the number once, then offer a human agent. - The action fails with an error. The engine has no status to report, so what it says depends on your instructions. If they do not cover failures, the reply is less predictable.
The engine can only use what the action returns. If the Apex returns just a status string, the agent cannot tell the customer the delivery date, however good the instructions are.
A worked trace
Suppose a service agent has three topics: Order Status, Returns, and General FAQ.
| Turn | User or system | What the engine does |
|---|---|---|
| 1 | "Hi, where is my stuff?" | Matches Order Status. Needs `orderNumber`, which is required, so it asks for it. |
| 2 | "It's 00012345" | Stays in Order Status. Fills `orderNumber` and calls Get Order Status. |
| 3 | Action returns `Shipped` | Reads the result. No further action needed. |
| 4 | Agent reply | Reports that the order has shipped. |
| 5 | "Can I send it back?" | Request now fits Returns. Switches topic and loads that topic's instructions and actions. |
Turn 5 shows that topic selection is not locked for the whole conversation. The engine evaluates each new message, so a change of subject can change the topic.
Same engine, two agent types
Service agents and employee agents both use this loop. A customer asking about an order and an employee asking "show me my open cases" go through the same steps: select topic, pick action, read result, reply. The Open Cases topic below has the same structure as Order Status:
Topic: My Open Cases
Classification description: An employee asks about support cases assigned to them or their team.
Scope: Summarize cases the employee already has permission to view.
Instructions:
- Use the Get Open Cases action to retrieve cases for the current user.
- Present each case with its number, subject, and status.
- If the employee asks about data they cannot access, say that you cannot show it and suggest contacting their administrator.What differs is the identity the action runs under and the data it can reach. The engine decides which action to call. Access control decides what that action may return. Topic instructions can ask the agent to refuse a request, but they do not replace permissions. Protect data with permissions and with sharing, and use instructions for conversation behavior.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Agent answers from general knowledge instead of using a topic | No topic's classification description matches the request | Add or widen a topic description, or add an off-topic response |
| Wrong topic is selected | Two topics have overlapping descriptions | Rewrite descriptions so each covers a distinct intent |
| Action is never called | Action is not assigned to the topic, or its description is vague | Assign the action to the topic and write a specific description |
| Agent invents an input value | Instructions do not say to ask for missing data, or the input is not marked required | Mark the input required and add an instruction to ask for it |
| Agent drifts into another topic's work | Scope is too broad or missing limits | Narrow the scope and add "Do not discuss..." instructions |
| Reply ignores part of the result | Action output does not contain that field | Return the field from the Flow or Apex |
Writing for the engine
A few habits make selection more reliable:
- Give each topic one clear job, and name the action it uses in the instructions.
- State what to do when an input is missing and when an action returns nothing useful.
- Put limits in the topic that needs them, such as "Do not discuss refunds or cancellations in this topic", and route that request to the topic that handles it.
- Keep action descriptions in plain language that says what the action returns, not how it is implemented.
Exercise
A customer writes "Where is my order?" to an agent whose Order Status topic has a required orderNumber input on its action. The customer gave no number. What does the reasoning engine do, and why?
Show answer
It selects the Order Status topic from the classification description, then finds that the action cannot run without the required orderNumber. It asks the customer for the number, as the topic instructions also say, and calls the action only after the customer replies.
Exercise
An agent keeps answering refund questions inside the Order Status topic. List two changes to the topic configuration that would help.
Show answer
Narrow the scope and add an instruction such as "Do not discuss refunds or cancellations in this topic." Also make sure a separate Returns or Refunds topic has a distinct classification description, so refund requests are matched there instead.
Exercise
Why is "Do not show other users' cases" in a topic's instructions not enough to protect case data in an employee agent?
Show answer
Instructions only guide the language model's behavior and are not an enforcement mechanism. The data an action can return is controlled by the permissions of the user it runs as and by the action's code, such as with sharing in Apex.