ExAgent.Eval (ExAgent v2.3.0)

View Source

Measure an agent on a set of cases: run each case, score the result with evaluators and summarize pass rate, latency, usage and cost.

cases = [
  ExAgent.Eval.Case.new("refund", "Refund order 7", expected: "refunded"),
  ExAgent.Eval.Case.new("lookup", "Where is order 9?", expected: "shipped")
]

report =
  ExAgent.Eval.run(agent, cases,
    evaluators: [
      ExAgent.Eval.contains(),
      ExAgent.Eval.tool_called("lookup_order"),
      ExAgent.Eval.judge(model: reviewer, criteria: "Polite and accurate.")
    ],
    concurrency: 4,
    repeat: 3
  )

IO.puts(ExAgent.Eval.Report.format(report))
report.summary.pass_rate

Use it to compare prompts, models or tools before shipping a change (ExAgent.Eval.Report.compare/2 lists the cases that regressed), or in CI with a pass-rate threshold. Runs call the model for real unless the agent uses a test model, so cost is real; repeat measures how stable the agent is.

Targets

The target is an %ExAgent{} agent, run with ExAgent.run/3, or a function fn input, run_opts -> ExAgent.run(...) end returning {:ok, result} or {:error, reason}, for agents that need per-case setup.

Evaluators

An evaluator has a name and a function of the case's outcome (outcome/0) that returns true, false, a score between 0 and 1, {score, reason} or :skip. A result passes an evaluator when its score is at least the evaluator's threshold (default 1.0, so a boolean must be true). Build one with evaluator/3 or use the built-ins: equals/1, contains/1, matches/2, tool_called/2 and judge/1. A case whose run fails, raises or times out gets no scores and does not pass; an evaluator that raises scores 0.

Summary

Types

What evaluators receive for one run of one case.

Functions

The text output contains the case's :expected (a string, or a list of strings that must all appear). The score is the fraction found. Options: case_sensitive (default false), :name, :threshold.

The output equals the case's :expected. For strings, trim: true (default) ignores surrounding whitespace and case_sensitive: false ignores case. Option :name (default "equals") names it in reports.

Build an evaluator from a name and a function of outcome/0. Options: :threshold (default 1.0).

A reviewer model grades the answer against :criteria and replies PASS or FAIL with a reason.

The text output matches regex. Option: :name (default "matches").

Run every case and score it.

The run called the tool name. :args (a function of the arguments map) narrows the match; :times requires an exact number of calls.

Types

outcome()

@type outcome() :: %{
  case: ExAgent.Eval.Case.t(),
  output: term(),
  result: ExAgent.result() | nil,
  error: term() | nil
}

What evaluators receive for one run of one case.

score()

@type score() :: boolean() | number() | {boolean() | number(), String.t()} | :skip

Functions

contains(opts \\ [])

@spec contains(keyword()) :: ExAgent.Eval.Evaluator.t()

The text output contains the case's :expected (a string, or a list of strings that must all appear). The score is the fraction found. Options: case_sensitive (default false), :name, :threshold.

equals(opts \\ [])

@spec equals(keyword()) :: ExAgent.Eval.Evaluator.t()

The output equals the case's :expected. For strings, trim: true (default) ignores surrounding whitespace and case_sensitive: false ignores case. Option :name (default "equals") names it in reports.

evaluator(name, fun, opts \\ [])

@spec evaluator(String.t(), (outcome() -> score()), keyword()) ::
  ExAgent.Eval.Evaluator.t()

Build an evaluator from a name and a function of outcome/0. Options: :threshold (default 1.0).

judge(opts)

@spec judge(keyword()) :: ExAgent.Eval.Evaluator.t()

A reviewer model grades the answer against :criteria and replies PASS or FAIL with a reason.

The reviewer sees the task, a transcript of the run (tool calls and results included, so facts the agent looked up count as supported) and the answer, each bounded by :max_transcript_bytes; images and files in the task appear as placeholders. The case's :expected, when present, is shown as the key content a correct answer must convey, not as the only acceptable wording.

Options: :model and :criteria (required), :model_settings, :max_transcript_bytes (default 16_000, at least 1_024), :name. The reviewer's usage is not included in the report; an unusable reply scores 0 with the error as reason.

matches(regex, opts \\ [])

@spec matches(Regex.t(), keyword()) :: ExAgent.Eval.Evaluator.t()

The text output matches regex. Option: :name (default "matches").

run(target, cases, opts \\ [])

@spec run(
  ExAgent.t() | (term(), keyword() -> term()),
  [ExAgent.Eval.Case.t()],
  keyword()
) ::
  ExAgent.Eval.Report.t()

Run every case and score it.

Options:

  • :evaluators — list of ExAgent.Eval.Evaluator.t/0 (default []: a case passes when its run succeeds).
  • :concurrency — runs (and scorings) at a time (default 4).
  • :timeout — milliseconds per run (default 120_000); a run that exceeds it is killed and its result's error is :timeout.
  • :evaluator_timeout — milliseconds to score one run with all evaluators (default 120_000); when exceeded, every evaluator of that run fails with a timeout reason. Scoring time is not part of the run's duration_ms.
  • :repeat — runs per case (default 1).
  • :run_opts — options for every run, merged under each case's own.

Case names must be unique. A target that raises, exits or throws produces a failed result; so does an evaluator, which then scores 0 and fails regardless of its threshold.

tool_called(name, opts \\ [])

@spec tool_called(String.t(), keyword()) :: ExAgent.Eval.Evaluator.t()

The run called the tool name. :args (a function of the arguments map) narrows the match; :times requires an exact number of calls.