Sub topic 5
Variables: giving the agent memory
This chapter assumes you have read the earlier chapters on the structure of an Agent Script file and on topics. It also assumes you can read indentation-based syntax and know what a typed variable is in a language such as Python. The code samples are illustrative sketches. Check exact keys and type names against the current Agent Script documentation before you use them.
Why a script needs variables
A language model reads the conversation so far and produces the next reply. It has no guaranteed place to store facts. If a customer gave an order number five turns ago, the model may still use it, or it may ask again, or it may repeat a wrong number. Anything that depends on the model remembering is probabilistic.
A variable in Agent Script is a named, typed value that belongs to the agent's state. The script reads it, and the runtime keeps it between conversation turns. Because conditions in your script can test a variable, a variable lets fixed logic react to what has happened so far. The script, not the model, decides what "we already have the order number" means.
The working model has three parts:
- You declare each variable once, with a type and usually a default value.
- Something writes to it: the model filling a slot, an action's output, or a script statement.
- Something reads it: a condition, a prompt line, or an action input.
Declaring variables
Variables live in their own block near the top of the file. Each entry gives a name, a modifier, a type and an optional default.
# Illustrative sketch: declaring state for an order-support agent.
variables:
order_id: mutable string = ""
order_verified: mutable boolean = False
refund_attempts: mutable number = 0Read each declaration from left to right:
| Part | Example | Meaning |
|---|---|---|
| Name | `order_id` | How you refer to it elsewhere in the script. |
| Modifier | `mutable` | The script and runtime may change the value during the conversation. |
| Type | `string` | The kind of value it holds. |
| Default | `= ""` | The value at the start of every new conversation. |
A variable that is not mutable cannot be changed by your logic. Some variables are not set by the script at all. They are linked to context supplied by the platform, such as the identity of the current user. Check the documentation for how linked variables are declared in your version. The point for design is that you decide, for each piece of state, who is allowed to change it.
Choosing defaults
A default is the starting state, and your conditions will test it. An empty string for an identifier, False for a flag, and 0 for a counter are good defaults because they mean "not yet known" or "not yet done". Pick defaults that make that meaning obvious. If order_id defaulted to "none", a later check against "" would silently never match.
Reading variables
You read a variable by its full reference, @variables.<name>. You use it in two places.
In a condition, the reference is a value like any other:
# Illustrative sketch: fixed logic reads state.
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}.In prompt text, the lines that start with |, you insert the current value with {[email protected]_id}. The runtime substitutes the value before the model sees the text. The model receives "Help the customer with order 48213", not a template.
Because the if runs in the script, the check never depends on the model deciding to ask. On the first turn order_id is empty, so the model sees only the "ask" instruction. After the variable is set, the model sees only the "help" instruction.
Writing variables
Reading is easy. Writing is where most of the design decisions lie, because each way of writing gives the model a different amount of control.
From an action's output
When an action returns data, you can store a result in a variable. This is the most dependable route, because the value comes from your backend and not from the model's wording.
# Illustrative sketch: store an action result in state.
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
| Look up the order the customer asked about.
actions:
lookup_order: @actions.get_order
with order_id=...
set @variables.order_verified = @outputs.foundHere with order_id=... asks the model to fill the input from the conversation. The set line then copies the action's found output into order_verified. The model chose when to call the action and supplied the input, but the script decided what to remember.
By the model, through a slot
A variable can also be filled by the model from what the customer said. This is convenient. It also means the stored value is only as good as the model's extraction, so validate before you rely on it. A common approach is to store the value, then confirm it through an action before setting a flag such as order_verified.
By the script
Procedural logic can change state directly, for example to count attempts:
# Illustrative sketch: the script updates a counter itself.
reasoning:
instructions: ->
set @variables.refund_attempts = @variables.refund_attempts + 1
if @variables.refund_attempts > 2:
| Explain that a human agent must review this request.
else:
| Offer to retry the refund.A script-driven write is the most predictable kind, because no model is involved.
How state moves through a conversation
State persists across turns within one conversation. The runtime keeps the variable values, and each turn's conditions see the values left by the previous turn.
Variables are global to the agent, not private to a topic. A value set in the order_status topic is still there after a transition to another topic. This is how topics share context, such as an identity check done once and relied on later. It also means two topics can overwrite each other's values if you reuse a name.
Using variables to enforce a sequence
Variables are the glue between steps. A required order, such as verify the customer, then look up the order, then offer a refund, becomes a set of flags:
order_verifiedstarts asFalse.- The verification action sets it to
Trueonly on success. - Instructions for the refund step test
order_verifiedand refuse to proceed otherwise.
# Illustrative sketch: a flag gates the next step.
topic refunds:
description: "Handle refund requests"
reasoning:
instructions: ->
if @variables.order_verified == False:
| Tell the customer you must verify their order first.
else:
| Ask which item they want refunded for order {[email protected]_id}.The refund prompt is not even sent to the model until the flag is true. Later chapters show how to also hide the refund action itself until that point.
Common errors
- Using a variable the model never fills. If nothing in your script, actions or slots writes
order_id, the conditionorder_id == ""stays true forever and the agent loops on the same question. Trace every variable to at least one writer. - Mismatched types. Comparing a number variable to a string, such as
refund_attempts == "0", fails or never matches. Keep the declared type and the compared value the same. - Wrong default. A default that is not the "empty" value your condition tests for makes the first branch unreachable.
- Forgetting
mutable. A variable you try to set but declared as fixed cannot change, so the script will not behave as you expect. - Trusting model-filled values. A customer can type a wrong or malicious value. Treat a model-filled variable as unverified until an action confirms it.
- Name collisions across topics. Variables are shared. Use specific names such as
refund_attemptsinstead ofcount. - Storing too much. Put in a variable only what a later condition, prompt or action input needs. Everything else is already in the conversation history.
Summary of the working model
| Question | Answer |
|---|---|
| What is it? | A named, typed value kept in agent state. |
| When is it created? | At the start of a conversation, with its default. |
| Who can change it? | Whatever you allow: action outputs, model slots, or script statements, if it is mutable. |
| Who can read it? | Conditions, prompt interpolation and action inputs, in any topic. |
| How long does it last? | For the conversation, across turns. |
Exercise
An agent has order_id: mutable string = "" and a topic that says "if order_id == '': ask for the order number". The customer gives their order number, but the agent asks for it again on every turn. Name two likely causes and a fix for each.
Show answer
Cause one: nothing writes the variable. The model heard the number, but no slot, action output or script statement stored it in orderid, so the condition stays true. Fix: capture the value, for example with an action input the model fills and a `set @variables.orderid = ...` line that stores it. Cause two: the value is stored in a different variable or with a different type or format than the one the condition tests, such as a separate variable with a similar name. Fix: write to the same declared variable and compare against the right type. In both cases, trace which writer sets order_id and confirm its value after the first turn.