Sub topic 1
What an Agentforce agent is made of
An Agentforce agent is a configured set of instructions and capabilities that a large language model (LLM) uses to handle a conversation inside a Salesforce org. Before you generate specs or run sf agent commands, you need a clear model of the parts. Each CLI command in this tutorial creates, changes, or checks one of them.
Prerequisites
This chapter assumes you already have:
- Basic command-line use.
- Familiarity with Salesforce orgs, sandboxes, and scratch orgs.
- A working idea of a Salesforce DX project (
sfdx-project.json,force-app). - Basic knowledge of Apex or Flow.
- Git fundamentals.
You do not need the Salesforce CLI installed yet. Chapter 2 covers setup. This chapter is conceptual and has no required commands.
The core idea: the model chooses, your configuration constrains
A traditional automation follows a fixed path you wrote. An agent works differently. The LLM reads the user's message, decides what the user wants, and picks from capabilities you have defined. You do not script each branch. You describe what the agent may do and how it should behave, and the model selects at run time.
Your job as a developer is therefore to configure and constrain, not to code the flow. Four building blocks do this:
- The agent itself, which holds its identity and overall behavior.
- Topics, which group related jobs.
- Instructions, which tell the model how to act inside a topic.
- Actions, which are the things the agent can actually execute.
The four building blocks
The agent
The agent is the top-level container. It carries:
- A name and an API name, such as
Case_Helper. - A role description that states what the agent is for.
- Company context, such as the company name and a short description. The model uses this to set tone and scope.
- A type. The spec command in a later chapter asks for
customerorinternal. A customer agent talks to people outside your company. An internal agent serves employees. - The user the agent runs as, which controls what data it can read and change.
Topics
A topic is a category of work the agent can handle, for example "case status" or "order returns". Each topic has:
- A name and a classification description. The model reads this description to decide whether a user message belongs to the topic.
- A scope that says what the topic covers and what it does not.
- A set of instructions.
- A list of actions available in that topic.
Topic selection is the first decision the agent makes for each request. If the classification description is vague or overlaps with another topic, the agent routes requests to the wrong place. Most bad agent behavior traces back to this step.
Instructions
Instructions are natural-language rules that shape the model's behavior. They exist at more than one level:
- Agent-level text such as the role and company description.
- Topic-level instructions, such as "Always ask for the case number before looking up a case."
Instructions are guidance, not guarantees. The model usually follows them, but it is not a rules engine. If an outcome must happen every time, put that logic in an action (Apex or Flow) and not in a sentence of instruction. Later chapters, including Agent Script, show ways to add more deterministic control.
Actions
An action is a unit of work the agent can run, such as querying records or calling an Apex method. Actions are the only way an agent affects your org. Without them, an agent can only talk.
Each action has:
- A name and a description that tells the model when to use it.
- Inputs, which the model fills in from the conversation.
- Outputs, which the model reads and turns into a reply.
- A target, the thing that runs, such as an invocable Apex method, a Flow, or a prompt template.
The description matters as much as the code. The model sees the label and description, not your implementation. An action called doStuff with an empty description is hard for the model to use correctly.
How the pieces work together at run time
For each user message, the agent runs a reasoning loop. The loop is handled by the platform, so you do not code it. In outline:
- The agent receives the utterance (the user's message) and the conversation so far.
- It picks the topic that best matches the request.
- It reads that topic's instructions and lists its actions.
- It decides whether to call an action, ask the user for missing input, or answer directly.
- If it calls an action, it reads the output and may call another action or reply.
- It sends a response, subject to the platform's safety and trust checks.
The loop explains why the CLI has a preview and test workflow. You cannot read the code to know what the agent will do. You have to try utterances and check which topic and actions the agent chose.
Where action targets come from
Actions can wrap several kinds of Salesforce logic. A later chapter covers them in detail. The common ones:
| Target | Typical use | Notes |
|---|---|---|
| Invocable Apex | Custom logic, complex queries | Needs `@InvocableMethod` with labels and descriptions |
| Flow | Declarative logic, record updates | Must be a flow type that can be used as an action |
| Prompt template | Generate or summarize text | Uses the Einstein Trust Layer for grounding |
| Standard action | Built-in capabilities | Provided by the platform |
Choose by what the logic needs. Use Apex when you need control over queries and error handling. Use Flow when an admin team will maintain it.
Agents are metadata
An agent is not a separate service. It is stored in the org as metadata, like objects and Apex classes. That is why this tutorial can treat it as source code: you can retrieve it, commit it, review changes in a pull request, and deploy it to another org.
The exact metadata types depend on your API version and CLI plugin version. In general, an agent is represented by a bot definition and its versions, plus planner and topic definitions that describe its topics and actions. Newer API versions bundle these into a planner bundle. Check the metadata coverage report for your API version before you write retrieve or deploy scripts.
Recent versions of the CLI accept an Agent metadata type that gathers the related pieces for you:
sf project retrieve start --metadata "Agent:Case_Helper" --target-org my-dev-orgIf your CLI version rejects this, update the Salesforce CLI and its plugins first. We cover retrieval and versioning in a later chapter.
What an agent is not
Some common misunderstandings cause wasted work:
- It is not a chatbot script. There is no decision tree to step through.
- It is not deterministic. The same utterance can produce slightly different wording, so tests check topic, actions, and outcome, not exact text.
- It does not bypass security. The agent can only see and change what its running user and your sharing rules allow. Write Apex actions with
with sharing, as in the example in the later action chapter. - It does not learn from conversations on its own. Behavior changes only when you change the configuration.
Mapping the concepts to the CLI workflow
Each stage of this tutorial touches one of the building blocks.
| Stage | CLI or tool | Building block it affects |
|---|---|---|
| Generate a spec | `sf agent generate agent-spec` | Agent role, company, and initial topics |
| Create | `sf agent create` | The agent record and its metadata in the org |
| Preview | `sf agent preview` | Runs the reasoning loop interactively |
| Add an action | Apex plus deploy | Actions and their descriptions |
| Test | `sf agent test` commands | Checks topic, action, and outcome |
| Activate | Activation commands | Makes a version available to users |
Keep this table in mind. When an agent misbehaves, find the stage and building block that produced the behavior before changing anything.
Common errors at this stage
- Overlapping topics. Two topics with similar descriptions make routing unpredictable. Give each topic a clear scope and say what it excludes.
- Rules written as hopes. "Try to verify the user" is weak. "Ask for the case number before calling the case lookup action" is specific and testable.
- Under-described actions. The model picks actions by their text. Write labels and descriptions for the model as the reader.
- Too many actions in one topic. A long list makes selection harder. Split the topic or remove actions that are rarely used.
- Assuming instructions enforce security. Enforce access with permissions, sharing, and Apex, not with prompt text.
Summary
An agent is a role plus topics. Each topic has instructions and actions. The model routes each message to a topic and may call actions to read or change data. Everything is stored as org metadata, which is why you can build and check it from the command line.
Exercise
A user asks your agent, "Where is my order?" The agent answers with a refund policy. Name the building block most likely at fault and one fix.
Show answer
The topic selection step is most likely at fault, because the model routed the message to a refund topic instead of an order status topic. Fix it by rewriting the classification descriptions so each topic has a distinct scope, and add an explicit "does not cover" statement to the refund topic. Then check the result with sf agent preview.
Exercise
A rule must run every time an agent closes a case: log an audit record. Should you put this in topic instructions or in an action? Why?
Show answer
Put it in an action, such as an Apex method or Flow that writes the audit record. Instructions are guidance for the model and are not guaranteed to be followed on every run, while an action runs as code when it is called.