All articles

Agentforce

The Agent Script rules that stop your first .agent file compiling

Agent Script looks like YAML and reads like Python, which is exactly why the first file fails to compile. Here are the rules that cause most of it: strict indentation, two different transition syntaxes, mutable-versus-linked variables, and the operators that are missing.

The first Agent Script file almost never compiles, and the reason is rarely the logic. It is that the language borrows the look of YAML and the feel of Python without being either, so you write what seems obvious and the compiler rejects it over a space, a lowercase true, or a transition keyword used in the wrong block.

I would put a printed cheat sheet next to the keyboard for the first week. Agent Script is deterministic by design, which is the whole point of writing an agent in script instead of tuning a prompt, and determinism means the grammar is strict and unforgiving in ways a prose instruction never is. Most of the early failures come from the same handful of rules. Learn those and the file compiles.

I will take them in the order they tend to bite: indentation, block order, the two transition syntaxes, variable declarations, action references, and the operators that are not there.

Indentation is structural, and it is spaces

Agent Script uses indentation to express structure the way Python does, so a misaligned line is not a style issue, it is a parse error. Two rules settle most of it.

Use spaces, not tabs. The convention in Salesforce’s own recipe library is three spaces per level, and the thing that breaks compilation is mixing the two: a tab-indented line followed by a space-indented line inside the same block fails to parse. Set your editor to insert spaces and the single most common first-file error disappears.

Comments are Python-style with #, inline or on their own line:

# This is a comment
config:
   developer_name: "Order_Agent"  # inline comment

If a script will not compile and the message is vague, check indentation before you check anything else. It is the leading cause, and it is invisible until you turn on whitespace rendering.

The top-level blocks have a required order

Agent Script is not a bag of blocks you arrange to taste. The top-level blocks must appear in a fixed sequence, and putting variables after system, or start_agent before your system block, is a compile error rather than a preference the compiler forgives.

The order is: config, then variables, then system, then the optional connection / knowledge / language blocks, then start_agent, then your subagent blocks. Four things are mandatory, and the first file usually omits one of them:

  • config with a developer_name.
  • system with messages.welcome, messages.error, and instructions.
  • start_agent with a reasoning block that contains at least one transition action.
  • at least one subagent with a description and a reasoning block.

Here is the smallest thing that compiles, which is worth keeping as a skeleton:

config:
   developer_name: "Simple_QA"

system:
   messages:
      welcome: "Hi, how can I help?"
      error: "Sorry, something went wrong."
   instructions: "You are a helpful assistant. Answer clearly."

start_agent agent_router:
   description: "Entry point"
   reasoning:
      actions:
         start: @utils.transition to @subagent.main

subagent main:
   description: "Answer the user's questions"
   reasoning:
      instructions:->
         | Help the user with their questions.

One naming note before you rename anything in it: what the documentation used to call a topic is now a subagent. The keyword, the routing, and the design thinking behind classification are the same; only the word changed. If you are following an older tutorial that uses topic, read it as subagent.

There are two transition syntaxes, and they are not interchangeable

This one causes more head-scratching than indentation, because both forms are correct Agent Script and the compiler will not let you swap them. Moving from one subagent to another is written one way inside reasoning.actions and a different way inside a directive block like after_reasoning.

Inside reasoning.actions, where the LLM chooses whether to fire the transition, you use the @utils form and give it a name:

reasoning:
   actions:
      go_to_checkout: @utils.transition to @subagent.checkout
         description: "Move to checkout when the cart is ready"
         available when @variables.cart_has_items == True

Inside after_reasoning, which runs deterministically after reasoning finishes, you use the bare form with no @utils and no name:

after_reasoning:
   if @variables.order_complete:
      transition to @subagent.summary

The rule to memorise: @utils.transition to belongs in reasoning.actions, and bare transition to belongs in directive blocks. Use the bare form in reasoning.actions and it fails; use the @utils form in after_reasoning and it fails. They are two tools for two contexts, one LLM-selected and one guaranteed.

Variables: mutable needs a default, linked needs a source

The variables block holds two kinds of variable, and the compiler enforces opposite rules for each. Getting them backwards is the most common declaration error.

A mutable variable is state the agent reads and writes, and it must have a default value. A linked variable is read-only context pulled from outside the conversation, and it must have a source and must not have a default.

variables:
   # mutable: the agent owns it, so it needs a starting value
   customer_name: mutable string = ""
   order_count: mutable number = 0
   is_verified: mutable boolean = False
   items: mutable list[string] = []

   # linked: comes from session context, so it has a source and NO default
   session_id: linked string
      source: @session.sessionID

Three traps live in that block. Booleans are True and False, capitalised like Python, never true or false. A mutable variable with no default fails, and so does a linked variable that carries one. And list and object types cannot be linked: only mutable variables hold collections.

