Sub topic 3
Anatomy of an Agent Script file
This chapter assumes you can read indentation-based configuration such as YAML, and that you know what an LLM agent, a tool call, and a conversation turn are. It also assumes you know basic programming ideas: variables, if/else, and function calls. The earlier chapters explained why some agent behaviour should be fixed in advance and what Agent Script is. This chapter shows how a script file is laid out, so that later chapters can go deep on each part.
The file as a set of named blocks
An Agent Script file is a plain text file organised into top-level blocks. Each block has a keyword, sometimes a name, a colon, and an indented body. Indentation defines nesting, as in YAML or Python. Lines starting with # are comments.
A script usually contains these kinds of blocks:
| Block | Purpose | How many |
|---|---|---|
| `system` | Agent-wide instructions and settings | One |
| `variables` | Named values that hold state | One block, many variables |
| `start_agent` | The starting topic, which routes the first request | One |
| `topic` | A scoped area of work with its own instructions and actions | One per area |
Configuration settings, such as the agent's name and description, also sit at the top level. The next chapter covers them. Treat the key names in this chapter as an illustration of the shape and check exact spellings against the current Agent Script documentation, because the language is still evolving.
A minimal skeleton
The sketch below contains every kind of block, with only one line or two in each.
# Illustrative sketch of the overall shape of an Agent Script file.
# Check exact keys against the current Agent Script documentation.
system:
instructions: "You are a support agent for order questions."
variables:
order_id: mutable string = ""
start_agent topic_selector:
description: "Route the customer to the right topic"
topic order_status:
description: "Answer questions about an existing order"Read it from top to bottom:
systemholds instructions that apply to the whole agent, whichever topic is active.variablesdeclaresorder_idas a string that the script can change (mutable), with an empty string as its starting value.start_agent topic_selectordefines the topic that receives the customer's first request.topic order_statusdefines one area of work. A real script would have several of these.
Each block has a description. The model reads descriptions to decide what a topic is for, so they are part of the behaviour and not just documentation.
How a conversation moves through the blocks
The blocks run in a predictable order. The diagram shows the path of one conversation.
Three points follow from this flow.
- The first request always enters through the starting topic. It cannot go straight to an arbitrary topic.
- Whichever topic is active, the
systeminstructions and the variables are available to it. This is how agent-wide rules and conversation state reach every topic. - The topic's instructions are resolved before the model sees them. Script logic runs first and decides which prompt text is sent. This is the key difference from a purely prompt-driven agent.
The system block
The system block sets what is true of the agent everywhere: its role, tone, and rules that hold in every topic. Keep it short and general. A line such as "You are a support agent for order questions." belongs here. A rule like "ask for the order number before anything else" does not, because it only matters in one topic and should be enforced by logic, not wording.
A common mistake is putting every rule in the system block. It then behaves like a long system prompt, and you lose the benefit of scoping. The model must weigh all the rules on every turn. Put a rule in the narrowest block where it applies.
The variables block
The variables block declares state. Each entry has a name, a modifier, a type, and a default value:
variables:
order_id: mutable string = ""Declaring a variable gives the script a place to remember something across turns, such as an order number the customer gave earlier. Later in the file you refer to it with the @variables. prefix, for example @variables.order_id. The variables chapter covers modifiers, types, and how values get set. For this chapter, remember two things:
- A variable must be declared before you refer to it.
- The default value is what the script sees before anything has set it. In the example, the empty string means "we do not have an order number yet", and conditions can test for that.
Topics and the starting topic
A topic is a scoped unit of work. Its description tells the model when the topic applies. Its body holds the instructions and actions for that area. Because each topic has its own instructions, the agent carries only the rules relevant to what the customer is doing.
start_agent looks like a topic and has the same shape, but it plays a different role: it is where routing begins. The routing chapter covers it in depth. Here is a sketch that gives it a transition to the topic from the skeleton:
# Illustrative sketch: a transition the model may choose, and a variable the script reads.
start_agent topic_selector:
description: "Route the customer to the right topic"
reasoning:
instructions: ->
| Decide whether the customer is asking about an order.
actions:
go_to_order_status: @utils.transition to @topic.order_statusSeveral things appear here that you will see throughout scripts:
reasoning:groups what drives the model's behaviour inside the topic.instructions: ->introduces a procedural block. The arrow means the body is evaluated as logic, and lines beginning with|are natural-language text that goes to the model.actions:lists named capabilities.go_to_order_statusis a transition, written with@utils.transition to @topic.order_status. The model decides whether to take it, but the destination is fixed by the script.@topic.order_statusis a reference to a topic block elsewhere in the file. The name must match a defined topic exactly.
Fixed logic inside a topic
Inside a topic, a procedural instructions block can use ordinary if/else to choose which text the model receives:
# Illustrative sketch: fixed logic decides which prompt text the model sees.
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}.The check on order_id runs in the script, not in the model. When the variable is empty, the model only ever sees the "ask for the order number" line. The model cannot forget to ask, because it never receives the other branch. The expression {[email protected]_id} inserts the variable's current value into the text.
Compare this with a prompt-only version, "Always ask for the order number first." That relies on the model following the instruction on every turn. The scripted version removes that dependency.
Where each concern belongs
When you add a rule to a script, ask which block should own it.
- Applies to the whole agent, in every topic:
system. - A fact the agent must remember between turns:
variables. - Applies only to one area of work: that
topic. - Must happen in a fixed order or under a fixed condition: procedural logic inside the topic's instructions.
- Depends on the customer's intent, with no fixed answer: leave it to the model, in prompt text.
Common errors
- Indentation mistakes. A key at the wrong depth ends up in the wrong block or fails to parse. Use consistent spaces, not tabs mixed with spaces.
- Undeclared variables. Referring to
@variables.order_idwithout declaring it is an error. - Dangling topic references. A transition to
@topic.order_statusfails if no topic of that name exists. Watch for typos and renamed topics. - Logic written as prose. A line like "if the order number is missing, ask for it" inside a
|text line is only a suggestion to the model. Real conditions go in the procedural part of the block. - Vague descriptions. Two topics with overlapping descriptions make routing unreliable, because the model chooses between them by reading those descriptions.
- Oversized system block. Rules that apply to one topic placed in
systemadd noise to every other topic.
Exercise
Exercise
A script has a rule that the agent must have an order number before it answers any question about an order. Where would you declare the order number, and where would you enforce the rule? Name the blocks and say why the rule should not live in the system block.
Show answer
Declare the order number in the variables block, for example orderid: mutable string = "". Enforce the rule in the orderstatus topic, in a procedural instructions block that uses if @variables.order_id == "" to send only the "ask for the order number" text to the model. The system block should not hold the rule because it applies to every topic and is only a prompt the model may or may not follow. Logic inside the topic makes the check run in the script on every turn.
Exercise
A transition to @topic.refund_status fails when the script loads. List two likely causes found by looking at the file's structure.
Show answer
There may be no topic block named refund_status, for example because of a typo or because the topic was renamed. The topic block may also be indented incorrectly, so it is not a top-level block and the reference cannot find it.