---
name: claude-p
description: "Delegate a coding task to Claude Code CLI via print mode (oai -p). Use for one-shot, non-interactive tasks: fix a bug, add a feature, review code, extract structured data. No PTY needed."
---

# OpenCode — Print Mode (`claude -p`)

One-shot delegation to Claude Code CLI. Runs the task, returns the result, and exits. No interactive prompts, no tmux needed.

## Basic Invocation

Use `claudep` instead of `claude` — it's a wrapper that routes to DeepSeek via the Anthropic-compatible API and enables `--dangerously-skip-permissions` automatically.

```bash
claudep -p "<prompt>" \
  --allowedTools "Read,Edit,Bash" \
  --max-turns 10 \
  --output-format json \
  > /tmp/claude_result.json
```

Parse the result:
```bash
cat /tmp/claude_result.json | python3 -c 'import json,sys; r=json.load(sys.stdin); print(r["result"])'
```

## When to Use

- One-shot coding tasks (fix a bug, add a feature, refactor)
- Code review / security audit
- CI/CD automation and scripting
- Structured data extraction with `--json-schema`
- Piped input: `cat file | claude -p "analyze this"`
- Any task fully describable upfront

Print mode **skips ALL interactive dialogs** — no workspace trust prompt, no permission confirmations.

## Key Flags

### Model & Cost Control
| Flag | Effect |
|------|--------|
| `--model sonnet\|opus\|haiku` | Model selection |
| `--effort low\|medium\|high\|max` | Reasoning depth |
| `--max-turns <n>` | Limit agentic loops (prevents runaway) |
| `--max-budget-usd <n>` | Cap API spend in dollars (min ~$0.05) |
| `--fallback-model haiku` | Auto-fallback when default model is overloaded |

### Permissions
| Flag | Effect |
|------|--------|
| `--allowedTools <tools>` | Whitelist specific tools (comma-separated) |
| `--disallowedTools <tools>` | Blacklist specific tools |
| `--dangerously-skip-permissions` | Auto-approve ALL tool use |

Tool name syntax:
```
Read                   # all file reading
Edit                   # editing existing files
Write                  # creating new files
Bash                   # all shell commands
Bash(git *)            # only git commands
Bash(npm run lint:*)   # pattern matching
```

### Output Format
| Flag | Effect |
|------|--------|
| `--output-format text` | Plain text (default) |
| `--output-format json` | Single JSON result object |
| `--output-format stream-json` | Newline-delimited streaming JSON |
| `--json-schema <schema>` | Force structured JSON output matching a schema |
| `--verbose` | Full turn-by-turn output |
| `--include-partial-messages` | Partial chunks as they arrive (stream-json) |

### Context & Session
| Flag | Effect |
|------|--------|
| `--add-dir <path>` | Grant access to additional directories |
| `--continue` | Resume most recent session in current directory |
| `--resume <id>` | Resume specific session by ID |
| `--fork-session` | Resume but create a new session ID |
| `--no-session-persistence` | Don't save session to disk |
| `--append-system-prompt <text>` | Add to default system prompt |
| `--bare` | Skip hooks, plugins, MCP, CLAUDE.md (fastest, needs API key) |

## Output JSON Schema

```json
{
  "type": "result",
  "subtype": "success",
  "result": "The output text...",
  "session_id": "75e2167f-...",
  "num_turns": 3,
  "total_cost_usd": 0.0787,
  "duration_ms": 10276,
  "stop_reason": "end_turn",
  "usage": { "input_tokens": 5, "output_tokens": 603 }
}
```

`subtype` values: `success`, `error_max_turns`, `error_budget`.

## Common Patterns

### Review a diff
```bash
git diff main...feature | claude -p "Review for bugs, security issues, and style." \
  --allowedTools "Read" --max-turns 1
```

### Fix a bug
```bash
claudep -p "Fix the null pointer error in src/auth.py" \
  --allowedTools "Read,Edit" --max-turns 10 \
  --output-format json > /tmp/result.json
```

### Structured extraction
```bash
claudep -p "List all exported functions in src/" \
  --output-format json \
  --json-schema '{"type":"object","properties":{"functions":{"type":"array","items":{"type":"string"}}},"required":["functions"]}' \
  --max-turns 5
```

### Session resume
```bash
# Start
claudep -p "Refactor the database layer" --output-format json --max-turns 10 > /tmp/s.json

# Resume with session ID
SESSION=$(cat /tmp/s.json | python3 -c 'import json,sys; print(json.load(sys.stdin)["session_id"])')
claudep -p "Now add connection pooling" --resume "$SESSION" --max-turns 5
```

### Pipe input
```bash
cat src/auth.py | claude -p "Review for security issues" --max-turns 1
cat src/*.py    | claude -p "Find all TODO comments"     --max-turns 1
```

### Streaming output (real-time)
```bash
claudep -p "Explain the auth module" \
  --output-format stream-json --verbose --include-partial-messages | \
  jq -rj 'select(.type=="stream_event" and .event.delta.type?=="text_delta") | .event.delta.text'
```

## Gotchas

1. **`--max-turns` is print-mode only** — ignored in interactive sessions.
2. **`--max-budget-usd` minimum is ~$0.05** — system prompt cache creation costs this much.
3. **`--bare` requires `ANTHROPIC_API_KEY`** — skips OAuth.
4. **`--json-schema` needs enough `--max-turns`** — Claude reads files before producing structured output.
5. **Slash commands don't work in `-p` mode** — describe the task in natural language instead.
6. **`--continue` finds the most recent session for the current working directory** — must run from the same directory.
