Skip to Content
Judgment Labs
Tests

Set up tests in your repo

Add a .judgment folder to your agent's repo, run your test suites with one command, and gate pull requests on the result.

The judgeval command runs your test suites against the agent in your repo. You tell it once how to run your agent on one test case. After that, judgeval test run plays every case, waits for the scores and fails when a case fails.

Set up Judgment tests with your coding agent

Paste it into your agent from your repo's root.

At the end of this guide, your repo has a .judgment/ folder, judgeval test run passes locally, and a CI job runs it on every pull request.

Prerequisites:

What the folder holds

your-agent/
  .judgment/
    judgment.json        # your project, and the test suites to run by default
    run.py               # run(case, environment): runs your agent on one case
    environments/        # optional: test environments as files, one per environment
      baseline.json

Commit the whole folder. It holds no credentials.

FileWhat it is
judgment.json{"project": "<project name>", "suites": ["<suite name or ID>"]}. project is the Judgment project the suites live in. suites lists the test suites judgeval test run runs when you name none.
run.pyDefines run(case, environment). judgeval test run calls it once per case. You write its body: it sends case["input"] to your agent.
environments/<name>.jsonThe seed data and mocked tool results a case runs in. See test environments.

How a run reaches your agent

Nothing here is hidden. When you run judgeval test run, this happens in order:

  1. The command reads .judgment/judgment.json for the project and the suites.
  2. For each suite, it starts a run of the suite's latest published version, and Judgment returns the cases.
  3. The command calls your run(case, environment) once per case, four cases at a time. case["input"] is the case's input. Your run calls your agent, and the trace the agent produces is tagged with the run and the case, so Judgment knows which case it belongs to.
  4. Judgment scores each case's trace with the judges the case lists. The command waits, prints each suite's result, and exits 1 if any case failed.

So the only code that connects a case to your agent is run. environment is None unless the case names a test environment. How an environment reaches your agent follows a definition from the case to your tools.

Instructions for the coding agent

Work through these steps in order, from the agent's root folder: the folder you run the agent from, where its top-level package is importable. Tell the user what you're doing at each step in one line. Never print, log or commit an API key.

1. Check what is in place

  1. If .judgment/judgment.json exists, read it and .judgment/run.py, keep them, and finish only the steps that are missing.
  2. Check that the agent is traced with judgeval: it calls Tracer.init, and its entry point is observed. If it isn't, stop and follow Set up Judgment first.
  3. Check the installed version with judgeval version (prefix commands with uv run in a uv project). If it is below 1.10.2, upgrade judgeval in the project's dependency file.
  4. Check that the repo's .env sets JUDGMENT_API_KEY and JUDGMENT_ORG_ID. If the key starts with jli_, it is an ingest API key. Ask the user to create a personal API key under Settings > Personal API Keys and put it in .env themselves. Never ask for a key in chat.
  5. Find the project name the agent passes to Tracer.init. Tests use the same project.

2. Create the folder

judgeval test init --project <project name> --suite Baseline

This writes two files and prints their paths:

Wrote .judgment/judgment.json
Wrote .judgment/run.py

--suite names the test suite that judgeval test run runs by default: your baseline suite, the cases every change must keep passing. Give its name or ID. Repeat --suite for more than one. The command doesn't check that the suite exists, so you can name it now and create it in step 4.

Always pass --project and --suite when you run this command as a coding agent or a script. The command only asks questions on a terminal:

On a terminalWithout a terminal
No --projectLists the organization's projects and asks which one.Exits with code 2: Pass --project: there is no terminal to ask on.
No --suiteLists the project's test suites and asks which is the baseline. Create a new test suite creates an empty one, named Baseline unless you type another name.Writes "suites": [] and prints No test suite set, with how to add one: put its name or ID in "suites", or pass --suite to judgeval test run.

The command needs JUDGMENT_API_KEY and JUDGMENT_ORG_ID, and exits with code 2 when either is missing. It never replaces a file: if .judgment/judgment.json or .judgment/run.py exists, it exits with code 2 and names the file, before it asks anything.

3. Write run.py

The generated run.py imports a placeholder agent. Replace the import and the body so that run sends one case to your real agent:

"""How Judgment's test suites run this agent. `judgeval test run` calls run() once per case."""

from booking_agent import agent, store


def run(case, environment):
    # environment is None for a case that names no test environment.
    seed = environment["seed"] if environment else store.DEFAULT_SEED
    with store.temporary(seed):  # a store that only this case uses
        return agent.reply(case["input"]["message"])

booking_agent, store.temporary and store.DEFAULT_SEED stand for your own code. The rules run must follow:

  • Import the agent from the root folder. .judgment/run.py is loaded with the folder you ran the command in on the import path. Files next to run.py are not importable.
  • Call the agent the way production does: the same entry point, system prompt and tools.
  • Read the input in the shape your cases use. case["input"] is whatever the case stores, such as {"message": "..."}. case["case_id"] identifies the case.
  • Produce exactly one trace per call. If your agent's entry point is already observed, calling it once does that. If one case is a conversation of several turns, put @Tracer.observe(span_type="agent") on run, so every turn lands in one trace.
  • Keep per-case state out of module globals. Cases run four at a time, each in its own thread. Give each case its own store, as above, or pass --concurrency 1.
  • run may be async. Its return value is ignored. Judgment scores the trace.

If the agent calls Tracer.init when it is imported, it keeps that tracer, so set its environment to development or ci for test runs, for example from an environment variable that run.py sets before the import. If nothing has started a tracer, the command starts one for the project with environment="development". Either way, test traces stay apart from production traffic; see Keep test traces separate.

