Sub topic 6
Previewing conversations with sf agent preview
This chapter assumes you have the Salesforce CLI with the agent plugin installed and an authorized org. It also assumes you have created the Case_Helper agent from the previous chapter. You should know how topics, instructions, and actions fit together. Command output and available flags change between plugin releases, so check sf agent preview --help against what you see here.
Why preview before you test
After sf agent create, you have an agent in the org, but you don't yet know how it behaves. Writing a formal test spec requires you to know the topic names, the action names, and the outcomes you expect. You usually learn those by talking to the agent first.
sf agent preview starts an interactive conversation in your terminal. You type an utterance, which is one message from a user, and the agent replies. This gives you a fast loop: change an instruction, deploy, ask again.
Preview and tests answer different questions:
| Aspect | `sf agent preview` | `sf agent test run` |
|---|---|---|
| Purpose | Explore and debug behavior | Check behavior against expectations |
| Input | You type utterances by hand | Utterances written in a YAML spec |
| Result | You read the replies yourself | Pass or fail per test case |
| Repeatable | No, it depends on your typing | Yes |
| Good for | Early iteration | Regression checks and CI |
Use preview to discover what the agent does. Use tests to lock in what it should keep doing.
Running a preview session
Start a session with the agent's API name and the org alias:
sf agent preview --api-name Case_Helper --target-org my-dev-orgThe --api-name value is the API name you gave at creation time (Case_Helper), not the display name ("Case Helper"). If you omit the flag, the command may prompt you to pick from the agents it finds in the org. In scripts, always pass it explicitly.
Once the session opens, type a message and press Enter. A typical first check is a question that should match your agent's main topic:
- Type
What is the status of case 00001026? - Read the reply.
- Type a second message, such as
Can you also reset my password?, to see how the agent handles a request outside its scope. - End the session with the exit key shown in the terminal prompt.
Asking an out-of-scope question matters as much as asking an in-scope one. A well-built agent should decline or redirect. If it improvises an answer, your topic instructions or scope need tightening.
Simulated and live modes
Preview can run in two ways, and the difference is a common source of confusion.
- Simulated actions. The agent decides which action to call, but the platform does not run your Apex or Flow. A model produces plausible output instead. Nothing in your org is read or changed.
- Live actions. The agent calls the real actions you deployed, such as the
CaseStatusActionApex class. Queries run, and DML runs too, which means records can change.
In recent plugin versions, the flag --use-live-actions switches to live mode. Check --help to confirm it exists in your version and which mode is the default.
sf agent preview \
--api-name Case_Helper \
--target-org my-dev-org \
--use-live-actionsUse simulated mode while you are still shaping topics and instructions. Its replies show whether the agent picks the right topic and action, but they say nothing about whether your Apex works. Switch to live mode once the instructions look right and you want to verify the full path, including your code.
What happens during a turn
Each message you send goes through the same reasoning steps the agent uses in production.
The agent first selects a topic from the topic descriptions. It then follows that topic's instructions to choose an action, fills in the action's inputs from the conversation, and phrases a reply from the result. If any step goes wrong, the reply is wrong. Preview lets you see which step failed, as long as you can see the intermediate details.
Inspecting what the agent did
A reply alone does not tell you whether the agent used the topic and action you intended. A correct-sounding answer can come from the wrong path. Two options help.
First, many plugin versions can save a transcript and trace data for each session to a directory. Look for an --output-dir flag in --help. Saved files give you something to attach to a bug report or compare between runs.
sf agent preview \
--api-name Case_Helper \
--target-org my-dev-org \
--use-live-actions \
--output-dir previews/case-helperSecond, when you run in live mode, you can ask for Apex debug logging with --apex-debug if your version supports it. This is useful when the agent calls CaseStatusAction and returns "Not found" for a case you know exists. The debug log shows the SOQL query and the value of caseNumber that reached it.
A worked debugging session
Suppose you run a live preview and type:
What is the status of case 00001026?
The agent replies that it cannot find case information. Work through the possible causes in order.
- Topic selection. Did the agent pick the case status topic? If not, rewrite the topic's description so it names the user's intent in plain words, for example "Answer questions about the status of a support case."
- Action selection. If the topic was right, did the agent call the action? The action's label and description are what the agent reads.
Get Case Statuswith the description "Returns the status of a case from its case number" is clear. A vague label such asProcessleads to misses. - Input extraction. Did the agent pass
00001026ascaseNumber? An@InvocableVariablewith an unclear label can cause the agent to ask for the wrong detail. - Action result. Did the Apex query find a record? The sample class returns
'Not found'when the SOQL query finds nothing. The case may not exist in this org, or the running user may not be able to see it, because the class useswith sharing. - Reply phrasing. If the action returned a status but the reply ignored it, tighten the topic instructions about how to report results.
Check each stage in this order, because a failure early in the chain hides everything after it.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Agent not found | Wrong API name, or wrong org alias | Check the API name with the org's agent list and confirm `--target-org` |
| Authorization or session error | Expired login or missing permission | Re-run `sf org login web --alias my-dev-org` and check permissions from the org-authorization chapter |
| Replies look plausible but actions never ran | You are in simulated mode | Add `--use-live-actions` |
| Edits to instructions have no effect | Local changes not deployed, or you edited a different version | Deploy the metadata, then start a new preview session |
| Agent answers out-of-scope questions | Topic scope or instructions too loose | Add explicit limits to the topic instructions |
| "Not found" for a case that exists | Sharing, wrong value passed, or wrong org | Use `--apex-debug` and confirm the case exists in the target org |
Start a fresh session after each deploy. A session that was open before your change may keep earlier context, so its replies can mislead you.
Getting the most from preview
- Prepare a short list of utterances before you start. Include one in-scope request, one ambiguous request, and one request that should be declined.
- Repeat an important utterance two or three times. Agent replies are generated, so wording may vary even when the topic and action stay the same. If the topic or action varies, the instructions are underspecified.
- Change one thing at a time. If you edit a topic description and an action label together, you cannot tell which change helped.
- Record the utterances that worked. They become the
utterancefields in your test spec in the testing chapter.
Exercise
You preview Case_Helper and ask "What is the status of case 00001026?" The reply is fluent and gives a status, but no Apex debug log shows a query for the case. What is the most likely explanation, and what is your next step?
Show answer
The session is most likely running in simulated mode, so a model produced the status and your CaseStatusAction code never ran. Rerun the preview with --use-live-actions (in a sandbox or scratch org with test data) and check the Apex debug log, using --apex-debug if your version supports it, to confirm the SOQL query ran with the right case number.
Exercise
Why should you start a new preview session after deploying a change to a topic's instructions, and why preview the same utterance more than once?
Show answer
An older session may carry earlier conversation context and may not reflect the deployed change, so a new session gives a clean test of the new instructions. Agent replies are generated and can vary, so repeating an utterance shows whether the topic and action selection is stable or underspecified.