Sub topic 11
Testing the agent after deployment
A successful deployment and activation only prove that the metadata arrived. They don't prove the agent behaves the way it did in the source org. This chapter covers how to test the deployed agent, what to compare against the source org, and how to make the tests repeatable.
Prerequisites and scope
This chapter assumes you have:
- Deployed and activated the agent in the target org, as in the previous chapters.
- A Salesforce DX project and the
sfCLI authorized to the target org (aliastarget-orghere). - Familiarity with topics, actions, and agent instructions.
Testing is the step where target-org differences show up. The usual causes are missing data, permissions that differ, and configuration that was not carried over.
Why a deployed agent can behave differently
The agent's metadata can be identical in both orgs while its behavior differs. An agent's answer depends on three things beyond its own definition:
- Data. An action that looks up a Case, Account, or Knowledge article returns nothing if the target org has no matching records. Sandboxes are often refreshed from production, but a new scratch org or a separate sandbox may be empty.
- Access. Actions run with the permissions of the running user. Missing object, field, or Apex class access produces failed actions or empty results.
- Configuration. Custom settings, named credentials, email templates, and similar environment-specific items may point to different values or not exist.
Large language model output is also non-deterministic. The same utterance can produce slightly different wording on each run. Test the agent's decisions (which topic, which action), not exact sentences.
Plan the test layers
Test in three layers, from cheapest to most thorough.
| Layer | What it checks | Tool |
|---|---|---|
| Smoke check | The agent is active and answers at all | `sf agent preview` or the Agent Builder testing panel |
| Action-level check | Each Apex class or Flow action works for the agent user | Run the Flow or Apex directly as the agent user |
| Automated test suite | Topic selection, action choice, and outcomes for many utterances | `sf agent test` commands |
Smoke check with the preview command
Start with a quick conversation. The Salesforce CLI can start an interactive preview session with the deployed agent:
sf agent preview --api-name My_Service_Agent --target-org target-orgThe preview requires the agent to be reachable by your authorized user. Some CLI versions also ask you to choose between a simulated and a live mode, or require extra authentication setup. Run sf agent preview --help to see the options your installed version supports.
Use the preview in live mode if you can. Simulated mode does not call your real Apex and Flows, so it cannot confirm that actions work in the target org.
Try three kinds of messages:
- A typical request for each topic.
- A request that should be refused or redirected, such as one outside the agent's scope.
- A request that needs data you know exists in the target org.
The Agent Builder in Setup offers the same kind of conversation, and it also shows the reasoning: which topic was chosen and which actions ran. Use it when you need to see why an answer went wrong.
Check the actions directly
If the agent picks the right topic but an answer is empty or an action reports an error, test the action on its own.
- For a Flow action, open the Flow and run it with sample input in a debug run, or call it from a test record-triggered context.
- For an Apex action, execute it from anonymous Apex or through a unit test.
- For both, confirm the running user can reach the objects and fields involved.
The running user matters. An action that works for you as an administrator can fail for the agent user. For a service agent, the action runs in the context of the agent user you configured earlier, so verify access with that user's permission sets, not your own.
Write an automated test suite
For anything beyond a handful of manual checks, define tests in a YAML spec file. Each test case contains an utterance and what you expect the agent to do.
mkdir -p specsCreate specs/My_Service_Agent_Tests.yaml:
name: My Service Agent Tests
description: Post-deployment checks for the service agent
subjectType: AGENT
subjectName: My_Service_Agent
testCases:
- utterance: Where is my order 00012345?
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent reports the status of the order.
- utterance: Can you write me a poem about the ocean?
expectedTopic: Off_Topic
expectedActions: []
expectedOutcome: The agent politely declines and explains what it can help with.The topic and action names above are placeholders. Replace them with the API names from your own agent. If a name is wrong, the test fails even though the agent works.
You can also generate a starter spec interactively with sf agent generate test-spec, then edit it.
Each test case checks up to three things:
- expectedTopic: the topic the agent should choose.
- expectedActions: the actions the agent should invoke.
- expectedOutcome: a plain-language description of the result. A model evaluates the reply against this description, so keep it specific but not tied to exact wording.
Create and run the test in the target org
Creating the test deploys it as an evaluation definition (the AiEvaluationDefinition metadata type) in the org. Then you can run it:
sf agent test create \
--spec specs/My_Service_Agent_Tests.yaml \
--api-name My_Service_Agent_Tests \
--target-org target-org
sf agent test run \
--api-name My_Service_Agent_Tests \
--wait 10 \
--result-format human \
--target-org target-orgThe --wait 10 flag makes the command wait up to ten minutes for results. Without it, the command returns a run ID. You can fetch the results later with sf agent test results --job-id <run ID> --target-org target-org.
Because the test is stored as metadata, you can retrieve it and commit it with the rest of the project. After that, the same suite can run against every org you deploy to.
Read the results
Look at each failed case and sort it into one of these causes.
| Symptom | Likely cause | Where to look |
|---|---|---|
| Wrong topic chosen | Topic descriptions or classification instructions differ, or the agent version is not the one you deployed | Agent Builder, topic definitions, active version |
| Right topic, expected action not called | Action not assigned to the topic, or the action's inputs cannot be filled from the utterance | Topic's action list, input descriptions |
| Action called but returned an error | Agent user lacks access, or a dependency is missing | Permission sets, Apex class access, Flow status |
| Action called but returned no data | Test data missing in the target org | Records referenced in the utterance |
| Outcome mismatch with correct topic and actions | Expected outcome is too strict, or the data differs | The expected outcome text, the actual reply |
A failure on the first run in a new org is common and usually points to data or permissions, not to the agent.
Use test data you control
Tests that refer to a specific record, such as order 00012345, break when that record does not exist in the target org. There are two reliable approaches:
- Create a small, fixed set of test records in each org, with known identifiers, and write utterances that refer to them.
- Phrase some tests so they don't depend on data, for example scope checks and refusals. These are the most portable tests, and they check topic boundaries and instructions.
Do not run tests that write data, such as creating cases or sending emails, against production without thinking it through. Actions with side effects are real. Prefer a sandbox for those cases, or use records and recipients that are safe to modify.
Compare against the source org
To confirm "behaves the same", run the same spec in both orgs and compare the results.
- Create the test in the source org with the same spec (adjusting names if needed).
- Run it in both orgs.
- Compare which cases pass or fail in each.
If a case fails only in the target org, the agent definition is not the cause, since the metadata is the same. Look at data, permissions, and environment configuration. If it fails in both, the problem exists before deployment and you should fix it in the source org first.
Edge cases to cover
- Multiple phrasings. Include two or three ways to ask for the same thing, to catch weak topic descriptions.
- Out-of-scope requests. Verify the agent refuses or hands off, as your instructions require.
- Ambiguous requests. Check that the agent asks a clarifying question instead of guessing.
- Missing data. Ask about a record that does not exist and check the agent says so instead of inventing an answer.
- Different user context. If different users reach the agent, test with each relevant permission setup.
- Active version. Confirm the version under test is the active one. A test against an inactive draft tells you nothing about what customers see.
Keep the tests with the project
Store the YAML spec in your repository next to the manifest. Treat a change to topic descriptions or instructions like a code change: update the spec, run the suite, and review the results before deploying onward. The final chapter shows how to run this suite automatically in a pipeline.
Exercise
A test passes in the source org but fails in the target org for the utterance "What is the status of order 00012345?". The agent chose the right topic and called the right action, but the reply says no order was found. The agent metadata is identical in both orgs. What is the most likely cause, and how do you confirm it?
Show answer
The target org most likely has no order record 00012345. The topic and action are correct, so the agent definition is not at fault. Confirm by querying for the record in the target org. If it is missing, create fixed test data there (or change the utterance to use a record that exists). If the record exists, check whether the agent user has access to the object and fields, since missing access can also produce an empty result.
Exercise
Why should expectedOutcome in a test case describe the result in general terms instead of quoting an exact sentence?
Show answer
The agent's replies come from a language model, so the wording changes between runs even when the behavior is correct. An exact-sentence check would fail on harmless variations. A general description lets the evaluation judge whether the meaning is right, and the topic and action checks cover the agent's decisions precisely.