# Create Custom Scorers for Agent Testing

Custom scorers allow you to define evaluation logic for agent testing. [Standard Expectations](/docs/ai/agentforce/guide/testing-api-use-results.md) cover common test scenarios like topic matching and response coherence, but custom scorers create expectations tailored to your business requirements.

Custom scorers evaluate agent behavior at different levels of granularity. A custom scorer can test an entire conversation, a single interaction, or a specific moment within an interaction. Scorers use a prompt template to evaluate agent behavior automatically with an LLM.

:::note
Custom scorers are supported for turn-level tests only, which you define with `AiTestingDefinition` metadata. Conversation-level tests don't support custom scorers yet. See [Get Started with Agentforce Testing API](/docs/ai/agentforce/guide/testing-api-agentforce-get-started.md#choose-turn-level-or-conversation-level-testing).
:::

Use the `AiAgentScorerDefinition` Metadata API type to define custom scorers and deploy them to your org.

## How Custom Scorers Work

A custom scorer evaluates agent behavior and produces a result that maps to an outcome: pass, fail, or not applicable. Each scorer has two key components:

- **Engine**: The evaluation logic. Use a `PromptTemplate` engine to assess the agent's behavior with an LLM.
- **Output mapping**: Rules that translate the engine's result into a pass, fail, or not-applicable outcome.

In this guide, we define a custom scorer that uses a prompt template to detect whether a customer dropped off before the conversation resolved.

## Prerequisites