One more that looks reasonable and is not. The ... token is slot-filling syntax, the thing you use to tell the LLM to extract a value from the conversation when it calls an action. It is not a placeholder default. Writing my_var: mutable string = ... is a mistake the compiler catches, because ... has a specific job elsewhere.

Reference actions with @actions, and mind where directives are allowed

Actions are declared in a subagent’s actions block with a target that points at the implementation, a Flow, an Apex class, a prompt template, and then referenced with the @actions. prefix. The reference prefix is not optional. A bare action name, in a run statement or in instruction text, does not resolve.

subagent customer_service:
   description: "Look up and greet a customer"
   actions:
      fetch_customer:
         description: "Get customer details"
         inputs:
            id: string
         outputs:
            name: string
         target: "flow://FetchCustomer"
   reasoning:
      instructions:->
         if @variables.customer_id and not @variables.customer_loaded:
            run @actions.fetch_customer
               with [email protected]_id
               set @variables.customer_name = @outputs.name
               set @variables.customer_loaded = True
         if @variables.customer_name:
            | Hello, {[email protected]_name}!
         else:
            | Please share your customer ID.

That snippet also shows the input-binding grammar, which trips people in two specific ways. Post-action directives, the set, run, and if lines that fire after an action, work only on @actions.*, not on utilities like @utils.transition to or @utils.escalate. And you cannot read an action’s own @inputs inside a set directive; that throws an opaque deploy-time error. If you need to capture what the user supplied, collect it with @utils.setVariables into a variable first, then pass the variable to the action. Only @outputs belongs on the right-hand side of a set.

The operators that are missing

Expressions look Python-like, which sets an expectation the language does not fully meet, and the gaps are worth knowing before you write a condition that silently will not compile.

Arithmetic is addition and subtraction only. There is no multiply, divide, or modulus operator, so any real math belongs in a Flow or Apex action that you call and read back, not in the script. Comparison and logical operators are all there, along with a ternary for inline choices:

# available
==   not-equal   <   <=   >   >=   is   is not
and   or   not
x if condition else y

# not available
*   /   %

Control flow has one more gap that surprises everyone. There is no else-if branch. You get if and else, and a third branch means a nested if inside the else, or separate if statements. String interpolation wraps an expression in the brace-and-bang delimiters you saw in the greeting snippet above, which drops a variable’s value into instruction text. Those two facts, no else-if and brace-bang for templates, cover most of what you will reach for in a reasoning block.

Where I would start

Keep the skeleton above in a scratch file and build outward from something that already compiles, rather than writing a hundred lines and bisecting the parse error afterwards. Add one subagent, compile. Add one action with its target, compile. Add the transition, compile. The feedback loop is fast, and it tells you which line you just broke instead of leaving you to find it.

Then lean on the tooling. Salesforce’s recipe library is a set of known-good .agent files you can read for the exact grammar of a pattern you are stuck on, and authoring through Agentforce DX puts the compiler in your terminal so a bad transition or an uninitialised variable fails in seconds, not after a deploy. The grammar is strict, but it is small and consistent. Once the first file compiles, the second one does too.

Understanding the basics

Does Agent Script use tabs or spaces for indentation?

Spaces. Salesforce’s recipe library uses three spaces per level, and the error that most often stops a first file compiling is mixing tabs and spaces within a block. Set your editor to insert spaces for .agent files and keep the indentation consistent throughout, since the language uses indentation to express structure the way Python does.

What is the difference between mutable and linked variables?

A mutable variable is state the agent reads and writes, and it must be declared with a default value, such as mutable string = "". A linked variable is read-only context supplied from outside the conversation, such as a session ID; it must have a source and must not have a default. Collection types (list, object) can only be mutable, and boolean defaults must be capitalised as True or False.

Why does my transition keyword cause a compile error?

Agent Script has two transition syntaxes for two contexts. Inside reasoning.actions, where the LLM decides whether to transition, use @utils.transition to @subagent.name. Inside a directive block such as after_reasoning, which runs deterministically, use the bare transition to @subagent.name. The two are not interchangeable, so using the bare form in reasoning.actions, or the @utils form in a directive block, fails to compile.

Can Agent Script do multiplication or an else-if?

No to both. Arithmetic in Agent Script expressions is limited to addition and subtraction; there is no *, /, or %, so any real math belongs in a Flow or Apex action you call. There is also no else if construct: use if and else, with a nested if inside the else for a third branch, or separate if statements.


Standing up a deterministic agent in Agent Script, or porting a prompt-tuned agent into a script you can version and test, is day-to-day work for our Agentforce practice. If your team is moving from clicks-first authoring into pro-code agents and wants the patterns right from the first file, talk to us.

Keep reading

All articles