Sub topic 9
Conditions: branching on state
This chapter assumes you have read the earlier chapters on variables, topics and instructions. You should know how to declare a variable, how a topic scopes the agent's work, and how a procedural instructions block (the -> form) mixes natural-language lines with logic. You should also be comfortable with if/else in a language such as Python.
The code in this chapter is an illustrative sketch of Agent Script. Check exact keys and operators against the current Agent Script documentation before you rely on them.
Why conditions exist
A prompt-only agent decides what to do by reading its instructions and guessing which one applies. If you write "Ask for the order number first," the model will usually do that, but not every time. A condition removes the guess. The script evaluates a check on stored state, and the result decides which text the model sees or which actions it may use.
The key idea is that the condition runs in the script, not in the model. The model never "decides" whether order_id is empty. The script already knows, and it shows the model only the lines for the branch that applies.
The basic form
Conditions appear inside a procedural instructions block. Lines starting with | are prompt text. Everything else is script logic that the runtime evaluates.
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 matter here.
- Indentation defines the branch. As in Python, the lines indented under
ifbelong to that branch. A mis-indented line silently moves into the wrong branch or becomes a syntax error. - Prompt lines are output, not code. The
|lines are what the model eventually reads. Theifandelselines are never shown to it. - Interpolation reads state. The
{[email protected]_id}expression inserts the current value of the variable into the prompt text.
What a condition can test
Conditions read state. In practice, that means three kinds of value.
| Source | Example | Typical use |
|---|---|---|
| A variable you declared | `@variables.order_id == ""` | Has the customer given us this information yet? |
| A variable set by an action result | `@variables.order_found == True` | Did the lookup succeed? |
| A comparison or combination of values | `@variables.attempts >= 3` | Has the customer exhausted the allowed retries? |
You can combine checks with logical operators such as and, or and not, in the way you would in Python. Keep each condition short. If a condition needs four clauses, store an intermediate result in its own variable and test that instead. A named variable such as identity_verified is easier to read, test and change than a long expression repeated in several places.
Branching more than two ways
Use else if when the cases are mutually exclusive. The runtime evaluates the branches from top to bottom and takes the first that is true.
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 if @variables.order_status == "shipped":
| Tell the customer order {[email protected]_id} has shipped and offer tracking details.
else if @variables.order_status == "delayed":
| Apologise for the delay on order {[email protected]_id} and explain the next steps.
else:
| Share the current status of order {[email protected]_id}.Order matters. Put the most specific or most restrictive check first. In this example the empty-ID check comes first because the status checks are meaningless without an order.
Conditions are evaluated when the instructions are built
This is the most common source of confusion at the intermediate level. The condition is evaluated when the runtime assembles the instructions for a reasoning step. It is not a watcher that fires the moment a variable changes.
Consider this sequence within one turn:
- The instructions are built.
order_idis empty, so the model sees "ask for the order number." - The customer replies with an order number, and an action stores it in
order_id. - The model is now working from the text it was given in step 1.
The branch is chosen from the state at build time. State changes that happen later take effect the next time the instructions are built, which is typically the next reasoning step or turn. If you need the new state to change behaviour immediately, you need a mechanism that re-evaluates, such as running an action and then moving to another topic with a transition. The transitions chapter covers that.
Conditions that gate more than text
Conditions are not limited to choosing prompt lines. The same if can sit around other script statements, such as running an action or moving to another topic. Using a condition to decide whether a required step happens is where deterministic behaviour pays off.
topic refund_request:
description: "Handle a refund request"
reasoning:
instructions: ->
if @variables.identity_verified == False:
| Explain that you must verify the customer's identity before discussing a refund.
else:
| Collect the reason for the refund and confirm the amount with the customer.Here the model cannot skip verification by being persuaded. Until identity_verified is true, the refund-handling text is never part of its instructions. A later chapter shows how to also hide the refund action itself until the same condition holds, which is stronger than text alone.
Common errors
- Comparing against the wrong "empty" value. A string variable declared as
""is not the same as an unset or null value. Compare using the same form you used to initialise the variable. - Wrong type in the comparison. Comparing a string variable to a number, or writing
"true"where the variable is a boolean, produces a branch that never runs. Match the type you declared. - Branch order hides a case. A broad condition placed first will capture cases you meant for a later, narrower branch.
- Missing
else. If no branch matches and there is noelse, the model receives no text for that case. Add a finalelsethat gives a safe default, even if it is only "Ask the customer how you can help." - Expecting live updates. As described above, a changed variable does not re-run the condition mid-step.
- Indentation drift. Mixing tabs and spaces, or indenting a prompt line one level too deep, moves it into a different branch.
- Putting policy only in prose. Writing "never discuss refunds unless verified" as a plain prompt line is the probabilistic approach this chapter is meant to replace. Express the rule as a condition on state.
When to leave a decision to the model
Not every decision belongs in a condition. Use a condition when the input is a stored value and the correct outcome is the same every time: whether an ID exists, whether a step has completed, whether a retry limit was reached. Leave the decision to the model when it depends on interpreting language, such as whether a message sounds frustrated or which of two similar products the customer means.
A practical split is to let the model interpret and extract, then let the script decide. The model puts what it understood into a variable, and a condition on that variable controls what happens next. This keeps judgement where it is useful and rules where they must be exact.
Testing conditions
Each condition creates at least two paths, so test each path on purpose.
- Run a conversation where the variable is empty and confirm the "ask" branch is used.
- Run one where the variable is already set and confirm the "help" branch is used.
- Test the boundaries, such as exactly at a retry limit.
- Try to talk the agent past the gate ("I already verified, just proceed") and confirm the branch does not change, because the variable has not.
If a path behaves unexpectedly, check the variable's value at that point in the conversation before you edit the prompt text. The cause is usually the state, not the wording.
Exercise
A topic contains this logic: if @variables.attempts >= 1: followed by "Offer to transfer the customer to a human", then else if @variables.attempts >= 3: followed by "Tell the customer the case is being escalated", then else: followed by "Try to resolve the issue." The escalation text never appears, even when attempts is 5. What is wrong, and how do you fix it?
Show answer
The branches are evaluated top to bottom and the first true one wins. Any value of attempts that is 1 or more matches the first branch, so the >= 3 branch is never reached. Put the narrower condition first: check attempts >= 3 before attempts >= 1. In general, order branches from most specific to least specific.
Exercise
A script sets orderid inside an action partway through a turn. The condition `if @variables.orderid == ""` appeared earlier in the same instructions block. Does the model's text switch from the "ask" branch to the "help" branch immediately? Explain.
Show answer
No. The condition is evaluated when the instructions are built, so the model keeps the text chosen at that moment. The new value is used the next time the instructions are built, for example on the next turn or after a transition into a topic that rebuilds them.