CLI Control Surface: Print Mode, JSON, and Sessions
Operate Claude Code as a bounded CLI process with print mode, structured output, turn limits, permissions, and resumable sessions.
What You’ll Learn
Claude Code is not limited to its interactive terminal UI. Its released CLI surface can run one bounded task, emit machine-readable output, enforce a JSON Schema, limit agent turns, select a permission mode, and resume a saved session.
This lesson was verified against the Claude Code 2.1.305 command contract.
Check claude --version and the current reference before relying on it in
consequential automation. claude --help does not list every documented flag:
--max-turns, for example, appears only in the CLI reference.
Start with Print Mode
-p is short for --print: Claude runs non-interactively, writes the response,
and exits.
claude -p "Summarize the changed files and report which tests ran."
Do not copy the meaning of -p from another CLI. Codex uses -p for a
configuration profile, while Grok Build uses it for a single-turn prompt.
Choose an Output Contract
Use text for humans, JSON for one structured result, and stream JSON when a consumer needs incremental events.
claude -p --output-format text "Explain the failing test."
claude -p --output-format json "Return the repository status."
claude -p --output-format stream-json --verbose --include-partial-messages \
"Report progress while reviewing this repository."
The JSON result holds the response text in result, plus metadata such as
session_id. The documented streaming form pairs stream-json with
--verbose; --include-partial-messages adds token-level events.
Treat output as untrusted data. Parse it, validate its shape, and keep it separate from shell command construction.
Require a JSON Schema
--json-schema validates print-mode output against a schema. Pair it with
--output-format json: the validated object arrives in the structured_output
field. Quote the schema so the shell does not split or expand it.
claude -p --output-format json \
--json-schema '{"type":"object","properties":{"summary":{"type":"string"},"tests":{"type":"array","items":{"type":"string"}}},"required":["summary","tests"],"additionalProperties":false}' \
"Review the current diff and return the requested fields." \
| jq '.structured_output'
If a run ends without valid structured output, for example because Claude
declined the request, the response has no structured_output field. Check that
the field exists before using it. A schema validates shape, not truth. The
caller must still verify files, tests, and external state.
Bound Authority and Work
Turn limits and permission modes are separate controls:
claude -p \
--max-turns 6 \
--permission-mode plan \
"Map the implementation path and risks without editing."
--max-turns limits the agent loop and exits with an error when the limit is
reached. --permission-mode determines how tool authority is handled. Neither
replaces an OS sandbox, repository isolation, or review of destructive commands.
When neither a flag nor permissions.defaultMode sets a mode, a claude -p run
on v2.1.302 or later starts in Manual mode (config value default). Any call
that needs approval is denied unless an allow rule, such as
--allowedTools "Bash(npm test *)", a hook, or --permission-prompt-tool
approves it. Grant the narrow set the task needs, or choose a mode such as
auto or dontAsk deliberately.
If the run has a permission host, such as an Agent SDK canUseTool callback or
a --permission-prompt-tool, but nobody will answer, add
--permission-prompts none (v2.1.259 or later) so those prompts are denied
instead of waited on.
Continue Deliberately
Resume by an explicit session identifier when reproducibility matters. Capture the ID from JSON output:
session_id=$(claude -p --output-format json "Start the review." | jq -r '.session_id')
claude -p --resume "$session_id" \
"Continue from the saved context and re-check the current worktree first."
--continue selects the most recent conversation in the current directory and
is convenient but less explicit:
claude -p --continue "Continue only if the repository state still matches."
Saved context can be stale. Re-check instructions, permissions, branch, diff, and test state after every resume.
Production Checklist
- Pin and record the Claude Code version.
- Use
-ponly for Claude print mode. - Select an output format and validate machine-readable output.
- Add a JSON Schema when downstream code depends on fields, and check that
structured_outputis present. - Cap turns and set an intentional permission mode instead of relying on the default.
- Run
-ponly in repositories you trust: it skips the workspace trust dialog and still runs project hooks and.mcp.jsonservers. For code you didn’t write, start with--bare; on the Anthropic API it then authenticates only withANTHROPIC_API_KEYor anapiKeyHelperpassed via--settings. - Resume explicit sessions and revalidate the worktree.
- Never interpolate model output directly into a shell.
Next
Session 14 connects this bounded CLI workflow to MCP servers while keeping server trust and tool authority explicit.