CloudCodeTree LogoCloudCodeTree
AI NewsTutorialsAbout
CloudCodeTree Logo
CloudCodeTree
  • AI News
  • Tutorials
  • About
← Back to AI News
Automate Claude Code with --print and --output-format json: Scripting and CI/CD Pipelines

Automate Claude Code with --print and --output-format json: Scripting and CI/CD Pipelines

Chris Harper

3 min read

Aug 7, 2026 · 20:05 UTC

AI
Workflow
Claude Code
Best Practices

claude -p "prompt" --output-format json turns Claude Code into a scriptable pipeline step — structured output, deterministic exit codes, and schema-constrained responses for CI/CD automation.

Most Claude Code usage is interactive. But -p (non-interactive/print mode) is the bridge between the agent and your toolchain: one command in, parseable data out. Every workflow that runs on a schedule, inside GitHub Actions, or as part of a build pipeline benefits from this.

The three flags you need

-p / --print — non-interactive mode. Claude reads the prompt, runs the agent loop, and exits. No REPL, no UI.

--output-format — controls what comes out on stdout:

  • text (default) — plain text result
  • json — structured object with result, session_id, total_cost_usd, and duration_ms
  • stream-json — newline-delimited JSON, one event per line; useful for real-time streaming in long-running jobs

--allowedTools — restrict which tools Claude can use. Critical for CI: only allow what the task actually needs.

Minimal example

# One-shot code review, JSON output
claude -p "Review the staged diff for security issues. Be concise." \
  --output-format json \
  --allowedTools "Bash(git diff:*),Read" \
  | jq -r '.result'

Exit code 0 = success; any non-zero code = error. Unix-standard, so set -e pipelines work correctly.

GitHub Actions integration

- name: AI security scan
  run: |
    REVIEW=$(claude -p "Scan the staged files for hardcoded secrets or SQL injection risks" \
      --output-format json \
      --allowedTools "Read,Bash(git diff:*)" \
      | jq -r '.result')
    echo "## Security Review" >> $GITHUB_STEP_SUMMARY
    echo "$REVIEW" >> $GITHUB_STEP_SUMMARY

Structured output with --json-schema

Force Claude to return a JSON object matching your schema:

claude -p "Analyze the test results in ./test-output.xml" \
  --output-format json \
  --allowedTools "Read" \
  --json-schema '{
    "type": "object",
    "properties": {
      "passed": {"type": "boolean"},
      "failures": {"type": "array", "items": {"type": "string"}},
      "summary": {"type": "string"}
    },
    "required": ["passed", "failures", "summary"]
  }' \
  | jq '.result'

The schema-constrained response lands in the result field alongside total_cost_usd — ready to pipe into jq, a script, or a monitoring system.

What to lock down in CI

  • Always pass --allowedTools listing only what the task needs — omit Bash entirely for read-only analysis
  • Store ANTHROPIC_API_KEY in your CI secrets; Claude Code picks it up from the environment
  • Use --max-turns N to cap the agent loop length for predictable runtime

Sources: Run Claude Code programmatically — code.claude.com · Headless Mode and CI/CD — SFEIR Institute · Claude Code Headless Mode guide — buildthisnow.com