Agent Script Reference: Variables

Variables let agents deterministically remember information across conversation turns, track progress, and maintain context throughout the session. You define all variables in the variables block, and all subagents in the agent can access the variables.

There are several types of variables:

  • custom variable: You can initialize a variable with a default value, and the agent can change the variable’s value.
  • linked variable: The value of a linked variable is tied to an output such as an action’s output. Linked variables can’t have a default value.
  • system variable: Predefined, prepopulated system variables that you can use in your agent.

Defining a Variable 

Define variables in the Variables block.

Reference a Variable From Script
1CurrentState: mutable string = "gatheringInfo"
2        description: "The current state, or step, of the interview."
3        label: "State"
4        visibility: "External"

Variable Names 

Variable names must follow Salesforce developer name standards:

  • Begin with a letter, not an underscore.
  • Contain only alphanumeric characters and underscores.
  • Can’t end with underscore.
  • Can’t contain consecutive underscores (__).
  • Maximum length of 80 characters.

Referencing Variables 

To reference a variable from the script, use @variables.<variable_name>.

Reference a Variable From Script
1if @variables.Customer_Contact is None:
2                set @variables.No_Matching_Contact = True

To reference a variable from within reasoning instructions, use {!@variables.<variable_name>}.

Reference a Variable From Reasoning Instructions
1reasoning:
2    instructions: ->
3        | Always use {!@variables.Customer_Email} for the customer's email address.

Custom Variables 

Custom variables have these properties:

  • mutable - Optional. Allows the agent to change the variable’s value. To ensure a variable’s value is never changed, define the variable without mutable.
  • description - describes the variable. Optional. If you want the LLM to use reasoning to set the variable’s value, include a description to help the LLM set the value correctly. See Let the LLM set variables with user-entered information (slot filling).
  • label - Optional. The variable’s name as displayed in the UI. By default, the description is generated from the name. For example, if your variable’s name is my_var, the UI displays the label My Var.
  • visibility - Optional. Default value is Internal. Set visibility to External to allow an API to set the variable’s value, or to change the variable’s value when testing the agent in simulate mode.
Example: Define Custom Variables
1variables:
2    isPremiumUser: mutable boolean = False
3        description: "Indicates whether the user is a premium user."
4        label: "Has Gold Status"
5
6    customer_loyalty_tier: mutable string = "standard"
7        description:|
8            Stores the customer's membership tier level.

Custom variables can have these types:

TypeNotesExample
stringAny alphanumeric string without special characters.name: mutable string = ""
numberUse for both integers and decimals. For example, 42 or 3.14. Compiles to IEEE 754 double-precision floating point.age: mutable number, price: mutable number = 99.99
booleanAllowed values are True or False. The value is case-sensitive, so capitalize the first letter.is_active: mutable boolean = True
objectValue is a complex JSON object in the form {"key": "value"}.order_line: mutable object = {"SKU": "abc12344409","count": 42}
dateAny valid date format.start_date: mutable date
idDeprecated. Use string to store a Salesforce record ID.See string type.
list[type]A list of values of the specified type. All primitive types and object type are supported.flags: mutable list[boolean] = [True, False, True], scores: list[number] = [95, 87.5, 92], obj_list: mutable list[object] = None

No Value (None) and Empty String ("") 

Use None to check whether a variable has a value. You can use None with any variable type. For a string variable, you can also use "" to check if the variable is set to an empty string. When checking string variables in conditional statements, you might want to use both None and "".

For more information, see Agent Script Reference: Conditional Expressions.

Linked Variables 

A linked variable’s value is tied to a source, such as an action’s output. Linked variables have these restrictions:

  • can’t have a default value
  • can’t be set by the agent
  • can’t be an object or a list

The source field references where the variable gets its value. Supported source namespaces are:

NamespaceAvailable PropertiesDescription
@MessagingSessionId, MessagingEndUserId, EndUserLanguageProperties of the messaging session
@MessagingEndUserContactIdProperties of the messaging end user
@VoiceCallIdProperties of the voice call
Example: Define Linked Variables
1variables:
2    session_id: linked string
3        source: @MessagingSession.Id
4        description: "The messaging session ID"
5    contact_id: linked string
6        source: @MessagingEndUser.ContactId
7        description: "The contact ID of the end user"
8    voice_call_id: linked string
9        source: @VoiceCall.Id
10        description: "The voice call ID"

Linked variables can have these types:

  • string
  • number
  • boolean
  • date
  • id (deprecated; use string for Salesforce record IDs)

System Variables 

Agent Script provides predefined, prepopulated system variables that you can use in your agent. To access system variables, use @system_variables.<variable_name>. A system variable is:

  • read-only, so you can’t change its value
  • predefined, so you don’t define it in the variables block
  • used in the same places as a custom variable or a linked variable

Currently, @system_variables.user_input is the only system variable.

@system_variables.user_input 

The user_input system variable contains the customer’s most recent utterance (not the entire conversation history).

The LLM remembers the entire conversation history, so you don’t typically need to use @system_variables.user_input unless you’re passing the last thing a customer said into an action.

Note

Example - Analyze Customer Sentiment 

In this example, we pass the last customer utterance into a sentiment analysis action. Although the agent’s LLM can also analyze sentiment, we want to use a prompt template action that understands industry-specific terminology and our customer’s rapidly-changing language patterns.

Example: Analyze sentiment of most recent customer utterance
1reasoning:
2    actions:
3        AnalyzeSentiment: @actions.AnalyzeSentiment
4            with utterance = @system_variables.user_input
5            set @variables.customer_sentiment = @outputs.sentiment_classification

Examples and Patterns 

For examples and patterns using variables, see Agent Script Pattern: Using Variables Effectively.

Related Topics