Sub topic 13
Agent Script and authoring bundles
This chapter assumes you have created an agent from a spec, previewed it, and retrieved its metadata into a Salesforce DX project. The earlier chapters on sf agent create, sf agent preview, and source control cover those steps.
Why a second way to author agents
The spec file from sf agent generate agent-spec describes an agent in broad terms: its role, company, and topics. Salesforce turns that description into topics and instructions, and you refine the result in the org. The agent's behavior is therefore partly generated, and it is hard to review in a pull request.
Agent Script is a text language for writing agent behavior directly. You state the topics, the instructions, the actions, and the conditions that decide which path the agent takes. Because the logic lives in a file you wrote, the diff shows exactly what changed.
An authoring bundle is the metadata container for an Agent Script file. It sits in your project under aiAuthoringBundles, so Git tracks it like other source.
The workflow
The commands for each step look like this:
sf agent generate authoring-bundle \
--spec specs/caseAgentSpec.yaml \
--name "Case Helper" \
--target-org my-dev-org
sf agent validate authoring-bundle --api-name Case_Helper --target-org my-dev-org
sf agent preview --authoring-bundle Case_Helper --target-org my-dev-org
sf agent publish authoring-bundle --api-name Case_Helper --target-org my-dev-orgGenerate starts from the spec you already have, so earlier work carries over. Validate checks the script's syntax and structure without a conversation. Preview runs the script in the terminal. Publish creates or updates the agent in the org.
What an Agent Script file contains
A script is a set of named blocks. This trimmed example shows the main ones:
system:
instructions: "You help customers check the status of support cases."
messages:
welcome: "Hi, I can look up your case status."
error: "Something went wrong. Please try again."
config:
developer_name: "Case_Helper"
agent_label: "Case Helper"
description: "Answers questions about case status"
start_agent topic_selector:
description: "Decide which topic fits the user's request"
reasoning:
instructions: ->
| Send case status questions to the case status topic.
actions:
go_to_case_status: @utils.transition to @topic.case_status
topic case_status:
description: "Tell the user the status of a case"
reasoning:
instructions: ->
| Ask for the case number if you do not have it.
| Then report the status you find.The system block holds agent-wide instructions and canned messages. The config block names the agent. start_agent is the entry point that routes to a topic. Each topic has its own instructions, and a topic can declare actions that point at the same invocable Apex or Flow you wired up earlier.
Lines that start with | are text passed to the model. Instructions marked with -> can also hold conditions and variable logic. This is where a script gives you stricter control than prose instructions alone, for example by requiring a case number before an action runs.
Decisions and gotchas
- Treat the generated file as the source of truth for syntax. Agent Script is new and changes between releases. Compare any example, including the one above, against the file your CLI version generates.
- Choose one authoring path per agent. Editing the same agent in Setup and in a script leads to confusing differences. Pick the script and make changes there.
- Validate before preview. A syntax error is cheaper to find with
validatethan during a conversation. - Preview uses your org. Confirm whether your preview run calls real actions or simulated ones before testing anything that changes data.
Exercise
A teammate wants a pull request reviewer to see exactly how the agent's routing between topics changed. Why does an authoring bundle help more than the spec file, and which command should they run before opening the pull request?
Show answer
The spec file only describes the agent in broad terms, and the org generates the topics and instructions from it, so the routing is not in the diff. An authoring bundle holds the Agent Script, which states topics, instructions, and transitions directly, so the reviewer sees each change. They should run sf agent validate authoring-bundle to catch syntax and structure errors first, and preview if they want to check behavior.