Agent Script Reference: Conditional Expressions
if and else conditions deterministically specify what actions to take or which prompts to include. To specify complex conditions, use parentheses () to group and and or operators within a condition.
For example, you can check a variable’s value and then run an action based on the result:
1# If the tracking number (represented as a string) isn't empty and is not None, get the tracking updates
2if @variables.tracking_number is not None and @variables.tracking_number != "":
3 run @actions.Get_Tracking_Updates
4else:
5 run @actions.Ask_Tracking_Number
If you need to nest and and oroperators, you can use parentheses (). In this example, the grouped condition (@variables.HasSalesInterest == True or @variables.WantsMeeting == True) is evaluated first. The condition evaluates as true if either one of HasSalesInterest or WantsMeeting is true.
1reasoning:
2 instructions: ->
3 | Select the best tool to call based on conversation history and user's intent.
4 actions:
5 go_to_Qualify: @utils.transition to @subagent.Qualify
6 available when @variables.customerType == "Valued" and @variables.QualificationEnabled == True and (@variables.HasSalesInterest == True or @variables.WantsMeeting == True) and @variables.QualificationFlowStep != "COMPLETE"
You can also check a variable in order to set other variables.
1if @variables.order_number == "" and @variables.customer_email == "":
2 set @variables.order_found = False
3 set @variables.customer_verified = False
You can use a conditional expression to determine which natural-language prompt to include.
1if @variables.is_late == True:
2 | Apologize to the customer for the delay in receiving their order.
3else:
4 | Tell the customer their order is arriving as scheduled.
You can use if and else inside reasoning instructions to conditionally include different prompt lines for the LLM.
1reasoning:
2 instructions: ->
3 | Your job is solely to help with issues and answer questions by searching knowledge articles.
4 if @variables.support_tier == "premium":
5 | This is a premium customer. Prioritize their request and offer proactive suggestions.
6 else:
7 | This is a standard customer. Answer their questions helpfully and suggest upgrading for faster support.
8 | If the customer's question is too vague, ask for more details.
You can check whether a variable has a value.
1# Note this example demonstrates correct syntax, NOT how to design a production agent
2reasoning:
3 instructions: ->
4 if @variables.account_id is None:
5 | What's the account ID for this order?
6 if @variables.is_premium_user is not None:
7 | This customer's premium status is set to {!@variables.is_premium_user}.
8 if @variables.order_lines != None:
9 | Order lines on this order: {!@variables.order_lines}.
10 if @variables.scheduled_date is None:
11 | When would you like to schedule this appointment?
Currently, Agent Script supports if and else logic, but it doesn’t support else if logic after an if statement.
Related Topics