Sub topic 6
Topics: scoping what the agent does
This chapter assumes you have read the previous chapters on the anatomy of an Agent Script file and on variables. You should be comfortable with indentation-based syntax, and you should know that an agent works in conversation turns and calls actions (backend capabilities such as a lookup or an update).
The examples are illustrative sketches of the shape of the language. Check exact keys against the current Agent Script documentation before you use them.
What a topic is
A topic is a named block in an Agent Script file that defines one job the agent can do. Examples are answering order questions, handling returns, or collecting feedback. Each topic bundles three things:
- A description, which says what the topic is for.
- Instructions, which are the prompt text and logic that shape what the model sees while the topic is active.
- Actions, which are the backend capabilities the model may use while the topic is active.
The agent is in one topic at a time. The topic that is active decides what the model is told, what it can do, and what it should ignore. This is what "scoping" means: each topic narrows the agent's context to the part of the job at hand.
Anatomy of a topic
The sketch below shows a topic with a description and a reasoning block that holds instructions and one action.
# Illustrative sketch of a topic. Check exact keys against the current documentation.
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}.
actions:
go_to_returns: @utils.transition to @topic.returnsRead it from the top:
topic order_status:declares the topic and gives it a name. Other parts of the script refer to the topic by this name, for example@topic.order_status.description:is a short natural-language statement of the topic's purpose.reasoning:holds the parts that guide the model's turn. Theinstructions: ->line starts a procedural block. The->means the lines under it are logic that runs first and produces the prompt text.- Lines that begin with
|are prompt text. Theifandelseare ordinary logic that picks which prompt lines the model receives. actions:lists what the model may choose to do during this topic. Here, one entry is a transition to another topic.
The point to notice is the division of labour. The script decides which instructions appear. The model reads those instructions and decides how to talk to the customer and which listed action to use.
Why scoping helps
A single agent with one large prompt has to carry every rule and every tool at once. Topics split that load in three ways.
- Smaller prompts. Only the active topic's instructions are in play. The model is not weighed down by rules for situations that are not happening.
- Smaller action surface. An action not listed in the active topic is not one the model can pick. A refund action that lives only in the returns topic cannot be called from the order status topic.
- Testable units. You can reason about, and test, one topic at a time. A bug in returns logic stays in the returns topic.
The last two points matter most for deterministic behaviour. You cannot make the model follow a rule reliably by asking nicely in a long prompt. You can make some mistakes impossible by keeping the relevant capability out of reach until the script says it is time.
The description is a routing signal
The description does more than document the topic. The starting topic uses descriptions when the model decides where to send the first request. A vague description gives the model a vague basis for routing.
Compare:
| Description | Problem or benefit |
|---|---|
| `"Orders"` | Too broad. Overlaps with returns, shipping, and billing. |
| `"Help with stuff"` | Matches nearly any request, so routing is unreliable. |
| `"Answer questions about an existing order, such as its status or delivery date"` | States the job and the typical requests. Easy to tell apart from returns. |
| `"Start a return or exchange for a delivered order"` | Distinct from order status because it names a different outcome. |
A good description names the user's goal, uses the words customers use, and does not overlap with its neighbours. Descriptions that overlap produce inconsistent routing because the model has to guess between two plausible matches.
Topics and shared state
Topics do not each have their own memory. Variables are declared at the script level, so every topic can read and write them. This is how information survives a move from one topic to the next.
# Illustrative sketch: one variable shared by two topics.
variables:
order_id: mutable string = ""
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}.
topic returns:
description: "Start a return or exchange for a delivered order"
reasoning:
instructions: ->
if @variables.order_id == "":
| Ask the customer for their order number before starting a return.
else:
| Help the customer return items from order {[email protected]_id}.Both topics check the same order_id. If the customer gave the number during order status, the returns topic does not ask again. If you instead relied on the model remembering the number, a long conversation or a topic change could lose it. Putting it in a variable makes the state explicit.
Notice that the two topics repeat a similar check. That is acceptable for a few topics. If the same check appears in many topics, it is a sign to route through one topic that collects the order number first. The later chapter on design patterns covers that.
The shape of the whole agent
Topics sit under a starting topic that routes the first request. From there the agent can move between topics through transitions, which a later chapter covers in detail.
The starting topic is not special in its structure. It is a topic whose job is to choose the next one. The destination of each transition is fixed in the script, while the model decides whether to take it.
Choosing topic boundaries
There is no single correct number of topics. Use these criteria.
- Split when the rules differ. If two jobs need different instructions, different actions, or different required steps, they are separate topics.
- Split when the actions differ. If one job needs an action that the other must never call, they should be separate so the second cannot reach it.
- Merge when the jobs share nearly everything. Two topics with almost identical instructions and the same actions add routing risk without adding control.
- Keep each topic describable in one sentence. If the description needs "and also", consider two topics.
Too few topics gives you the original problem: one large prompt with many tools. Too many topics gives you a routing problem, because the starting topic must pick correctly between many similar descriptions, and every extra transition is another path to test.
Common errors
- Overlapping descriptions. Two topics both claim "order questions". The model routes unpredictably. Rewrite the descriptions around distinct outcomes.
- Rules only in the description. The description guides routing and is not the place for step-by-step rules. Put required behaviour in the instructions, and put required order in conditions.
- Relying on the model to remember values. If a later topic needs the order number, store it in a variable. Do not assume it will recall it.
- Listing every action in every topic. This removes the benefit of scoping. List only the actions the topic needs.
- No path out of a topic. A customer who asks something outside the active topic needs a way to leave. Without a transition, the agent may answer poorly or stay stuck. Plan at least one exit per topic, such as a transition back to the starting topic.
- Unchecked key names. The sketches here show the shape of the language. Verify keys and syntax against the current documentation, because the language may change.
Exercises
Exercise
A script has two topics, order_status and returns. Both have the description "Help with customer orders". Customers asking for a return are sometimes sent to order_status. What is the likely cause, and how do you fix it?
Show answer
The descriptions overlap, so the starting topic has no clear basis to tell them apart and routing is inconsistent. Rewrite each description around its distinct goal, for example "Answer questions about an existing order, such as status or delivery date" and "Start a return or exchange for a delivered order".
Exercise
The returns topic needs the order number the customer already gave in the order status topic. Where should that number be kept, and why not rely on the model to remember it?
Show answer
Keep it in a script-level variable such as order_id, which every topic can read and write. The model's memory of earlier turns can be lost or distorted over a long conversation or after a topic change. A variable holds the value explicitly, and the instructions can check it with an if condition.
Exercise
You want an agent to be unable to issue a refund until it has confirmed the order number. How does scoping topics help with this, even before you add conditions?
Show answer
Put the refund action only in the returns topic, not in the starting topic or order status topic. The model can only choose actions listed in the active topic, so it cannot call the refund from elsewhere. Conditions in the returns topic can then require the order number before the action is available.