Claude Code is designed for interactive use by default. Drop it into a CI/CD pipeline without configuration and the job will hang waiting for user input that never comes. The fixes are specific and non-obvious โ which is why they appear directly in Domain 3 of the Claude Certified Architect โ Foundations exam (Claude Code Configuration & Workflows, 20% weight).
This guide covers the four technical requirements for reliable CI/CD integration: the -p flag, JSON output, CLAUDE.md for project context, and session isolation for independent review.
The -p flag: why pipelines hang without it
Claude Code operates in two modes. In interactive mode (the default), it waits for the user to review proposed changes, approve commands, and provide additional direction. In a CI job, there is no user โ so the process blocks indefinitely on the first approval prompt.
The --print flag (short form: -p) switches Claude Code to non-interactive print mode:
claude -p "Review the diff in the last commit for security vulnerabilities. Output findings as JSON."
In print mode, Claude Code:
- Executes the prompt without waiting for interactive approval
- Writes output to stdout
- Exits with a non-zero code on failure
This is the minimum change required to make Claude Code usable in a pipeline. Without it, the CI job times out.
The --print flag is not equivalent to "trust all operations." Claude Code in CI still respects the permission model defined in CLAUDE.md and the project's .claude/settings.json. Non-interactive mode means no approval prompts, not no restrictions.
--output-format json and JSON Schema validation
Raw text output from Claude Code is not machine-processable in a pipeline. The --output-format json flag produces structured output that downstream steps can parse:
claude -p "Analyze this file for issues" \
--output-format json \
--json-schema ./schemas/review-findings.json \
src/auth/token-validator.ts
The --json-schema flag takes a path to a JSON Schema file that Claude must conform to. This gives you schema validation at the pipeline level โ if Claude's output does not match the schema, the command exits with an error code.
A minimal review findings schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["findings", "summary", "riskLevel"],
"properties": {
"findings": {
"type": "array",
"items": {
"type": "object",
"required": ["file", "line", "severity", "description", "suggestion"],
"properties": {
"file": { "type": "string" },
"line": { "type": "integer" },
"severity": { "enum": ["critical", "high", "medium", "low", "info"] },
"description": { "type": "string" },
"suggestion": { "type": "string" }
}
}
},
"summary": { "type": "string" },
"riskLevel": { "enum": ["critical", "high", "medium", "low", "clean"] }
}
}
The structured output feeds into downstream steps: a GitHub Actions step that posts findings as PR comments, a Slack notification on riskLevel: critical, or a database write for trend tracking across PRs.
The exam tests your ability to design the schema alongside the prompt โ specifically, knowing that severity as an enum prevents Claude from inventing severity levels that downstream code does not handle.
CLAUDE.md: providing project context in CI runs
Every Claude Code run in CI starts cold. Without project context, Claude Code does not know:
- Which patterns are acceptable in this codebase
- Which directories are auto-generated (skip review)
- Which security standards apply to this project
- What the naming conventions are
CLAUDE.md provides this context automatically. Claude Code reads CLAUDE.md files at project startup and incorporates them into its operating context. In CI, this means your review prompts do not need to repeat boilerplate about the project โ CLAUDE.md carries it.
A CI-focused CLAUDE.md section:
## CI Review Context
### Auto-generated files (skip review)
- `src/generated/**` โ protobuf outputs, do not flag
- `dist/**` โ build artifacts
- `coverage/**` โ test coverage reports
### Security standards
- All external API calls must use the approved HTTP client in `src/lib/http.ts`
- No hardcoded credentials โ use environment variable references only
- SQL queries must use parameterized form via `src/db/query-builder.ts`
### False positive reduction
- Console.log statements in `src/scripts/**` are acceptable (not production code)
- Type assertions (`as SomeType`) are acceptable in test files only
The CLAUDE.md path hierarchy matters in CI: Claude Code reads the root CLAUDE.md first, then any subdirectory CLAUDE.md files for the specific files being reviewed. This means you can have project-wide standards in root and component-specific guidance in subdirectories.
Task Statement 3.6 in the exam directly references using CLAUDE.md to reduce false positives in automated review โ a concrete scenario where the CLAUDE.md content filters out known acceptable patterns.
Session isolation: catching bugs the generator missed
A session isolation pattern runs code generation and code review in separate Claude Code instances:
jobs:
generate:
runs-on: ubuntu-latest
steps:
- name: Generate implementation
run: claude -p "$GENERATION_PROMPT" --output-format json > generated.json
review:
needs: generate
runs-on: ubuntu-latest
steps:
- name: Independent review
run: claude -p "$REVIEW_PROMPT" --output-format json > review.json
The review instance has no shared context with the generation instance. This is intentional. A reviewer that shares context with the generator inherits the generator's assumptions โ including its blind spots. The same reasoning error that caused a bug during generation will cause the same reasoning error during review if context is shared.
Independent instances read the diff or the files as an external reviewer would: without knowing what the generator was "trying to do." This surfaces bugs that shared-context review misses.
The exam frames this as a reliability question in the Claude Code in CI/CD scenario: "What is the most effective way to catch logic errors in AI-generated code before merging?" The answer is session isolation, not self-review with shared context.
Multi-pass review for large PRs
A single Claude Code invocation reviewing a 3,000-line PR produces lower-quality findings than structured multi-pass review. Context dilution is real โ findings from the first file in a large diff get less attention by the end.
The multi-pass pattern:
for file in $(git diff --name-only HEAD~1); do
claude -p "Security review for $file. Check: injection, authentication, data validation." \
--output-format json \
--json-schema ./schemas/security-findings.json \
"$file" > "findings-$(basename $file).json"
done
claude -p "Review the full diff for cross-cutting concerns: API contract changes,
shared state mutations, error propagation gaps. Reference per-file
findings in findings-*.json." \
--output-format json \
--json-schema ./schemas/integration-findings.json \
$(git diff HEAD~1) > integration-findings.json
claude -p "Synthesize findings from all per-file and integration passes.
Deduplicate, prioritize, and produce final PR summary." \
--output-format json \
--json-schema ./schemas/pr-summary.json \
findings-*.json integration-findings.json > pr-summary.json
Each pass has a focused scope. The integration pass explicitly knows it is looking at cross-cutting concerns, not re-reviewing individual files. The aggregation pass deduplicates rather than re-analyzing.
This pattern aligns with Task Statement 3.6 (optimizing automated review quality) and Domain 4 Task Statement 4.5 (structured output across pipeline stages).
Batch API vs synchronous API: cost and SLA tradeoffs (Task Statements 4.5-4.6)
CI/CD creates two distinct review timing requirements:
Pre-merge checks (blocking): Must complete before the PR can merge. Latency matters. Use the synchronous API directly. Maximum 5-minute review time is acceptable for blocking checks.
Overnight reports (non-blocking): Comprehensive analysis run on merged code, large historical diffs, or weekly security sweeps. Latency does not matter. Use the Batch API.
The Batch API offers:
- 50% cost reduction compared to synchronous calls
- 24-hour SLA for completion
- Suitable for requests that do not block a human workflow
The exam tests this tradeoff directly in Task Statements 4.5-4.6: "A team wants to run comprehensive security analysis across 200 files nightly. Which API mode is appropriate and why?" Answer: Batch API โ the 50% cost savings are significant at scale, the 24-hour SLA is acceptable for overnight reports, and blocking developers is not required.
Pre-merge checks use synchronous API for low-latency feedback. Overnight and batch reporting use Batch API for cost efficiency. Mixing these up โ using Batch API for pre-merge checks โ produces a pipeline that blocks deploys for up to 24 hours.
What the exam tests in Domain 3 and Domain 4 overlap
The CI/CD scenario frame tests across Domain 3 (Claude Code) and Domain 4 (structured output):
-p/--print flag: non-interactive mode and why it is required
--output-format json with --json-schema: structured pipeline output
CLAUDE.md: project context delivery and false positive reduction
- Session isolation: independent reviewer instances
- Multi-pass review: per-file vs integration vs aggregation passes
- Batch vs synchronous API: pre-merge (synchronous) vs overnight (Batch, 50% cost, 24h SLA)
The exam does not test deep knowledge of CI/CD tools (GitHub Actions, Jenkins, etc.) โ it tests your understanding of how Claude Code features map to pipeline requirements.
Key takeaways
- Use
--print/-p for all CI/CD invocations. Without it, the job hangs on the first approval prompt.
- Use
--output-format json with --json-schema for machine-parseable output with schema validation.
- Commit
CLAUDE.md with CI-specific context: auto-generated directories to skip, project security standards, false-positive reduction guidance.
- Run generation and review in separate sessions. Independent instances catch what shared-context review misses.
- For large PRs, use multi-pass review: per-file passes for depth, integration pass for cross-cutting concerns, aggregation pass for synthesis.
- Pre-merge checks use the synchronous API. Overnight reports use the Batch API (50% cost reduction, 24h SLA).
This is a core topic in Domain 3 of the Claude Certified Architect โ Foundations exam. Build fluency with these patterns using CCA practice exams on explainx.ai.
Exam domain weights and task statements are based on the Claude Certified Architect โ Foundations Certification Exam Guide published by Anthropic Academy. Verify current content on Anthropic Academy before your exam date.