Sub topic 8
Instructions: mixing prompt text with logic
This chapter covers how a topic's instructions block combines fixed logic with natural-language prompt text. It assumes you can read indentation-based syntax, understand if/else and variables, and have seen the script structure from earlier chapters. The code samples are sketches. Check exact keys against the current Agent Script documentation.
Two kinds of lines
An instructions block holds two kinds of content:
- Prompt text is natural language that the model reads. In a procedural block, each prompt line starts with a pipe character (
|). - Logic is ordinary script code, such as
ifandelse. The script runs it before the model sees anything.
The logic decides which prompt lines are assembled into the final prompt. The model never sees the logic itself, only the text that was selected.
Plain and procedural instructions
A plain instructions block is only prompt text. The model gets all of it every time, and it must decide for itself which parts apply. That is the probabilistic behaviour the first chapter warned about.
A procedural block is marked with an arrow (->) after instructions:. Inside it you can mix logic with | lines.
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}.Reading the example
Follow what happens on each turn:
- The script checks
@variables.order_id. - If it is empty, only the "ask for the order number" line goes into the prompt.
- If it holds a value, only the "help with order" line goes in. The
{[email protected]_id}expression is replaced with the stored value first. - The model receives that one line and writes its reply.
The check does not depend on the model remembering to ask. The script enforces it by never showing the model the other branch.
Why mix the two
Each part does what it is good at.
| Concern | Best handled by |
|---|---|
| Is a required value present? | Logic |
| Which step comes next? | Logic |
| Tone and wording of the reply | Prompt text |
| Understanding a free-form question | Model |
A common rule of thumb is to use logic for anything that must happen the same way every time, and prompt text for anything that benefits from judgement.
Common errors
- Putting a rule only in prose. A line such as "Never continue without an order number" is a request, and the model may ignore it. Put the check in an
ifinstead. - Forgetting the
->arrow. Without it the block is plain text, and youriflines may be read as prompt content instead of logic. - Leaving out the
|on a prompt line. The script then tries to read the line as logic. - Reading a variable before anything sets it. An unset variable may hold its default, such as an empty string. Declare sensible defaults, as covered in the variables chapter.
- Writing so many branches that the block is hard to follow. Keep each branch to a few short prompt lines.
Next step
Here the if tests one variable with a simple comparison. The next chapter covers conditions in more detail, including how they combine with state set earlier in the conversation.
Exercise
In the order_status example, the customer has not yet given an order number. Which prompt line does the model receive, and why does the script not need to trust the model to ask for the number?
Show answer
The model receives only "Ask the customer for their order number before doing anything else." The if condition finds order_id empty, so the script selects that line and leaves the other branch out. The model never sees the alternative, so the check does not rely on the model remembering the rule.
Exercise
Add a third case to the example so that a customer with an order number is told to confirm it when a variable order_confirmed is false. What construct would you use, and where would the new prompt line go?
Show answer
Use an else if (elif, depending on the current syntax) inside the same procedural block, testing @variables.order_confirmed, with a | prompt line such as "Ask the customer to confirm order {[email protected]_id}." It goes between the empty-check branch and the final else. Check the exact keyword in the Agent Script documentation.