Sub topic 5
Creating the agent in your org with sf agent create
This chapter covers turning a reviewed agent spec into a real agent in your org. It assumes you have the Salesforce CLI with the agent plugin installed, an authorized org, and a spec file from sf agent generate agent-spec. You should also know the basic layout of a Salesforce DX project.
What the command does
sf agent create reads your spec file and asks the org to build an agent from it. The org creates the agent and its metadata, including the topics and actions it proposes from the role and descriptions in the spec. The result is an agent you can preview and test, and it is not yet active for users.
Review the spec first
The spec file is the input, so its quality decides the quality of the agent. Open specs/caseAgentSpec.yaml and check these parts before you run the command:
- The role describes one clear job. A vague role produces vague topics.
- The company name and description are accurate, because the agent uses them as context.
- Any topics the spec proposes match what you want the agent to handle. Edit or remove ones you do not want.
Editing the YAML is cheaper than fixing the agent later.
Run the command
Run the command from the root of your DX project, with the spec file path and the org alias you authorized earlier.
sf agent create \
--name "Case Helper" \
--api-name Case_Helper \
--spec specs/caseAgentSpec.yaml \
--target-org my-dev-orgThe flags work like this:
| Flag | Purpose |
|---|---|
| `--name` | The label people see for the agent |
| `--api-name` | The unique developer name used in metadata and later commands |
| `--spec` | Path to the spec file |
| `--target-org` | Alias or username of the org that receives the agent |
Choose the API name with care. Later commands such as sf agent preview --api-name Case_Helper and your test definitions refer to it, and it becomes part of the metadata file names in source control. If you leave out --api-name, the CLI derives one from the name.
Confirm the result
When the command succeeds, check that the agent exists. Open the org and look at the agent in Agentforce Studio, or preview it from the terminal.
sf agent preview --api-name Case_Helper --target-org my-dev-orgThen retrieve the new metadata into your project so that the agent is under source control. Chapter-level details on retrieval come later, but the key point is that creation happens in the org. Your local project does not contain the agent until you retrieve it.
Common errors
- Authorization failure. The alias in
--target-orgis missing or expired. Runsf org listto check, then log in again withsf org login web --alias my-dev-org. - Permission error. Your user lacks the permissions needed to create agents, or the org does not have Agentforce enabled. Ask an administrator to check the permission sets and the org features.
- Duplicate API name. An agent with that API name already exists. Pick a new name or work with the existing agent.
- Invalid spec. The YAML is malformed or a required field is empty. Fix the file and run the command again.
- Poor topics. The command succeeded, but the topics are wrong. The cause is almost always the role or descriptions in the spec. Adjust them and create the agent again under a new API name, or edit the topics in the org.
Exercise
You ran sf agent create and the command succeeded, but the agent's topics do not match what you wanted. Which file should you look at first, and why?
Show answer
The spec file. The org generates topics from the role, company description, and any topics listed in the spec, so unclear or inaccurate text there leads to topics you did not intend. Edit the spec and create the agent again.
Exercise
Why should you choose the --api-name value carefully?
Show answer
Later commands, such as preview and test runs, refer to the agent by its API name, and it also forms part of the metadata file names in source control. Changing it later means updating those references.