- Agentforce is enabled in your org with at least one active agent. See [Set up Agents](https://help.salesforce.com/s/articleView?id=ai.copilot_setup.htm) in Salesforce Help.
- If your scorer uses the `PromptTemplate` engine type, the prompt template must exist in your org or get deployed alongside the scorer. See [Deploy a Scorer with a Prompt Template](#deploy-a-scorer-with-a-prompt-template) for details on deploying both together.

## `AiAgentScorerDefinition` Reference

Create an `AiAgentScorerDefinition` metadata component to define your scorer. The component exists in the `aiAgentScorerDefinitions` folder with the `.aiAgentScorerDefinition` file suffix.

### `inputScope`

The scorer defines two `inputScope` fields that work together to control what data the scorer evaluates:

| Field                         | Valid values        | Description                                                                                                           |
| ----------------------------- | ------------------- | --------------------------------------------------------------------------------------------------------------------- |
| Top-level `inputScope`        | `Session`           | The data the scorer's evaluation logic (its prompt template or manual scores) operates on.                            |
| `agentAssociation.inputScope` | `Session`, `Intent` | The unit of agent activity that the scorer runs against. Use `Intent` to score each intent in a session individually. |

To reference the latest interaction inside a session-scoped scorer, use the [`getLastInteraction`](/docs/ai/agentforce/guide/testing-api-scorer-actions.md#getlastinteraction) invocable action within your prompt template.

### Scorer Definition Reference

| Field           | Type                                | Description                                                                                                                                                                                                          |
| --------------- | ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputScope`    | string                              | Required. The scope of agent data the scorer's evaluation logic operates on. Valid value: `Session`.                                                                                                                 |
| `scorerType`    | string                              | Required. The scorer's output shape. Valid values: `Predefined`, `OpenEnded`. Use `Predefined` when the scorer returns one of a fixed set of values; use `OpenEnded` for free-form output typed by a Lightning Type. |
| `dataType`      | string                              | Required. The data type of the scorer's output. Valid values: `Text`, `Number`, `LightningType`. Use `Text` or `Number` with `scorerType: Predefined`; use `LightningType` with `scorerType: OpenEnded`.             |
| `lightningType` | string                              | Required when `dataType` is `LightningType`. The Lightning Type that describes the scorer's output — for example, `lightning__numberType`, `lightning__textType`, or `lightning__booleanType`.                       |
| `semanticType`  | string                              | Optional. Describes how to interpret the scorer's output for reporting. Valid values: `Dimension`, `Measurement`.                                                                                                    |
| `scorerVersion` | [scorerVersion\[\]](#scorerversion) | Required. The version configuration for the scorer. Scorers support multiple versions.                                                                                                                               |

### `scorerVersion`

Version numbers must be sequential starting from 1, and each scorer supports a maximum of 100 versions.

| Field              | Type                                    | Description                                                                                                          |
| ------------------ | --------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| `versionNumber`    | integer                                 | Required. The version number. Must be sequential starting from `1`.                                                  |
| `status`           | string                                  | Required. The lifecycle status. Valid values: `Draft`, `Available`, `Archived`. See [Scorer Status](#scorer-status). |
| `description`      | string                                  | Required. A description of what the scorer evaluates.                                                                |
| `label`            | string                                  | Required. A display label for the scorer version.                                                                    |
| `agentAssociation` | AgentAssociation                        | Required. Associates the scorer with a specific agent.                                                               |
| `engine`           | [engine\[\]](#engine)                   | Required. The evaluation logic for the scorer.                                                                       |
| `outputEnumValue`  | [outputEnumValue\[\]](#outputenumvalue) | Required. One or more mappings that translate engine output values to pass or fail outcomes.                         |
| `specification`    | [specification\[\]](#specification)     | Optional. Constraints on the scorer's output values, such as min, max, step, and threshold.                          |

### `agentAssociation`

| Field          | Type    | Description                                                                                                                                                                                                                                                                                                                         |
| -------------- | ------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `isActive`     | boolean | Required. Whether the scorer is active for the associated agent. Can only be `true` for versions with `Available` status. Only one agent association per scorer can have `isActive` set to `true`. Set to `false` for scorers with `engineType: Manual` — manual scorers are annotated by human reviewers and have no active state. |
| `agentApiName` | string  | Required. The API name of the agent. The agent must exist in the org. For example, `Copilot_for_Salesforce`.                                                                                                                                                                                                                        |
| `inputScope`   | string  | Optional. The unit of agent activity that this scorer runs against for the associated agent. Valid values: `Session`, `Intent`. Use `Intent` to score each intent in a session individually.                                                                                                                                        |
| `samplingRate` | double  | Optional. A value greater than `0` and up to `1.0` that controls the sampling rate. Default is `1.0`.                                                                                                                                                                                                                               |

### `engine`

| Field        | Type   | Description                                                                                                                                    |
| ------------ | ------ | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `engineType` | string | Required. The type of evaluation engine. Valid values: `PromptTemplate`, `Manual`. Use `Manual` for scorers that accept human-reviewed scores. |
| `engineRef`  | string | Required when `engineType` is `PromptTemplate`. The API name of the prompt template. Omit for `Manual` scorers.                                |

### `outputEnumValue`

| Field              | Type    | Description                                                                                                                                                                                                                                                  |
| ------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `value`            | string  | Required. The engine output value that maps to this outcome.                                                                                                                                                                                                 |
| `outcomeType`      | string  | Optional. The test outcome. Valid values are: `Pass`, `Fail`, `NotApplicable`. Default value is `NotApplicable`.                                                                                                                                             |
| `description`      | string  | Optional. A description of what this output value represents.                                                                                                                                                                                                |
| `isFallback`       | boolean | Optional. Whether the LLM can emit this value as the scorer's fallback — for example, `Neutral` for a `High`/`Normal`/`Low` scorer. Required for `Predefined` scorers with `dataType: Text`; not applicable to `Predefined` scorers with `dataType: Number`. |
| `isSystemFallback` | boolean | Optional. Whether the system uses this value when the prompt template or LLM execution fails. Often set together with `isFallback` on the same value.                                                                                                        |

### `specification`

| Field       | Type   | Description                                                        |
| ----------- | ------ | ------------------------------------------------------------------ |
| `max`       | double | The maximum valid output value.                                    |
| `min`       | double | The minimum valid output value.                                    |
| `step`      | double | The increment between valid output values.                         |
| `threshold` | double | Optional. Output values greater than or equal to `threshold` pass. |

## Scorer Status

Each scorer version progresses through a lifecycle controlled by the `status` field.

| Status      | Description                                                                                                                                                                                                                                            |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Draft`     | A work in progress. Use `Draft` to edit the scorer's fields freely. A draft version can't run evaluations.                                                                                                                                             |
| `Available` | A finalized scorer. For scorers with `engineType: PromptTemplate`, set `agentAssociation.isActive` to `true` to activate it for evaluation. Manual scorers keep `isActive` set to `false` — reviewers post scores directly through the Annotation API. |
| `Archived`  | A retired version. The Testing API no longer runs evaluations against this version.                                                                                                                                                                    |

Create a scorer version in `Draft` status when you want to iterate on the scorer's configuration before finalizing it. When the scorer is ready for use, promote the version to `Available`. After a version is marked `Available`, its status can't be changed back to `Draft`.

## Example Scorer Definition

This example defines a custom scorer that evaluates whether a customer drops off before a conversation resolves. The scorer uses a prompt template to analyze the session and outputs a value of `0` (no drop-off, pass) or `1` (drop-off detected, fail).

```sfdocs-code {"lang":"xml", "title": "Example Custom Scorer Definition" }
<?xml version="1.0" encoding="UTF-8"?>
<AiAgentScorerDefinition xmlns="http://soap.sforce.com/2006/04/metadata">
    <inputScope>Session</inputScope>
    <dataType>Number</dataType>
    <scorerVersion>
        <versionNumber>1</versionNumber>
        <status>Available</status>
        <description>Detects whether the customer dropped off before the conversation was resolved.</description>
        <label>Customer Drop-Off Scorer</label>
        <agentAssociation>
            <isActive>true</isActive>
            <agentApiName>Copilot_for_Salesforce</agentApiName>
            <inputScope>Session</inputScope>
        </agentAssociation>
        <engine>
            <engineType>PromptTemplate</engineType>
            <engineRef>Drop_Off_Detection_Template</engineRef>
        </engine>
        <outputEnumValue>
            <isFallback>true</isFallback>
            <outcomeType>Pass</outcomeType>
            <value>0</value>
        </outputEnumValue>
        <outputEnumValue>
            <isFallback>false</isFallback>
            <outcomeType>Fail</outcomeType>
            <value>1</value>
        </outputEnumValue>
        <specification>
            <valueSpecification>
                <max>1</max>
                <min>0</min>
                <step>1</step>
                <threshold>1</threshold>
            </valueSpecification>
        </specification>
    </scorerVersion>
</AiAgentScorerDefinition>
```

## Deploy a Custom Scorer

To deploy a custom scorer, create a project directory with this structure:

```sfdocs-code {"lang":"text", "title": "Custom Scorer Project Directory" }
my-scorer-project/
├── package.xml
└── aiAgentScorerDefinitions/
    └── my_scorer_name.aiAgentScorerDefinition
```

The `package.xml` file specifies the scorer to deploy:

```sfdocs-code {"lang":"xml", "title": "Custom Scorer package.xml" }
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>my_scorer_name</members>
        <name>AiAgentScorerDefinition</name>
    </types>
    <version>66.0</version>
</Package>
```

The `members` value must match the filename of your `.aiAgentScorerDefinition` file (without the extension).

Deploy the scorer to your org with the Salesforce CLI:

```sfdocs-code {"lang":"bash", "title": "Deploy a Custom Scorer" }
sf project deploy start --metadata-dir my-scorer-project
```

### Deploy a Scorer with a Prompt Template

If your scorer uses the `PromptTemplate` engine type, you can deploy both the template and the scorer together. Add a `genAiPromptTemplates` folder containing your prompt template definition, and add the template to `package.xml`.

```sfdocs-code {"lang":"text", "title": "Custom Scorer with Prompt Template Directory" }
my-scorer-project/
├── package.xml
├── genAiPromptTemplates/
│   └── my_prompt_template.genAiPromptTemplate
└── aiAgentScorerDefinitions/
    └── my_scorer_name.aiAgentScorerDefinition
```

```sfdocs-code {"lang":"xml", "title": "Custom Scorer with Prompt Template package.xml" }
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>my_prompt_template</members>
        <name>GenAiPromptTemplate</name>
    </types>
    <types>
        <members>my_scorer_name</members>
        <name>AiAgentScorerDefinition</name>
    </types>
    <version>66.0</version>
</Package>
```

:::note
In `package.xml`, the `GenAiPromptTemplate` type must appear before `AiAgentScorerDefinition`. Metadata API deploys types in the order that they appear, and the prompt template must exist before the scorer that references it can successfully deploy.
:::

### Retrieve an Existing Scorer

To retrieve a scorer definition from your org, use the Salesforce CLI:

```sfdocs-code {"lang":"bash", "title": "Retrieve a Custom Scorer Definition" }
sf project retrieve start --metadata AiAgentScorerDefinition:my_scorer_name
```

You can also retrieve a scorer definition by using the same `package.xml` that you used for deployment.

## Update a Custom Scorer

To update an existing scorer, modify the `.aiAgentScorerDefinition` file and redeploy. Keep in mind these constraints:

- You can add new versions to a scorer, but you can't delete existing versions.
- You can edit a version's fields while its `status` is `Draft`.
- You can promote a version's `status` from `Draft` to `Available`, or move an `Available` version to `Archived`. After a version is marked `Available`, you can't change its status back to `Draft`.
- You can update the `agentAssociation` `isActive` and `samplingRate` values.
- The scorer checks the `members` name in `package.xml`. If a scorer with that name already exists, the deployment updates the existing scorer.
- Scorers with `engineType: Manual` don't have an active state — always set `agentAssociation.isActive` to `false`. Human reviewers post scores directly through the Annotation API regardless of the association's `isActive` value.

## See Also

- [Invocable Actions for Custom Scorers](/docs/ai/agentforce/guide/testing-api-scorer-actions.md)
- [Build Tests in Metadata API](/docs/ai/agentforce/guide/testing-api-build-tests.md)
- [Add Custom Evaluation Criteria to a Test Case](/docs/ai/agentforce/guide/testing-api-custom-evaluation-criteria.md)
- [Metadata API Reference](/docs/ai/agentforce/references/testing-api/testing-metadata-reference.md)
- [Deploy and Run Tests in the Command Line](/docs/ai/agentforce/guide/testing-api-cli.md)
- [Create Custom Scorers](https://help.salesforce.com/s/articleView?id=ai.agent_studio_testing_center_custom_evaluations.htm\&type=5) in Salesforce Help
- [Scorers and Custom Scorers](https://help.salesforce.com/s/articleView?id=ai.generative_ai_optimize_scorers.htm\&type=5) in Salesforce Help
