Sub topic 8
Instructions: mixing prompt text with logic
This chapter assumes you have read the earlier chapters on the structure of an Agent Script file, variables, and topics. You should be comfortable with if/else, with indentation-based syntax, and with the idea that a topic is a scoped unit of agent behavior.
The examples are illustrative sketches of the syntax. Check exact keys against the current Agent Script documentation before you rely on them.
What an instructions block is for
Every topic needs to tell the model what to do. In a purely prompt-driven agent, you write one block of text and hope the model follows it. Agent Script lets you split that text into two layers:
- Prompt text is natural language that the model reads. Examples are "Ask the customer for their order number" or "Be concise and polite."
- Procedural logic is ordinary script code that runs outside the model. Examples are
ifconditions that test variables.
An instructions block combines both. The script runs the logic first, and the logic decides which prompt lines are included. The model then receives only the resulting text. It never sees the if statement and cannot skip it.
This is the main way Agent Script makes behavior repeatable. The decision "do we already have an order number?" is made by code comparing a variable to a value. It is not made by the model remembering a rule from a long prompt.
Two forms: static and procedural
There are two ways to write instructions.
A static block is plain prompt text. Everything in it goes to the model on every turn. Use it when the guidance never changes.
A procedural block starts with an arrow (->) after the key. Inside it, indented lines can contain logic, and lines that begin with a pipe (|) are prompt text. The arrow tells the script to evaluate the block as logic rather than send it verbatim.
# Illustrative sketch: a static block and a procedural block.
topic faq:
description: "Answer general store questions"
reasoning:
instructions: |
Answer questions about store hours and returns.
Keep answers under three sentences.
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 the faq topic, the model always sees the same two sentences. In order_status, the model sees one of two different sentences depending on the value of order_id.
How the pipe character works
The | marks a line as text to emit. Text after the pipe is sent to the model. Lines without a pipe are logic that the script runs.
You can put several pipe lines in a row. They are joined into one prompt in order, so you can build the final instruction from several pieces.
# Illustrative sketch: shared text plus conditional text.
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
| You help customers with questions about their orders.
| Do not discuss topics unrelated to orders.
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 first two lines are always included. The last line depends on state. Putting the stable text first and the conditional text last keeps the prompt predictable and easy to read.
Inserting variable values
The expression {[email protected]_id} is a template expression. When the script builds the prompt, it replaces the expression with the current value of the variable. If order_id holds "A-1042", the model reads "Help the customer with order A-1042."
This is how state flows into the prompt. You do not need to ask the model to remember the order number. The script reads it from the variable and writes it into the text on every turn.
Some points to keep in mind:
- The expression is evaluated when the instructions are resolved, not when you save the script. A value changed earlier in the conversation shows up the next time the block runs.
- Only put values into the prompt that the model needs. A variable that holds internal state, such as a verification flag, is better used in a condition than printed in the text.
- Treat values that came from the customer as untrusted text. A customer can type anything into a field that you later insert into a prompt.
Resolution order
The script resolves the block top to bottom on each turn. The flow below shows what happens.
The important point is that steps B to E are fixed code. Only step G involves model judgement. Everything the model is allowed to see has already been decided by the time it starts.
Writing good prompt lines
Logic gives you control over which lines the model sees. The lines themselves still need to be good prompts. A few guidelines:
- Write one instruction per branch. If a branch tells the model to do three things, the model may do two. Split the work across branches or steps instead.
- Say what to do, not only what to avoid. "Ask for the order number" is clearer than "Do not proceed without an order number."
- Keep branches mutually exclusive. Use
elseso the model never sees two contradictory lines at once. - Put rules the model must always follow outside conditionals. Shared lines at the top of the block apply in every state.
Branching on more than one condition
Conditions can test more than one variable, so you can build a required sequence from a few checks. Chapter details on conditions follow later. This is a short preview of how they combine with instructions.
# Illustrative sketch: an ordered set of checks inside instructions.
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
if @variables.order_id == "":
| Ask the customer for their order number. Do nothing else yet.
else:
if @variables.verified == False:
| Ask the customer to confirm the email address on the order.
else:
| Answer the customer's question about order {[email protected]_id}.The three states are handled in a fixed order. The model cannot skip to the final answer, because the final prompt line is not present until both variables are in the right state. Something else must set verified. For example, an action can set it after a lookup succeeds. Actions are covered in a later chapter.
Common errors
These problems come up often when you first mix text and logic.
| Symptom | Likely cause | Fix | Column | Column |
|---|---|---|---|---|
| The model quotes your `if` statement or the word "else" | The block was written as static text, with no `->` arrow | Add `->` after `instructions:` so the block is evaluated as logic | ||
| A line of prompt text is missing from the prompt | The line lacks a leading `\ | ` so the script treated it as logic | Start every line of prompt text with `\ | ` |
| The prompt shows `{[email protected]_id}` literally | The expression sits in a static block, or it is mistyped | Use a procedural block and check the variable name | ||
| The model sees no instruction at all | No branch matched, for example a missing `else` | Add an `else` branch that covers the remaining states | ||
| The script fails to load or parses oddly | Inconsistent indentation inside the block | Use the same indent width and align `if` and `else` | ||
| The model ignores a rule you wrote | The rule only appears as a hint, and nothing in the script enforces it | Move the check into a condition, or restrict the actions available |
Limits of instructions
Instructions control what the model reads. They do not control what the model does with it. A model that reads "Ask for the order number" will usually ask, but it is still a language model. For steps that must happen, combine instructions with other tools:
- Use a condition so the next step's text is not shown until the earlier step is done.
- Use action availability to hide actions the model should not call yet.
- Use a transition to move the conversation to a different topic with different instructions.
Later chapters cover each of these. A useful rule is to use instructions to guide the model's wording and focus, and use conditions, availability and transitions to enforce the order of events.
Summary
- An instructions block mixes prompt text with logic. The script runs the logic and sends only the resulting text to the model.
- A static block, written with
|, sends the same text every time. A procedural block, written with->, can branch on state. - Lines that begin with
|are prompt text. Other lines are logic. {[email protected]}inserts a variable's current value into the prompt.- Instructions shape what the model reads. To enforce an order of steps, combine them with conditions, action availability and transitions.
Exercise
A topic called refund has this procedural block. The variable order_id is an empty string. What text does the model receive, and what would you change so the model receives a sensible instruction when order_id is set?
instructions: ->
| You handle refund requests.
if @variables.order_id == "":
| Ask for the order number.
Show answer
The model receives two lines: "You handle refund requests." and "Ask for the order number." When order_id is set, the condition is false and there is no else branch, so the model receives only "You handle refund requests." and has no guidance about the order. Add an else: branch with a pipe line such as "| Help the customer request a refund for order {[email protected]_id}." so every state produces a clear instruction.
Exercise
Why is the check "has the customer given an order number?" more reliable as an if condition on a variable than as a sentence in a long static prompt?
Show answer
The condition is evaluated by the script every turn. It compares the variable to a value and picks the prompt text, so the result is the same each time. A sentence in a static prompt is only a request. The model might follow it, forget it, or apply it inconsistently, particularly in long conversations.