Agent Script Pattern: Using Conditionals
Use conditionals to deterministically control agent behavior based on variable values. Conditionals evaluate before the prompt reaches the LLM.
Conditionals let you make deterministic decisions about which instructions are included in the prompt, which actions run, or which topics to transition to. Conditionals don't rely on LLM interpretation.
Pattern Example: Show Gold members a thank-you message and their points balance, while Platinum VIP members see an exclusive welcome with additional perks.
Customize the prompt based on variable values.
Only the relevant instruction is included in the prompt sent to the LLM.
- Prompt sent to LLM if the customer’s name is Jo Richards and they're a VIP member:
Refer to the user by Jo Richards. Thank the customer for being a Platinum VIP member. - Prompt sent to the LLM if the customer’s name is Jane Smith and they're a Gold member:
Refer to the user by Jane Smith. Thank the customer for being a Gold member.
Run actions only when specific conditions are met. In this example, the agent only looks up the current order if we don't have an order summary, improving response time and reducing system usage.
Route users to different topics based on conditions.
The transition happens immediately, before the LLM processes any other instructions. See Required Topic Flow for related patterns.
Handle mutually exclusive conditions with if and else logic.
Keep in mind that you can combine conditions using and or or.
- Initialize variables for reliable checks: It's typically a good practice to give variables default values (for example,
= ""or= False) so conditional checks work correctly. - Use
is Nonefor null checks: Use@variables.value is Noneto check whether a variable has no value assigned. This check is different from checking for an empty string (@variables.value == ""), for example. The== ""expression checks for an empty string (which is a valid assignment), whereasis Nonechecks for an unassigned value. - Keep conditions simple: Complex nested conditions are hard to debug; consider breaking them into separate variables or topics.
- Pattern: Required Topic Flow
- Reference: Conditional Expressions
- Reference: Supported Operators
- Reference: Reasoning Instructions