Sub topic 6
Previewing conversations with sf agent preview
This chapter assumes you have created an agent with sf agent create and authorized an org with the Salesforce CLI. It also assumes you are comfortable in a terminal. Preview is the quickest way to see how the agent behaves before you write formal tests.
What preview does
sf agent preview starts an interactive conversation with your agent in the terminal. You type an utterance (a message a user might send), and the agent replies. Nothing is added to a test suite, and no files are needed beyond the agent itself.
Use preview to answer questions like these:
- Does the agent pick the topic I expected?
- Does it call the right action?
- Is the wording of the reply acceptable?
Run a preview
Run the command against the agent you created earlier:
sf agent preview --api-name Case_Helper --target-org my-dev-orgIf you omit --api-name, the CLI normally lists the agents it finds and asks you to pick one. After the session starts, type a message and press Enter. Exit the session with the exit key shown in the prompt, usually Ctrl+C or Esc depending on your plugin version.
Try an utterance that should match your topic:
What is the status of case 00001026?Then try one that should not match:
What is the weather in Paris?The second test matters as much as the first. A well-scoped agent should decline or redirect. If it answers anyway, your topic instructions are too loose.
Simulated and live actions
Preview can run actions in two ways:
| Mode | What happens | When to use it |
|---|---|---|
| Simulated | The model pretends the action ran and invents a plausible result | Early drafts, or when actions change data |
| Live | The real Flow, Apex, or prompt template runs in the org | Checking that your invocable Apex returns real data |
Recent versions of the plugin expose this choice as a flag. Run sf agent preview --help to see which flags your installed version supports. Common options include --use-live-actions and --apex-debug, which saves Apex debug logs for the session. Live mode can create or change records, so use a sandbox or scratch org.
What happens during a session
The agent selects a topic, then decides which action in that topic to call. Both choices come from the descriptions and instructions in your spec. Preview shows you the outcome, so a wrong answer usually points back to a vague description.
A practical loop
- Create or update the agent.
- Preview with three or four utterances: one typical, one with a different phrasing, one off-topic.
- Change the topic instructions or action descriptions.
- Redeploy and preview again.
- Move the utterances that worked into a test spec.
Keep a note of the utterances you tried. They become the utterance fields of your test cases in a later chapter.
Common errors
- The agent is not found. The API name is case sensitive and must match the one used in
sf agent create. Also confirm that--target-orgpoints to the org where you created the agent. - Authentication or connected app errors. Preview talks to the agent through an API, so your user needs permission to use agents, and your org may need a connected app set up for the CLI. If the error names a client app, check the preview flags in
--help. - The action returns "Not found" or nothing. In live mode, check the data. The case number in your utterance must exist in that org.
- Different answers to the same question. Model responses vary between turns. Treat one good reply as a sign, not proof. Repeat important utterances several times.
Exercise
Your agent answers "What is the weather in Paris?" with a made-up forecast. Which part of the agent should you change, and how do you confirm the fix?
Show answer
Tighten the topic's scope and instructions so the agent declines questions outside case status. Redeploy the change, then run sf agent preview again with the same off-topic utterance and a few rephrasings. Once the agent declines consistently, add the utterance to a test spec.