Skip to main content

Evals as code

Evals as code means your evals live in your repository, beside the prompts and code they test, and run the same way on a laptop and in CI. You describe an eval in a small file or a pytest test, the gateway runs it, compares the result with the last trusted run, and answers with a verdict: passed, failed, neutral or error. Nothing is scored on your machine, so a developer, a CI job and an admin looking at the Admin UI all see the same verdict. This is an enterprise feature like the rest of evals.

There are three ways to use it, and they all start the same gateway eval run and read the same verdict from GET /eval/runs/{run_id}/verdict:

  • spanwayctl evals run runs one or more spec files and prints each verdict. With --watch it stays open and runs them again every time you save a spec, a prompt file or anything else you tell it to watch.
  • The pytest plugin in the Python client lets a test file start an eval run through the spanway_evals fixture. A failed verdict fails the test.
  • In CI, either of them exits with a non-zero code when the verdict is failed, so the pull request check turns red on a regression. Use a CI key, which can only start eval runs and read their verdicts.

A real-world scenario​

A support team keeps its bot's system prompt in prompts/support.txt and a golden dataset of 200 refund questions on the gateway. While editing the prompt, an engineer keeps spanwayctl evals run .spanway/support.yaml --watch --watch-path prompts open in a terminal. Each save starts a new run, and a minute later the terminal shows whether the pass rate held. The same eval is also a pytest test, so the pull request job runs pytest -m spanway_eval, and when a prompt edit drops the pass rate by more than three points against main, the check fails and the job summary shows which questions got worse.

Steps​

  1. Create the dataset and check that it scores the way you want, as described in Evals. In the Admin UI open Evals, then the Datasets tab, and note the dataset name.

  2. Create the keys. Developers can use their own virtual key. For CI, a proxy admin opens Virtual Keys in the Admin UI, clicks + Create New Key, chooses the key type CI (evals), picks the team, the models and the allowed datasets, sets a budget and clicks Create Key. CI keys has the matching curl call and explains the baseline writer key for main.

  3. Write a spec file, for example .spanway/support.yaml. It is the same format spanwayctl evals gate reads.

    dataset: support-golden
    models: [gpt-5.5]
    scorer: {type: exact_match, case_insensitive: true}
    baseline_key: support-bot
    gate:
    min_pass_rate: 0.85
    max_pass_rate_drop: 0.03
  4. Run it, or keep it running while you work.

    export SPANWAY_PROXY_URL=https://gateway.example.com
    export SPANWAY_PROXY_API_KEY=sk-...
    spanwayctl evals run .spanway/support.yaml
    spanwayctl evals run .spanway/support.yaml --watch --watch-path prompts

    Each run is an ordinary gateway eval run, so you can start the same one with curl and read its verdict:

    curl -X POST "$SPANWAY_PROXY_URL/eval/runs" \
    -H "Authorization: Bearer $SPANWAY_PROXY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"dataset_name": "support-golden", "models": ["gpt-5.5"], "scorer": {"type": "exact_match"},
    "baseline_key": "support-bot", "gate": {"min_pass_rate": 0.85, "max_pass_rate_drop": 0.03}}'
    curl "$SPANWAY_PROXY_URL/eval/runs/$RUN_ID/verdict?report_detail=summary" \
    -H "Authorization: Bearer $SPANWAY_PROXY_API_KEY"
  5. To write the eval as a test instead, install the Python client wheel into your test environment as described in Python client and add a test file. pytest finds the plugin by itself once the client is installed.

    from spanway_client.schema import EvalGate, NewEvalRun


    def test_support_bot_refunds(spanway_evals):
    spanway_evals.gate(
    NewEvalRun(
    dataset_name="support-golden",
    models=["gpt-5.5"],
    scorer={"type": "exact_match", "case_insensitive": True},
    baseline_key="support-bot",
    gate=EvalGate(min_pass_rate=0.85, max_pass_rate_drop=0.03),
    )
    )
    pytest -m spanway_eval
  6. Add the CI job from Failing a pull request on a regression.

  7. In the Admin UI open Evals, then the Runs tab, and click the newest run. The CI gate box shows the same verdict your terminal or test printed. Runs started from CI carry a CI badge.

Expected result​

