Sub topic 13
Patterns for guardrails and required sequences
This chapter assumes you have read the earlier chapters on variables, topic instructions, conditions, actions, and transitions. You should be comfortable reading an Agent Script file and know what @variables, @utils.transition, and a procedural instructions: -> block do.
What a pattern is here
A guardrail is a rule that must hold on every conversation. A required sequence is a set of steps that must run in a fixed order. Prompt text alone can only ask the model to follow either one. Agent Script lets you enforce them with variables, conditions, and action availability, so the model's judgement is limited to the parts you chose to leave open.
The three patterns below combine features you have already seen. The code is an illustrative sketch. Check exact keys against the current Agent Script documentation.
Pattern 1: gate on state
Check a variable before the model sees any task instructions. If the required value is missing, the only prompt text the model receives is the request for that value.
# Illustrative sketch: the script decides what the model is told.
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 check does not depend on the model remembering to ask. The branch runs on every turn, so the gate holds even in a long conversation.
Pattern 2: order steps with flags
A required sequence needs a record of which steps are done. Store each completed step in a variable, then branch on those variables.
# Illustrative sketch: a refund requires verification first.
variables:
identity_verified: mutable boolean = False
refund_requested: mutable boolean = False
topic refunds:
description: "Handle refund requests"
reasoning:
instructions: ->
if @variables.identity_verified == False:
| Verify the customer's identity. Do not discuss refunds yet.
else:
| Collect the refund reason and process the refund.The flag identity_verified is set by an action or transition in your script, not by the model's own claim that verification happened.
Pattern 3: hide actions until they are allowed
A prompt that says "never issue a refund before verification" can still fail. If the refund action is not available to the model at that moment, it cannot be called. Combine this with the flag from Pattern 2 so that the dangerous action appears only after the precondition holds.
# Illustrative sketch: the refund action is offered only after verification.
topic refunds:
reasoning:
actions:
issue_refund: @actions.issue_refund
available when @variables.identity_verified == TrueTreat the exact availability syntax as something to confirm in the documentation. The design idea is stable: remove the option instead of asking the model not to use it.
Choosing between patterns
| Need | Pattern | Why |
|---|---|---|
| A required input must exist | Gate on state | The prompt asks only for the missing value |
| Steps must happen in order | Flags | Progress is stored outside the model |
| An action must not run early | Hide actions | The model cannot call what it cannot see |
Common errors
- Relying on wording alone. "Always verify first" in a prompt is a request, not a guarantee.
- Setting a flag from model output. Set flags from action results so a wrong claim cannot unlock a step.
- Forgetting the failure path. If verification fails, route to a topic that explains the next step instead of leaving the customer in a loop.
- Over-scripting. Leave open-ended parts, such as wording and clarifying questions, to the model.
Exercise
A refund action sometimes runs before identity is verified, even though the prompt says to verify first. Name two script changes that fix this without relying on the model's compliance.
Show answer
First, store verification in a variable such as identity_verified that an action result sets, and branch on it in the instructions so the refund prompt text appears only when it is true. Second, make the refund action available only when that variable is true, so the model cannot call it earlier.