# Record metrics

Report a numeric measurement your own system produced about an interaction — an eval score, a rating, a latency — as a first-class metric.

`record_metric` / `recordMetric` reports a number your own system already computes about an interaction — an eval score, a customer rating, a latency, a cost — as a first-class Brizz metric. Brizz records it against the session so you can track it over time and slice it alongside everything else it knows about that conversation.

It becomes a real metric, not an event: the value shows up in the session's **Metrics** panel and as a numeric filter on the Sessions page ("every session my judge scored below 0.6"). It does **not** clutter your conversation transcript or your Events page.

It's the typed replacement for hand-rolling this with [custom events](/docs/instrument/custom-events.md): you get a fixed shape (name, value, unit, scale, polarity) instead of a free-form attribute bag, so Brizz can chart and compare the metric without being told how to read it.

## Online or offline?

Where the score is computed decides which page you want.

| You score… | Read |
|---|---|
| Inside the agent's own process, during the session — a guardrail check, an inline judge, a latency you just measured | [Online evaluation](/docs/instrument/record-metric/online-evaluation.md) |
| Later, or somewhere else — a nightly job, a separate eval team, a human reviewer, one process grading conversations from many services | [Offline evaluation](/docs/instrument/record-metric/offline-evaluation.md) |

Both use the same call and the same fields. Online recording picks the session up from the surrounding scope; offline recording names the service and the conversation (by session id, trace id, or both) explicitly.

## Describe a metric once

`unit`, `polarity`, `min_value` and `max_value` describe the *metric*, not one report of it — so Brizz remembers them per metric name. Declare them on any call, and later bare reports inherit them:

```python
record_metric("quality_score", 0.85, unit="score", polarity="positive", min_value=0, max_value=1)
record_metric("quality_score", 0.42)  # same scale and polarity — still renders correctly
```

`polarity` tells Brizz which direction is good — whether a rising `quality_score` is an improvement (`"positive"`) or a rising `hallucination_rate` is a regression (`"negative"`). `min_value` / `max_value` describe the scale, so a 4 on a 1–5 rating isn't read as a 4 on a 0–1 score.

A descriptor you pass explicitly always wins, so you can re-scale a metric later without an earlier declaration overriding it.

Brizz never *guesses* these. A metric never described anywhere renders as a plain number rather than a gauge — assuming a scale, or which direction is good, would be worse than admitting we don't know. (A `hallucination_rate` of 0.9 assumed to be `positive` would show up bright green.)

## Fields

- **`name`** *(required)* — the metric identifier, e.g. `"quality_score"`. Emitted as `brizz.internal.metric.name`.
- **`value`** *(required)* — the measurement. A finite number, or a boolean (recorded as `1` / `0` — handy for pass/fail checks).
- **`unit`** *(optional)* — e.g. `"score"` / `"ms"` / `"usd"`.
- **`comment`** *(optional)* — free text, e.g. the evaluator's rationale.
- **`attributes`** *(optional)* — flat labels, each emitted as `brizz.internal.metric.attribute.<key>`.
- **`polarity`** *(optional)* — `"positive"` if a higher value is better, `"negative"` if a higher value is worse.
- **`min_value` / `minValue`**, **`max_value` / `maxValue`** *(optional)* — the metric's scale.
- **`timestamp`** *(optional)* — when the measured thing happened. Defaults to now.
- **`session_id` / `sessionId`** *(optional)* — target conversation. Defaults to the active session; an explicit value wins.
- **`trace_id` / `traceId`** *(optional)* — a trace in the target conversation, as 32 hex characters. Brizz resolves it to that trace's session. See [Offline evaluation](/docs/instrument/record-metric/offline-evaluation.md#identify-the-conversation) for how it combines with `session_id`.
- **`service_name` / `serviceName`** *(optional)* — the service the metric belongs to. Defaults to the `app_name` / `appName` you initialized Brizz with.
- **`scope`** *(optional)* — what one value describes: `"session"` (the default) keeps one value per conversation; `"trace"` keeps one value per trace, so each turn of a conversation keeps its own and the session reads as their average. The trace is `trace_id` when given, else the span you call it from. See [Per-turn metrics](/docs/platform/external-metrics.md#per-turn-metrics).

## Validation

Telemetry must never break your code, so bad input is warned about and dropped rather than raised:

- **`name`** is lowercased, then must match `^[a-z][a-z0-9_.-]{0,63}$`. An invalid name emits nothing.
- **`value`** must be a finite number. `NaN`, infinity, and non-numeric values emit nothing.
- **`comment`** is truncated at 4096 characters.
- **`attributes`** are capped at 20 keys. Keys are lowercased and then validated like `name` (so `Complexity` becomes `complexity`; a key that's still invalid, like `has spaces`, is dropped); values are truncated at 256 characters.
- **`polarity`** accepts only `"positive"` / `"negative"`. Anything else is dropped, and the metric still emits without it.
- **`service_name`** must be a non-empty string of at most 256 bytes with no line breaks or NUL characters. An invalid service name emits nothing — Brizz won't file the metric under your default service instead.
- **`scope`** accepts only `"session"` / `"trace"`; anything else emits nothing. A `scope="trace"` call with no `trace_id` and no surrounding span emits nothing either — Brizz won't file a per-turn value as the session's, where the next turn would overwrite it.
- **`trace_id`** is sent as given; Brizz checks it at ingest. A trace id that isn't 32 hex characters, or that Brizz can't find, drops the metric.
- **The metric needs a conversation to attach to**: a session id, or a trace id that resolves to one. With no `session_id`, no valid `trace_id`, and no surrounding session scope, the SDK logs a warning and emits nothing. A trace id Brizz can't resolve to a session is dropped when it arrives — see [Offline evaluation](/docs/instrument/record-metric/offline-evaluation.md#identify-the-conversation).

## Known limitation

The `comment` field and any `attributes` values are stored **unmasked** on the metric record — the default log masking rules don't reach them. Don't put personal data in either.

## See also

- [External metrics](/docs/platform/external-metrics.md) — where your metrics show up in the product, and how to slice by them.
- [Custom events](/docs/instrument/custom-events.md) — the free-form escape hatch for anything that isn't a metric.
- [Record feedback](/docs/instrument/record-feedback.md) — capture an end-user's reaction to a reply.
- [Sessions](/docs/instrument/sessions.md) — the conversation a metric attaches to.
- [PII & privacy](/docs/instrument/pii-and-privacy.md) — masking configuration.

---

[All Brizz documentation](https://docs.brizz.ai/llms.txt) · [Full documentation (single file)](https://docs.brizz.ai/llms-full.txt)
