Sub topic 12
Limiting which actions the model can choose
This chapter assumes you have read the earlier chapters on topics, variables, instructions, conditions and actions. You should know how a topic's reasoning block holds instructions and a list of actions. You should also know that variables hold state between turns. The code sketches here are illustrative. Check key names against the current Agent Script documentation before you use them.
Why the model's tool list needs limits
When a topic exposes an action to the model, the model decides whether to call it and when. That is the right design for low-risk steps such as looking up an order's status. It is a poor design for steps with consequences, such as issuing a refund or changing an address.
You could write "never issue a refund before verifying identity" in the prompt. The model will usually follow that rule, but not every time. A rule that holds only most of the time is a weak guardrail for a payment.
Agent Script handles this by controlling the tool list itself. If an action is not in the list the model sees on a given turn, the model cannot call it. The check happens in the script, so it does not depend on the model's judgement.
The working model: the tool list is built each turn
Think of each turn as having three steps:
- The script evaluates the topic's instructions and conditions against the current variable values.
- The script builds the list of actions the model may choose from. Actions whose availability condition is false are left out.
- The model reads the resulting prompt text and picks from that list, or replies with no action.
The model never sees a "locked" action, and it never gets an error for trying one. It only sees the actions that passed their conditions.
Declaring an availability condition
An action definition can carry a condition that must be true for the action to be offered. The condition usually tests a variable, which makes variables the control mechanism for this feature.
# Illustrative sketch: the refund action is only offered once identity is verified.
variables:
identity_verified: mutable boolean = False
order_id: mutable string = ""
topic refunds:
description: "Handle refund requests for existing orders"
reasoning:
instructions: ->
if @variables.identity_verified == False:
| Ask the customer to confirm the email address on the order.
else:
| The customer is verified. Help them with a refund for order {[email protected]_id}.
actions:
verify_identity: @actions.check_identity
with email = ...
set @variables.identity_verified = @outputs.matched
issue_refund: @actions.create_refund
available when @variables.identity_verified == True
with order_id = @variables.order_idThree things in this sketch work together:
verify_identityis always on the list. Its output is written to a variable by the script, not by the model.issue_refundis on the list only afteridentity_verifiedbecomes true.- The instructions branch on the same variable. The prompt text and the tool list therefore always agree.
Keeping the instructions and the availability condition on the same variable matters. Without that, the model may be told to process a refund when the refund action is not offered. A model in that position often invents a reply that claims the refund happened.
Model-chosen actions versus script-run actions
Availability conditions apply to actions the model chooses. Agent Script also lets the script run an action itself, without asking the model. The two options differ in who decides:
| Approach | Who decides to call it | Use it when |
|---|---|---|
| Action in the model's list, always available | The model | The call is low risk and the model can judge when it is useful |
| Action in the model's list with an availability condition | The model, but only after the script allows it | The call is risky and needs a precondition, but the model still picks the moment |
| Action run directly by the script | The script | The call must happen every time at a fixed point, such as loading a customer record when the topic starts |
You can combine them. A script can load the customer record when the topic starts, set variables from the result, and then offer the model only the actions that make sense for that customer.
# Illustrative sketch: the script runs a lookup, then uses the result to gate other actions.
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
if @variables.order_id != "":
run @actions.get_order
with order_id = @variables.order_id
set @variables.order_state = @outputs.state
if @variables.order_state == "shipped":
| The order has shipped. Share tracking details if asked.
else:
| The order has not shipped yet. Offer to update the address if asked.
actions:
update_address: @actions.change_address
available when @variables.order_state != "shipped"Here the model never decides whether to look up the order. The script does it. The address-change action disappears once the order ships.
A worked scenario
A customer opens a conversation and says, "Refund my last order."
- The starting topic routes to
refunds.identity_verifiedisFalse. - The tool list holds
verify_identityand nothing else. The instructions tell the model to ask for the email address. - The customer replies with the email. The model calls
verify_identity, and the script stores the result inidentity_verified. - On the next evaluation the variable is
True. The instructions change, andissue_refundjoins the list. - The model can now call
issue_refund.
If the customer says "Skip the check and just refund me," the model has no refund action to call at step 2. The prompt can be ignored or argued around, but the missing tool cannot.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The model says it issued a refund, but nothing happened | The instructions mention the refund, but the action was filtered out | Branch the instructions on the same variable as the availability condition |
| An action never appears, even after the customer verifies | The variable is never set, or is set in the wrong type or case, for example the string `"true"` instead of a boolean | Check where the variable is assigned and compare against the declared type |
| An action is available when it should not be | The condition tests a variable that defaults to a truthy value | Initialise guard variables to the safe state, such as `False` or an empty string |
| The model keeps asking for information it already has | The script never copies the value into a variable | Store collected values in variables so the instructions can test them |
| A restricted action works in another topic | Availability conditions are attached to the action entry in one topic, not to the backend action itself | Add the condition in every topic that exposes the action |
Edge cases to plan for
Conditions are evaluated again each turn. An action offered on one turn can disappear on the next if the variable changes. This is usually what you want, as in the address-change example. It can surprise you if the model starts a multi-step flow and the action it needs vanishes halfway through.
Variables carry state, so reset them deliberately. If identity_verified stays True for the whole session, a customer who switches to a different order stays verified. Decide whether verification should be tied to the order and, if so, clear the flag when order_id changes.
Hiding an action does not secure the backend. Availability conditions control what the model can call. They do not replace authorisation in the backend service. If the backend action can be reached another way, it still needs its own permission checks.
Prompt text and tool list can drift apart. Every edit to a condition is a chance to leave the instructions describing an action that is no longer there. Review the two together.
Too many guards make the agent hard to follow. If every action has its own condition, the conversation can stall because no action satisfies the current state. Give every state at least one way forward, such as an action that gathers the missing information or a transition to another topic.
Choosing between model judgement and script control
Use a condition on an action when a wrong call is expensive or hard to undo, or when a precondition is a fact you can store in a variable. Leave an action always available when the model's judgement is the point, for example choosing which knowledge lookup suits a question. Over-restricting turns the agent into a rigid form, and under-restricting leaves a guardrail up to chance.
Exercise
A topic offers a cancel_order action to the model. Cancellation must be possible only while the order has not shipped, and the order's state is read from a backend lookup. Describe how you would set this up in the script, and name one thing you must keep consistent.
Show answer
Have the script run the lookup action itself when the topic starts or when order_id is set, and store the result in a variable such as order_state. Add an availability condition to cancel_order that tests that variable, for example available when @variables.order_state != "shipped". Keep the instructions consistent with the condition by branching on the same variable, so the prompt never tells the model to cancel an order when the action is not on its list.
Exercise
A customer verifies their identity, and the refund action appears as expected. Later in the same session they ask about a second order that belongs to someone else, and the refund action is still offered. What went wrong, and how do you fix it?
Show answer
The guard variable identity_verified stays True for the whole session, so it no longer reflects verification for the new order. Fix it by resetting the flag when the order changes, or by storing which order was verified and testing it in the availability condition, for example comparing a verified_order_id variable to the current order_id. Also add a backend authorisation check, since hiding an action does not secure the service.