Sub topic 5
Creating the agent in your org with sf agent create
This chapter covers the step after you generate a spec: turning the reviewed YAML file into a real agent in your org. It assumes you have the Salesforce CLI with the agent plugin installed, an authorized org alias, and a spec file from sf agent generate agent-spec. It also assumes you know basic Salesforce DX project structure and can read YAML. The earlier chapters in this tutorial cover setup, authorization, and spec generation.
What the command does
sf agent create reads an agent spec and creates the agent in the target org. The spec holds the agent's type, role, company details, and the topics the agent should handle. The command sends this to the org, which creates the agent and its related metadata.
After the command finishes, the agent exists in the org as a draft. It is not active, so users cannot talk to it yet. You can still preview it, retrieve its metadata, and test it.
Run the command
The basic form uses the name, the API name, the spec file, and the target org.
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, such as "Case Helper". |
| `--api-name` | The unique API name used by the CLI and metadata. |
| `--spec` | Path to the reviewed spec file. |
| `--target-org` | Alias or username of the org where the agent is created. |
Run sf agent create --help to see the full list of flags in your plugin version. Flags can change between plugin releases, so treat the help output as the final source.
Review the spec first
The agent you get is only as good as the spec you give it. Before you run the command, open the YAML file and check these parts:
- Role. Make sure it states one clear job. A vague role produces vague topics.
- Company name and description. The agent uses these for context and tone, so correct them.
- Topics. Remove topics you do not want. Rename ones whose names are unclear. Add descriptions that say when each topic applies.
- Counts. A spec with many overlapping topics makes the agent harder to predict. Start small and add more later.
Generation uses a language model, so two runs of sf agent generate agent-spec can give different results. For that reason, commit the spec you reviewed. That file is your record of what you asked for.
Choose names carefully
The --name value is a label and can be changed later in Setup. The --api-name value is different. Other metadata and your source control history refer to it, and changing it later is awkward. Pick a stable name before you create the agent.
Follow these rules for the API name:
- Use letters, numbers, and underscores only.
- Start with a letter.
- Do not end with an underscore or use two underscores in a row.
- Keep it short and descriptive, such as
Case_Helper.
These are the usual Salesforce API name rules. If the CLI rejects your value, the error message tells you which rule it broke.
Check the result
Confirm the agent exists before you build on it. You can check in two ways.
First, open the org and look at the agent in Setup. Run sf org open --target-org my-dev-org, then search for Agents. The new agent should appear with a status of inactive.
Second, retrieve the metadata into your project. This is the more useful check, because it also gets the files into source control. The next chapters cover retrieval in detail. In short, you run a retrieve for the agent's metadata types and then see what files appear under force-app.
sf project retrieve start \
--metadata "Bot:Case_Helper" \
--target-org my-dev-orgAn Agentforce agent is stored in the same metadata family as earlier bot features. The main type is Bot, with related BotVersion and GenAiPlannerBundle components for the planner that holds topics and actions. If a retrieve returns nothing, check the type name against the metadata coverage for your API version. Component names vary across releases.
What gets created
You should expect these pieces in the org:
- The agent record itself, with its name and API name.
- A version of the agent, which holds its configuration.
- The planner that links topics, instructions, and actions.
- Topics derived from the spec, each with instructions and a description.
- A default user for the agent to run as, depending on the agent type.
The spec drives topics and instructions. It does not create custom actions for you. If a topic needs to read or change data, you add an action yourself, for example an invocable Apex method. A later chapter covers that.
Common errors
Most failures at this stage come from one of a few causes.
| Symptom | Likely cause | Fix |
|---|---|---|
| Org not found or not authorized | The alias is wrong, or the login expired | Run `sf org list`, then log in again with `sf org login web`. |
| Permission or feature error | The org does not have Agentforce enabled, or your user lacks permission | Check that Agentforce is turned on and that your user has the required permission sets. |
| Spec cannot be parsed | Bad indentation or a missing required field in the YAML | Fix the YAML. A linter or editor with YAML support catches most problems. |
| API name already exists | An agent with that API name is already in the org | Pick a different API name, or work with the existing agent. |
| Command not found | The agent plugin is missing or out of date | Update the CLI and plugins, then run `sf plugins` to confirm. |
If the error is unclear, rerun the command with --json. The structured output often shows the underlying error code and message.
Create once, then iterate
Running sf agent create a second time with the same API name does not update the existing agent. Treat it as a one-time step for a new agent. For later changes, edit the agent and its metadata, then deploy.
The usual loop looks like this:
- Create the agent from the spec.
- Retrieve its metadata and commit the files.
- Preview conversations and note where the agent goes wrong.
- Edit topics, instructions, or actions in the metadata.
- Deploy and preview again.
Sandboxes versus production
Create agents in a scratch org or sandbox first. Creating directly in production makes mistakes visible to real users once the agent is activated, and it skips the chance to test. A safer pattern is to create the agent in a development org, commit its metadata, test it, and then deploy the metadata to higher environments through your normal release path. Each org needs Agentforce enabled and the right permissions before the deploy will work.
Because the create command targets one org, use a separate alias for each environment and always pass --target-org explicitly. Relying on a default org is a common way to create an agent in the wrong place.
Verify with a first preview
Before you write any tests, do a short conversation check. This confirms the agent responds and that its topics are reachable.
sf agent preview --api-name Case_Helper --target-org my-dev-orgAsk one question per topic. If the agent gives a generic answer where you expected a specific topic, the topic description probably needs work. The preview chapter covers this in more depth.
Exercise
You ran sf agent create with --api-name Case_Helper, then realized the instructions in one topic are wrong. Why should you not fix this by running the same create command again, and what should you do instead?
Show answer
Running create again does not update an existing agent, and the API name is already taken, so it fails or gives you the wrong result. Instead, retrieve the agent's metadata into your project, edit the topic instructions in the metadata files, deploy the changes, and preview again.
Exercise
Name two things you should check in the spec file before running sf agent create, and explain why you should commit the spec afterward.
Show answer
Check that the role states one clear job, and that the topics are limited, clearly named, and described. Commit the spec because generation can vary between runs, so the reviewed file is the record of what you asked the org to create.