spanwayctl evals run prints one block per spec, starting with a line such as == .spanway/support.yaml: passed (run 3f2a...), then the verdict report, and ends with 1 of 1 specs passed. It exits 0. With --watch it then prints watching for changes, press Ctrl-C to stop, and after you save a watched file, change detected, running again. In pytest the eval test passes, and the end of the pytest output has a Spanway evals section listing each eval test with its outcome. When the pass rate falls past max_pass_rate_drop, the verdict is failed, spanwayctl evals run exits 1 and the pytest test fails with the verdict report as its message.

spanwayctl evals run​

spanwayctl evals run SPEC [SPEC ...] runs each spec file one after another, prints each verdict and exits with the worst result: 0 when every spec passed or had nothing to gate, 1 when any failed, 2 when any ended in an error, a timeout or a gateway it cannot reach, and 3 for bad usage. Every spec is read before anything starts, so one broken spec starts no run at all and the error names the file. It reads SPANWAY_PROXY_URL and SPANWAY_PROXY_API_KEY like every other command. Inside a GitHub Actions or GitLab CI job it sends the job's git context with each run, the same way spanwayctl evals gate does.

OptionDefaultWhat it does
--modelthe spec's modelsRuns these models instead of the ones each spec lists. Repeat it for several models. Handy for trying a new model against all your evals at once.
--watchoffKeeps the command open and runs every spec again whenever a spec or a watched path changes. Turn it on while you edit prompts, leave it off in CI. Press Ctrl-C to stop. Each new round is a new run, so it costs the same as running the command again.
--watch-pathnoneAn extra file or folder to watch, repeatable. A folder covers every file below it except hidden ones such as .git. The specs themselves are always watched.
--interval1 secondHow often the watcher checks for changes. A larger value saves a little CPU on very large folders.
--timeout1800 secondsHow long to wait for each verdict. A run with no verdict in time is cancelled so it stops spending.
--poll10 secondsHow long to wait between verdict checks.
--report-detailfullsummary leaves item ids and the run link out of the report, which suits shared screens and public logs.

What happens when you press Ctrl-C: the run in flight is cancelled on the gateway right away, no further spec starts, and the command exits with the last finished round's code. A change you save while a round is running is not lost, the next round starts as soon as the current one ends. If a spec becomes invalid while you watch, the command prints the error, starts nothing, and keeps watching until you fix it.

spanwayctl evals gate stays the command for a single spec in CI, with step outputs and a pull request comment. spanwayctl evals run is for running several specs at once and for the edit, save, check loop.

The pytest plugin​

The plugin ships inside the spanway-client wheel. Installing the wheel registers it with pytest, there is nothing to add to conftest.py.

spanway_evals is a fixture with two methods. gate(run) starts the run, waits for the final verdict and fails the test unless it is passed or neutral. run(run) does the same but only returns the verdict, so you can write your own assertions on verdict.checks or verdict.models. Both take the client's NewEvalRun model, so every field the gateway accepts, such as dataset_name, models, scorer, prompt_id, baseline_key and gate, is checked for typos before anything is sent.

The test also fails, with the reason as its message, when the key is missing, the gateway refuses the run, cannot be reached five times in a row, or gives no verdict in time. In that last case the run is cancelled first. A failing infrastructure never lets an eval test pass.

Every test that uses the fixture gets the spanway_eval marker, so pytest -m spanway_eval runs only evals and pytest -m "not spanway_eval" leaves them out of a fast unit test run. The plugin works with pytest-xdist, each eval test starts its own run, and only the main pytest process writes the job summary, so every report appears in it once.

The plugin is configured with environment variables.

VariableDefaultWhat it does
SPANWAY_PROXY_URLhttp://localhost:4000The gateway to run evals on.
SPANWAY_PROXY_API_KEYnone, requiredThe key that starts the runs. Use a CI key in CI. When it is missing, every eval test fails, so a misconfigured job can never pass by accident.
SPANWAY_EVAL_TIMEOUT1800 secondsHow long a test waits for its verdict before cancelling the run and failing.
SPANWAY_EVAL_POLL10 secondsHow long to wait between verdict checks. Server errors back off by doubling this, up to a minute.
SPANWAY_EVAL_REPORT_DETAILfullsummary leaves item ids and the run link out of the reports, for public repositories.

