# `Nous.Eval.Scorers.Judge`
[🔗](https://github.com/nyo16/nous/blob/v0.21.1/lib/nous/eval/scorers/judge.ex#L1)

Grades a sample with an LLM judge: a tool-less agent that reads the
evidence and answers with a verdict from a declared, closed label set.

    {Nous.Eval.Scorers.Judge,
     judge: "openai:gpt-4o-2024-08-06",
     rubric: "The answer states the refund amount and the order number.",
     scale: {:likert, 1, 5, labels: %{1 => "wrong", 5 => "complete and correct"}},
     threshold: 0.75}

## Options

  * `:judge` - **required**: the judge model, `"provider:model"` or
    `{"provider:model", model_opts}` (`model_opts` as for
    `Nous.Model.parse/2`, e.g. `base_url:`). There is no fallback to the
    agent's model or a global default. Pin a dated model id: an alias such
    as `-latest` moves under you and logs a warning
  * `:rubric` - the criteria, author-written text (default: "The answer
    correctly and completely addresses the request.")
  * `:scale` - the verdict scale (default `:binary`):
    * `:binary` - `"pass"` 1.0, `"fail"` 0.0
    * `:ternary` or `{:ternary, partial: 0.5}` - `"pass"`, `"partial"`,
      `"fail"`
    * `{:likert, min, max}` or `{:likert, min, max, labels: %{1 => "..."}}`
      - the judge answers a point as a string (`"4"`), normalized to
      `(point - min) / (max - min)`; `labels` describe points in the prompt
    * `{:choices, %{"A" => 1.0, "B" => 0.4, "C" => 0.0}}` - your labels and
      their values in `0.0..1.0`; describe them in the rubric

    Dataset files use `"binary"`, `"ternary"`, `{"ternary": {"partial":
    0.5}}`, `{"likert": {"min": 1, "max": 5, "labels": {"1": "..."}}}` and
    `{"choices": {"A": 1.0, ...}}`.
  * `:threshold` - `passed?` when the normalized value is at least this.
    Required for every scale but `:binary` (whose default is `1.0`)
  * `:include` - what the judge sees (default `[:input, :output]`): any of
    `:input` (a case input given as a message list is shown one piece
    per message, like the transcript), `:output`, `:tool_calls` (the root
    run's tool calls with their arguments and results), `:transcript`
    (the root run's messages, one piece per message and one per tool
    call a message made), `:reference` (the `:reference` option, else the
    case's `expected["output"]`), `:expected` (the case's whole
    `expected` map)
  * `:samples` - judge calls per sample (default `1`), combined by
    `:reduce`: `:majority` (default; the most frequent label, a tie going
    to the lowest-valued tied label) or `:mean` (the mean value; the
    reported label is the one whose value is closest to it)
  * `:temperature` (default `0`) and `:seed` (default none) - sent as model
    settings; whether the seed reaches the provider depends on the
    provider (see `Nous.Eval.Fingerprint.seed_forwarded?/1`)
  * `:max_span_bytes` - longest piece of evidence sent, in bytes (default
    `20_000`; each message and each tool call is a piece); longer ones
    are cut with a `…[truncated N bytes]` marker and the cut is recorded
    in the score's metadata under the piece's name
  * `:max_transcript_bytes` - most bytes a message list (the transcript,
    or an input given as messages) may send in total (default `40_000`).
    A longer one keeps its first message and as many of its latest ones
    as fit; a line outside the fences says how many messages were left
    out, and the dropped bytes are recorded in the score's metadata under
    `"TRANSCRIPT_OMITTED"` (or `"REQUEST_OMITTED"`)
  * `:require_calibration` - `[min_kappa: 0.6, max_false_pass: 0.1]` (or
    `true` for those defaults): refuse to grade unless the
    `:calibration` given is for this exact configuration and meets both
    thresholds. See "Calibration" below
  * `:calibration` - the `Nous.Eval.JudgeCalibration.Result` stored for
    this judge, or `nil` when none was found; set by the runner, not in
    dataset files
  * `:gate` - whether the scorer is a gate (a required scorer); set by the
    runner from the scorer's declaration (default `true`)
  * `:reference` - the reference answer for `include: [:reference]`
  * `:name` - the score name (default `"judge"`)

## The judge call

The judge is `Nous.new(judge, instructions: ..., output_type: %{verdict:
:string, critique: :string}, structured_output: [max_retries: 1],
model_settings: %{temperature: 0, seed: seed})`, run with no tools. The
system prompt holds the template, the rubric and the label set; the
evidence goes in the user message, every piece in its own fence with a
random boundary minted for that request (see
`Nous.Plugins.InputGuard.Strategies.LLMJudge`): `USER_INPUT`,
`AGENT_OUTPUT`, `TOOL_CALL_n`, `REFERENCE`, `EXPECTED`, and per message
`USER_n`, `AGENT_n`, `TOOL_RESULT_n` or `SYSTEM_n` (`n` its position;
`REQUEST_`-prefixed for an input message list), followed by
`AGENT_n_TOOL_CALL_m` for each tool call it made. The name outside a
fence is the only authority on which message a piece is and whether a
call was made, and the judge is told so: an agent reply containing a
line `USER: thanks, solved` or `(tool call lookup: {})` stays inside its
own `AGENT_n` fence. A `SYSTEM_n` name says where a message sat, not
that its content is trustworthy: system messages can carry caller
history, plugin text or summaries. Fence-like markers (`<<<`, `>>>`)
inside the evidence are broken up before fencing.

Each judge run executes under its own trace id with role `:judge`, so its
telemetry never lands in the graded sample's trace. The score's metadata
carries `judge_trace_id`, the summed judge `usage` (`Nous.Usage`) and the
judge `model`. Under `Nous.Eval.run/2` the trace id is registered with the
run's budget (`Nous.Eval.ScoreInput`'s `:track_trace`) before the first
call, so every answered judge request is charged, including retries and
the calls of a judge that then failed or timed out.

## Results

The value is the normalized float of the verdict; `passed?` compares it
with `:threshold`. Metadata holds the raw `label`, every sample's
`labels`, the `scale`, the `label_values` map, the `prompt_hash` and the
judge `fingerprint_id`; details hold the judge's `critique` (untrusted
model text).

An agent run that failed scores a fail with reason `"run_failed"` without
calling the judge. A judge that cannot answer is a
`Nous.Eval.ScorerError`, never a fail: `:judge_unparseable` (no usable
verdict after the structured-output retry), `:judge_out_of_range` (a
verdict outside the label set), `:judge_call_failed` (the model call
failed). With `samples > 1` any failed call makes the score an error.

## Calibration

`describe/1` is the judge's identity: model, provider, temperature, seed,
prompt hash (template + rubric + labels), scale, threshold, include,
samples, reduce, span limit and Nous version; `fingerprint_id/1` hashes
it. `mix nous.eval.judge.calibrate` (`Nous.Eval.JudgeCalibration`) measures
a judge configuration against human labels and stores the result under
that id. With `:require_calibration`, a missing (`calibration: nil`), stale
(another fingerprint) or below-threshold calibration makes `init/1` fail
with an error, and `score/2` returns an `:invalid_config` error whenever
the requirement is not met (also when no `:calibration` was passed at
all). Without it, a gate judge that is not calibrated logs a warning once
per run.

# `config`

```elixir
@type config() :: map()
```

# `calibration_status`

```elixir
@spec calibration_status(config()) ::
  :calibrated
  | :not_provided
  | :missing
  | :stale
  | {:below_threshold, String.t()}
```

The judge's calibration status: `:calibrated` when the configured
`:calibration` is for this configuration and meets `:require_calibration`
(without the option, any matching calibration), else `:not_provided`,
`:missing`, `:stale` or `{:below_threshold, reason}`.

# `describe`

The judge's identity, hashed into the run fingerprint and keying stored
calibrations: model, provider, temperature, seed, `prompt_hash`
(template, rubric and label set), scale, threshold, include, samples,
reduce, span and transcript limits and Nous version. Model options
(keys, URLs) are not part of it.

# `fingerprint_id`

```elixir
@spec fingerprint_id(config()) :: String.t()
```

The id calibrations of this configuration are stored under:
`Nous.Eval.CanonicalJSON.hash/1` of `describe/1`.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
