Sub topic 2
What Agent Script is
This chapter assumes you know what an LLM-based agent is, and that you are comfortable with prompts, actions (also called tools), and conversation turns. You should also be able to read indentation-based syntax such as YAML or Python.
The problem it addresses
The previous chapter covered why a purely prompt-driven agent is non-deterministic. The model reads instructions as text and decides what to do. Two runs of the same conversation can take different paths. For a rule like "never issue a refund before verifying the customer", that variation is a defect.
Agent Script is the scripting language used to define Salesforce Agentforce agents. It lets you write some behaviour as fixed logic and leave the rest to the model. The fixed logic runs the same way every time. The model handles language: understanding the request, choosing among permitted options, and wording replies.
The working model
An Agent Script file is a description of an agent, not a program that runs from top to bottom. The platform reads it each turn and does two kinds of work.
- Deterministic work. It evaluates conditions, reads and writes variables, runs actions you told it to run, and moves between topics when you tell it to.
- Probabilistic work. It sends natural-language text to the model, which replies to the customer or picks from the actions and transitions you made available.
The key idea is that your script decides what the model sees. An if statement chooses which prompt lines are sent. The model never gets a chance to skip a step that the script never offered.
A first look
The snippet below is an illustrative sketch, not a complete agent. Check exact keys against the current Agent Script documentation.
# Illustrative sketch: fixed logic decides which prompt text the model sees.
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
if @variables.order_id == "":
| Ask the customer for their order number before doing anything else.
else:
| Help the customer with order {[email protected]_id}.Read it as two layers:
- The
if/elseis ordinary logic. It checks a variable, and the result never depends on the model remembering to ask. - The lines starting with
|are prompt text. Only the branch that matches is sent to the model.
If order_id is empty, the model is told to ask for the order number. Once the variable holds a value, the model is told to help with that order. The order-number check is a rule of the script.
What you control and what you leave open
Agent Script does not remove the model. It gives you a choice per decision.
| Decision | Good fit for fixed logic | Good fit for the model |
|---|---|---|
| Is a required value present? | Yes | No |
| Which step must come next? | Yes | No |
| What does the customer mean? | No | Yes |
| How should the reply be worded? | No | Yes |
| Which of several allowed topics fits? | Sometimes, via conditions | Often, via a transition the model may choose |
Common mistakes at this stage
- Treating the script as a longer prompt. If you write a rule only as prompt text, such as "always verify the customer first", the model can still ignore it. Put the rule in a condition.
- Expecting top-to-bottom execution. The file declares topics and their parts. The platform decides when each part is used.
- Copying syntax from sketches as-is. The examples in this tutorial show the shape of the language. Confirm the exact keys and spelling against the current documentation before you build.
Exercise
A support agent must collect an order number before discussing an order. Name the part of the script that should enforce this, and say why a line in the prompt text, such as "always ask for the order number first", is weaker.
Show answer
Enforce it with a condition in the topic's instructions that checks whether the order number variable is empty and, if so, sends only the "ask for the order number" text to the model. A prompt line is only a request to the model, which can skip it or forget it. The condition is evaluated by the platform on every turn, so the behaviour is the same each time.
Exercise
For each of these, decide whether fixed logic or model judgement is the better fit: (a) checking that a variable has a value, (b) understanding that "where's my stuff?" is an order question, (c) writing a friendly closing sentence.
Show answer
(a) Fixed logic, because it is a yes/no check that must always happen. (b) Model judgement, because it requires interpreting free-form language. (c) Model judgement, because wording is a language task with no single correct output.