Sub topic 9
How the Agent Chooses an Action
This chapter assumes you know basic Salesforce concepts (objects, records, Setup), have seen Flow or Apex at a high level, and know roughly what a large language model does. It also assumes you have read the chapters on topics, instructions, and actions. Here you will see how those pieces combine when the agent decides which action to run.
Where action selection sits in the loop
An Agentforce agent does not run a fixed script. For each customer message, the reasoning engine works through a cycle. It first classifies the request into a topic. It then loads only that topic's instructions and actions. Finally, it asks the large language model (LLM) what to do next, given those instructions, the conversation so far, and the list of actions.
Action selection is that last step. The model can pick one of three outcomes:
- Call an action from the current topic.
- Ask the user for missing information.
- Write a final answer with no action.
After an action runs, the result goes back to the model. The model then decides again, so one message can lead to several actions in a row.
What the model sees
The model does not read your Flow or Apex code. It reads text that describes the options. For each candidate action, that text includes:
- The action's name or label.
- The action's description.
- The names, labels, and descriptions of its inputs.
- The topic's classification description, scope, and instructions.
- The conversation history and any variables that hold values.
This is why the Apex example in the topic spine puts so much text into annotations. The label and description on @InvocableMethod, and the description on each @InvocableVariable, are what the model uses to judge whether Get Order Status fits a message like "Where is my order 10042?"
The model compares meaning, not exact words. A customer does not need to say "order status" for the action to match. The match still depends on how well the descriptions state what the action does.
Narrowing the choices
Topic classification does the first round of filtering. Once the agent is in the Order Support topic, only that topic's actions are candidates. Actions from other topics are not offered to the model.
This has two practical effects:
- A well-scoped topic with two or three actions makes the choice easy. The model has few options and each one is distinct.
- A topic with many overlapping actions makes the choice harder. The model may pick the wrong one, or alternate between similar ones across runs.
Putting an action in the wrong topic can make it effectively unreachable. If the request is classified into Order Support, an action that lives only in a Returns topic will never be offered, however good its description is.
How instructions shape the choice
Instructions are the second filter. They tell the model when and how to use actions. Compare these two lines from the Order Support topic:
- "Use the order status action to look up the order before answering."
- "Answer order questions helpfully."
The first names an action and sets an order of operations. The second leaves the model to guess. Good instructions usually do one of these things:
- Require an action before an answer, so the model does not reply from memory.
- Set a precondition, such as asking for the order number first.
- Say what to do on failure, such as "If the order cannot be found, say so and do not guess."
- Mark a boundary, such as "Does not process returns, refunds, or changes to an order."
Instructions are guidance given to an LLM. They improve the odds of the right behavior, but they do not guarantee it. If an outcome must always hold, such as never issuing a refund over a limit, enforce it in the action itself (a Flow decision element or Apex validation) and not only in the instructions.
Worked example: a single request
Take the Order Support topic and the Apex action from the spine. A customer writes:
> Hi, where is my order?
Because this tutorial format does not allow block quotes, here is the message as plain text: "Hi, where is my order?"
The steps run like this:
- The agent classifies the message into Order Support, because the topic's classification description mentions "where it is or when it will arrive."
- The model sees the instruction "Ask for the order number if the customer has not given one." The
Order Numberinput is markedrequired=true, and no value is in the conversation. - The model chooses the second outcome and asks: "Sure, what is your order number?"
- The customer replies "10042."
- The model now has a value for the required input. It chooses
Get Order Statusand fillsorderNumberwith10042. - The action runs and returns
statusset to a value such asShipped, orNot Found. - The result goes back to the model. If the status is
Not Found, the instruction says to report that and not guess. Otherwise, it writes the final answer.
Notice that the model did not call the action at step 2. A required input with no value is a reason to ask first. Without the instruction and the required=true flag, the model might have called the action with an empty or invented value.
Inputs: where the values come from
The model fills an action's inputs from three places:
- The conversation. The customer says the order number and the model copies it.
- Variables. Values stored earlier, such as the verified customer's ID, can be passed along without asking again.
- Earlier action results. One action's output can become the next action's input.
Descriptions on inputs matter here. description='The order number the customer provided' tells the model to take the value from the customer's words. If a description were vague, such as "Order info", the model might pass a whole sentence or the wrong identifier.
Common failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
| Wrong action chosen | Two descriptions overlap | Rewrite each description to state what makes it different |
| Action never chosen | Description is generic, or the action is in another topic | Describe the specific task; check topic placement |
| Action called with a guessed value | Input description is vague, or no instruction says to ask | Clarify the input description; add an "ask first" instruction |
| Answer given without an action | No instruction requires a lookup | Add "Use the action before answering" |
| Different action on repeated runs | Several actions fit equally well | Narrow the topic or merge the actions |
| Action chosen for an out-of-scope request | Scope is missing or too broad | State what the topic does not do |
Descriptions that separate similar actions
Suppose a topic has two actions, Get Order Status and Get Order History. Compare weak and strong descriptions.
Weak:
- Get Order Status: "Gets order information."
- Get Order History: "Gets order information."
Strong:
- Get Order Status: "Looks up the current status of one order by its order number."
- Get Order History: "Lists all past orders for a customer, newest first. Use when the customer asks about earlier purchases and gives no order number."
The strong versions say what the input is, what comes back, and when to prefer each one. The model can now route "Did my last order ship?" and "What have I bought this year?" to different actions.
Checking the choice
Because the choice depends on text, you can test it. In the Agentforce Builder's test and debug tools, send a sample message and review the trace of the run. The trace shows the topic chosen, the actions offered, the inputs passed, and the results. When the agent picks wrongly, look at the trace first, then edit the description or instruction that misled the model, then run the same message again. Change one thing at a time so you know which edit made the difference.
Exercises
Exercise
A topic has an action with the description "Gets customer data." The agent often calls it for unrelated questions and sometimes passes a full sentence as the customer ID input. Name two changes that would improve the choice.
Show answer
First, rewrite the action description to say exactly what it returns and when to use it, for example "Returns the name and account tier of one customer by customer ID. Use only when the customer asks about their own account." Second, rewrite the input description so it asks for a specific value, for example "The customer ID, a number with no spaces." Adding an instruction such as "Ask for the customer ID if it has not been given" also helps.
Exercise
In the Order Support worked example, why does the agent ask for the order number instead of calling Get Order Status right away?
Show answer
The Order Number input is required and no value exists in the conversation. The topic instruction also says to ask for the order number if the customer has not given one. Together these make asking the better next step than calling the action with an empty or invented value.
Exercise
A returns action is defined in a Returns topic. A customer in the Order Support topic asks to return an item, and the agent never offers the returns action. Why?
Show answer
The agent only offers the model the actions of the current topic. Because the request was classified into Order Support, the returns action is not a candidate. The fix is to check topic classification so return requests reach the Returns topic, and to state in the Order Support scope that returns are handled elsewhere.