Sub topic 11
Action Inputs, Variables, and Conversation Context
This chapter assumes you know what topics and actions are, and that you have seen how the reasoning loop picks an action. You should be comfortable reading a simple Apex class or Flow. Here you will look at what the agent hands to an action, where those values come from, and how the conversation carries them from one turn to the next.
What an action input is
An action input is a named, typed value that an action needs before it can run. The input comes from the action's definition:
- In a Flow, each input variable (marked "Available for input") becomes an action input.
- In Apex, each field with
@InvocableVariableon the request class becomes an input. - In a prompt template, each template input becomes an action input.
The agent does not run code to fill these in. The language model reads the input's name, label, and description, then looks at the conversation and proposes a value. That makes input descriptions part of your prompt, just like topic instructions.
The spine's Order Support example shows this. The input is declared like this:
@InvocableVariable(label='Order Number' description='The order number the customer provided' required=true)
public String orderNumber;When the customer writes "Where is order 00001234?", the model matches the phrase to the Order Number input and passes 00001234. If the customer never gave a number, the input is still empty. The topic instruction "Ask for the order number if the customer has not given one" then tells the agent to ask a question instead of calling the action.
Where input values come from
An input value can have several sources. Knowing which one applies explains most surprising behavior.
| Source | How it works | Typical use |
|---|---|---|
| Current user message | The model extracts the value from what the user just said | Order number, date, product name |
| Earlier turns | The model reuses a value from the conversation history | "Check that order again" |
| Earlier action output | A value returned by a previous action is passed to the next one | Record Id from a lookup, then an update |
| Variables | A stored value, such as one linked to the session or record | Contact Id, case Id, channel details |
| Asking the user | The agent asks a follow-up question to collect a missing value | Missing required input |
Which of these applies depends on how you configure each input in the agent builder. Typical settings let you mark an input as required, let the agent collect it from the user, or supply it from a variable. Exact labels vary by release, so check the action's input settings in your org.
Variables
A variable is a named slot that holds a value for the length of a conversation. Two kinds matter in practice:
- Context variables are filled by the platform when the session starts. Examples are the messaging session, the end user's Id, or the record page the agent was opened from. You read them, but you do not ask the user for them.
- Custom variables are ones you define. An action's output can be stored in one, and later actions or instructions can read it.
Variables solve two problems that conversation text alone does not.
- Reliability. A model reading the history can misread or drop an Id. A variable holds the exact value.
- Trust. Values such as the signed-in user's Id should never come from what the user types. Pulling them from a context variable keeps the user from supplying someone else's Id.
Conversation context
Conversation context is the text history of the current session: the user's messages, the agent's replies, and the results of actions already run. On every turn, the reasoning engine reads this history along with the active topic's instructions and action descriptions.
Context has three practical effects:
- Follow-up questions work. "What about the other one?" can resolve to a value from two turns ago.
- Topic classification sees history. A short message such as "yes" is classified using the surrounding turns, not by itself.
- Context has limits. A long conversation may push old details out of what the model uses, and a model can confuse two similar values, such as two order numbers.
Do not treat conversation context as storage. If a value must survive a long session, put it in a variable.
The flow of a value
The loop on the right is why multi-step requests work. A lookup action returns an Id, the Id is stored, and an update action takes it as its input on the next step.
Writing inputs the agent can fill correctly
The following Apex action extends the spine's example. It adds an optional email input for verification and returns the order Id so a follow-up action can use it.
public with sharing class OrderLookupAction {
public class Request {
@InvocableVariable(
label='Order Number'
description='The order number exactly as the customer wrote it, for example 00001234. Never guess. Ask the customer if it is missing.'
required=true
)
public String orderNumber;
@InvocableVariable(
label='Customer Email'
description='Email address the customer gave to verify who they are. Leave empty if the customer has not provided one.'
required=false
)
public String customerEmail;
}
public class Response {
@InvocableVariable(label='Status' description='Current status of the order, or Not Found')
public String status;
@InvocableVariable(label='Order Id' description='Salesforce record Id of the order, for use by follow-up actions')
public Id orderId;
}
@InvocableMethod(
label='Look Up Order'
description='Finds an order by order number and returns its status and record Id. Optionally checks the customer email.'
)
public static List<Response> lookup(List<Request> requests) {
Set<String> numbers = new Set<String>();
for (Request r : requests) {
if (String.isNotBlank(r.orderNumber)) {
numbers.add(r.orderNumber.trim());
}
}
Map<String, Order> byNumber = new Map<String, Order>();
for (Order o : [
SELECT Id, OrderNumber, Status, BillToContact.Email
FROM Order
WHERE OrderNumber IN :numbers
]) {
byNumber.put(o.OrderNumber, o);
}
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
res.status = 'Not Found';
Order o = String.isBlank(r.orderNumber) ? null : byNumber.get(r.orderNumber.trim());
if (o != null) {
Boolean emailOk = String.isBlank(r.customerEmail)
|| (o.BillToContact?.Email != null && o.BillToContact.Email.equalsIgnoreCase(r.customerEmail.trim()));
if (emailOk) {
res.status = o.Status;
res.orderId = o.Id;
}
}
results.add(res);
}
return results;
}
}Three design choices in this class are worth copying:
- Descriptions give an example and a rule. "For example 00001234" shows the format, and "Never guess" discourages invented values.
- Optional means optional. The email input says when to leave it empty, so the agent does not interrogate the customer for it.
- The action validates its own input. The class handles blank and unknown order numbers and returns
Not Found. The model's proposed value is a suggestion, and your code is the last check.
Common errors
- Vague input names. An input labeled
IdorValuegives the model nothing to match. UseOrder Number,Case Id,Start Date. - Required input the user cannot know. If an input is required but only the system knows it, the agent will ask the customer for something they cannot answer. Supply it from a variable or look it up in an earlier action.
- Hallucinated values. With a weak description, the model may fill an input with a plausible guess. Add "Never guess" and make the action return a clear not-found result.
- Format mismatches. A date input given "next Friday" may fail. State the expected format in the description, or accept free text and parse it in the action.
- Stale variables. A stored order Id stays in place after the customer switches to a different order. Instructions should say when to refresh it, such as "If the customer mentions a new order number, look it up again."
- Sensitive outputs shown by default. An action may return fields the customer should not see. Return only what the agent needs, and review the output settings so internal fields are hidden from the reply.
- Over-long actions. Many optional inputs raise the chance of wrong values. Split one large action into two focused ones.
Checking your work
Use the testing tools covered in a later chapter to confirm three things for each action: which inputs the agent proposed, which source each value came from, and what the action returned. A good test set includes a message with the value, a message without it, a follow-up that relies on history, and a message that contradicts an earlier value.
Exercise
A customer says "Check order 00001234", gets a status, then says "Actually, check 00005678 instead." The agent reports the status of the first order again. Name two likely causes and one fix for each.
Show answer
Cause 1: the order number is held in a variable that was never refreshed, so the action receives the stored value. Fix: add an instruction to look up the order again whenever the customer gives a new order number, and make the action input come from the latest message. Cause 2: the input description does not say to use the most recent value, so the model reuses the earlier one from history. Fix: change the description to "The order number the customer most recently asked about" and test with a message that changes the order number.
Exercise
Why should an action take the signed-in user's Id from a context variable and not from an input the model fills from chat text?
Show answer
Anything typed in chat can be faked. A context variable is set by the platform at session start, so the user cannot replace it with another person's Id.