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

# Evaluate model behaviour

> Define evaluations, judge answers with typed model tasks, gate releases on checks and grade live traffic.

The evaluation library, `@bijectionhq/library-evaluation`, measures AI
behaviour for the app that places it. It is built on three owners: the
experiments library (offline comparison, holdout qualification and the arm
of a live item), the models library (a judge is a [typed
task](https://docs.bijection.com/models/tasks.md)), and `checks` from `@bijectionhq/library-checks`
(release eligibility and live grades are verdicts). `evaluations({...})`
returns a subtree with its tables at `<path>.tables.*` and its functions at
`<path>.runs.*` and `<path>.grades.*`.

```ts bijection/quality.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, role, t } from "@bijectionhq/bijection";
import { checks } from "@bijectionhq/library-checks";
import { experiments } from "@bijectionhq/library-experiments";
import { evaluations, judge } from "@bijectionhq/library-evaluation";
import type { LanguageModel, Models } from "@bijectionhq/library-models";

// The pack for this instance is cleared for `quality`.
declare const ai: Models<{ judge: LanguageModel }>;

export const quality = label();
export const customers = label.family(t.string);
export const operator = role({ clear: [quality] });
export const support = role({ clear: [quality] });

export const grounded = judge(ai, {
  name: "grounded",
  revision: "1",
  model: "judge",
  instructions: "Pass only when every claim in the output is supported by the evidence.",
});

export const gates = checks({ writers: [operator], label: quality });
export const exp = experiments({ label: quality, subjects: customers, operators: [operator] });
export const evals = evaluations({
  label: quality,
  operators: [operator],
  observers: [support],
  checks: gates,
  judges: { grounded },
  experiments: exp,
  monitors: { answers: { revision: "1", rubrics: ["grounded"], rate: { num: 1, den: 10 } } },
});

// app({ quality, customers, operator, support, gates, exp, evals, ... })
```

Give the `checks` instance the library's label: the library records its
verdicts in its own functions' commits.

## Definitions and reports

`evaluation(definition, { metrics })` admits a definition and names it by
its `digest`. A definition is `{ name, revision, baseline, candidates,
cases, repetitions, metrics, limits }`. Each case has an `input`, a
population (`training` or `holdout`), optional lineage that clusters cases
and never crosses populations, and 1 to 32 assertions:

| Kind | Checks |
| - | - |
| `output`, `unordered-array`, `object-set` | Exact values by canonical encoding |
| `schema` | A value type's JSON (`validatorJson({ queue: t.string })`) |
| `text`, `string-length`, `regex`, `keywords`, `edit-distance`, `rouge` | Bounded text matchers |
| `range`, `time-range` | Numeric and time bounds |
| `metric` | A declared metric's field, pinned by revision |
| `tool`, `allowed-tools`, `citations` | The observation's trace |
| `latency`, `cost` | The observation's latency and micro-USD cost |
| `judge` | The judgment of the judge of that rubric and revision |

`evaluate(observations)` folds one observation per (candidate, case,
repetition) slot into a report. A slot is `passed`, `failed` or
`unavailable`; missing evidence is unavailable, never a pass.
`fromTask(task.check(result))` turns a typed task's result into an
observation's `state`, `output` and `costMicroUsd`.

`assessRelease(report, definition, policy)` measures a release: paired
non-inferiority on the holdout over independent case clusters, every
safety assertion present and passing, and cost and latency bounds. Missing
evidence is `insufficient`.

## Judges

`judge(ai, { name, revision, model, instructions })` is a typed model task
answering `{ passed, reason }` over `{ rubric, input, output, evidence }`.
`judge.submit(ctx, subject, { once, then })` sends it and
`judge.judgment(result)` decodes it: a failed call or a non-conforming
answer is never a pass. `pairwiseJudge` runs one round per order, and
`pairwiseConsensus` keeps a winner only when both orders agree.
`calibrateJudge(labels, policy)` bounds false-accept and false-reject rates
per slice, and `eligibility(...)` checks a judge against your calibration
record.

## The library's functions

| Function | Who | What |
| - | - | - |
| `runs.register({ definition })` | operators | Store a definition once under its digest, the run's key. |
| `runs.observe({ run, observation })` | observers | Record one slot once, and submit one judge task per judge assertion. |
| `runs.get`, `runs.report({ run })` | readers | The definition; the report over the recorded slots. |
| `runs.release({ run, policy })` | operators | `assessRelease`, recorded as a check with verdict `eligible`. |
| `grades.grade({ monitor, subject, revision, input, output, evidence, deterministic, registration, unit })` | observers | Grade one live item, sampled at the monitor's rate, with one judge task per rubric. |
| `grades.get`, `grades.summary({ monitor, registration })` | readers | One grade; grades by arm with an interval on the pass rate. |

The same operations are in-transaction helpers (`register`, `observe`,
`report`, `release`, `grade`, `gradeOf`, `summary`) for your own functions,
such as the continuation of a candidate's answer:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, t } from "@bijectionhq/bijection";
import { fromTask } from "@bijectionhq/library-evaluation";
import { answer } from "./candidates";   // a typed task
import { evals, quality } from "./quality";

export const answered = mutation(
  { under: quality, args: { run: t.string, candidate: t.string, case: t.string, result: answer.result } },
  async (ctx, args) => {
    await evals.observe(ctx, args.run, {
      candidate: args.candidate,
      case: args.case,
      repetition: 0,
      ...fromTask(answer.check(args.result)),
      latencyMs: null,
    });
    return null;
  },
);
```

A release gate reads the check:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const eligible = await gates.passed(ctx, "quality/v2", revision, [`${RELEASE_CHECK}:support`]);
```

## Offline comparison

`plan({ trial, seed, alpha, grid })` on an admitted evaluation is the
experiments library's offline registration for it. Your trial mutation
runs the candidate in a scenario branch and answers its score; the
experiments library's `plans.compare` and `plans.qualify` decide. Judge
assertions are unavailable inside a trial, so judge-graded comparisons are
runs.


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