Agent Script Flow of Control
Understanding the order of execution and flow of control helps you to design better agents. Agentforce has these main execution paths:
- First request to an agent
- Processing a topic
- Transitioning between topics
All requests, including the first request, begin at the start_agent topic. You typically use the start_agent topic to set the initial value of variables, and to perform topic classification. Topic classification tells the LLM which topic to choose based on the current context.
See Start Agent Block.
Agentforce uses a topic's text instructions, variables, if/else conditions, and other programmatic instructions to create an LLM prompt. The reasoning instructions are processed sequentially, in top-to-bottom order. While the reasoning instructions can contain programmatic logic and text instructions, the LLM only starts reasoning after it has received the resolved prompt, not while Agentforce is still parsing.
If reasoning instructions contain a transition command, Agentforce immediately transitions to the specified topic, preserving any existing prompts that it resolved before the transition occurred. Agentforce doesn't parse any instructions in the original topic that occur after the transition.
Agentforce processes a topic to create a prompt, which it then sends to the LLM.
Consider this topic's reasoning instructions.
Suppose that:
- the order ID is
1234 - the current delivery date is
February 10, 2026 - the package is late
Here's the prompt that Agentforce creates after processing the reasoning instructions:
To construct the prompt, Agentforce parses the reasoning instructions line by line, following these steps:
- Initialize the prompt to empty.
- Run the action
get_delivery_date. - Set the variable
updated_delivery_dateto the value ofoutputs.delivery_date, which was returned by the action. - Concatenate this string to the prompt:
Tell the user that the expected delivery date for order number 1234 is February 10, 2026. - Run action
check_if_late. - Set the variable
is_lateto the value ofoutputs.is_late, which was returned by the action. - Check whether the value of if
@variables.is_late=="True". - Concatenate this string to the prompt:
Apologize to the customer for the delay in receiving their order. - Send the prompt to the LLM and return the LLM's response to the customer.
You can transition between topics from a reasoning action, reasoning instructions, or before and after reasoning blocks. A transition is one-way and control doesn't return to the previous topic. Agentforce keeps any prompt instructions that it resolved from the previous topic, up to the transition. Then, Agentforce reads the second topic from top to bottom. The final prompt contains any instructions from the first topic, followed by instructions from the second topic.
After the second topic completes, Agentforce waits for the next customer utterance, at which point it returns to the start_agent topic.
In this example, we've defined a reasoning action called go_to_account_help that transitions to the topic account_help.
See @utils.transition to.