> ## 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.

# Choose tags or metadata for row flags

> Decide whether to store a categorical flag, such as a row type, in tags or a metadata field, and filter on it efficiently with SQL.

> [!NOTE]
>
> **Applies to:**
>
> * Plan - Any
> * Deployment - Any
> *
> * Use case - Mark logs, experiment rows, or dataset records with a categorical flag and filter or group on it

## Summary

Both `tags` and `metadata` fields can carry a categorical flag, such as a row type with two or three values. Use `tags` for labels that apply to a whole trace, including labels that reviewers add in the UI. Use a top-level `metadata` field, such as `metadata.row_type`, for a key-value attribute that belongs to a specific span or that can take many values. Filter tags with `IN`, not `=`.

## What is happening

`tags` and `metadata` behave differently:

| | `tags` | `metadata.<field>` |
| - | - | - |
| Shape | An array of strings | A key with one value per span |
| Where it lives | Any span. Tags from all spans are aggregated at the trace level. | The span you log it on |
| When you log it again | New tags are merged (union) with existing tags | The new value replaces the old one |
| Added in the UI | Select traces and click  **Tag** in the toolbar | Logged from your code |
| SQL filter | `tags IN ('row-type:a')` | `metadata.row_type = 'a'` |

Tags are configured per project in ** Settings** > [** Tags**](https://www.braintrust.dev/app/~/configuration/tags) and shared across logs, experiments, dataset records, and datasets.

## Fix or suggestion

### Option 1: Store the flag as a tag

Tags fit flags that describe a whole trace, such as a row type set on the root span. Log the tag on the span:

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

logger = init_logger(project="example-project")  # Replace with your project name

with logger.start_span(name="handle-request") as span:
    span.log(input=request, output=response, tags=["row-type:a"])  # Replace with your values
```

Filter with `IN`, which checks array membership. `tags = 'row-type:a'` doesn't match the array.

```sql theme={null}
SELECT id, created, tags
FROM project_logs('<PROJECT_ID>')
WHERE tags IN ('row-type:a')
  AND created > now() - interval 7 day
LIMIT 100
```

On the trace shape, wrap the condition in `ANY_SPAN()` to match traces where any span has the tag:

```sql theme={null}
SELECT *
FROM project_logs('<PROJECT_ID>', shape => 'traces')
WHERE ANY_SPAN(tags IN ('row-type:a'))
  AND created > now() - interval 7 day
LIMIT 100
```

### Option 2: Store the flag as a metadata field

A `metadata` field fits values that belong to a specific span, or values that change from span to span within a trace. Log it as a top-level key so that filters stay efficient:

```python theme={null}
with logger.start_span(name="classify") as span:
    span.log(input=request, output=label, metadata={"row_type": "a"})  # Replace with your values
```

```sql theme={null}
SELECT id, created
FROM project_logs('<PROJECT_ID>')
WHERE metadata.row_type = 'a'
  AND created > now() - interval 7 day
LIMIT 100
```

### Keep metadata queries fast

* A flag with only a few distinct values scans efficiently without an index. For high-cardinality keys that you filter on often, such as `metadata.user_id`, add a subfield index in ** Settings** > [** Query optimization**](https://www.braintrust.dev/app/~/configuration/query-performance). See [Speed up log filtering](https://docs.braintrust.dev/admin/projects.md#speed-up-log-filtering).
* Filter on `metadata` keys, but avoid selecting them when you only need scalar fields. Selecting arbitrary `metadata` keys fetches the full document for every matching row.
* Grouping on an arbitrary `metadata` key uses the slower aggregation path. When you can, filter on the metadata key and group on a fast field such as `span_attributes.type`.

See [SQL best practices](https://docs.braintrust.dev/reference/sql/best-practices.md) for details.

## How to confirm it worked

* Run the filter query for your flag value and check that it returns the rows you labeled.
* Open a trace in [** Logs**](https://www.braintrust.dev/app/~/logs) and check that the tag appears on the trace, or that `row_type` appears in the metadata of the span you logged it on.

## Notes

* If you need both behaviors, log the value in both fields. Write both from the same code path so they stay consistent.
* In the logs filter UI, use the **includes** operator for tags. See [Log filtering with tags stalls after 50 results](https://docs.braintrust.dev/kb/log-filtering-with-tags-stalls-after-50-results).


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