4. Create the baseline suite

If the project has no suite with the name you gave in step 2, create it:

  1. Load the build-test-suite skill with the Judgment MCP tool get_skill, if you have it.
  2. Find the project's judges with list_judges. Each check on a case needs a judge's ID and version.
  3. Create a suite named Baseline with create_test_suite, add 5 to 10 cases with add_test_cases, and publish it with publish_test_suite. Take inputs from real traces of behavior that works today. Create and edit a test suite has the case format and the same calls in the SDK.
  4. Check that "suites" in .judgment/judgment.json holds the suite's exact name or its ID.

Only a published version runs. A suite with a draft and no published version has no cases to run.

5. Add test environments

Skip this step if every case can run against the agent's normal development data.

A case that only means something in a particular situation, such as an order at a policy edge or a service that is down, names a test environment. Follow Run cases in a test environment: write .judgment/environments/<name>.json, publish it with judgeval test env push <name>, and name it on the case.

6. Run the suites

A test run spends Judgment Units on its judge evaluations. Tell the user before the first run.

judgeval test run

The command plays every case, waits for the scores, and prints one result per suite with a link to the run:

### Baseline: 5/6 passed ([run](https://app.judgmentlabs.ai/org/.../offline-tests/.../runs/...))
- **Refund outside the window is refused**
  - Refund decision: expected eq `Refused`, got `Refunded`
Exit codeMeaning
0Every case passed. A case with no checks counts as neither passed nor failed.
1At least one case failed. The last line says how many.
2The suites could not be run: a missing .judgment/ file, a suite the project doesn't have, a rejected API key, an error raised inside run, or cases that were never scored. The reason is printed.
OptionMeaning
--suite, -sRun this suite, by name or ID, instead of the ones in judgment.json. Repeat for each.
--retry-failuresRun the failed cases once more, and count a case that then passes as passed. Agents and model judges vary from run to run, so use this in CI.
--candidateA label for what is being tested, such as a git SHA or a model name. In GitHub Actions it defaults to the commit.
--concurrencyHow many cases run at a time. The default is 4.

Read a failed case before you change anything: open the run link, or call get_test_suite_run_results with the run's ID. A failed check says what the judge returned and why.

7. Run the suites in CI

Add one job that runs the same command. This is a complete GitHub Actions workflow for a uv project:

name: Judgment tests

on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  judgment-tests:
    # Pull requests from forks receive no secrets.
    if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
    runs-on: ubuntu-latest
    timeout-minutes: 45
    env:
      JUDGMENT_API_KEY: ${{ secrets.JUDGMENT_API_KEY }}
      JUDGMENT_ORG_ID: ${{ vars.JUDGMENT_ORG_ID }}
      ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
    steps:
      - uses: actions/checkout@v4
      - uses: astral-sh/setup-uv@v6
      - run: uv run judgeval test run --retry-failures

Change three things for your repo:

  • Working directory. If the agent's root folder is not the repo root, add defaults: { run: { working-directory: <folder> } } to the job.
  • Provider keys. Replace ANTHROPIC_API_KEY with the secrets your agent needs to run.
  • Install step. Without uv, set up Python, install the project's dependencies, and run judgeval test run --retry-failures.

The job needs these values. Ask the user to add them in the repository's Settings > Secrets and variables > Actions; don't read or set them yourself.

NameKindValue
JUDGMENT_API_KEYSecretA personal API key from a member with the Developer role or higher. An ingest API key (it starts with jli_) is rejected with 403 when the run starts.
JUDGMENT_ORG_IDVariableThe organization ID.
Your provider keysSecretWhatever the agent needs to call its model.

In GitHub Actions the command also records the commit, the pull request and the job on the run, and adds each suite's result to the job summary. A run gives up 15 minutes after its last case was played if cases are still unscored, so the job timeout above covers one suite and its retry. Raise it when you add suites.

Open a pull request and check that the job runs and passes. Then tell the user what you set up: the suite and its cases, the files you added, and the secrets the job still needs.

If something goes wrong

SymptomFix
No .judgment/judgment.json in <folder>Run the command from the agent's root folder, or run judgeval test init there first.
Already exists: .judgment/... from judgeval test initThe folder is already set up. Edit the files instead.
No test suites to runAdd the suite's name or ID to "suites" in .judgment/judgment.json, or pass --suite.
No test suite with the name or ID '...' in this projectThe name must match exactly, and the suite must be in the project that judgment.json names.
ModuleNotFoundError when run.py is loadedImport the agent by the path that works from the folder you run the command in. Files next to run.py are not importable.
403 when the run startsThe key is an ingest API key, or its owner's role is below Developer. Use a personal API key.
The SDK reports a missing API key or organization IDThe command reads JUDGMENT_API_KEY and JUDGMENT_ORG_ID from the environment and from the repo's .env. If .env isn't picked up, export them in the shell before you run the command.
run_case raised on N of M cases and a tracebackYour run or your agent raised. The traceback shows where. Every case is still played before the command exits.
Run ... is unfinished after 900sSome cases produced no trace, or two cases shared one trace. Check that run makes one trace per call and that the tracer sends to the project in judgment.json. See a case is never scored.
environment is always NoneThe case has no metadata.environment, or test environments are not turned on for the organization.
A case passes alone and fails with othersCases share state. Give each case its own store, or pass --concurrency 1.

Next steps

On this page