Sub topic 5
Variables: giving the agent memory
This chapter assumes you have read the chapter on script structure. You should know that a script is a set of named blocks, and you should be comfortable with typed variables and if/else conditions from YAML or Python.
Why an agent needs variables
A model sees the conversation as text. It can usually recall that a customer gave an order number three turns ago. But "usually" is the problem. If a required step depends on that number, you do not want the step to depend on the model remembering it.
A variable is a named value that the script stores and reads. Fixed logic, such as an if check, can test it. Because the script holds the value, the check gives the same answer every time.
Declaring variables
Variables live in their own variables block. Each declaration gives a name, a mutability keyword, a type and an initial value.
# Illustrative sketch. Check exact keys against the current Agent Script documentation.
variables:
order_id: mutable string = ""
verified: mutable boolean = False
attempts: mutable number = 0The parts of each line are:
order_idis the name you use to refer to the value.mutablemeans the value can change during the conversation. A variable that is not mutable keeps its initial value.string,booleanandnumberare the declared types.- The value after
=is the starting value. Every conversation begins with it.
Choose starting values that mean "not set yet". An empty string for order_id lets a later condition ask whether the customer has supplied one.
Reading variables
Inside instructions and conditions you refer to a variable with the @variables prefix. The earlier chapter showed this in a procedural instructions block.
# Illustrative sketch: the script reads state to decide 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}.Two uses appear here. In the if line, the variable is compared in ordinary logic. In the prompt line starting with |, {[email protected]_id} inserts the current value into the text the model receives.
How state persists across turns
Variables keep their values from one turn to the next within a conversation. The script runs again on each customer message, and it sees whatever the variables held at the end of the previous turn. This is what lets the if check above stop asking once the number is stored.
The diagram below traces one variable across two turns.
Common errors
- Forgetting to set the variable. Declaring
order_iddoes not fill it. Something must write the value, such as an action result or a value the model supplies. If nothing does, theifbranch asking for the number runs forever. - Wrong starting value. Starting
verifiedasTrueskips the check you meant to enforce. Start with the safe value. - Type mismatch. Comparing a number variable to a string, such as
attempts == "0", does not behave as you intend. Match the declared type. - Relying on the model instead. Writing "remember the order number" in prompt text brings back the problem variables solve. Store anything a condition depends on.
Exercise
A script declares refund_approved: mutable boolean = True, and a later condition only lets the model issue a refund when refund_approved is true. What is wrong, and how do you fix it?
Show answer
The starting value is True, so the condition passes in every conversation before any approval has happened. Start the variable as False, and set it to True only after the approval step has run.