Evaluation¶
The eval framework lets you systematically test agents against datasets with multiple scorers. It runs entirely offline — no cloud service required. Supports built-in scorers, LLM-as-judge, custom code scorers, trajectory evaluation, and multi-turn session scoring.
Quick Start¶
from fastaiagent.eval import evaluate
def my_agent(input_text: str) -> str:
# Your agent logic here
return input_text.upper()
results = evaluate(
agent_fn=my_agent,
dataset=[
{"input": "hello", "expected": "HELLO"},
{"input": "world", "expected": "WORLD"},
],
scorers=["exact_match"],
)
print(results.summary())
# Evaluation Results
# ==================================================
# exact_match: avg=1.00 pass_rate=100% (2 scored)
# cases: 2 passed, 0 failed, 0 errored (pass_rate=100%)
The evaluate() Function¶
from fastaiagent.eval import evaluate
results = evaluate(
agent_fn=my_agent, # Any callable: function, agent.run, lambda
dataset=dataset, # Dataset, file path, or list of dicts
scorers=["exact_match"], # Built-in names or Scorer instances
concurrency=4, # Parallel evaluation (default: 4)
)
agent_fn accepts any callable that takes a string and returns a string (or an object with .output):
# Plain function
evaluate(agent_fn=lambda x: x.upper(), dataset=dataset)
# Agent.run (support_agent is an Agent)
evaluate(agent_fn=support_agent.run, dataset=dataset)
# Custom wrapper
def run_pipeline(input_text):
result = chain.execute({"message": input_text})
return result.output
evaluate(agent_fn=run_pipeline, dataset=dataset)
Each case is linked to the trace its run produced, so the Local UI opens the run
behind a verdict. An AgentResult names its trace; for any other return value
(another framework's agent behind a plain function, traced through
enable_otel_capture()) the case takes the first
trace the call started. Before 1.96.0 such cases had no trace.
Async¶
Every entry point has an a-prefixed coroutine — aevaluate, asimulate,
agenerate_scenarios, aharden — for use inside async apps (FastAPI, etc.); the
sync versions just wrap them. aevaluate lives in fastaiagent.eval.evaluate:
from fastaiagent.eval.evaluate import aevaluate
results = await aevaluate(agent.run, dataset, scorers=["contains"])
See examples/79_async_eval.py for the full async loop.
Datasets¶
From a List¶
from fastaiagent.eval import Dataset
dataset = Dataset.from_list([
{"input": "What is 2+2?", "expected": "4"},
{"input": "Capital of France?", "expected": "Paris"},
])
From JSONL¶
From a failing trace (Replay → regression test)¶
Failed production traces are the most valuable test cases — they're
the bugs your users actually hit. Once you've debugged one with
Replay, ReplayResult.save_as_test() appends
the corrected case directly to the JSONL dataset evaluate() reads:
rerun = replay.fork_at(step=3).modify_prompt("...").rerun()
rerun.save_as_test(
"regression_tests.jsonl",
input="...",
expected_output=str(rerun.new_output),
source_trace_id=failure.trace_id, # provenance back to the bug
)
# Same file, now ready for evaluate()
results = evaluate(
agent_fn=agent.run,
dataset="regression_tests.jsonl",
scorers=["exact_match"], # or LLMJudge for semantic checks
)
The Local UI's Save as regression test button calls the same underlying endpoint and writes an identical record — UI-saved and code-saved cases are interchangeable. See Replay → From a Rerun to a Regression Test for the full walkthrough.
From captured traces in bulk (curation)¶
To turn many captured traces into a dataset at once — by favorites, notes,
guardrail-fired, or all — use Dataset.from_traces(...) or
fastaiagent eval curate. See Trace Curation.
From CSV¶
Dataset Item Fields¶
Each item is a dict. The only required field is input. Other common fields:
| Field | Used By | Description |
|---|---|---|
input |
All scorers | The input to send to the agent |
expected or expected_output |
ExactMatch, Contains, LLMJudge | The expected correct answer |
conversation |
Session scorers (not forwarded by evaluate(); see below) |
Multi-turn chat history |
expected_trajectory |
Trajectory scorers | Expected tool call sequence |
tags |
Filtering | Labels for grouping test cases |
What
evaluate()forwards to scorers: per case, the eval loop passesinput, the agent's output, andexpected/expected_output, plus these dataset row fields when the row has them:expected_trajectory,expected_tool_calls,expected_steps,contextandcontexts. It also passes what the run measured (cost,cost_known,latency_ms), the agent's tool calls (actual_trajectory,actual_tool_calls,actual_steps, only when the agent returns anAgentResult), and any extra keyword arguments you giveevaluate()(applied to every case; a row's own value wins for the row fields). It does not forwardconversation(orgoal): pass them as keyword arguments toevaluate()or call the scorer's.score(...)directly per case. See the trajectory and session docs.
Built-in Scorers¶
ExactMatch¶
Passes if the agent's output exactly matches the expected output (whitespace trimmed).
from fastaiagent.eval.builtins import ExactMatch
scorer = ExactMatch()
result = scorer.score(input="q", output="Hello", expected="Hello")
# score=1.0, passed=True
Contains¶
Passes if the expected text appears anywhere in the output (case-insensitive).
from fastaiagent.eval.builtins import Contains
scorer = Contains()
result = scorer.score(input="q", output="The answer is 42", expected="42")
# score=1.0, passed=True
JSONValid¶
Passes if the output is valid JSON.
from fastaiagent.eval.builtins import JSONValid
scorer = JSONValid()
scorer.score(input="q", output='{"key": "value"}') # passed=True
scorer.score(input="q", output="not json") # passed=False
RegexMatch¶
Passes if the output matches a regex pattern.
from fastaiagent.eval.builtins import RegexMatch
scorer = RegexMatch(pattern=r"\d{3}-\d{4}")
scorer.score(input="q", output="Call 555-1234") # passed=True
LengthBetween¶
Passes if the output length is within a range.
from fastaiagent.eval.builtins import LengthBetween
scorer = LengthBetween(min_len=10, max_len=500)
scorer.score(input="q", output="Short") # passed=False (5 chars)
scorer.score(input="q", output="A longer answer") # passed=True
Latency¶
Passes if execution latency is under a threshold.
evaluate() supplies latency_ms automatically — from the AgentResult when
your callable returns one, and measured around the call otherwise, so the gate
works even for a callable that returns a bare string. You can still pass it by
hand when calling the scorer directly.
from fastaiagent.eval.builtins import Latency
# Inside evaluate(): latency is filled in for you.
evaluate(agent_fn=agent.run, dataset=ds, scorers=[Latency(max_ms=2000)])
# Standalone:
scorer = Latency(max_ms=2000)
scorer.score(input="q", output="answer", latency_ms=1500) # passed=True
scorer.score(input="q", output="answer", latency_ms=3000) # passed=False
CostUnder¶
Passes if the run's USD spend is under a threshold.
evaluate() supplies cost and cost_known automatically from the
AgentResult. A value you pass yourself still wins.
from fastaiagent.eval.builtins import CostUnder
evaluate(agent_fn=agent.run, dataset=ds, scorers=[CostUnder(max_usd=0.05)])
scorer = CostUnder(max_usd=0.05)
scorer.score(input="q", output="answer", cost=0.03) # passed=True
Unknown cost is not treated as free
When the model has no rate in the pricing table (a private fine-tune, a
bedrock or azure deployment id), the run cannot be priced and cost_known
is False. CostUnder then reports errored (could not run) and says
so, rather than certifying a run it never priced; a gate on it is invalid
(exit 3). Before 1.89.0 it reported a fail. Give the model a rate with
set_rate_overrides() or the pricing block of models.json to gate on it
properly. A direct .score() call with no cost at all is skipped, and so
is Latency with no latency_ms; inside evaluate() both are always
supplied.
Self-hosted providers are the exception, and they are a known zero
rather than an unknown: ollama, lmstudio and vllm run on hardware you
already pay for, so a run costs nothing to the provider, cost_known is
True, and a budget gate passes it. Treating local inference as unpriced
would refuse every laptop running ollama — a false positive, not a safety
property.
Before 1.67.0 both of these could not fail
evaluate() passed neither cost nor latency_ms, so both scorers read
0 out of an empty **kwargs and passed every case — CostUnder reported
"Cost: $0.0000" and Latency "Latency: 0ms" regardless of the run. If you
have a CostUnder or Latency gate in CI that has always been green,
expect it to start giving a real verdict.
Using by Name¶
Pass built-in scorer names as strings to evaluate():
results = evaluate(
agent_fn=my_agent,
dataset=dataset,
scorers=["exact_match", "contains"], # Resolved automatically
)
Available names (resolved by evaluate() automatically):
- Core:
exact_match,contains,json_valid,regex_match,length_between,latency,cost_under - RAG:
faithfulness,answer_relevancy,context_precision,context_recall - Safety:
toxicity,bias,pii_leakage,prompt_injection,moderation - Agent metrics:
task_completion,hallucination,reflection_quality - Similarity:
semantic_similarity,bleu,rouge,levenshtein
Trajectory and session scorers are not string-resolvable: pass an instance (e.g.
ToolUsageAccuracy()). Since 1.89.0evaluate()gives trajectory scorers the agent's own tool calls (whenagent_fnreturns anAgentResult) and each row'sexpected_trajectory; see Trajectory Scoring. Session scorers take conversation turns: call.score(turns=...)directly; see Session Scoring.A dataset row's
expected_trajectory,expected_tool_calls,expected_steps,contextandcontextsreach every scorer for that row, so RAG scorers can be driven per case too.
Custom Code Scorers¶
The @Scorer.code Decorator¶
from fastaiagent.eval import Scorer, ScorerResult
@Scorer.code("has_greeting")
def has_greeting(input, output, expected=None):
"""Check if the output starts with a greeting."""
greetings = ["hello", "hi", "hey", "greetings"]
starts_with_greeting = any(output.lower().startswith(g) for g in greetings)
return ScorerResult(
score=1.0 if starts_with_greeting else 0.0,
passed=starts_with_greeting,
reason=f"Starts with greeting: {starts_with_greeting}",
)
# Use in evaluation
results = evaluate(agent_fn=my_agent, dataset=dataset, scorers=[has_greeting])
Return Types¶
Custom scorers can return different types:
# Return ScorerResult (full control)
@Scorer.code("detailed")
def detailed(input, output, expected=None):
return ScorerResult(score=0.8, passed=True, reason="Almost perfect")
# Return bool (simple pass/fail)
@Scorer.code("simple")
def simple(input, output, expected=None):
return len(output) > 10
# Return float (score, passed if >= 0.5)
@Scorer.code("scored")
def scored(input, output, expected=None):
return len(output) / 100 # Score based on length
# Not a verdict (1.89.0): could not run, or does not apply to this case
@Scorer.code("needs_context")
def needs_context(input, output, expected=None, context=None):
if not context:
return ScorerResult.not_applicable("No context on this row")
try:
return ScorerResult(score=check(output, context), passed=True)
except TimeoutError as e:
return ScorerResult.error(f"checker timed out: {e}")
A scorer that raises is recorded as errored for that case, and the run carries on; it used to abort the whole run with nothing persisted.
EvalResults¶
Summary¶
results = evaluate(agent_fn=my_fn, dataset=data, scorers=[ExactMatch(), Contains()])
print(results.summary())
# Evaluation Results
# ==================================================
# exact_match: avg=0.80 pass_rate=80% (10 scored)
# contains: avg=0.95 pass_rate=95% (10 scored)
# cases: 8 passed, 2 failed, 0 errored (pass_rate=80%)
A scorer's rate covers its verdicts only; errored and skipped results are
counted beside it ((8 scored, 1 errored, 1 skipped)). The cases line counts
dataset rows: a row passes only when every check that applied to it passed.
See Four outcomes, cases and checks.
Accessing Scores¶
for scorer_name, scores in results.scores.items():
for s in scores:
print(f"{scorer_name}: score={s.score}, passed={s.passed}, reason={s.reason}")
Export¶
Produces:
{
"exact_match": [
{"score": 1.0, "passed": true, "reason": null},
{"score": 0.0, "passed": false, "reason": null}
],
"contains": [...]
}
Compare¶
Compare two evaluation runs:
results_v1 = evaluate(agent_fn=agent_v1, dataset=data, scorers=scorers)
results_v2 = evaluate(agent_fn=agent_v2, dataset=data, scorers=scorers)
print(results_v1.compare(results_v2))
# Comparison
# ==================================================
# exact_match: 0.80 → 0.90 (+0.10)
# contains: 0.95 → 0.98 (+0.03)
Combining Multiple Scorers¶
results = evaluate(
agent_fn=my_agent,
dataset=Dataset.from_jsonl("test_cases.jsonl"),
scorers=[
ExactMatch(), # Exact string match
Contains(), # Substring check
LengthBetween(min_len=20, max_len=500), # Length constraint
has_greeting, # Custom code scorer
LLMJudge(criteria="helpfulness"), # LLM-as-judge
],
)
DecisionJudge (1.84.0) is the probability-based alternative to LLMJudge. It
asks OpenAI's Decisions API whether a criterion holds, so
there's no verdict to parse. See
LLM judge → DecisionJudge.
ScorerResult¶
| Field | Type | Description |
|---|---|---|
score |
float |
Numeric score (0.0-1.0) |
passed |
bool |
Whether the test case passed |
reason |
str \| None |
Explanation of the score |
errored |
bool |
The check could not run (1.89.0) |
skipped |
bool |
The check does not apply to this case (1.89.0) |
details |
dict \| None |
Non-content extras, e.g. a judge's raw_score, a trajectory's tool names |
outcome |
property | "passed", "failed", "errored" or "skipped" |
An errored or skipped result always has passed=False and score=0.0.
Build them with ScorerResult.error(reason) and
ScorerResult.not_applicable(reason). In local.db a case's stored verdict
adds errored, skipped or details only when set, so a run without them is
stored exactly as before.
CLI Commands¶
# Run a dataset against an agent and gate on the result
fastaiagent eval run \
--agent app/agents.py:support_agent \
--dataset cases.jsonl \
--scorers exact_match,faithfulness \
--fail-under "overall.pass_rate=0.9" \
--max-error-rate 0.1 \
--json report.json
# Compare two persisted runs (baseline first)
fastaiagent eval compare main pr --tolerance 0.02
# Turn production failures into eval cases
fastaiagent eval curate --filter failed -o cases/regressions.jsonl
Exit codes: 0 gate passed · 1 quality gate failed · 3 run invalid
(infra error rate exceeded, a gated scorer could not run, nothing scored, the
dataset was not found, or the agent module failed to import). A malformed
--agent spec or a missing attribute is a usage error and exits 2.
--fail-under "cases.pass_rate=0.8" gates on cases instead of checks. See
Agent CI for the
pytest-native gate, baselines, and the GitHub Actions recipe.
The equivalent Python API:
from fastaiagent.eval import evaluate
results = evaluate(
agent_fn=my_agent,
dataset="test_cases.jsonl",
scorers=["exact_match", "contains"],
)
print(results.summary())
Error Handling¶
from fastaiagent._internal.errors import EvalError
try:
results = evaluate(
agent_fn=my_agent,
dataset=data,
scorers=["nonexistent_scorer"],
)
except ValueError as e:
print(f"Unknown scorer: {e}")
Platform Integration¶
When connected to the FastAIAgent Platform, you can pull shared datasets, publish results, and pull scorer configs:
import fastaiagent as fa
fa.connect(api_key="fa_k_...", target="https://<label>-dev.api.fastaiagent.net", project="my-project")
# Pull dataset from platform
dataset = Dataset.from_platform("golden-test-set")
# Run eval locally — scoring happens on your machine
results = evaluate(agent, dataset=dataset)
# Publish results to platform dashboard
results.publish(run_name="v2.1-release-candidate")
# Push a local dataset to platform for team sharing
local_dataset = Dataset.from_jsonl("my_tests.jsonl")
local_dataset.publish("regression-tests")
# Pull scorer config from platform (e.g., LLM judge)
scorer = Scorer.from_platform("correctness-judge")
results = evaluate(agent, dataset=dataset, scorers=[scorer])
results.publish()
All eval execution runs locally (your scorers, your LLM costs). The platform provides dataset sharing, result dashboards, and score trend tracking.
Internals¶
For contributors who need to understand the evaluation loop, scorer resolution pipeline, how built-in scorers are implemented (pure code vs LLM-based vs embedding-based), or how to add a new scorer, see Evaluation System Internals.
Next Steps¶
- LLM Judge — Use an LLM to evaluate output quality
- RAG Metrics — Faithfulness, relevancy, and context evaluation
- Safety Metrics — Toxicity, bias, and PII detection
- Similarity Metrics — Embedding-based and classical NLP metrics
- Trajectory Scoring — Evaluate the path an agent took
- Session Scoring — Evaluate multi-turn conversations
- Trace Curation — Build datasets from captured agent traces
- Agents — Build agents to evaluate