
Some rules in a project are easy for a human and impossible for a linter. “If you change the behaviour of a public method, add a line to CHANGELOG.md.” “If you add a new extension setting, document it.” PHPStan does not know what “behaviour” means, and a regex on the diff gives either nothing or nonsense.
These are exactly the checks I now hand to Claude Code in headless mode, inside the CI pipeline. Not as a reviewer with opinions, but as a narrow check with a yes/no answer. This post shows the job, the flags I use, and how I keep the cost and the secrets under control.
Problems I faced
- A CI job needs a machine-readable answer. Free text like “Looks mostly fine, but…” cannot fail a pipeline.
- A CI job runs unattended. Nobody answers permission prompts, so every tool the agent may use must be decided upfront.
- A merge request is untrusted input. It can change files in
.claude/too, and I do not want a merge request to bring its own hooks into a job that holds my API key. - An agent that loops is an agent that spends money.
Solution
Step 1: The command
Headless mode is claude -p (or --print): one prompt in, one result out, then the process exits. These are the flags I combine for a check, all from the official CLI reference:
git diff "origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}...HEAD"
| claude --bare -p "$(cat .ci/changelog-check.md)"
--tools "Read"
--permission-mode dontAsk
--max-turns 10
--max-budget-usd 0.50
--no-session-persistence
--output-format json
--json-schema "$(cat .ci/findings.schema.json)"
> claude-result.jsonWhat each part does:
--bareskips the auto-discovery of hooks, skills, plugins, MCP servers, auto memory andCLAUDE.md. According to the docs, a normal-prun executes the hooks from the project’s.claude/settings.jsonand connects the servers in.mcp.json, even in a folder nobody trusted. In a pipeline that checks other people’s branches, I want none of that. Bare mode also reads onlyANTHROPIC_API_KEY, no OAuth login.--tools "Read"restricts the built-in tools to reading files. The diff comes in through stdin, so the agent needs no shell to get it.--permission-mode dontAskdenies everything that would otherwise need a prompt.--max-turnsand--max-budget-usdput a ceiling on the run. The budget is checked against a client-side estimate, so keep some headroom.--no-session-persistencedoes not write the session to disk; there is nothing to resume in CI.--output-format jsonwith--json-schemaputs the validated answer into thestructured_outputfield, next to metadata liketotal_cost_usdandsession_id.
Step 2: The prompt and the schema
The prompt is a file in the repository, .ci/changelog-check.md:
You get a git diff on stdin. Check two rules and nothing else:
1. If the diff changes the behaviour of a public method in Classes/
(signature, return value, thrown exceptions, side effects), the diff
must also add a line to CHANGELOG.md under "Unreleased".
2. If the diff adds a setting to Configuration/Sets/*/settings.definitions.yaml,
the diff must also mention that setting in Documentation/.
Refactorings without behaviour change, tests and comments need no entry.
You may open files with Read to understand a change.
Report one finding per violated rule and file. If nothing is violated,
return an empty findings list.And the schema, .ci/findings.schema.json:
{
"type": "object",
"properties": {
"findings": {
"type": "array",
"items": {
"type": "object",
"properties": {
"rule": { "type": "integer" },
"file": { "type": "string" },
"reason": { "type": "string" }
},
"required": ["rule", "file", "reason"]
}
}
},
"required": ["findings"]
}With a schema, I never parse prose. Either there is a findings array or the run failed.
Step 3: The GitLab CI job
changelog-check:
stage: test
image: node:24-alpine3.21
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
changes:
- Classes/**/*
- Configuration/**/*
timeout: 10m
before_script:
- apk add --no-cache git curl bash jq
- curl -fsSL https://claude.ai/install.sh | bash
- export PATH="$HOME/.local/bin:$PATH"
- git fetch origin "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
script:
- ./.ci/run-changelog-check.sh
artifacts:
when: always
paths:
- claude-result.jsonThe install lines follow the GitLab example in the Claude Code docs. run-changelog-check.sh contains the command from step 1 and then decides the exit code:
#!/usr/bin/env bash
set -euo pipefail
# ... the claude command from step 1 writes claude-result.json ...
echo "Estimated cost: $(jq -r '.total_cost_usd' claude-result.json) USD"
if ! jq -e '.structured_output.findings' claude-result.json > /dev/null; then
echo "Check did not finish, see claude-result.json"
exit 2
fi
count=$(jq '.structured_output.findings | length' claude-result.json)
if [ "$count" -gt 0 ]; then
jq -r '.structured_output.findings[] | "rule (.rule): (.file): (.reason)"' claude-result.json
exit 1
fi
echo "No findings."Because of set -e, a run that hits --max-turns (the CLI exits with an error then) or fails to authenticate already stops the job before the jq part. The extra check catches a result without structured output. In both cases the job is red with a different message than “you forgot the changelog”, which matters: people must be able to tell “rule violated” from “the check broke”.
Step 4: Secrets and cost
- Store
ANTHROPIC_API_KEYas a masked CI/CD variable, never in the repository. If you also mark it protected, it is only available on protected branches, so merge request pipelines from feature branches will not get it. Decide that consciously; for public projects with outside contributors I would not run this job on their branches at all. - Use a separate API key for CI, ideally in its own Console workspace with a spend limit, so a misbehaving pipeline cannot eat the budget of your daily work, and so you can revoke it alone.
- Print
total_cost_usdin every job log. After a week you know what a check really costs, and thechanges:rule keeps the job away from merge requests that only touch documentation. - Keep the tool list minimal. A check that needs only
Readgets onlyRead.
What I do not use it for
Anything a deterministic tool can do stays with that tool: coding standards, types, tests, composer validation. The AI job runs after those, and only for rules that need some reading comprehension. Also, it never writes. A check that edits code in the pipeline is a different animal, with different permissions, and it ends in a merge request that a human reviews.
If you set something similar up, I would love to hear which fuzzy rules you automated. Drop a comment!