When a test run is inside GitHub Actions or GitLab CI, the plugin sends the job's repository, ref, commit, pull request number, job link and actor with every run. That is what lets a pull request run be compared with the latest trusted run on main, and it is why a run that names a pull request can never become a baseline. An invalid optional value, such as a job link that is not https, is left out, and an invalid ref or commit leaves the run without a git context. In GitHub Actions the plugin also appends every verdict report to the job summary.

Failing a pull request on a regression​

Both commands exit non-zero on a failed verdict, which is all a CI system needs to fail the check. The regression itself is decided by the gateway from the spec's gate: max_pass_rate_drop, max_mean_score_drop, max_cost_increase and max_new_failures compare with the last trusted run on your main branch, as The gate explains. The workflow below runs eval tests with pytest on every pull request and records a new baseline on every push to main.

name: evals
on:
pull_request:
push:
branches: [main]

permissions:
contents: read

jobs:
evals:
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository
runs-on: ubuntu-latest
environment: ${{ github.event_name == 'push' && 'evals-baseline' || 'evals' }}
timeout-minutes: 40
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install the tests and the Spanway client
run: pip install pytest ./vendor/spanway_client-2.0.0-py3-none-any.whl
- name: Run the evals
env:
SPANWAY_PROXY_URL: ${{ vars.SPANWAY_URL }}
SPANWAY_PROXY_API_KEY: ${{ secrets.SPANWAY_CI_KEY }}
run: pytest -m spanway_eval

Keep the verified client wheel in your repository or in an internal package mirror, so the job does not download it from the release page on every run. Store the pull request key as SPANWAY_CI_KEY in the evals environment and the baseline writer key under the same name in the evals-baseline environment, and restrict evals-baseline to main. Pull requests from forks are skipped because they cannot read your secrets. Do not switch the trigger to pull_request_target, which runs with your secrets on code from forks. To gate with spec files instead of tests, replace the last step with spanwayctl evals run .spanway/*.yaml inside the gateway image, as in the workflow in Gating pull requests on evals.

Errors​

The gateway errors are the ones every eval run can return, listed in Gating pull requests on evals and Evals. These come from the command and the plugin.

WhereMessageMeaning and fix
spanwayctl evals run, exit code 3error: <file>: the spec is invalid: ... or error: cannot read <file>: ...A spec file is broken or missing, so no run was started. Fix the named file. In watch mode the command keeps watching.
spanwayctl evals run, exit code 3error: no API key, set SPANWAY_PROXY_API_KEY to a ci keySet SPANWAY_PROXY_API_KEY.
spanwayctl evals run, exit code 3error: --interval must be more than 0Give --interval a positive number of seconds.
spanwayctl evals run, exit code 2no verdict within <n>s, the run was cancelled or interrupted, the run was cancelledThe run took longer than --timeout, or you pressed Ctrl-C. Raise --timeout for large datasets.
pytestSPANWAY_PROXY_API_KEY is not set, set it to a gateway key that may run evalsThe eval test errors at setup. Set the variable in the job's environment.
pytestSPANWAY_EVAL_TIMEOUT must be more than 0, got '<value>', the same for SPANWAY_EVAL_POLL, or SPANWAY_EVAL_REPORT_DETAIL must be full or summaryFix the variable's value.
pytestthe eval run could not be started: POST /eval/runs returned HTTP <status>The gateway refused the run, for example 403 for a dataset or model the CI key may not use, or 429 for a spent budget. The gateway error table above says why.
pytestthe verdict could not be read: ... or giving up after 5 errors in a row: ...The gateway could not answer the verdict request. Check that it is up and that the key may read the run.
pytestno verdict within <n>s, the run was cancelledRaise SPANWAY_EVAL_TIMEOUT for large datasets.
pytestthe eval gate failed or the eval gate error, followed by the reportThe run was judged. Read the report's failed checks and regressed items, or open the run in the Admin UI.

Logs​

The command and the plugin write no gateway log lines of their own. Each run they start logs like any other eval run, as listed in Evals logs, and its creation and any cancellation are written to the audit log with the key that made them, ci key <alias> for a CI key. On the client side, spanwayctl evals run --watch prints watching for changes, press Ctrl-C to stop and change detected, running again to standard error, and the plugin prints a Spanway evals section at the end of the pytest output and, in GitHub Actions, writes the job summary could not be written: <reason> when it cannot append to the job summary file.