Sub topic 10
Actions: calling backend capabilities
An agent that only talks cannot look up an order, update a record, or issue a refund. Actions are how an Agent Script agent reaches backend capabilities. This chapter covers how actions are declared, how they run, and how you control whether the model or the script decides to run them.
Prerequisites
This chapter assumes you have read the earlier chapters on topics, variables, instructions and conditions. You should be comfortable with these ideas:
- A topic scopes what the agent does, and has a
reasoningblock withinstructions. - Variables hold state across turns and can be read with
@variables.name. - A procedural
instructions: ->block usesif/elseto decide which|prompt lines the model sees. - Tools or functions that an LLM can call, and conversation turns.
The code in this tutorial is an illustrative sketch of the language. Check exact keys and syntax against the current Agent Script documentation before you use them in a real agent.
What an action is
An action is a named capability that lives outside the model. It might be a flow, an Apex class, a prompt template, or an API call. The script does not contain the backend logic. It contains a declaration that says what the action is called, what inputs it takes, and what outputs it returns.
Separating the declaration from the implementation gives you three benefits:
- The backend logic is ordinary code that you can test without a model.
- The result is the same every time for the same inputs, because a backend system produces it and the model does not.
- The script can state exactly when the action runs.
The model is good at language, and backends are good at facts. If the agent must tell a customer the status of order 1042, that status should come from an action and not from the model's guess.
Two ways an action runs
An action can be started in two different ways. This is the main idea of the chapter.
| Way to run | Who decides | Typical use |
|---|---|---|
| The script runs it directly | Fixed logic in the script | Steps that must always happen, such as a lookup before any answer |
| The model chooses it as a tool | The model, during reasoning | Steps that depend on what the customer said |
When the script runs an action, the call is deterministic. When the model chooses an action, the call is probabilistic. The model may call it, skip it, or call it with different inputs than you expected. Choose the mode by asking whether skipping the step would cause harm or a wrong answer.
Declaring an action
You declare an action inside a topic, with a description, inputs and outputs. The sketch below defines a lookup that takes an order number and returns a status.
# Illustrative sketch: declaring an action with inputs and outputs.
# Check exact keys against the current Agent Script documentation.
topic order_status:
description: "Answer questions about an existing order"
actions:
get_order_status:
description: "Look up the current status of an order"
inputs:
order_id: string
outputs:
status: string
delivery_date: string
target: "flow://Get_Order_Status"Each part has a job:
descriptionis text the model reads when it decides whether to use the action. Write it as a clear statement of what the action does and when it applies.inputslists the values the action needs. These are the values the backend requires, so the names and types must match what the backend expects.outputslists the values that come back. The script and the model can use them afterwards.targetpoints to the backend implementation. Its format depends on the kind of backend.
A vague description is a common source of bugs. If two actions have descriptions such as "Get information", the model cannot tell them apart and may choose the wrong one.
Running an action from the script
To make a step certain, run the action from procedural logic and not from a prompt that asks the model to do it. The sketch below runs the lookup whenever an order number is known and the status is not yet fetched.
# Illustrative sketch: the script runs the action, then tells the model the result.
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:
run @actions.get_order_status
with order_id = @variables.order_id
set @variables.order_state = @outputs.status
| The order status is {[email protected]_state}.
| Tell the customer this status in one or two sentences.Read this block in order:
- The
ifchecks state. If there is no order number, the model only sees the line that asks for one. - Otherwise the script runs
get_order_statuswith the stored order number. The model is not involved in this decision. - The output is stored in a variable,
order_state. The variable must be declared asmutablein thevariablesblock, or the script cannot change it. - The prompt lines come last. By the time the model reads them, the status is already in the text.
The model never has to remember to look up the order. It also cannot invent a status, because the correct value is in front of it.
Offering an action to the model
Some steps depend on conversation content. A customer might ask to cancel an order or might only ask about delivery. For those cases, expose the action as a tool in the reasoning actions block, as you saw with the transition in the routing chapter.
# Illustrative sketch: actions the model may choose during reasoning.
topic order_status:
description: "Answer questions about an existing order"
reasoning:
instructions: ->
| Answer the customer's question about order {[email protected]_id}.
| If they ask for the delivery date, look it up.
actions:
check_status: @actions.get_order_status
with order_id = @variables.order_idHere the script fills in order_id from a variable. The model decides only whether to call the action. This is a useful split. The model should not guess an identifier that the script already knows, so the script binds it. The model keeps the judgement about timing.
The model can also fill an input itself when the value comes from the conversation, for example a free-text reason for a return. Bind inputs in the script wherever you already hold the value.
Using results afterwards
An action result is only useful if later logic can read it. There are two common ways to use it.
- Store it in a variable, then branch on that variable in a later condition or in the next turn.
- Place it in the prompt text with
{[email protected]}so the model can phrase it for the customer.
A branch on the result is how you build a required sequence. For example, you can route to a different topic only if the status is delivered.
# Illustrative sketch: branching on an action result.
topic order_status:
reasoning:
instructions: ->
run @actions.get_order_status
with order_id = @variables.order_id
set @variables.order_state = @outputs.status
if @variables.order_state == "delivered":
| The order has arrived. Ask whether the customer wants to start a return.
else:
| The order has not arrived yet. Do not offer a return.The refund rule here does not depend on the model remembering the status. The else branch never shows the model a prompt that offers a return.
Edge cases and common errors
Actions fail in ways that prompts do not. Plan for these cases.
- The backend returns an error or an empty value. Branch on the output before you use it. If
order_stateis empty, tell the model to apologise and offer to try again, and do not let it describe a status. - The input is missing. Run an action only inside a condition that checks that its inputs are set, as the
order_id == ""check does above. - The action runs on every turn. Procedural instructions are evaluated each time the topic reasons. An unguarded
runrepeats the backend call each turn. Guard it with a variable, such as a flag that records the lookup is done, if the call is slow or has side effects. - A side-effecting action is offered as a tool. An action that changes data, such as a cancellation, can be called by the model at the wrong moment. Run it from the script after a confirmation condition, or limit when it is available. The next chapters cover availability and transitions.
- Type mismatches. An input declared as
stringshould receive a string. Convert values before you bind them if the backend is strict. - Stale variables. A variable set from an earlier result keeps its value until you change it. If the customer switches to a different order, clear
order_state, or the script may reuse the old status.
Choosing between the two modes
Use these criteria when you decide who runs an action.
| Question | If yes | If no |
|---|---|---|
| Would skipping the step give a wrong or unsafe answer? | Script runs it | Model may choose it |
| Does the action change data or cost money? | Script runs it after a condition | Either mode can work |
| Does the need depend on the customer's wording? | Offer as a tool | Script runs it |
| Is the input already stored in a variable? | Bind it in the script | Let the model supply it |
Most agents mix both. A typical topic runs one lookup from the script, then offers two or three optional actions as tools.
Summary
- An action is a declared backend capability with a description, inputs, outputs and a target.
- The script can run an action directly, which is deterministic, or offer it as a tool, which leaves the call to the model.
- Store outputs in mutable variables so that conditions and prompt text can use them.
- Guard runs against missing inputs, repeated calls and empty results.
Exercise
A topic must always check whether a customer's account is verified before it discusses billing. Should the verification action be run by the script or offered to the model as a tool? Give one reason and say where you would store the result.
Show answer
The script should run it. A required step must not depend on the model choosing to call it, and a skipped check could expose billing details to an unverified customer. Store the output in a mutable variable such as is_verified, then use an if condition on that variable so the billing prompt lines are only shown when it is true.
Exercise
Your script contains an unguarded run of a slow lookup action at the top of a procedural instructions block, and customers report long delays on every message. What is the cause, and what is one fix?
Show answer
Procedural instructions are evaluated each time the topic reasons, so the lookup runs again on every turn. Guard the run with a variable, for example a lookup_done flag that the script sets after the first call, and run the action only when the flag is false.