# Develop and Package Agent Templates Using Scratch Orgs

At a high-level, agents are distributed by ISVs as agent templates. To package an agent template, you first create and test an agent in a namespaced scratch org. Then retrieve the agent to your Salesforce DX project, generate an agent template from the agent using Salesforce CLI, and finally package the agent template.

:::important
You can't create packageable agent templates from agents that use Agent Script as their blueprint.
:::

Workflow for Agent Template Development

![Six squares of text showing the agent template workflow](https://a.sfdcstatic.com/developer-website/sfdocs/platform-pkg-exchange/media/pkg2\_dev/pkg_agent_template_workflow.png)

## Agent and Agent Template Metadata

To package an agent template, it helps to first understand the metadata types that make up an agent and an agent template.

Agents are defined by these major metadata types.

- Bot
- BotVersion
- GenAiPlannerBundle

The GenAiPlannerBundle type in turn defines the agent's actions and subagents. The `agent generate template` Salesforce CLI command brings together the metadata files for these three types and generates a BotTemplate file for a specific agent (Bot and BotVersion). The command also generates a new GenAiPlannerBundle file, which is different from the original file. Specifically, the new packageable GenAiPlannerBundle file replaces references to the local version of assets (which can't be packaged) with references to their global equivalents.

You then use the BotTemplate file, and the new GenAiPlannerBundle file, to package the agent template in a managed package.

![Compare the MD types for agent MD with the agent template MD types](https://a.sfdcstatic.com/developer-website/sfdocs/platform-pkg-exchange/media/pkg2\_dev/pkg_agent_metadata.png)

## Create an Agent

Create and test your agent.

## Before You Begin

From Setup in your scratch org, enter `Agents` in the Quick Find box, and select **Agentforce Agents**. Then locate and enable the **Agentforce** setting and refresh the page.

1. Click **New Agent**, and then select an agent type.

2. Follow the guided setup steps, and then click **Create**.

## Next Steps

For more guidance, see the [documentation](https://help.salesforce.com/s/articleView?id=ai.agent_setup_explore_types.htm) for the agent type you chose. For details about creating an agent, see [Set Up Your Agent](https://help.salesforce.com/s/articleView?id=ai.agent_parent_setup.htm).

Agentforce-enabled scratch orgs have access to the [Agentforce Testing Center](https://help.salesforce.com/s/articleView?id=ai.agent_testing_center.htm). For more detailed information on testing your agents directly in your DX project, see [Test an Agent with Agentforce DX](https://developer.salesforce.com/docs/einstein/genai/guide/agent-dx-test.md) in the *Agentforce Developer Guide*.

## Set Up Your Salesforce DX Project and Scratch Org

To set up a Salesforce DX project and scratch org, you must already have a namespace and scratch org ready to use.

## Before You Begin

For guidance on obtaining a namespace or an Agentforce-enabled scratch org, see [Get Access to Scratch Orgs with Agentforce Enabled](https://developer.salesforce.com/docs/atlas.en-us.pkg2\_dev.meta/pkg2\_dev/dev2gp_scratch_orgs_agentforce.htm).

:::note
To package BotTemplate metadata, you must first [enable Einstein Chatbot](https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_setup_enable_einstein.htm) in your Dev Hub org. You must also specify this metadata in your `project-scratch-def.json` file.
:::

1. If you’re using an existing Salesforce DX project that contains Apex classes, flows, or prompt templates for your agent, deploy them to the scratch org.

   ```nolang
   sf project deploy start --source-dir force-app --target-org MyNamespacedScratchOrg
   ```

2. Open the scratch org.

   ```nolang
   sf org open
   ```

## Develop Your Agentforce Package

After you have built and tested your agent, you're ready to start packaging it.

1. Retrieve the relevant metadata into your Salesforce DX project.

   ```nolang
   sf project retrieve start --metadata Agent:My_Awesome_Agent –-target-org MyNamespacedScratchOrg
   ```

2. Create an agent template metadata source file.

   In this example, we're generating an agent template from a Bot metadata file in your DX project that corresponds to the `My_Awesome_Agent` agent. A single Bot can have multiple BotVersions. Use the `--agent-version` flag to specify the version. Set the required `--source-org` flag to your scratch org.

   ```nolang
   sf agent generate template --agent-file force-app/main/default/bots/My_Awesome_Agent/My_Awesome_Agent.bot-meta.xml --agent-version 1 --source-org MyNamespacedScratchOrg
   ```

   For more details on the `agent generate template` command, see the [Salesforce CLI Reference Guide](https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_agent_commands_unified.htm#cli_reference_agent_generate_template_unified).

3. Deploy the agent template metadata source file to your scratch org.

   ```nolang
   sf project deploy start  --source-dir force-app --target-org MyNamespacedScratchOrg
   ```

4. Test your agent template in your scratch org by creating an agent using the template you just deployed. Ensure that the new agent works as you expect.

5. When you're satisfied with your agent template, remove the following metadata from your package directory.

   1. The GenAiPlannerBundle file that was part of your original agent. This file was used to create a new, separate GenAiPlannerBundle file for your agent template and is not necessary to package. Remove the GenAiPlannerBundle file that does not have “Template” in the name.

   2. The Bot and BotVersion. Removing these metadata types prevents errors during packaging, since agents aren’t packageable.

   :::note
   To package prompt templates, you must assign permissions in the `sfdx-project.json` file. See [Packaging Considerations for Prompt Templates](https://help.salesforce.com/s/articleView?id=ai.prompt_builder_considerations_packaging.htm\&type=5).
   :::

6. [Create a new package version](https://developer.salesforce.com/docs/atlas.en-us.pkg2\_dev.meta/pkg2\_dev/sfdx_dev_dev2gp_create_pkg_ver.htm) that contains the template and all dependencies. Possible dependencies include: topics, actions, Apex classes, flows, and prompt templates.

   ```nolang
   sf package version create --package "Agentforce App" --installation-key “HIF83kS8kS7C” --definition-file config/project-scratch-def.json --code-coverage --wait 10
   ```

## Next Steps

After a subscriber installs your package in their Agentforce-enabled org, they can use the Agentforce UI to create an agent from your template.

![User interface with ISV Agent Template tile](https://a.sfdcstatic.com/developer-website/sfdocs/platform-pkg-exchange/media/pkg2\_dev/pkg2\_agent_template_ui.png)
