Sub topic 6
Topics: scoping what the agent does
This chapter assumes you have read the earlier chapters on script structure and variables. You should be comfortable with indentation-based syntax and with the idea that an agent is a model plus instructions plus actions.
What a topic is
A topic is a named block that defines one area of the agent's work. It holds a description, the instructions for that area, and the actions the model may use there. The agent is always inside exactly one topic at a time. That topic decides which prompt text the model sees and which actions it can call.
Topics keep the model's job small. A single large prompt that covers orders, returns and billing forces the model to sort out which rules apply. A topic for each area lets the script load only the rules that matter for the current request.
The shape of a topic
The examples in this tutorial are illustrative sketches. 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 before doing anything else.
else:
| Help the customer with order {[email protected]_id}.The block has three parts:
- Name.
order_statusis the identifier other parts of the script use to refer to the topic, for example@topic.order_status. - Description. A short sentence saying what the topic is for. The model reads it when it chooses between topics, so write it for that reader.
- Reasoning block. The instructions and actions that apply while the agent is in this topic. The
->afterinstructions:marks a procedural block, where ordinaryif/elselogic selects the lines that are sent to the model. Lines starting with|are prompt text.
How topics fit together
The starting topic receives the first request and routes it. After that, the agent stays in the chosen topic until something moves it elsewhere. A transition does that, and a later chapter covers transitions.
Scoping in practice
Scoping means that each topic exposes only what its job needs. Consider two topics in one script:
topic order_status:
description: "Answer questions about an existing order"
topic returns:
description: "Start a return for a delivered order"When the agent is in order_status, the model sees the order-status instructions and actions. It does not see the return rules. That limits the ways the model can go wrong. It cannot start a return from the wrong topic, because that action is not available there.
Common errors
- Overlapping descriptions. If two topics have similar descriptions, the model's routing becomes unreliable. Make each description say what is in scope and, where useful, what is not.
- One topic that does everything. This brings back the prompt-only problem of non-deterministic behaviour. Split by task, not by convenience.
- Too many tiny topics. Every topic is one more routing choice. Merge topics that always run together.
- Assuming the model remembers topic rules. Rules for a topic apply only while the agent is in it. If a rule must hold everywhere, put it in the agent-wide system instructions.
Choosing topic boundaries
A useful test is to ask whether two requests need different instructions or different actions. If they do, they probably belong in different topics. If they share both, keep them together.
| Situation | Suggested choice |
|---|---|
| Requests need different actions | Separate topics |
| Requests need different rules or tone | Separate topics |
| Requests differ only in a detail the model can handle | One topic |
| A step must always happen first | One topic with a condition, not two topics |
Exercise
A script has one topic called support that covers order lookups and starting returns. The model sometimes begins a return when the customer only asked where their package is. Which change addresses this, and why?
Show answer
Split support into order_status and returns, each with its own description, instructions and actions. While the agent is in order_status, the return action is not available, so the model cannot start a return from that topic.