Sub topic 2
What Agent Script is
Prerequisites
This chapter assumes you know what an LLM-based agent is and how prompts, tools (called actions here), and conversation turns fit together. You should be able to read indentation-based configuration such as YAML or Python. You should also know basic programming ideas: variables, if/else conditions, and function calls. No prior Agentforce experience is needed.
The gap Agent Script fills
The previous chapter showed that an agent driven only by a prompt is non-deterministic. You can write "always ask for the order number first", but the model treats that as a strong suggestion. On most runs it complies. On some runs it skips the step, asks in a different order, or calls an action too early. For a chatty assistant this is acceptable. For a refund, an identity check, or a required disclosure, it is not.
Agent Script is Salesforce's answer to that gap. It is the scripting language used to define Agentforce agents. You write a text file that describes the agent. Parts of that file are fixed logic that runs the same way on every conversation. Other parts are natural-language text that the model reads and responds to.
A working definition
Agent Script is a declarative, indentation-based language for describing an agent in one place. A script has four jobs:
- It names the pieces of the agent: topics, variables, and actions.
- It holds state in variables, so the agent can remember facts across turns.
- It uses ordinary conditions and transitions to decide which prompt text the model sees, which actions it can reach, and which topic is active.
- It lets you write prompt text in the places where you want the model's judgement.
The central idea is that the script controls the model's context and options. The model still writes the replies and still interprets what the customer means. The script decides what the model is told on each turn, and what it is allowed to do.
Where it fits
Think of an Agentforce agent as three layers.
| Layer | What it does | Who controls it |
|---|---|---|
| Language model | Understands the customer's message, writes replies, chooses among the options it is given | The model, steered by what it reads |
| Agent Script | Decides which instructions, actions, and topic apply at this moment | You, as fixed logic |
| Backend capabilities | Look up records, update data, run business logic | Your platform and its code |
Agent Script sits in the middle. It does not replace the model, and it does not replace backend logic. It orchestrates between them. An action in a script is a named handle for a backend capability. The script says when that handle is available or required, and the backend does the work.
The loop in the diagram is the key point. State flows from backend results into variables. The script reads those variables on the next turn. Because the script is ordinary logic, the same state always produces the same instructions and the same set of allowed actions.
What the script fixes and what it leaves open
A common mistake is to think "deterministic" means the whole agent becomes predictable. It does not. Be precise about the split.
| Concern | Deterministic (script) | Probabilistic (model) |
|---|---|---|
| Which topic is active | Fixed when you use a transition or a condition | Chosen by the model when you expose a transition as a tool |
| Which instruction text is included | Chosen by if/else on variables | The model interprets the text it receives |
| Whether an action is available | Controlled by script conditions | Whether to call an available action |
| Order of required steps | Enforced by conditions and transitions | Not decided by the model |
| Exact wording of replies | Not fixed | Generated by the model |
The reply wording stays variable. You get a guarantee about structure and sequence, not about phrasing. If you need an exact sentence, such as a legal notice, that is a separate design question for later chapters.
A first look at a script
The following sketch shows the overall shape. It is illustrative. Check exact keys against the current Agent Script documentation before you rely on them.
# Illustrative sketch of the overall shape of an Agent Script file.
# Check exact keys against the current Agent Script documentation.
system:
instructions: "You are a support agent for order questions."
variables:
order_id: mutable string = ""
start_agent topic_selector:
description: "Route the customer to the right topic"
topic order_status:
description: "Answer questions about an existing order"Read it top to bottom.
systemholds agent-wide settings. Here it gives the agent its overall role.variablesdeclares state.order_idstarts as an empty string, andmutablemeans the agent can change it during the conversation.start_agent topic_selectoris the entry point. The first customer request lands here, and this block routes it.topic order_statusis a scoped area of behaviour with its own description.
The next chapter goes through each block in detail. For now, notice that the file is a structured description, not a single long prompt.
Mixing fixed logic and prompt text
The feature that sets Agent Script apart from a plain configuration file is that one block can contain both logic and natural language. Here is a second sketch.
# 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}.The -> after instructions: marks a procedural block, meaning the lines below run as logic in order. Lines starting with | are prompt text that goes to the model. The if and else are not prompts. The platform evaluates them, and only the branch that matches is sent to the model.
This is the important consequence. In a prompt-only design you would write "if you do not have an order number, ask for it", and the model would have to notice that condition itself. Here the check against order_id happens in the script. If the variable is empty, the model only ever sees the "ask for the order number" text. It never needs to remember the rule, because the rule has already been applied.
The {[email protected]_id} expression inserts the variable's current value into the prompt text. State written by the script or by actions can therefore reach the model without relying on the model to carry it forward from earlier turns.
Leaving routing to the model on purpose
Agent Script does not force everything to be rigid. You choose where to be strict. This sketch shows a decision left to the model inside a fixed boundary.
# Illustrative sketch: a transition the model may choose, and a variable the script reads.
start_agent topic_selector:
description: "Route the customer to the right topic"
reasoning:
instructions: ->
| Decide whether the customer is asking about an order.
actions:
go_to_order_status: @utils.transition to @topic.order_statusHere the model decides whether the customer is asking about an order, which is a language-understanding task it handles well. What it cannot do is choose a destination the script did not expose. The only available move is order_status, and the script fixed that. The model's judgement operates inside a menu that you wrote.
Common misunderstandings
- "It is a stricter prompt." It is not. Prompt text in a script is still advisory. The guarantee comes from conditions, variables, and transitions that run outside the model.
- "Everything should be scripted." A fully scripted agent loses the main benefit of using a language model, which is flexible handling of free-form requests. Script the steps that must be reliable. Leave interpretation and wording to the model.
- "Replies will now be identical." Only structure and sequence become repeatable. Wording can still vary between runs.
- "The sketches are the exact syntax." The examples here are simplified to show the model of the language. Key names and syntax details can change between releases, so confirm them against the current documentation.
- "The script performs the business logic." Actions call backend capabilities. Keep the heavy work in those capabilities and use the script to decide when they run.
When Agent Script is the right tool
Use these criteria to decide how much to script.
- Cost of a wrong step. If skipping a step causes a compliance, financial, or trust problem, enforce it in the script.
- Dependence on state. If behaviour depends on facts such as "verified", "order found", or "refund already issued", store that in variables and branch on it.
- Need for repeatable testing. Fixed logic can be tested with representative conversations and gives stable results. Pure prompts give results that you can only sample.
- Benefit of flexibility. If a task is open-ended, such as summarising a customer's mood or phrasing an explanation, keep it in natural language.
Summary
Agent Script describes an agent as named blocks (system settings, variables, topics, and actions). It lets you mix fixed logic with natural-language prompt text. The script controls the context and the options the model gets on each turn. That makes sequences and guardrails repeatable, while the model still interprets requests and writes replies.
Exercise
A prompt says "Never process a refund until the customer's identity is verified." Testing shows the agent skips verification in about 1 in 20 conversations. Explain why adding stronger wording to the prompt will not fully fix this, and describe in one or two sentences what Agent Script does differently.
Show answer
The model treats the prompt text as guidance, so the rule is followed only when the model chooses to follow it, and stronger wording only lowers the failure rate. In Agent Script you store verification in a variable and use a condition on that variable to decide which instruction text and actions are available. Before verification, the refund action and refund instructions never reach the model, so skipping the step is not possible.
Exercise
In the orderstatus sketch, what does the model see when orderid is an empty string, and who evaluated the condition?
Show answer
The model sees only the line "Ask the customer for their order number before doing anything else." The script evaluated the if/else against the order_id variable, so the other branch was never sent to the model.