Sub topic 4
Generating an agent spec with sf agent generate agent-spec
An agent spec is a YAML file that describes what an agent is for. You produce it with one CLI command, review it, and then use it to create the agent. This chapter covers what the command does, what each flag controls, what the output looks like, and how to fix the usual problems.
This chapter assumes you have the Salesforce CLI with the agent plugin installed. It also assumes you have a Salesforce DX project and an org where Agentforce is enabled. Authorizing the org is covered in the previous chapter. Basic YAML and Git are enough for the rest.
What the command does
sf agent generate agent-spec does not create anything in your org. It asks the org's generative AI service to propose a list of topics for an agent, based on the role and company details you provide. It then writes the result to a local YAML file.
This has three consequences:
- The command needs an authorized org, because the topic suggestions come from the org, not from your machine.
- The output is a draft. Two runs with the same input can produce different topic names and descriptions.
- Nothing is deployed. You can edit, delete, or regenerate the file freely before the next step.
The spec is the input to sf agent create, which is covered in the next chapter.
Authorizing and generating
First authorize the org and give it an alias, then run the generate command. This is the same pair of commands as in the tutorial's first example.
sf org login web --alias my-dev-org
sf agent generate agent-spec \
--type customer \
--role "Answer questions about the status of customer support cases" \
--company-name "Acme Support" \
--company-description "Acme provides hardware and support plans" \
--output-file specs/caseAgentSpec.yaml \
--target-org my-dev-orgThe command prints a confirmation when the file is written. Open the file before you do anything else with it.
The flags that matter
| Flag | Purpose |
|---|---|
| `--type` | The kind of agent: `customer` for an agent that talks to your customers, or `internal` for one that helps employees. |
| `--role` | One or two sentences describing the agent's job. This has the largest effect on the topics you get. |
| `--company-name` | The company name, used as context for the generated text. |
| `--company-description` | A short description of what the company does. |
| `--company-website` | Optional. The website address of the company, added as extra context. |
| `--max-topics` | Optional. An upper limit on how many topics the service proposes. |
| `--output-file` | Where to write the YAML. If you leave it out, the CLI uses a default path under `specs/`. |
| `--target-org` | The alias or username of the org that generates the topics. |
| `--no-prompt` | Skips interactive questions. Use it in scripts. |
Run sf agent generate agent-spec --help to see the exact flag list for your installed plugin version, since the plugin changes between releases.
Interactive mode
If you omit required values such as --type or --role, the command asks for them in the terminal. It then shows the proposed topics and lets you regenerate them or accept them. This is useful for exploring. It is a poor fit for automation, because a prompt in a CI job waits for input that never arrives. In scripts, pass every value as a flag and add --no-prompt.
Reading the generated file
The exact fields depend on your plugin version. A generated spec looks like this:
agentType: customer
role: Answer questions about the status of customer support cases
companyName: Acme Support
companyDescription: Acme provides hardware and support plans
maxNumOfTopics: 5
topics:
- name: Case Status Lookup
description: Help customers find the current status of an existing support case using a case number.
- name: Support Plan Questions
description: Answer general questions about Acme support plans and what they include.
- name: Escalation Guidance
description: Explain how to request escalation when a case is delayed.The file has two parts.
- Agent-level fields.
agentType,role,companyName, andcompanyDescriptionare your inputs echoed back. They set the agent's overall identity. - Topics. Each topic has a
nameand adescription. A topic is a category of job the agent can do. The description tells the agent's reasoning engine when this topic applies to a user's request.
Topic descriptions matter more than the names. When a user sends a message, the agent chooses a topic by comparing the message to the descriptions. A vague description such as "Help with cases" will overlap with other topics and cause wrong selections later.
Reviewing the draft
Treat the generated file like a pull request from a junior colleague. It is usually close, but you must read it. Check each point below.
- Scope. Does every topic fit the role you stated? Remove topics that extend the agent's job beyond what you intend. An agent with fewer, sharper topics is easier to test.
- Overlap. Do two topics describe similar requests? If so, merge them or rewrite the descriptions so each has a clear boundary.
- Descriptions. Does each one say what the user wants and what the agent does? Prefer "Help customers find the current status of an existing support case using a case number" over "Case status".
- Missing topics. Is there a job you need that was not proposed? Add it by hand. The file is plain YAML, so you can append a new item under
topics. - Names. Topic names become labels in the org. Keep them short and consistent in style.
Where the spec ends and actions begin
The spec lists topics, not actions. Actions are what the agent can actually do, such as running a flow or an Apex method. You add them after the agent exists, as covered in the chapters on topics and custom actions. So a topic like "Case Status Lookup" in the spec has a description but no way yet to fetch real data. That is expected.
Keeping the spec under source control
Commit the spec file once you have edited it. It records the intent behind the agent, and reviewers can read topic changes in a diff. A reasonable layout is a top-level specs/ folder next to force-app:
git add specs/caseAgentSpec.yaml
git commit -m "Add reviewed agent spec for Case Helper"Keep in mind that the spec is an input to creation. After the agent exists in the org, the org's metadata is the source of truth, not the spec. Editing the spec later does not update an existing agent. To change a live agent you edit its retrieved metadata or regenerate and create a new one. Later chapters cover that workflow.
Common errors and how to fix them
| Symptom | Likely cause | Fix |
|---|---|---|
| The command reports that no default org is set, or the org cannot be found. | `--target-org` is missing or the alias is wrong. | Run `sf org list`, confirm the alias, and pass `--target-org` explicitly. |
| The command fails with a message about the feature being unavailable or not enabled. | Agentforce or Einstein generative AI is not turned on in the org, or your user lacks the permission. | Enable the features in Setup and assign the required permission sets to your user. |
| The command waits for input and hangs in a script. | A required flag is missing, so it opened an interactive prompt. | Pass `--type`, `--role`, and the company flags, and add `--no-prompt`. |
| The topics are generic or off-target. | The role and company description are too vague. | Rewrite them with specifics, then run the command again. Add `--max-topics` to keep the list short. |
| The output file is missing from where you expected. | `--output-file` was omitted, so the default path was used, or the path was relative to a different folder. | Pass an explicit path and run the command from the project root. |
| `sf agent` is not a known command. | The agent plugin is not installed or is out of date. | Update the CLI and plugins, then run `sf agent --help` to confirm the command group appears. |
Edge cases
- Regenerating overwrites. If the output file already exists, running the command again with the same
--output-filereplaces it, and your manual edits are lost. Commit or copy the file first, or write drafts to a different name. - Non-deterministic output. Because the topics are generated, you cannot rely on getting the same list twice. Once you have a good spec, stop regenerating it and edit it by hand.
- Sandbox versus production. The generate command only needs an org that supports the feature. Generating in a sandbox or scratch org is fine, and the file is portable. The agent itself is created in whichever org you name in the next step.
- Internal agents. With
--type internal, the role should describe the employee task, for example "Help support engineers find the history of a case". The wording of the generated topic descriptions shifts toward staff users.
Exercise
You ran sf agent generate agent-spec in a CI job and it never finished. Locally, the same command works. What is the most likely cause, and what two changes fix it?
Show answer
The command is waiting on an interactive prompt because a required value, such as --type or --role, was not passed as a flag, and a CI job has no one to answer. Pass every required value as a flag and add --no-prompt so the command fails fast instead of waiting.
Exercise
A teammate edits topics in specs/caseAgentSpec.yaml after the agent was already created, then asks why the live agent did not change. What do you tell them?
Show answer
The spec is only an input to sf agent create. Once the agent exists, its metadata in the org is the source of truth, so editing the spec file does not update it. They need to change the agent's metadata and deploy it, or create a new agent from the updated spec.