Sub topic 14
Design Practices: Distinct Topics, Safe Actions, and Guardrails
This chapter assumes you know the basics of Salesforce objects and fields, have used Setup, and understand what topics, instructions, and actions are in Agentforce Builder. It also assumes you have a general idea of what a large language model does. You have already seen how to write each part. Here you will learn how to design them together so the agent stays predictable when real customers use it.
Why design practices matter
An agent decides what to do by reading text. It reads topic names, classification descriptions, scopes, instructions, and action descriptions. That text is the interface. If two topics read alike, the agent may pick the wrong one. If an action can change data and nothing limits it, the agent may use it at the wrong time.
Good design reduces these risks before you start testing. Three areas matter most:
- Distinct topics: each request should clearly belong to one topic.
- Safe actions: each action should do only what the job needs, and only for users who are allowed to do it.
- Guardrails: instructions and scope should say what the agent must not do.
Keep topics distinct
The agent first picks a topic, using the topic name and classification description. If descriptions overlap, the choice becomes a guess. Overlap is the most common design error in agents with more than a few topics.
Spot overlap
Compare two topics side by side. Ask: could one customer message fit both descriptions?
| Topic A | Topic B | Problem |
|---|---|---|
| Order Help: "Answer questions about orders" | Order Status: "Tell customers where an order is" | Status questions fit both. |
| Returns: "Handle returned items" | Refunds: "Handle money back for items" | A return request usually involves a refund. |
| General Questions: "Anything else" | Any other topic | A catch-all competes with every topic. |
Fix overlap
Use these techniques:
- Split by customer intent, not by object. "Order Status" and "Returns" describe what the customer wants. "Orders" describes a database table.
- Name the exclusions. The classification description in the earlier example says "Do not use for returns, refunds, or new orders." Exclusions point the agent to the neighboring topic.
- Merge topics that always travel together. If Returns and Refunds are never separate in practice, one topic with two ordered steps in its instructions is clearer than two topics.
- Avoid catch-all topics. Handle off-topic requests with a clear fallback, such as an instruction to tell the customer what the agent can help with.
- Keep the number of topics small. Each added topic is one more candidate the agent must tell apart from the others.
Topic name: Returns
Classification description:
Handle requests to send back an item that was already delivered and to start a return. Do not use for checking where an order is, or for questions about payment methods.
Scope:
Your job is to start a return for a delivered order. You cannot issue refunds.Notice how the scope and the exclusions say where the job ends. The refund step belongs to another topic, so the two topics do not compete.
Keep actions safe
An action runs real logic against real data. Treat each one like an API that a user can call by asking in plain language.
Match the action to the job
- Prefer read-only actions when the job is to look something up. The Order Status topic needs a lookup, not an update.
- Separate read and write. Make "Get Order Status" and "Cancel Order" two actions, not one action with a mode flag. Then you can add only the one a topic needs.
- Add only the actions a topic uses. An action that is not in the topic cannot be misused there.
- Return only the fields the agent needs. The agent can repeat any output it receives, so do not return data it should not share.
Enforce permissions in the action
Instructions are guidance, not security. A line such as "Never share details about orders that belong to other customers" helps, but the platform must enforce it too. In Apex, that means running the query in user mode so object and field permissions of the running user apply.
public with sharing class GetOrderStatus {
public class Request {
@InvocableVariable(label='Order Number' description='The order number the customer provided' required=true)
public String orderNumber;
}
public class Response {
@InvocableVariable(label='Status' description='Current status of the order, or Not found')
public String status;
}
@InvocableMethod(label='Get Order Status' description='Looks up the status of an order by its order number')
public static List<Response> getStatus(List<Request> requests) {
List<Response> results = new List<Response>();
for (Request r : requests) {
Response res = new Response();
List<Order> orders = [
SELECT Status FROM Order
WHERE OrderNumber = :r.orderNumber
WITH USER_MODE
LIMIT 1
];
res.status = orders.isEmpty() ? 'Not found' : orders[0].Status;
results.add(res);
}
return results;
}
}Three details make this action safer:
with sharingapplies the sharing rules of the running user.WITH USER_MODEapplies object and field permissions.- The query selects only
Statusand returnsNot foundwhen nothing matches, so a missing record and a hidden record look the same to the agent.
If a user cannot read an order, the query returns nothing and the agent reports "Not found". The platform has done the filtering, so it does not depend on the model following an instruction.
Treat inputs as untrusted
The agent fills action inputs from what the customer says. A customer can type anything. Validate inputs inside the action, using the same care you would give any public endpoint.
- Use typed inputs where possible, such as a date or a picklist value, so wrong values are rejected early.
- Use bind variables in queries, as the example does with
:r.orderNumber. Do not build query strings by joining customer text. - Check required values and return a clear message when they are missing.
Add friction to risky actions
Some actions are hard to undo, such as cancellations, deletions, or sending messages. For these:
- Require a confirmation step in the topic instructions, for example "Ask the customer to confirm the order number before cancelling."
- Limit the action to the record the customer identified, not a list.
- Keep a record of what the action did, so you can review it later.
- Consider whether the action should be offered to the agent at all, or handed to a person instead.
Set guardrails in instructions
Guardrails are instructions that say what to avoid. They work best when they are short, specific, and tied to a step.
Weak: "Be careful with customer data."
Better:
1. Ask the customer for their order number if they have not given one.
2. Use the Get Order Status action with that order number.
3. If the action returns 'Not found', tell the customer you could not find the order and ask them to check the number.
4. Otherwise, state the status in one sentence.
5. Never share details about orders that belong to other customers.The better version is ordered, names the action, handles the failure case, and ends with a testable rule. You can write a test message for each line.
Useful guardrail patterns:
- Refuse and redirect: "If the customer asks for a refund, tell them you cannot issue refunds here and explain how to contact support."
- Limit output: "State the status in one sentence." This prevents long, speculative answers.
- Handle failure: say what to do when an action returns an error or no result.
- Do not guess: "If you do not know, say so." Without this the agent may invent an answer.
How the pieces check each other
Each layer catches mistakes the others miss.
The top two layers are guidance the model interprets. The last layer is enforced by the platform. Design so that a failure in the first two cannot cause harm at the third.
Common errors
- Overlapping topics. Symptom: the agent picks different topics for similar messages. Fix: tighten descriptions and add exclusions.
- One broad action. Symptom: the agent has the power to do more than the topic needs. Fix: split the action and add only what the topic uses.
- Security by instruction. Symptom: a rule exists only in text. Fix: enforce it in the action with sharing and user mode.
- Vague guardrails. Symptom: you cannot write a test for the rule. Fix: rewrite it as a step you can check.
- No failure path. Symptom: the agent improvises when an action returns nothing. Fix: add a step for "Not found" and errors.
- Undescribed inputs and outputs. Symptom: the agent fills a field with the wrong value. Fix: label and describe each variable, as the Apex example does.
A review checklist
Before you release an agent, check each item and test it in Builder with a message that should pass and one that should fail.
- Can any single customer message fit two topic descriptions?
- Does each topic's scope say where its job ends?
- Does each topic contain only the actions it needs?
- Does each action enforce permissions itself?
- Is every input validated, and every query parameterized?
- Do risky actions require confirmation?
- Does every instruction have a step you can test?
- Is there a defined reply for failures and off-topic requests?
When a test fails, change one thing at a time, such as a description, an instruction, or an action, and run the same messages again. That shows which change fixed the problem.
Exercise
An agent has two topics. "Order Help" says "Answer questions about orders." "Order Status" says "Tell customers where an existing order is." The agent sometimes picks the wrong one for "Where is my package?" Give two design changes that fix this, and explain why instructions alone would not be enough to protect an action that cancels orders.
Show answer
First, remove the overlap. Either merge the topics, or rewrite "Order Help" around a distinct intent, such as changing or cancelling an order, and add exclusions to both descriptions, for example "Do not use for checking where an order is." Second, give each topic its own scope so each says where its job ends. Instructions alone are not enough for a cancel action because the model may misread a request and ignore a rule. The action itself must enforce limits: run in user mode with sharing so the user's permissions apply, validate the order number, act on one record only, and require a confirmation step in the topic instructions.
Exercise
Rewrite the weak guardrail "Be careful with refunds" as a testable instruction for a Returns topic that cannot issue refunds.
Show answer
"If the customer asks for a refund, tell them you cannot issue refunds in this conversation and explain how to contact support. Do not promise a refund amount or date." This names the trigger, the required reply, and a prohibited behavior, so you can test it by sending a refund request and checking the response.