Lesson 20.4 · 4 of 4 · 4 min read · Reviewed 24 Nov 2026
Worked example: evals with a headless coding agent
By the end, you can
- Run evals with Claude Code in headless mode.
- Wire an eval step into a CI pipeline.
Headless mode
claude -p "<prompt>" runs Claude CodeClaude Code: Anthropic’s agentic coding tool that works in your terminal, IDE and CI, reading and editing code and running commands. once without the interactive UI. With --output-format json the result comes back as JSON your script can parse.
Uses in an eval pipeline
| Job | How |
|---|---|
| Grade outputs | Feed each case’s output and rubric; ask for a structured verdict |
| Explain failures | Give the failing cases; ask for grouped causes for the PR comment |
| Review prompt diffs | Ask what behaviour the prompt change is likely to affect, and which cases to add |
A sketch
# run the system on the smoke set (your own script)
python run_cases.py --suite smoke --out outputs.jsonl
# grade with a pinned grader prompt; JSON out
claude -p "$(cat graders/policy_accuracy.md)" \
--output-format json < outputs.jsonl > grades.json
# the gate is plain code
python gate.py grades.json --config gates.yaml
Safety in CI
- Limit tools with allow and deny rules; a graderGrader: The part of an eval that decides whether an output passes, using code, a model or a person. needs no file edits or shell.
- Store the API key as a CIContinuous integration (CI): Automatically building and testing code on every change, usually in a hosted pipeline. secret, never in the repo or logs.
- Treat pull-request text as untrusted input to the model (chapter 65).
- Record the grader version with each score (18.5).
On this site, all of this is simulated: you can practise the flow without a key.
Key points
- Claude Code's headless mode (claude -p) runs a prompt non-interactively and can return JSON, so it fits CI jobs.
- Use it to grade outputs, summarise eval failures, or review prompt changes, with tools limited to what the job needs.
- Keep the gate decision in code: Claude can grade or explain, but a script compares scores with thresholds.
Grade a smoke set headlessly
Run Claude Code in headless mode as a grader in a script, and keep the gate decision in plain code.
You will have: A shell script that grades a small set of outputs with claude -p, saves JSON verdicts, and a gate script that turns them into pass or fail.
Steps
- Install Claude Code on your own machine and sign in, following the official setup page. Keep any API key in an environment variable, never in files you commit.
- Save five sample outputs and a pinned grader prompt (graders/policy_accuracy.md) that asks for a JSON verdict of pass, partial or fail.
- Run claude -p with your grader prompt and --output-format json for each output, and save the results to grades.json.
- Restrict tools for the run with the allow and deny settings so the grader cannot edit files or run shell commands.
- Write gate.py that counts verdicts, compares them with a threshold and exits 1 on failure; record the grader version next to the scores.
Done when
- grades.json holds one parseable verdict per output.
- gate.py exits 0 when all pass and 1 when you plant a failing output.
- The run used no file-editing or shell tools.
Docs: Claude Code headless mode (Claude docs) · Claude Code CLI reference (Claude docs)
Do it Practise headless output in the Playground in the Playground. No API key needed.
Finished the chapter? Take the chapter check.