Sub topic 13
Agent Script and authoring bundles
Earlier chapters built an agent from a YAML spec and then edited it as retrieved metadata. This chapter covers a third way to author agents: Agent Script, a text format for describing an agent's behavior as code. It also covers the authoring bundle, which holds that text in your DX project.
This chapter assumes you have finished the chapters on creating, previewing, and testing agents. You should be comfortable with sf agent create, sf agent preview, and sf agent test run. You should also know how a DX project is laid out and how agent metadata is retrieved and committed to Git.
Why a script format exists
An agent created from a spec is configured through metadata. The topics, instructions, and actions are stored as generated records. That works, but it has limits:
- Instructions are free text that the model interprets. The same instruction can produce slightly different behavior from one run to the next.
- Control flow is hard to express. "Always verify the case number before looking up the status" is a request to the model, not a rule the platform enforces.
- Reviewing a change means reading a diff of generated metadata, which is verbose.
Agent Script addresses this by mixing two kinds of logic in one file. Some parts are deterministic: variables, conditions, and explicit transitions that run the same way every time. Other parts are natural language instructions for the model to reason over. You decide which parts of a conversation need guarantees and which can stay flexible.
What an authoring bundle is
An authoring bundle is the metadata type that holds an Agent Script file. In a DX project it appears as a directory under your package directory:
force-app/main/default/aiAuthoringBundles/Case_Helper/
Case_Helper.agent
Case_Helper.bundle-meta.xmlThe two files have different jobs:
| File | Purpose |
|---|---|
| `Case_Helper.agent` | The Agent Script source. This is the file you edit and review. |
| `Case_Helper.bundle-meta.xml` | Metadata that tells Salesforce what kind of bundle this is. You rarely change it. |
The bundle-meta file is small:
<?xml version="1.0" encoding="UTF-8"?>
<AiAuthoringBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<bundleType>AGENT</bundleType>
</AiAuthoringBundle>An authoring bundle is the source. Publishing the bundle produces the runnable agent in the org, including the agent metadata you worked with in earlier chapters. Think of the bundle as the program and the published agent as the build output.
The authoring loop
The workflow keeps the same shape as before: author, check, preview, test, then ship. Two new steps are validation and publishing.
Generating a bundle from a spec
You do not need to write an .agent file from nothing. The spec you already know how to generate can seed one:
sf agent generate authoring-bundle \
--spec specs/caseAgentSpec.yaml \
--name "Case Helper" \
--api-name Case_Helper \
--target-org my-dev-orgThis creates the directory shown earlier, with a first draft of the script built from the role and topics in the spec. Treat the draft as a starting point. It will usually need edits before it does what you want.
Validating and publishing
Validation compiles the script and reports problems without changing the org's published agent. Publishing creates or updates the agent from the bundle.
sf agent validate authoring-bundle --api-name Case_Helper --target-org my-dev-org
sf agent publish authoring-bundle --api-name Case_Helper --target-org my-dev-orgPreviewing a bundle
You can preview the script before publishing it. This is the biggest practical gain over the spec-based flow. You can try a change in a conversation first and publish only when the behavior is right. Pass the authoring bundle to the preview command instead of an API name for a published agent:
sf agent preview --authoring-bundle Case_Helper --target-org my-dev-orgReading an Agent Script file
The sketch below shows the general structure of a script for the case agent. It is simplified for illustration. Consult the current Agent Script reference for exact keywords, because the language is evolving.
config:
agent_name: "Case_Helper"
description: "Answers questions about the status of support cases"
system:
instructions: "You help customers of Acme Support. Be brief and polite."
messages:
welcome: "Hi, I can look up the status of your case."
error: "Sorry, something went wrong. Please try again."
variables:
case_number: mutable string = ""
start_agent topic_selector:
description: "Route the user to the right topic"
reasoning:
instructions: ->
| Choose the topic that matches the user's request.
actions:
go_to_case_status: @utils.transition to @topic.case_status
topic case_status:
description: "Look up the status of a support case"
reasoning:
instructions: ->
if @variables.case_number == "":
| Ask the user for their case number.
else:
| Look up the case and report its status.
actions:
get_status: @actions.get_case_statusSeveral ideas are worth noting:
- Blocks. The file is divided into named blocks such as
config,system, andvariables, plus onestart_agentblock and one block per topic. - The entry point. The
start_agentblock runs first on each turn. Its job is to route the conversation to a topic. - Variables. A variable stores state across turns, such as the case number. The
mutablekeyword means the agent can change it during the conversation. - Logic and prose together. The
ifandelselines are evaluated deterministically. The lines starting with|are text handed to the model. This mix is the central feature of the language. - Actions. The
@actions.get_case_statusreference points to an action definition. That definition is backed by the same kinds of targets you used earlier, such as an invocable Apex method or a flow. TheCaseStatusActionclass from the custom actions chapter can fill this role.
Choosing between a spec-based agent and a bundle
Both approaches produce a working agent. The choice depends on how much control you need.
| Question | Spec-based agent | Authoring bundle |
|---|---|---|
| Where is behavior defined? | Generated metadata and free-text instructions | One readable `.agent` file |
| Can you enforce an order of steps? | Only by asking the model | Yes, with conditions and transitions |
| Can you preview before publishing? | Preview runs against the created agent | Preview can run against the local script |
| Review in pull requests | Large metadata diffs | Smaller text diffs |
| Maturity | Established | Newer, changes faster |
A reasonable rule: use the spec flow to get a first agent running quickly. Move to a bundle when you need guaranteed behavior, such as a required verification step, or when code review of instructions matters to your team.
Working with bundles in source control
Authoring bundles are ordinary files, so the Git habits from the metadata chapter apply. Commit the whole bundle directory, not only the .agent file. A few practices help in particular:
- Run validation before every commit. A script that fails validation should not reach the main branch.
- Keep the bundle API name stable. Renaming it creates a different agent rather than updating the existing one.
- Retrieve before you edit if others can change the agent in Setup. Edits made in the UI are not reflected in your local file until you retrieve them, and a later publish can overwrite them.
- Run your agent tests after every publish. Test specs check the published agent's topic and action selection, so they catch regressions that validation cannot see.
Failure modes
- Validation passes but behavior is wrong. Validation checks that the script is well-formed. It does not check whether the model reasons correctly. Preview and tests cover that gap.
- An action reference cannot be resolved. The action's target, such as an Apex class, must exist in the org. Deploy it before you publish the bundle.
- Test expectations stop matching. If you rename a topic or an action in the script, update
expectedTopicandexpectedActionsin your test spec. They must match the names in the org. - Preview and published behavior differ. Preview runs the script in a development session. If the org's published agent is an older version, tests run against that older version until you publish.
Exercise
Your team wants the agent to always ask for a case number before it calls the case status action, and a reviewer wants to see that rule in a pull request diff. Which approach fits better, a spec-based agent or an authoring bundle, and why? Name one command you would run before publishing.
Show answer
An authoring bundle fits better. In Agent Script you can write a condition, such as checking that the case number variable is empty, so the platform enforces the rule instead of asking the model to remember it. The rule also appears as a small text diff in the .agent file. Before publishing you would run sf agent validate authoring-bundle --api-name Case_Helper --target-org my-dev-org. Previewing the bundle with sf agent preview is also a good check.
Exercise
You renamed the topic case_status to status_lookup in your .agent file, then validated and published. Your agent test now fails even though the agent answers correctly in preview. What is the likely cause and the fix?
Show answer
The test spec still has expectedTopic: case_status, which no longer matches the topic name in the org. Update expectedTopic to status_lookup in the test spec, redeploy the test definition with sf agent test create, and run sf agent test run again.