Sub topic 13
Patterns for guardrails and required sequences
This chapter collects the building blocks from earlier chapters into reusable designs. It assumes you can read an Agent Script file and know what variables, topics, instructions, conditions, actions and transitions do. It also assumes you understand why a prompt-only agent can skip a step. The code samples are illustrative sketches. Check exact keys against the current Agent Script documentation before you copy them.
What a pattern is for
A prompt asks the model to follow a rule, and the model usually does. A script makes the rule part of the program, so the model cannot skip it. Each pattern below moves one kind of rule from the first category to the second.
Three questions help you choose a pattern:
- What must never happen? This is a guardrail.
- What must happen before something else? This is a required sequence.
- What can the model decide freely? Leave it as prompt text.
Every pattern follows the same recipe. Store the fact that matters in a variable. Test that variable in a condition. Use the result to change the prompt text, the available actions, or the next topic.
Pattern 1: gate on state
A gate blocks progress until a variable holds the right value. The example from the chapter on instructions is the simplest gate. The script checks whether an order number exists, and the model never has to remember to ask.
# Illustrative sketch: a gate on a missing value.
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}.The gate works because the model only sees the text from the branch that ran. If order_id is empty, the model never reads the "help the customer" line. It cannot act on instructions it was never given.
A gate controls what the model is told. It does not control what the model could do. If an action that looks up orders is still available, the model might call it with a guessed value. Pattern 2 closes that gap.
Pattern 2: gate the action as well as the prompt
A prompt gate and an action gate protect the same rule at two layers. The prompt tells the model what to do next. The action gate removes the tools that would be wrong at this moment.
# Illustrative sketch: the lookup action exists only after verification.
variables:
identity_verified: mutable boolean = False
order_id: mutable string = ""
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
if @variables.identity_verified == False:
| Verify the customer's identity first. Do not discuss order details.
else:
| Answer the customer's question about order {[email protected]_id}.
actions:
get_order_details: @actions.get_order_details
available when @variables.identity_verified == TrueUse both layers for any rule where a violation has a real cost, such as exposing personal data or issuing a refund. For low-risk preferences, such as tone or phrasing, a prompt line is enough.
Pattern 3: required sequence with a step variable
A required sequence is a list of steps that must run in order. The reliable way to enforce one is a variable that records the current step. Each step's instructions and actions depend on that variable.
# Illustrative sketch: three ordered steps tracked by one variable.
variables:
step: mutable string = "collect"
topic return_request:
description: "Process a product return"
reasoning:
instructions: ->
if @variables.step == "collect":
| Collect the order number and the reason for the return.
if @variables.step == "check_policy":
| Explain whether the item is eligible, using the policy result.
if @variables.step == "confirm":
| Ask the customer to confirm the return before submitting it.The step variable moves forward only when the script moves it, for example after an action succeeds. The model never sets it by announcing that a step is done. Design each step so its exit condition is something you can test, such as a non-empty variable or a successful action result. "The customer seems satisfied" is not testable.
The diagram below shows the same sequence as a state machine.
Pattern 4: run the step in the script
Some steps do not need the model's judgement at all. If an action must run every time a topic is entered, call it from the script and store the result. The model then reads the result as part of its instructions.
# Illustrative sketch: the script runs the check, the model explains the result.
topic return_request:
description: "Process a product return"
reasoning:
instructions: ->
run @actions.check_return_policy
with order_id = @variables.order_id
set @variables.eligible = @outputs.eligible
if @variables.eligible == True:
| Tell the customer the item can be returned and ask them to confirm.
else:
| Tell the customer the item cannot be returned and explain why.This is the strongest form of a required step, because no model decision sits between the topic starting and the check running. It also saves a model round trip. The cost is flexibility. The action runs even if the customer's question made it unnecessary, so use this pattern only when the step is always needed.
Pattern 5: transition as a forced exit
A transition moves the conversation to another topic. Used from a condition, it forces the exit instead of offering it.
# Illustrative sketch: leave the topic when a condition is met.
topic return_request:
description: "Process a product return"
reasoning:
instructions: ->
if @variables.step == "done":
transition to @topic.post_return_survey
else:
| Continue helping with the return.Compare this with the routing example from the starting-topic chapter, where the transition is exposed as a tool and the model decides whether to use it.
| Style | Who decides to move | Use when |
|---|---|---|
| Transition inside a condition | The script | The next topic is required once a state is reached |
| Transition exposed as a tool | The model | The customer's intent decides the route |
A good rule is that the destination is always fixed by the script, and you choose who decides the timing.
Pattern 6: refusal and escalation paths
Guardrails also cover what the agent does when it cannot or should not proceed. Give each unsafe or unsupported situation a dedicated topic, and route to it from a condition.
- Keep a counter variable, such as
failed_attempts, and transition to an escalation topic when it reaches a limit. - Keep a flag such as
needs_human, and make the escalation topic the only one that offers a handoff action. - Write the refusal topic's instructions as plain, short prompt text. It needs no branching.
This pattern prevents loops where the model keeps retrying something that will not work. The counter is state the script owns, so the model cannot talk itself into one more try.
Choosing between prompt text and logic
Not every rule belongs in logic. Over-scripting makes the agent rigid, and every branch is code you must test.
| Rule | Put it in | Reason |
|---|---|---|
| Verify identity before showing data | Condition plus action gate | A violation exposes data |
| Never issue a refund above a limit | Condition plus action gate | A violation costs money |
| Ask for order number before lookup | Condition | The check is simple and testable |
| Keep a friendly tone | Prompt text | A model can judge tone well |
| Decide which topic fits a vague request | Model-chosen transition | Needs language understanding |
Three failure modes are worth knowing:
- A variable that is never updated. A gate on
identity_verifiedstays shut if no step sets it to true. Trace each variable from where it is set to where it is read. - A branch with no else. If no condition matches, the model may receive no instructions for that turn. Add a fallback branch.
- Only a prompt gate. The instruction text says "do not discuss details", but the action is still available. Add the action gate.
Exercises
Exercise
A refund topic currently says in its prompt text, "Only issue a refund after the customer has confirmed." The model sometimes issues refunds without confirmation. Describe two script changes that make the rule reliable.
Show answer
First, add a variable such as refund_confirmed, set to false at the start. Only the script sets it to true, after the customer's confirmation step completes. Second, make the refund action available only when refund_confirmed == True, using an availability condition. Optionally, also branch the instructions so the model is told to ask for confirmation while the variable is false. The action gate is the essential change, because it removes the refund action from the model's options until the condition holds.
Exercise
A script has steps "collect", "check_policy" and "confirm" tracked by a step variable. The instructions have an if-branch for each step but no else. What can go wrong, and how do you fix it?
Show answer
If step ever holds an unexpected value, such as an empty string or a misspelled name, no branch matches and the model receives no instructions for that turn. Add an else branch that gives safe default text, or that transitions to an escalation topic. Also check that every place that sets step uses one of the three expected values.