Advanced Patterns
Headless / CI Usage
Section titled “Headless / CI Usage”Run Claude non-interactively in scripts, pipelines, and CI/CD:
# One-shot, structured outputclaude -p "fix all lint errors" --permissions acceptEdits --output-format json
# Check success/failureif claude -p "run tests and fix failures" --permissions acceptEdits; then echo "Done"else echo "Failed" && exit 1fi
# Stream JSON for large outputsclaude -p "analyze entire codebase" --output-format stream-json | jq -c '.content'GitHub Actions Example
Section titled “GitHub Actions Example”- name: Claude Code Review run: | git diff origin/main | claude -p "review these changes for bugs" \ --output-format md \ --model claude-sonnet-4-6Piping and Composition
Section titled “Piping and Composition”# Analyze logstail -100 app.log | claude -p "what went wrong?"
# Review a diffgit diff | claude -p "code review, focus on security"
# Fix test failuresnpm test 2>&1 | claude -p "fix these test failures" --permissions acceptEdits
# Chain with jqclaude -p "list exported functions" --output-format json \ | jq -r '.functions[].name'Custom System Prompts
Section titled “Custom System Prompts”# Domain-specific assistantclaude --system-prompt "You are a PostgreSQL expert. Focus on query optimization and indexing."
# Append style rules without replacing the defaultclaude --append-system-prompt "Always use pnpm. Never use npm. Explain every decision."In CLAUDE.md (applies every session):
## Behavior- Always explain your reasoning before making changes- Ask before making changes to more than 3 files at once- Prefer security over convenience- Use pnpm, not npmContext Management for Large Codebases
Section titled “Context Management for Large Codebases”Exclude large generated directories from reads:
{ "permissions": { "deny": [ "Read(node_modules/**)", "Read(dist/**)", "Read(build/**)", "Read(.git/**)" ] }}Create a domain-scoped subagent that ignores irrelevant code:
---name: "frontend-dev"description: "Frontend development in src/ui/ and src/components/"---
You focus exclusively on files in `src/ui/` and `src/components/`.Ignore backend code, database migrations, and infrastructure files.Multi-Agent Orchestration
Section titled “Multi-Agent Orchestration”Delegate independent tasks to specialized agents in one prompt:
I need three things done in parallel:1. @Explore: map the entire authentication flow across all files2. @code-reviewer: review my changes in src/auth/3. @Plan: design the new OAuth2 integration
Combine the findings into a single implementation plan.Background Sessions
Section titled “Background Sessions”Run a long task in the background while you continue working:
/backgroundOr start a session in the background from the CLI:
claude --background "refactor all API handlers to use the new error format"Session Forking
Section titled “Session Forking”Branch a session to explore alternatives without losing your current state:
claude --fork-session <session-id>Extend with apiKeyHelper
Section titled “Extend with apiKeyHelper”Rotate API keys dynamically (useful for temporary credentials):
{ "apiKeyHelper": "/usr/local/bin/get-claude-key.sh"}The script runs before each request and must print an API key to stdout.
Minimal CI Security Profile
Section titled “Minimal CI Security Profile”Lock down permissions to the minimum needed in CI:
{ "permissions": { "allow": [ "Read", "Bash(npm test)", "Bash(npm run lint)" ], "deny": [ "Bash(git push *)", "Bash(rm *)", "Edit(/secrets/**)", "WebFetch" ] }, "defaultMode": "dontAsk"}Multiple Working Directories
Section titled “Multiple Working Directories”# Grant access to a shared library alongside the projectclaude --add-dir /path/to/shared-lib
# Or in settings.json{ "additionalDirectories": ["/path/to/shared-lib"]}Rewind / Checkpoints
Section titled “Rewind / Checkpoints”Undo recent file changes (requires fileCheckpointingEnabled: true):
Esc + Esc # rewind to previous state/rewind # view checkpoint historyOutput Styles
Section titled “Output Styles”Create a custom output style in .claude/output-styles/concise.md:
---name: "concise"description: "Short, direct responses with minimal explanation"---
Respond concisely. Skip preamble. Use bullet points. No filler phrases.Activate with:
/output-style concise🧠 Quick Recall
Section titled “🧠 Quick Recall”- Trick: “
claude -pturns Claude into a Unix filter: pipe in, JSON out.”
👨🏫 Teach It
Section titled “👨🏫 Teach It”- Show:
git diff main | claude -p "review" --output-format json | jq— one line. - They’ll trip on: scripting without
--output-format jsonand parsing prose.
Learning path: ⬅ 32-agent-teams.md · Index · ➡ 34-routines-scheduling.md
Written by Fenil Patel