Sub topic 8
The reasoning loop from request to response
This chapter assumes you know basic Salesforce concepts (objects, records, Setup), have seen Flow or Apex at a high level, and have a general idea of what a large language model (LLM) does. It also assumes you have read the earlier chapters on topics, topic classification, instructions, and actions. Here those pieces are put together into one running process.
What the reasoning loop is
The reasoning loop is the cycle the agent's reasoning engine runs each time a user sends a message. The engine does not answer in one step. It repeats a short cycle of deciding, acting, and checking until it has enough to reply.
In plain terms, one turn looks like this:
- Read the user's message and the conversation so far.
- Decide which topic fits the request.
- Look at the instructions and actions in that topic.
- Decide whether to call an action, ask the user a question, or answer.
- If an action ran, read its result and go back to step 4.
- Produce the final response.
The key idea is that the LLM does the deciding at each step, but it only chooses from what you configured. It can pick a topic you defined and an action you attached to that topic. It cannot call something that is not there.
Step by step through one turn
1. Receive the request and context
The engine receives the new message along with the conversation history and any variables already stored for this session. Context matters because a message like "What about the other one?" only makes sense next to earlier turns.
2. Classify the topic
The engine compares the request against each topic's classification description and scope. It selects the best match. If the conversation is already inside a topic and the new message still fits, the agent can stay in it. If the user changes subject, classification can send the agent to a different topic.
If no topic fits, the agent has nowhere to take an action. Many agents include a fallback topic for off-topic or unsupported requests so that this case ends in a clear reply instead of a guess.
3. Load the topic's instructions and actions
Once a topic is chosen, the engine works with that topic's instructions and its list of actions. This narrowing is the main way the loop stays controlled. The LLM does not weigh every action in the org. It weighs only the actions in the active topic.
4. Choose the next step
The engine reasons over three things: the user's request, the instructions, and the descriptions of the available actions. It then picks one of these outcomes:
- Call an action.
- Ask the user for a missing input.
- Respond directly, because no action is needed or the work is done.
5. Run the action and read the result
When it calls an action, the platform runs the underlying Flow, Apex, prompt template, or other implementation. The output is returned to the engine as new information. The engine then returns to step 4 with that result in hand.
6. Write the response
When the engine decides it has what it needs, it composes the reply for the user. The reply is built from the conversation, the instructions, and any action output. This is why grounding the answer in action results matters: a reply based on a real lookup is more reliable than one based on the model's general knowledge.
A worked example
Use the Order Support topic from the Topic Spine. It handles order status questions, needs an order number, uses the Get Order Status action, and must not guess when an order is missing.
Suppose the user writes: "Where is my order?"
| Loop pass | What the engine decides | Why |
|---|---|---|
| 1 | Classify the request as Order Support | The classification description covers "where it is or when it will arrive" |
| 2 | Ask the user for the order number | The instruction says to ask if the customer has not given one, and the action requires it |
| 3 | After the user replies "10042", call Get Order Status | The input is now available, and the instruction says to look up the order before answering |
| 4 | Read the result and write the reply | The action returned a status, so no further action is needed |
The turn with the question ends the first run of the loop. The user's reply starts a new run, which carries the earlier context with it. So one conversation usually contains many loop runs, and each run may contain several internal steps.
What changes if the order is not found
The Apex action in the Topic Spine returns the status Not Found when the number is not in the org. The loop reads that result like any other. Because the topic instruction says to report a missing order and not guess, the reply should say the order could not be found. The instruction and the action's documented return value work together here. If the Response variable description did not mention Not Found, the engine would have less to go on when it interprets the result.
Here is the action again, with attention on the parts the loop reads:
public with sharing class OrderStatusAction {
public class Request {
@InvocableVariable(label='Order Number' description='The order number the customer provided' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Status' description='Current status of the order, or Not Found')
public String status;
}
@InvocableMethod(label='Get Order Status' description='Looks up the current status of an order by its 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) : 'Not Found';
results.add(res);
}
return results;
}
}The method label and description are what the engine reads when deciding to call the action. The required=true flag on the order number tells the engine it must have that value first, which is what triggers the question in pass 2. The description on status tells the engine how to interpret the output.
Multiple actions in one turn
The loop can call more than one action before replying. A request such as "Where is order 10042, and can you also tell me the shipping address?" might lead the engine to call one action, read the result, and then call another. Each result is added to what the engine knows before it picks the next step.
Two practical points follow from this:
- Each pass costs time. An agent with long chains of actions responds more slowly than one that answers in a single call.
- Later steps can depend on earlier outputs. If an action's output is vague, the next decision is made on weak information.
Where the loop is bounded
The loop is flexible, but several things limit it.
- Topic scope. Only the active topic's actions are candidates. An action in another topic is not available until classification moves the agent there.
- Instructions. They can require a step order, such as "look up the order before answering", or forbid behavior, such as guessing.
- Required inputs. An action with a required input that is not yet known cannot run. The engine must obtain it first.
- Platform controls. The Einstein Trust Layer and permissions on the underlying Flow or Apex still apply. Those are covered in a later chapter.
Common errors when reading the loop
These are the mistakes people make most often when an agent misbehaves.
- Blaming the wrong stage. If the agent called the wrong action, first check whether it was in the right topic. A wrong topic makes every later step wrong, even if the action descriptions are good.
- Expecting a fixed order. The loop is not a script. Without an instruction that sets the order, the engine may call actions in a different sequence on different runs.
- Vague action output. An action that returns an empty value or an unclear string leaves the engine guessing what happened.
- Missing fallback. With no topic for out-of-scope requests, users get odd or evasive replies.
- Putting every rule in instructions. Long instruction lists dilute each other. Keep each topic's instructions short and specific.
Reading the loop when you debug
Every pass leaves evidence you can inspect: the topic chosen, the instructions applied, the action selected, the inputs sent, and the output received. A later chapter covers the testing and tracing tools in detail. For now, keep this order of questions in mind:
- Was the right topic selected?
- Were the right instructions and actions available in that topic?
- Did the engine choose the right action, and with what inputs?
- Did the action return something the engine could interpret?
- Did the final reply reflect the action's output?
Working in this order finds the failing stage faster than rewriting descriptions at random.
Exercise
A user asks "Where is my order?" and gives no order number. Using the Order Support topic, describe what the reasoning loop does in this turn and why it does not call Get Order Status yet.
Show answer
The engine classifies the request as Order Support, then loads that topic's instructions and actions. The instruction says to ask for the order number if the customer has not given one, and the action's Order Number input is marked required. Because the required input is missing, the engine does not call Get Order Status. It replies with a question asking for the number, which ends this run of the loop. The user's answer starts a new run.
Exercise
An agent in the Order Support topic keeps answering "your order is on its way" for order numbers that do not exist. Name two places in the loop you would inspect, and one change you might make.
Show answer
Inspect the action output in the trace to see whether Get Order Status returned Not Found, and inspect the topic instructions to see whether they tell the agent not to guess. If the action returned Not Found but the reply ignored it, add or strengthen the instruction "If the order cannot be found, say so and do not guess", and make sure the Status variable description mentions Not Found. If the action returned a real status for a missing order, fix the Apex logic.