By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A team wires Claude Code into their pull-request pipeline. Week one, the bot catches a missing error branch and everyone is impressed. Week four, it comments forty times per pull request: three findings that matter and thirty-seven about naming, comment style and test layout. Reviewers mute it, and the three real findings go with it.
That outcome is normal, and it is a design problem rather than a model problem. A review gate survives only when its output is small enough to read and seldom wrong. Two levers control both: explicit review criteria and a narrow diff scope.
First, the mechanics. Headless mode runs Claude Code without an interactive session, so a pipeline can call it. You pass the prompt with -p (also written --print) and read the result on standard output.
No. The flag changes the shape of the output, not the policy of the job. A JSON result is still text until something parses it and exits non-zero. The exit code is your decision, and you have to write it.
| Flag | What it does | Why the job needs it |
|---|---|---|
-p / --print | Runs one prompt and prints the result | There is no terminal and nobody to answer a question |
--output-format json | Prints a JSON result instead of prose | The pipeline can parse it |
--json-schema | Constrains the result to a schema | You can reject a malformed review before acting on it |
--allowedTools | Names the tools the run may use | Limits what an untrusted pull request can reach |
A job that blocks on a permission prompt is a job that hangs until the runner times out. Every interactive decision has to become a flag, an environment setting or a file in the repository.
{
"type": "object",
"required": ["summary", "findings"],
"additionalProperties": false,
"properties": {
"summary": {"type": "string"},
"findings": {
"type": "array",
"items": {
"type": "object",
"required": ["path", "line", "severity", "rule_id", "message"],
"additionalProperties": false,
"properties": {
"path": {"type": "string"},
"line": {"type": "integer"},
"severity": {"enum": ["low", "medium", "high"]},
"rule_id": {"type": "string"},
"message": {"type": "string"}
}
}
}
}
}SEVERITY_RANK = {"low": 1, "medium": 2, "high": 3}
def gate(review: dict, threshold: int = 3) -> int:
"""Turn a schema-valid review into a process exit code."""
findings = review.get("findings")
if not isinstance(findings, list):
print("schema failure: findings must be a list")
return 2
blocking = [
item
for item in findings
if SEVERITY_RANK.get(item.get("severity"), 0) >= threshold
]
for item in blocking:
print(
f"::error file={item['path']},line={item['line']}::"
f"{item['rule_id']} {item['message']}"
)
print(f"{len(findings)} findings, {len(blocking)} blocking")
return 1 if blocking else 0
review = {
"summary": "three findings on the changed lines",
"findings": [
{
"path": "app/db.py",
"line": 42,
"severity": "high",
"rule_id": "no-string-sql",
"message": "SQL built by string concatenation",
},
{
"path": "app/api.py",
"line": 17,
"severity": "medium",
"rule_id": "unhandled-error",
"message": "network call has no error branch",
},
{
"path": "app/models.py",
"line": 8,
"severity": "low",
"rule_id": "naming",
"message": "local name is a single letter",
},
],
}
exit_code = gate(review)
print(f"job exit code: {exit_code}")| Noise source | Fix |
|---|---|
| A vague instruction such as review for quality | List the rules that matter and give each one an id |
| Style opinions | Ban them in the prompt: ignore formatting and naming |
| Whole-repository context | Review the diff, not the repository |
| Findings on untouched code | Report only lines that changed in this pull request |
| Model improvisation | Require a rule_id from your list and drop findings without one |
The rule list is the most valuable part of the job. Write the five rules your team already argues about, give each one an id, and ask for nothing else. A reviewer can then check the bot's rules the same way they check a colleague's, and the bot stops producing comments that no rule justifies.
Write a three-rule review policy and give each rule an id.
Put the result shape in a JSON Schema file and pass it with --json-schema.
Allow only the tools a review needs: reading files and running a diff. No writes, no pushes.
Run the job on a pull request you know is clean. It should pass with no findings.
Run it on a pull request with a deliberate bug. It should fail, and fail on the right rule id.
Definition of done: the job exits 0 on a clean diff, exits 1 on the planted bug, and exits non-zero when you feed it a malformed result. Test that third case by hand; it is the one teams forget.
Key takeaways
Headless mode (-p or --print) is how a pipeline calls Claude Code.
--output-format json changes the shape, not the policy: you parse and you set the exit code.
--json-schema lets you reject a malformed result before anything acts on it.
Allow-list tools by name; a review job needs no write access.
Fewer false positives come from explicit criteria and a narrow diff scope.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.