← Blog

How-to5 min read

Running Claude Code (`claude -p`) and Codex (`codex exec`) in scripts and CI

How to run Claude Code (`claude -p`, `--bare`, `--allowedTools`) and Codex (`codex exec`, `--sandbox`, `--json`) non-interactively in scripts and CI.

Mirasim workbench running Claude Code and Codex sessions alongside CLI automationMirasim workbench running Claude Code and Codex sessions alongside CLI automation

The short answer

Use claude -p (print mode) for Claude Code and codex exec for Codex when you want a single command to run inside a shell script, cron job, or CI step without opening an interactive TUI. Both support structured JSON output, explicit permission controls, and API-key authentication.

Claude Code (claude -p)Codex (codex exec)
Basic commandclaude -p "Summarize auth.ts"codex exec "Summarize auth.ts"
Permissions / writes--allowedTools "Read,Edit,Bash"--sandbox workspace-write
Machine-readable output--output-format json / stream-json--json (JSON Lines)
Skip repo auto-load--bare--ignore-user-config / --ignore-rules
API key in CIANTHROPIC_API_KEYCODEX_API_KEY (inline)

Claude Code non-interactive mode (claude -p)

Adding -p (or --print) runs one turn and exits with status 0 on success or non-zero on failure. In interactive scripts or untrusted folders, add --bare so Claude Code starts faster and skips auto-discovering project hooks, MCP servers, skills, plugins, and CLAUDE.md.

Headless Claude Code with tool approvals, bare mode, and budget cap
# Fast, isolated one-off call (uses ANTHROPIC_API_KEY; skips OAuth/keychain and repo hooks)
ANTHROPIC_API_KEY="YOUR_KEY" claude --bare -p "Summarize README.md" --allowedTools "Read"

# Edit code with JSON output and a dollar cap
claude -p "Fix the failing unit test in auth.test.ts"   --allowedTools "Read,Edit,Bash"   --output-format json   --max-budget-usd 1.50

Codex non-interactive mode (codex exec)

codex exec streams progress to stderr and writes only the final agent message to stdout, making it easy to pipe into files or other tools. You can also pipe content into stdin while giving instructions in the prompt argument.

Piping output, allowing edits, and reading JSONL events in codex exec
# Read-only summary piped to a file (default sandbox is read-only)
codex exec --ephemeral "Generate release notes for the last 10 commits" | tee release-notes.md

# Pipe data on stdin and format it
git diff main | codex exec "List any breaking API changes"

# Allow workspace edits (--full-auto is no longer supported) and stream JSONL events
CODEX_API_KEY="YOUR_KEY" codex exec --sandbox workspace-write --json "Fix lint errors" | jq
  • --full-auto was removed: update older scripts to --sandbox workspace-write (or --approve-for-me to route approval requests through automatic review in a writable sandbox).
  • --ephemeral avoids writing session rollout files to disk in CI or batch workers.
  • --json emits JSON Lines events (thread.started, turn.started, item.*, turn.completed, turn.failed) to stdout.

Driving sessions from the CLI without losing visibility in Mirasim

Headless CLI runs are great for pure CI pipelines, but debugging a failed overnight script from raw terminal logs is slow. In Mirasim you can automate from the command line while keeping full session transcripts, diffs, and worktrees visible on desktop and phone.

  • mirasim ui-cli: drive the running app from scripts — manage queues (queue steer / jump / resume), answer prompts, bind models, and probe/pull local models with the exact same effect as clicking in the UI.
  • Automations and /goal: schedule recurring runs for any of the 11 agents (saved in <workspace>/.mirasim/schedules.json) or hand off a multi-turn /goal, and review every run's diff in the app or on your phone.

Questions

Why did codex exec --full-auto stop working?
OpenAI removed --full-auto from codex exec. Replace it with --sandbox workspace-write (or --approve-for-me for automatic approval review inside a writable sandbox).
Can I use my Claude subscription with claude --bare -p?
No. --bare skips OAuth credentials and the system keychain; supply ANTHROPIC_API_KEY, an apiKeyHelper via --settings, or cloud provider credentials (Bedrock, Vertex, Foundry).
How do I parse output cleanly from claude -p or codex exec?
Pass --output-format json (or stream-json) to claude -p, and --json to codex exec (which writes JSON Lines to stdout while progress stays on stderr).

Sources

Checked on November 29, 2026 against Anthropic's headless CLI guide (code.claude.com/docs/en/headless) and OpenAI's Codex non-interactive mode docs (learn.chatgpt.com/docs/non-interactive-mode). Check those pages for current flags.

Keep reading