Lesson 5 of 5 in Claude Code Configuration & Workflows
2.5 · SDK & CI/CD integration
Running Claude Code non-interactively (in CI, in scripts, as part of a pipeline) has its own rules: the -p flag, tool restrictions, timeouts, and treating output as untrusted downstream. This lesson covers the SDK, the -p flag, and the security patterns for CI/CD.
The Claude Code SDK
The SDK lets you drive Claude Code sessions from your own code: send prompts, receive structured responses, manage tool permissions programmatically. Use it to build higher-level tools on top of Claude Code (dashboards, custom integrations, batch runs).
Non-interactive mode: claude -p
claude -p 'prompt' runs a single non-interactive prompt and exits, returning Claude's output. This is the building block for CI/CD: PR review on push, doc generation on commit, scheduled refactors on cron.
claude -p 'Review the diff in this PR and comment on issues' > review.md
CI/CD safety
In CI, always pin tool permissions. Never allow open-ended shell. Set hard timeouts on every run. Treat Claude's output as untrusted input downstream — if it's going into a shell, sanitize; if it's going into a comment, escape.
GitHub Actions integration pattern
Job triggers on PR event → checks out code → runs claude -p with a review prompt and restricted tools → posts output as a PR comment. Add a hook to block writes to protected paths. Add a timeout on the action itself.
# .github/workflows/claude-review.yml
name: Claude PR review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
timeout-minutes: 10 # hard timeout
permissions:
pull-requests: write # to post the comment
contents: read
steps:
- uses: actions/checkout@v4
- name: Install Claude Code
run: npm i -g @anthropic-ai/claude-code
- name: Review diff
env:
ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
run: |
git diff origin/main...HEAD > diff.patch
claude -p "$(cat .github/prompts/review.md)" \
--allowed-tools read_file \
--max-turns 10 \
< diff.patch > review.md
- name: Post comment
uses: peter-evans/create-or-update-comment@v4
with:
issue-number: ${{ github.event.pull_request.number }}
body-path: review.mdTakeaways
- claude -p for one-shot, scriptable runs
- SDK for programmatic, structured control
- In CI: restrict tools, set timeouts, sanitize output
- Hooks work in CI too — use them as safety nets
Exam traps
Practice scenario
You want to find every source file that mentions the string 'legacy_api_v1'. Which tool is correct?