> ## Documentation Index
> Fetch the complete documentation index at: https://docs.braintrust.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Scorer metadata is stored on the scorer span, not the scored span

> Find the metadata a scorer returns, and use classifiers or span logging when you need categorical or non-numeric values on the span being scored.

> [!NOTE]
>
> **Applies to:**
>
> * Plan - Any
> * Deployment - Any
> *
> * Use case - A custom scorer returns metadata, and you want that data on the span being scored so you can filter, group, or chart it.

## Summary

When a scorer returns `metadata`, Braintrust logs it on the scorer's own span. It isn't merged into the `metadata` of the span being scored. To attach categorical values to the scored span, use a [classifier](https://docs.braintrust.dev/evaluate/write-scorers.md#classifiers). To attach other values, log them as `metadata` from your application or eval task.

## What is happening

Every scorer run creates its own scorer span in the trace it scores. Where the returned `metadata` ends up on that scorer span depends on how the scorer runs:

* **Online scoring rules**: The scorer span's `output` holds the scorer's full return value, so returned metadata appears at `output.metadata` on the scorer span.
* **Experiments run with `Eval()`**: The TypeScript and Python SDKs log the returned metadata to the scorer span's top-level `metadata` field and the remaining score fields to its `output`.

In both cases, the metadata stays on the scorer span. An online scoring rule writes only the score values to the span being scored, under `scores`. See [View scoring results](https://docs.braintrust.dev/evaluate/score-online.md#view-scoring-results) for where scorer spans appear in the trace.

Classifiers work differently. Each classification has an `id`, an optional `label`, and optional `metadata`. Braintrust writes the classification, including its `metadata`, to the scored span's `classifications` field. In the logs and experiments tables, each classifier appears as its own column under a `classifications.` prefix that you can sort and filter.

## Solutions

### Option 1: Return a classification instead of a score

Use a classifier when the value you want to attach is a category, such as an intent, a bucket, or a pass reason. A custom code classifier can return any `id` your logic computes, plus `metadata` with supporting values.

```typescript theme={null}
import { Eval } from "braintrust";

function lengthBucket({ output }: { output: string }) {
  return {
    name: "length_bucket",
    id: output.length > 500 ? "long" : "short",
    metadata: { characters: output.length },
  };
}

Eval("Support bot", {
  data: [{ input: "Hello! Can you help me reset my password?" }],
  task: async (input: string) => `Thanks for reaching out. ${input}`, // Stand-in for your LLM call
  classifiers: [lengthBucket],
});
```

```python theme={null}
from braintrust import Classification, Eval


def length_bucket(input, output, expected):
    return Classification(
        name="length_bucket",
        id="long" if len(output) > 500 else "short",
        metadata={"characters": len(output)},
    )


Eval(
    "Support bot",
    data=[{"input": "Hello! Can you help me reset my password?"}],
    task=lambda input: f"Thanks for reaching out. {input}",  # Stand-in for your LLM call
    classifiers=[length_bucket],
)
```

Braintrust stores classifier results in an array under `classifications.length_bucket` on the scored span, even when the classifier returns one result. The first result's metadata is at `classifications.length_bucket[0].metadata`.

For production traces, select a saved classifier in an [online scoring rule](https://docs.braintrust.dev/evaluate/score-online.md) the same way you select a scorer. You can build an [LLM-as-a-judge classifier](https://docs.braintrust.dev/evaluate/llm-as-a-judge.md#apply-classification-labels) in the UI, or follow [Apply classification labels](https://docs.braintrust.dev/evaluate/custom-code.md#apply-classification-labels) to write a custom code classifier.

Classifiers require TypeScript SDK v3.9.0 or later, or Python SDK v0.16.0 or later. See [Classifiers](https://docs.braintrust.dev/evaluate/write-scorers.md#classifiers) for other SDKs.

### Option 2: Log the values as metadata on the span itself

If you can compute the value in your application or eval task, log it directly as `metadata` on the span instead of returning it from a scorer. Filters and aggregations on top-level `metadata` fields are more efficient than on values nested in `input` or `output`. See [Add metadata and tags](https://docs.braintrust.dev/instrument/trace-application-logic.md#add-metadata-and-tags).

In an `Eval()` task, add fields to `hooks.metadata`. The SDK logs them as the eval row's top-level `metadata`.

```typescript theme={null}
import { Eval } from "braintrust";

Eval("Support bot", {
  data: [{ input: "Hello! Can you help me reset my password?" }],
  task: async (input: string, hooks) => {
    const output = `Thanks for reaching out. ${input}`; // Stand-in for your LLM call
    hooks.metadata.characters = output.length;
    return output;
  },
  scores: [({ output }) => (output.length > 0 ? 1 : 0)],
});
```

```python theme={null}
from braintrust import Eval


def non_empty(input, output, expected):
    return 1 if output else 0


def task(input, hooks):
    output = f"Thanks for reaching out. {input}"  # Stand-in for your LLM call
    hooks.metadata["characters"] = len(output)
    return output


Eval(
    "Support bot",
    data=[{"input": "Hello! Can you help me reset my password?"}],
    task=task,
    scores=[non_empty],
)
```

In production logging, log `metadata` on the span that does the work, for example with `span.log({ metadata: { ... } })` in TypeScript or `span.log(metadata={...})` in Python. See [Wrap a custom LLM client](https://docs.braintrust.dev/instrument/advanced-tracing.md#wrap-a-custom-llm-client) for an example of logging `metadata` on a span.

## How to confirm it worked

* For a classifier, open a scored trace and confirm the scored span shows the classification under `classifications`. In the logs or experiments table, confirm the classifier's `classifications.` column appears and can be filtered.
* For logged metadata, open the span and confirm the fields appear under its top-level `metadata`.
* To read metadata that a scorer already returned, open the scorer span in the trace. Check `output.metadata` for online scoring rules, or `metadata` for `Eval()` runs.

## Notes

* Scorer spans have `span_attributes.purpose` set to `'scorer'`. Use this to select scorer spans when you query scorer output with SQL.
* An online scoring rule writes only scores or classifications to the span being scored. It doesn't update that span's `metadata`. Use a classifier for values that must come from the scoring step.
* To query classification fields with SQL, see [Query by classifications](https://docs.braintrust.dev/reference/sql/examples.md#query-by-classifications).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.