Claude Code Local
调用本机 Claude Code 作为非交互 coding agent。Orchestrator 负责指挥和验收,Claude Code 负责写代码。
Quick Reference
| 场景 | 命令模板 |
|---|
| 短任务 | cd <project> && claude --bare -p "<任务>" --max-turns 10 |
| 长任务 | cd <project> && claude --bare -p "<任务>" --output-format stream-json --verbose --include-partial-messages --max-turns 30 |
| 全权限 | cd <project> && claude --bare -p "<任务>" --dangerously-skip-permissions --output-format stream-json --verbose --include-partial-messages --max-turns 30 |
| 只读分析 | cd <project> && claude --bare -p "<任务>" --permission-mode plan --allowedTools "Read,Glob,Grep,LSP" --max-turns 10 |
| JSON 输出 | cd <project> && claude --bare -p "<任务>" --output-format json --max-turns 10 |
Key Flags
| Flag | Purpose |
|---|
--bare | Minimal mode, skip hooks/LSP/plugins, faster startup |
-p / --print | Non-interactive, output to stdout (do not use PTY) |
--max-turns N | Limit turns: 10 short, 30 long |
--output-format stream-json | Streaming output, prevents timeout |
--output-format json | Single JSON with result field |
--verbose | Detailed logging (with stream-json) |
--include-partial-messages | Incremental output (with stream-json) |
Permission Modes
Default to the lowest level that accomplishes the task.
| Level | Mode | Flags | Use Case |
|---|
| 1 Read | plan | --permission-mode plan --allowedTools "Read,Glob,Grep,LSP" | Code exploration, doc review |
| 2 Analyze | default | --allowedTools "Read,Bash,Glob,Grep,LSP" | Run diagnostic commands |
| 3 Edit | acceptEdits | --permission-mode acceptEdits --allowedTools "Read,Edit,Write,Bash,Glob,Grep,LSP" | Modify source files |
| 4 Full | bypass | --dangerously-skip-permissions | Trusted projects, batch ops |
Timing Expectations
| Complexity | Duration | max-turns |
|---|
| Simple (回答问题) | 2-5 min | 5-10 |
| Medium (探索+单文件) | 5-15 min | 10-20 |
| Complex (多文件+测试) | 15-30 min | 20-40 |
Never kill Claude Code when exec times out. Check ps aux | grep claude first.
Post-Task Checklist
References
references/streaming.md — Stream-JSON event types and filtering
references/troubleshooting.md — Common failures and fixes