Run your agent on a test suite
Run a test suite with judgeval test run or from your own code, and read each case's result.
A run has four steps: start the run, run your agent once per case, wait for the
cases to be scored, and read the results. Judgment does not call your agent;
your code runs it. For a Python agent, judgeval test run does all four. From
any other language, or for full control, do them from your own code.
Run your test suites with your coding agent
Paste it into your agent from your repo's root.
Prerequisites: a test suite with at
least one published version, and a
personal API key
from a member with the Developer role or higher, your organization ID, and
your project name. An ingest API key can't start a run. judgeval test run
needs judgeval 1.11.0 or later in Python. Starting runs from your own code
needs judgeval 1.4.0 or later.
Run with the judgeval command
From the folder that holds your agent's
.judgment/ folder:
judgeval test run # every suite in .judgment/judgment.json
judgeval test run --suite Baseline # one suite, by name or ID
judgeval test run --retry-failures # run failed cases once more before failingThe command starts a run of each suite's latest published version, calls the
run(case, environment) function in .judgment/run.py once per case, waits
for the scores, and prints each suite's result with a link to the run. It
exits with 0 when every case passed, 1 when a case failed, and 2 when the
suites could not be run. Every option is in
Set up tests in your repo.
If you are a coding agent: check that .judgment/judgment.json exists. If it
doesn't, follow
Set up tests in your repo
first. Tell the user that a run spends Judgment Units before you start one.
After the run, read each failed case with get_test_suite_run_results or the
printed link, and report the failed checks before you change any code.
Start a run from your own code
client.test_runs.run is the call behind the command. It starts a run, plays
every case through a function you give it, and waits for the scores:
from judgeval import Judgeval, Tracer
client = Judgeval(project_name="support-agent")
Tracer.init(project_name="support-agent", environment="development")
def run_case(case, environment):
support_agent(case["input"])
finished = client.test_runs.run(TEST_SUITE_ID, run_case, candidate="sonnet")
failed = [result for result in finished["results"] if result["passed"] is False]It needs judgeval 1.11.0 or later.
environment is the definition of the
test environment the
case names, or None.
To control each step yourself, or in TypeScript, start a run and loop over its
cases. Run your agent once per case with judgment.test.run_id and
judgment.test.case_id on the case's root span. The examples use the
Tracer.test_case() wrapper; setting the two attributes directly is
equivalent (see Connect your agent).
Judgment stores the traces under the Test Runs environment (see
Keep test traces separate).
import os
from judgeval import Judgeval, Tracer
client = Judgeval(project_name="support-agent")
Tracer.init(project_name="support-agent", environment="development")
run = client.test_runs.start(
TEST_SUITE_ID, candidate=os.environ["GIT_SHA"], trigger="ci"
)
for case in run["cases"]:
with Tracer.test_case(run["run_id"], case["case_id"]):
support_agent(case["input"])The run uses the test suite's latest published version. Each case in run["cases"]
has its case_id, the input to send to your agent, and the tagging headers
for an agent you call over HTTP. Judgment scores each case as soon as its trace
arrives.
Where test environments (in early access) are turned on, each case also has an
environment: the test environment it names, or None.
The run has the environments its cases name. Your code applies them before it plays
the case; see
If you play the cases yourself.
start accepts the following optional parameters:
| Option | Meaning |
|---|---|
case_filter | Which cases to run: {"kind": "all"} (default), {"kind": "tags", "tags": [...]}, {"kind": "case_ids", "case_ids": [...]}, or {"kind": "last_failures", "run_id": "..."}. |
candidate | What is being tested, such as a git SHA or agent version. |
candidate_meta | Any other details about the candidate, as an object. |
trigger | manual, ci, schedule, or api (default). |
stored_traces | true to grade each case's stored trace instead of running your agent. See Score stored traces. |
Keep test traces separate
Test runs send traces to the test suite's project, next to your production
traffic. Judgment stores them under the reserved environment Test Runs
(test_run in SQL and the API), whatever environment your agent reports. Filter
by it to see only test traces, or leave it out of a judge's or Issue
detection's environments.
A span is stored under Test Runs when it carries judgment.test.run_id.
judgeval test run, client.test_runs.run, and Tracer.test_case()
(TypeScript: Tracer.testCase()) put the ID on every span of the case's
trace, so you don't set anything. If you set the two attributes on the root span
yourself, the trace's other spans can keep the environment your agent reports,
so also give those runs a non-production environment in Tracer.init. See
Environment.
Don't name one of your own environments test_run: its traces would be treated
as test runs.
What leaves Test Runs out:
- SQL that the Judgment Agent or your coding agent runs with
sql_query. Read a run's results on its page or with the test suite tools instead. - Issue detection, unless you select Test Runs under its environments.
Judges with online monitoring still evaluate test traces when their scope is every environment or includes Test Runs; see Environments.
Read results
client.test_runs.get(run_id) returns the run's counts:
| Field | Meaning |
|---|---|
cases_total | Cases in the run. |
cases_with_output | Cases whose trace has arrived. |
cases_scored | Cases that have been scored. |
cases_passed | Scored cases whose checks all passed. |
The run is finished when cases_scored equals cases_total.
client.test_runs.list(test_suite_id) lists a test suite's runs, newest first.
client.test_runs.results(run_id) returns one entry per case, with one entry
per check:
{
"case_id": "…", "trace_id": "…", "scored_at": "…", "passed": false,
"checks": [{
"judge_id": "…", "judge_major_version": 1, "judge_minor_version": 0,
"op": "eq", "expected": "Yes", "rationale": "Double charges are refunded",
"judge_output": "No", "passed": false,
"reason": "The final answer does not mention a refund. <citation …>",
"error_message": null
}]
}Fail a CI job on a failed case
In CI, run the command. It exits with 1 when a case fails, which fails the
job:
judgeval test run --retry-failuresSet up tests in your repo has a complete GitHub Actions workflow and the secrets it needs.
If you start runs from your own code instead, wait until every case is scored and exit with an error when any case did not pass:
import sys
import time
Tracer.force_flush()
while (summary := client.test_runs.get(run["run_id"]))["cases_scored"] < summary["cases_total"]:
time.sleep(5)
if summary["cases_passed"] < summary["cases_total"]:
sys.exit(f"{summary['cases_total'] - summary['cases_passed']} test cases failed")A run has no deadline, so give the CI job a timeout in case a trace never
arrives (see a case is never scored).
judgeval test run and client.test_runs.run give up 15 minutes after the
last case was played. Store JUDGMENT_API_KEY as a CI secret. It must be a
personal API key from a member with the Developer role or higher.
Retry failures
judgeval test run --retry-failures runs the failed cases once more and counts
a case that then passes as passed. Agents and model judges vary from run to
run, so one retry separates a flaky case from a real failure.
From your own code, start a new run with
{"kind": "last_failures", "run_id": "<earlier run>"} as its case_filter. It
runs only the cases that failed or were never scored in that run.
Score an existing run
After you change a test suite's cases and publish a new version, you can score the existing traces again without rerunning your agent:
new_run = client.test_runs.score(run_id)This creates a new run, scored with the test suite's latest published version, from
the cases that have a trace and whose input did not change. The original run
is not modified. The new run's cases are scored in the background, so poll
client.test_runs.get(new_run["run_id"]) for progress. When the run already
used the latest version, the new run scores the same traces again. If no case
qualifies, no new run is created.
Score stored traces
To grade traces you already have, for example to check a judge against your
own labels, give each case the trace in metadata.trace_id (cases added from
traces already have it) and start the run with "stored_traces": true. Each
case is scored on its stored trace, without running your agent. Every case in
the published version needs a trace_id from this project, or the run doesn't
start. This needs judgeval 1.7.0 or later in Python, or 1.6.0 or later in
TypeScript:
from judgeval import Judgeval
client = Judgeval(project_name="support-agent")
run = client.test_runs.start(TEST_SUITE_ID, stored_traces=True)
# Don't run your agent: each case is graded on its stored trace.
print(client.test_runs.get(run["run_id"]))With MCP, call start_test_suite_run with stored_traces: true. Over the
HTTP API, send {"stored_traces": true} as the body of
POST /test-suites/{test_suite_id}/runs.
To add cases from traces, see Add cases from traces.
Troubleshooting: a case is never scored
A run has no deadline, so a case waits until its trace arrives. If
cases_with_output stays below cases_total, check that:
- The attributes are on the root span, not a child span.
- The trace lands in the test suite's project.
- Only
baggageis sent to a deployed agent. Atraceparentfrom a span Judgment never receives leaves the case without a root span. - Each case runs as its own trace. Judgment scores the first trace for each case and ignores later traces.
If a check fails with error_message: "Judge not found", its judge was
deleted after the case was created. A check can also fail with
rate limited or usage limit reached when your organization hits a limit.
Use the HTTP API
Every SDK call maps to a route under
https://api.judgmentlabs.ai/v1/projects/$JUDGMENT_PROJECT_ID/test-suites, called
with your API key and organization ID:
| SDK | Route |
|---|---|
test_runs.start(test_suite_id) | POST /test-suites/{test_suite_id}/runs |
test_runs.list(test_suite_id) | GET /test-suites/{test_suite_id}/runs |
test_runs.get(run_id) | GET /test-suites/runs/{run_id} |
test_runs.results(run_id) | GET /test-suites/runs/{run_id}/results |
test_runs.score(run_id) | POST /test-suites/runs/{run_id}/score |
Starting and scoring runs need the Developer role or higher; reading them needs Viewer.
curl --fail-with-body \
-H "Authorization: Bearer $JUDGMENT_API_KEY" \
-H "X-Organization-Id: $JUDGMENT_ORG_ID" \
-H "Content-Type: application/json" \
-X POST "https://api.judgmentlabs.ai/v1/projects/$JUDGMENT_PROJECT_ID/test-suites/$TEST_SUITE_ID/runs" \
--data '{"candidate": "'"$GIT_SHA"'", "trigger": "ci"}'