Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Invoke Claude Code CLI from Python orchestrators and shell scripts. Use when asked to "spawn claude as subprocess", "automate claude cli", "run claude headless", "configure --allowedTools", "set up claude hooks", or "parallel claude invocation". Covers permissions, directory access (--add-dir), hooks, sandbox mode, and async patterns.
.claude/skills/majiayu000-using-claude-code-cli/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 140% | 0% |
| case-10 | ✗→✓ | ▲ Improved | -10% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 37% | 0% |
| case-06 | ✗→✓ | ▲ Improved | -32% | 0% |
| case-07 | ✗→✓ | ▲ Improved | 30% | 0% |
Patterns for programmatically invoking Claude Code CLI from orchestrators, scripts, and automation pipelines.
Copy and track as you work:
CLI Automation Setup:
- [ ] 1. Identify required tools, add to --allowedTools
- [ ] 2. List directories Claude needs, add to --add-dir
- [ ] 3. Choose pattern: sync, async, or parallel (see references/)
- [ ] 4. Configure hooks if logging/monitoring needed
- [ ] 5. Enable sandbox mode for untrusted operations
- [ ] 6. Add fallback strategy if using OpenCode backup
- [ ] 7. Test with simple prompt before full automation
- [ ] 8. Validate setup (see Validation section)Minimal automation:
bashclaude -p "Your prompt" --allowedTools Write Read Edit --max-turns 5
Full automation:
bashclaude \ --model sonnet \ --add-dir /path/to/skills \ --add-dir /path/to/templates \ --allowedTools Write Read Edit Bash Task \ --output-format json \ -p "Your automation prompt"
With settings JSON:
bashclaude --settings '{"hooks":{},"sandbox":{"enabled":true}}' -p "prompt"
| Flag | Purpose | Example | |------|---------|---------| | -p | Non-interactive mode (required) | claude -p "query" | | --allowedTools | Pre-approve tools | --allowedTools Write Read Bash | | --add-dir | Grant directory access | --add-dir ../lib | | --settings | Pass hooks/permissions JSON | --settings ./settings.json | | --model | Select model | --model sonnet | | --output-format | Output: text, json, stream-json | --output-format json | | --max-turns | Limit agentic turns | --max-turns 5 |
Recommended tools for automation:
Write, Read, Edit, Glob, Grep, LSBash, Task, SkillWebFetch, WebSearchMCP tools require full path:
bash--allowedTools "mcp__perplexity-ask__perplexity_ask" --allowedTools "mcp__context7__get-library-docs"
See references/cli-reference.md for complete flag documentation.
Enable sandbox for safe file operations:
Via settings JSON:
bashclaude --settings '{"sandbox":{"enabled":true,"autoAllowBashIfSandboxed":true}}' -p "prompt"
Via stdin (pass /sandbox before prompt):
pythonstdin_content = "/sandbox\nYour actual prompt" subprocess.run(["claude", "-p"], input=stdin_content, text=True)
See references/hooks-examples.md for full sandbox options.
Before full automation, verify setup works:
bash# 1. Test tool pre-approval (should complete without prompts) claude -p "Create file test.txt with 'hello'" --allowedTools Write # 2. Test directory access claude -p "List files in /path/to/dir" --add-dir /path/to/dir --allowedTools LS # 3. Test JSON output claude -p "Return {\"status\": \"ok\"}" --output-format json | jq . # 4. Test hooks (check log file after) claude --settings '{"hooks":{...}}' -p "Simple task" --allowedTools Read
If any step fails, see Troubleshooting.
| Problem | Solution | |---------|----------| | Permission prompts appearing | Verify tool names match exactly (case-sensitive); MCP tools need full path | | Hooks not firing | Check script is executable (chmod +x), has shebang, timeout not exceeded | | Subprocess hangs | Add timeout; check if waiting for permission (missing --allowedTools) | | Working directory issues | Use absolute paths in --add-dir; set cwd in subprocess | | JSON parse errors | Use --output-format json; see json-extraction.md |
This skill is for subprocess invocation. Do not use for:
claude mcp command)| File | Contents | |------|----------| | cli-reference.md | Complete CLI commands and flags | | subprocess-patterns.md | Python sync/async/parallel patterns | | hooks-examples.md | Hook scripts and settings JSON | | json-extraction.md | Parsing JSON from CLI output | | orchestrator_example.py | Complete Python orchestrator class |
When using OpenCode as fallback, note key differences:
| Feature | Claude CLI | OpenCode CLI | |---------|------------|--------------| | Headless | claude -p "prompt" | opencode run "prompt" | | Model | --model sonnet | --model provider/model | | Settings | --settings JSON | Not supported | | Add dirs | --add-dir /path | Not supported |
Try-Claude-then-OpenCode pattern:
pythonimport subprocess def invoke_with_fallback(prompt: str, timeout: int = 300) -> str: """Try Claude CLI first, fall back to OpenCode on failure.""" try: result = subprocess.run( ["claude", "-p", prompt, "--allowedTools", "Write", "Read"], capture_output=True, text=True, timeout=timeout ) if result.returncode == 0: return result.stdout except (subprocess.TimeoutExpired, FileNotFoundError): pass # Fall through to OpenCode result = subprocess.run( ["opencode", "run", prompt], capture_output=True, text=True, timeout=timeout ) return result.stdout
See subprocess-patterns.md for advanced patterns.
| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 25,977 | 11,672 | -55% | 1 | 1 | 0% | 1,215 | 2,916 | +140% | 0 | 0 | — |
case-10 | fail→pass | 27,274 | 9,692 | -64% | 1 | 1 | 0% | 3,614 | 3,244 | -10% | 0 | 0 | — |
case-02 | pass→pass | 27,233 | 19,458 | -29% | 1 | 1 | 0% | 4,515 | 5,636 | +25% | 0 | 0 | — |
case-03 | fail→pass | 24,996 | 13,321 | -47% | 1 | 1 | 0% | 2,848 | 3,902 | +37% | 0 | 0 | — |
case-04 | pass→pass | 16,080 | 9,428 | -41% | 1 | 1 | 0% | 1,986 | 2,391 | +20% | 0 | 0 | — |
case-05 | pass→pass | 16,287 | 10,208 | -37% | 1 | 1 | 0% | 2,614 | 3,473 | +33% | 0 | 0 | — |
case-11 | pass→pass | 12,165 | 20,087 | +65% | 1 | 1 | 0% | 2,186 | 4,109 | +88% | 0 | 0 | — |
case-06 | fail→pass | 17,778 | 3,240 | -82% | 1 | 1 | 0% | 3,172 | 2,143 | -32% | 0 | 0 | — |
case-07 | fail→pass | 13,720 | 13,106 | -4% | 1 | 1 | 0% | 2,367 | 3,088 | +30% | 0 | 0 | — |
case-08 | fail→pass | 24,170 | 18,434 | -24% | 1 | 1 | 0% | 4,009 | 4,840 | +21% | 0 | 0 | — |
case-09 | pass→pass | 12,758 | 10,951 | -14% | 1 | 1 | 0% | 2,080 | 2,524 | +21% | 0 | 0 | — |
case-12 | pass→pass | 16,325 | 12,226 | -25% | 1 | 1 | 0% | 1,706 | 2,868 | +68% | 0 | 0 | — |
case-13 | pass→pass | 14,342 | 13,066 | -9% | 1 | 1 | 0% | 2,560 | 4,025 | +57% | 0 | 0 | — |
case-14 | pass→pass | 16,978 | 15,240 | -10% | 1 | 1 | 0% | 2,263 | 3,280 | +45% | 0 | 0 | — |
case-15 | fail→pass | 10,626 | 3,530 | -67% | 1 | 1 | 0% | 1,729 | 2,223 | +29% | 0 | 0 | — |
case-16 | pass→pass | 14,746 | 3,521 | -76% | 1 | 1 | 0% | 1,532 | 2,149 | +40% | 0 | 0 | — |
case-17 | pass→pass | 8,231 | 8,019 | -3% | 1 | 1 | 0% | 1,304 | 2,117 | +62% | 0 | 0 | — |
case-18 | pass→pass | 15,703 | 13,504 | -14% | 1 | 1 | 0% | 1,727 | 3,136 | +82% | 0 | 0 | — |
case-19 | fail→pass | 35,898 | 16,200 | -55% | 1 | 1 | 0% | 1,281 | 3,516 | +174% | 0 | 0 | — |
case-20 | pass→pass | 18,716 | 8,401 | -55% | 1 | 1 | 0% | 2,158 | 3,240 | +50% | 0 | 0 | — |
case-21 | fail→pass | 15,490 | 8,603 | -44% | 1 | 1 | 0% | 1,695 | 2,305 | +36% | 0 | 0 | — |
case-22 | fail→pass | 15,963 | 9,964 | -38% | 1 | 1 | 0% | 1,780 | 3,251 | +83% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted, and 20 counted toward the lift figure. The other 2 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +45 percentage points is the difference between those two pass rates over the 20 comparable cases.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.