Agent Script Hands-On: Build a Deterministic Agentforce Agent with Hybrid Reasoning
Every Agentforce team hits the same wall. The instructions grow to three paragraphs of "ALWAYS" and "NEVER", and the agent still offers a refund it shouldn't. Agent Script fixes this by moving business rules out of the prompt and into code that runs before the LLM sees anything.
In this hands-on you'll build an order-exception agent end to end: a router, identity verification, a Flow action, an Apex action, a credit rule the LLM can't break, and a turn limit. Then you'll step through the Prompt Resolver to watch exactly what the LLM receives, and ship the agent with Agentforce DX.
->: if, run, set and transition always behave the same way.| are natural-language prompt text the model reasons over.available when conditions you control.1. The mental model in four rules
- Every message starts at
start_agent. It's the router that runs on each customer message and picks a subagent. (Since April 2026, "topics" are called subagents; the behaviour is the same.) - Logic first, then the prompt. Agentforce resolves a subagent's instructions top to bottom, running actions, setting variables and evaluating
ifbranches. Only the resolved text goes to the LLM. - Transitions are one-way and wipe the slate. If a transition runs while the prompt is being built, the half-built prompt is discarded and the target subagent's instructions are used instead. Control doesn't come back.
- State lives in variables, not in the model's memory. A
mutablevariable can be changed by the agent; one declared withoutmutablekeeps its value.
That's the whole flow of control. Everything below is those four rules applied to a real use case.
2. The use case: an order-exception agent
Acme Outdoor wants an agent that handles "where's my order?" conversations, with three rules the business won't negotiate on:
- No order details before verification. The email must match the order.
- One goodwill credit, only on late orders. Never on time, never twice.
- Hand off after five turns. Long conversations go to a specialist.
In a prompt-only agent, all three would be sentences the LLM might ignore. Here, each one becomes code.
The Complete Agent
Switch tabs for the Agent Script file, the Apex action it calls, and the Agentforce DX commands that ship it.
3. How each business rule became code
Rule 1: verification as a gate, not a sentence
The router exposes two transition tools, and each has an available when condition on @variables.customer_verified. Until the Flow confirms the email matches the order, the LLM literally can't see the tool that leads to order details. In verify_customer, @utils.setVariables with ... lets the LLM fill in the email and order number from the conversation, and after_reasoning moves the customer on as soon as verification passes.
Rule 2: the credit decision lives in Apex
check_if_late runs deterministically on every turn (run @actions... inside the instructions), and the Apex returns both is_late and credit_eligible. The give_credit tool is only available when credit_eligible is True. The LLM still decides when to offer it in conversation, but it can't offer a credit the rules don't allow.
Rule 3: the turn limit in after_reasoning
A counter increments at the top of the instructions, and after_reasoning transitions to human_handoff once it passes five. This mirrors the example in Salesforce's flow-of-control guide. In production, swap the hand-off subagent for @utils.escalate, which routes to a human through an active Omni-Channel connection defined in a connection messaging block.
@InvocableMethod label and description aren't decoration: Salesforce's guidance is that the reasoning engine uses them to decide when to invoke an action. Keep the method bulk-safe (lists in, lists out), and if you ship it in a managed package, it must be global.
The Prompt Resolver
Set the scenario, then press Play or Step through order_exception. Watch which lines run, which are skipped, and what finally reaches the LLM.
What just happened
Variables
Resolved prompt for the LLM
Tools the LLM can call
4. Ship it with Agentforce DX
In a Salesforce DX project, an agent's .agent file lives inside an authoring bundle (the AiAuthoringBundle metadata type), so you can version it in Git like any other source. The loop is: generate the bundle, edit the script, validate, preview, publish. The CLI tab above has the commands, and VS Code has matching AFDX commands such as Validate This Agent.
- Validate after every edit. The language is whitespace-sensitive, like Python or YAML, so a wrong indent is a compile error.
- Preview with simulated actions first, then with live actions once your Flows and Apex are deployed.
- Publishing creates a new agent version, and you can only publish draft (unversioned) bundles.
- Test before you activate. Use the Testing Center or
sf agent testcommands to cover each branch: unverified customer, late with credit, late without credit, on time, and the sixth turn.
else if, expression functions, system variables and a top-level escalate statement to Agent Script. The build above sticks to constructs documented in the core reference, so it works on either side of the release; use the newer syntax once your org is on Winter '27.
5. Common mistakes
- Putting rules in
|lines. If it must always happen, write it as anif, arunor anavailable when, not as a sentence. - Expecting control to come back after a transition.
@utils.transition tois one-way. To call another subagent and return, reference it directly as a tool (@subagent.name). - Using
idfor record IDs. It's deprecated; usestring. - Lower-case booleans. Defaults must be
TrueorFalse. - Vague action descriptions. The LLM reads them to decide when to call a tool.
Predict the Outcome
Six Agent Script scenarios. Each answer names the rule.
Cheat sheet: Agent Script at a glance
| You want to... | Write |
|---|---|
| Give the LLM an instruction | | Apologize for the delay. |
| Always run an action | run @actions.x with a=@variables.b set @variables.c = @outputs.c |
| Branch the prompt | if @variables.is_late == True: ... else: ... |
| Let the LLM choose a tool | reasoning: actions: my_tool: @actions.x |
| Fence a tool | available when @variables.ok == True |
| Move on, one-way | @utils.transition to @subagent.y |
| Let the LLM fill a variable | @utils.setVariables with first_name = ... |
Syntax checked against Salesforce's Agent Script reference in November 2026. Always run Validate This Agent in your own org before publishing.
Official Resources
Straight from Salesforce: the docs this article is based on.
Trailhead
Developer Docs
Moving prompt-heavy agents to Agent Script?
We'll review your subagents and turn fragile instructions into rules that hold.
All information in this article was collected from publicly available resources, including official Salesforce documentation.