# promptfoo: a pairwise select-best grader

At the end of this page your promptfoo eval will pick the best output of each test case by comparing the outputs two at a time, instead of asking one model to read them all at once. It works with promptfoo's [`select-best`](https://www.promptfoo.dev/docs/configuration/expected-outputs/model-graded/select-best/) assertion, which passes the winning output and fails the others.

## Run the example

You need Node.js for promptfoo and [uv](https://docs.astral.sh/uv/) for Python. In an empty folder, save the two files below, then:

```sh
uv venv && uv pip install supposing
PROMPTFOO_PYTHON=.venv/bin/python npx promptfoo@latest eval --no-cache
```

`PROMPTFOO_PYTHON` tells promptfoo which Python runs the grader; it must be one with `supposing` installed. To turn off promptfoo's usage telemetry, also set `PROMPTFOO_DISABLE_TELEMETRY=1` ([promptfoo telemetry](https://www.promptfoo.dev/docs/configuration/telemetry/)).

The judge here is `mock`: free, offline, and fake. It prefers the text with the higher hidden `[[q=...]]` score, so you can see the machinery work before paying for a model. The run printed:

```text
Python worker stderr: output 1 ranks first, but the best could be any of the first 2. (8 judge calls)
Python worker stderr: 1. output 1 (could be 1-2)
Python worker stderr: 2. output 2 (could be 1-2)
Python worker stderr: 3. output 0 (could be 2-3)

│ the late delivery            │ [FAIL] Thank you for         │ [PASS] Sorry about the late  │ [FAIL] Refund sent for the   │
│                              │ reaching out! We value your  │ delivery. Your refund of $40 │ late delivery. Sorry.        │
│                              │ feedback about the late      │ is on its way today.         │ [[q=0.6]]                    │
```

The outputs are numbered from 0 in the order of your prompts and providers. "Could be 1-2" is the range of places the output could hold at 90% confidence: here the second and third replies are too close to separate after 8 calls, and the first is clearly last. promptfoo only records the winner, so read the log, or the grader's output in `promptfoo view`, when you need the ranges.

`promptfooconfig.yaml`:

```yaml
description: Supposing picks the best reply, in pairs

prompts:
  - "Thank you for reaching out! We value your feedback about {{topic}}. [[q=0.2]]"
  - "Sorry about {{topic}}. Your refund of $40 is on its way today. [[q=0.9]]"
  - "Refund sent for {{topic}}. Sorry. [[q=0.6]]"

providers:
  - echo   # returns each prompt as its output, so the three prompts are the three candidates

tests:
  - vars:
      topic: the late delivery
    assert:
      - type: select-best
        value: Which reply to an angry customer is most likely to keep them? Prefer specific, short and honest.
        provider:
          id: file://supposing_grader.py
          config:
            judge: mock
```

`supposing_grader.py`, a [promptfoo Python provider](https://www.promptfoo.dev/docs/providers/python/) used as the grader. The same file is in the engine's repository at `examples/promptfoo/`:

```python
from __future__ import annotations

import json
import sys
from typing import Any

from supposing import rank


def _text(output: Any) -> str:
    return output if isinstance(output, str) else json.dumps(output)


def call_api(prompt: str, options: dict, context: dict) -> dict:
    config = (options or {}).get("config") or {}
    test_vars = (context or {}).get("vars") or {}
    outputs, criteria = test_vars.get("outputs"), test_vars.get("criteria")
    if not outputs or not criteria:
        return {"error": "supposing_grader works only as the provider of a select-best assertion."}
    docs = {"output %d" % i: _text(o) for i, o in enumerate(outputs)}
    judge = config.get("judge", "quick")
    settings = {k: v for k, v in config.items() if k in ("rounds", "both_orders", "max_calls", "max_cost_usd", "seed")}
    try:
        ranking = rank(docs, criteria, judge=judge, **settings)
    except Exception as exc:  # a missing key or a bad judge name: show it in promptfoo, not a stack trace
        return {"error": "Supposing: %s" % exc}
    lines = ["%d. %s (could be %d-%d)" % (row["rank"], row["item_id"], row["rank_lo"], row["rank_hi"])
             for row in ranking.leaderboard]
    report = "%s (%d judge calls)\n%s" % (ranking.summary, len(ranking.comparisons), "\n".join(lines))
    print(report, file=sys.stderr)
    best = int(ranking.leaderboard[0]["item_id"].split()[-1])
    return {"output": "%d\n%s" % (best, report), "cost": ranking.cost_usd,
            "tokenUsage": {"numRequests": len(ranking.comparisons)}}
```

## Use a real judge

Change `judge: mock` to `judge: quick` and set `OPENROUTER_API_KEY`; replace `echo` with the providers or prompts you are comparing. `judge` takes the same values as everywhere else in Supposing: `quick`, `balanced`, `thorough`, an OpenRouter model id such as `anthropic/claude-haiku-5.5`, or `claude_cli` for local use ([Choosing a judge](choosing-a-judge.md)). The grader runs on your machine with your own key.

Other settings in `config`:

| Key | What it does |
|---|---|
| `rounds` | Opponents per output. Default 8: about 4 judge calls per output, and 8 for 2 or 3 outputs. More rounds narrow the ranges |
| `both_orders` | `true` judges every pair in both orders, and adds the order check. Doubles the calls |
| `max_calls` | A hard cap on judge calls per test case, for any judge |
| `max_cost_usd` | A hard cap on recorded spend per test case: no call starts that could pass it. Judges that report no price, such as `claude_cli`, cannot be capped this way |
| `seed` | Pairing and the ranges are deterministic for a given seed. Default 0 |

Each test case is one ranking, so a suite with 20 test cases and 3 outputs each makes about 20 × 8 = 160 judge calls. [What a run costs](../concepts/cost.md) turns calls into money.

## Why pairs

On four public datasets, comparing items in pairs agreed with human ratings or real outcomes more often than the same model scoring each item alone: 86% against 67% on ELLIPSE essays, 73% against 57% on PERSUADE essays, 77% against 71% on Upworthy headlines and 82% against 76% on UKPConvArg arguments. Pairs made more judge calls than scoring alone in each test. The method and the limits are in [Pairwise comparison or scoring each item](../concepts/pairwise-vs-scoring.md).
