Sub topic 9
Conditions: branching on state
This chapter assumes you have read the earlier chapters on variables and instructions. You should know that a variable holds state across turns and that a procedural instructions block (instructions: ->) can mix logic with prompt text. The examples are illustrative sketches. Check exact keys against the current Agent Script documentation.
Why conditions matter
A prompt can say "ask for the order number first". The model may still skip that step on some turns. A condition moves the rule out of the prompt and into the script. The script reads a variable, picks a branch, and sends only the matching text to the model. The check runs the same way every time.
The if/else form
Inside a procedural instructions block, if and else work like they do in Python. Lines that start with | are prompt text. Only the text in the chosen branch reaches the model.
# Illustrative sketch: the branch 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}.Three details to notice:
@variables.order_idreads the variable. The comparison uses==.- The
{!...}syntax inserts a variable's value into prompt text. - The model never sees the branch it was not given. It cannot ignore an instruction that was never sent.
How a turn flows
The script evaluates conditions each time the topic builds its instructions. The model then replies to the text that results.
Conditions can rely on changing state
State changes between turns. Suppose the customer gives an order number on turn one and an action stores it in order_id. On turn two the same condition takes the else branch. You wrote no extra code for that. The condition reads the current value each time.
You can also compare against other values and combine tests. Typical checks include:
- Whether a string variable is empty.
- Whether a boolean variable is true or false.
- Whether a number is above or below a limit.
The exact operators the language supports are listed in its documentation. Check there before you rely on and, or or not.
Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Putting the rule in prose only ("always ask for the order number") | The model sometimes skips it | Test the variable in an `if` |
| Using a variable that was never declared | The condition cannot be evaluated | Declare it in the `variables` block with a default |
| Forgetting a default value | The first turn compares against nothing useful | Give the variable an initial value such as `""` |
| Writing branches that overlap | The wrong text may be sent | Make the tests exclusive, or end with an `else` |
| Branching on something the model must decide | The script cannot decide it | Store the model's result in a variable first, then branch |
When not to use a condition
Conditions suit facts the script can read: a stored value, a flag, a count. They do not suit judgement calls such as "is the customer upset?". Leave those to the model, or have an action or the model set a variable that a later condition reads.
Exercise
In the orderstatus example, a customer supplies an order number on turn one, and an action stores it in orderid. Which branch runs on turn two, and why is this more reliable than a prompt line saying "ask for the order number if you don't have it"?
Show answer
The else branch runs, because order_id is no longer empty. The script reads the variable on every turn, so the check does not depend on the model remembering the rule. The model also never receives the "ask for the order number" text once the variable is filled.
Exercise
Write a condition for a topic that should say "Your order has shipped" when a boolean variable is_shipped is true, and "Your order is being prepared" otherwise.
Show answer
if @variables.is_shipped == True: | Tell the customer their order has shipped. else: | Tell the customer their order is being prepared. (Check the documentation for the exact boolean literal spelling.)