Sub topic 14
Testing and refining your script
This chapter assumes you have read the earlier chapters on topics, variables, instructions, conditions, actions and transitions. You should be able to read a small script and say which parts are fixed logic and which parts are prompt text for the model.
Why scripts still need testing
A script fixes the steps you wrote as logic. The model still decides how to phrase replies, which allowed action to call, and whether to take a transition you exposed as a tool. Testing has two jobs:
- Confirm that the fixed parts run in the order you intended.
- Find the places where the model's judgement drifts from what you wanted.
Build a set of representative conversations
Write your test conversations before you change the script. Cover three kinds of case:
| Kind of case | Example | What it checks |
|---|---|---|
| Expected path | Customer gives an order number and asks for status | The normal sequence runs in order |
| Missing state | Customer asks for status without an order number | The condition on the variable fires and the agent asks for it |
| Off-script request | Customer asks about something outside the topic | Routing or a refusal works, and no unavailable action is called |
Keep these cases and re-run all of them after every edit. A change that fixes one conversation can break another.
The refinement loop
While you inspect a run, check these things in order:
- Which topic was active at each turn.
- The value of each variable after each turn.
- Which actions were available and which one was called.
- Whether the reply matches the instruction text that was actually sent.
Choosing the right fix
Most fixes follow one rule. If a step must happen every time, do not repair it with stronger wording. Put it in logic.
Suppose the agent sometimes answers about an order before it has an order number. Adding "ALWAYS ask for the order number" to the prompt makes the failure rarer but does not remove it. The fix is a condition, as in this illustrative sketch. Check exact keys against the current Agent Script documentation.
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
if @variables.order_id == "":
| Ask the customer for their order number. Do not discuss the order yet.
else:
| Help the customer with order {[email protected]_id}.Now the model never sees the "help with the order" text while order_id is empty. The check no longer depends on the model remembering anything.
The opposite case also happens. If the logic is correct but the reply sounds curt, the problem is wording. Edit the prompt text and leave the logic alone.
Common problems
- A variable never updates. The run shows the same value every turn. Check that the action or step meant to set it actually ran, and that the variable is declared as mutable.
- The agent routes to the wrong topic. Read the
descriptionof each topic. The model chooses transitions partly from those descriptions, so overlapping descriptions cause misroutes. - An action is called too early. Limit its availability with a condition instead of telling the model to wait.
- A fix works once and fails later. Model output varies. Run an important case several times before you trust a change.
Know when to stop
Leave a decision to the model when a range of answers is acceptable, such as tone or how to explain a status. Fix it in logic when a wrong answer has a cost, such as skipping an identity check. Stop refining when your whole set of conversations passes and each remaining failure is a wording preference, not a broken rule.
Exercise
Your test shows the agent sometimes calls a refund action before the customer's identity is verified, even though the prompt says to verify first. What should you change, and why?
Show answer
Replace the prompt wording with logic. Make the refund action available only when a variable such as a verified flag is true, and set that flag only after the verification step succeeds. The model then cannot choose the action early, which wording alone cannot guarantee. After the change, re-run the full set of test conversations, including the unverified case.