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.


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 command | claude -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 CI | ANTHROPIC_API_KEY | CODEX_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.
# 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.50Codex 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.
# 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-autowas removed: update older scripts to--sandbox workspace-write(or--approve-for-meto route approval requests through automatic review in a writable sandbox).--ephemeralavoids writing session rollout files to disk in CI or batch workers.--jsonemits JSON Lines events (thread.started,turn.started,item.*,turn.completed,turn.failed) tostdout.
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.
Chapter 24 · Reference › ui-cli command catalog → Full `mirasim ui-cli` command catalog in the docs.
Questions
- Why did
codex exec --full-autostop working? - OpenAI removed
--full-autofromcodex exec. Replace it with--sandbox workspace-write(or--approve-for-mefor automatic approval review inside a writable sandbox). - Can I use my Claude subscription with
claude --bare -p? - No.
--bareskips OAuth credentials and the system keychain; supplyANTHROPIC_API_KEY, anapiKeyHelpervia--settings, or cloud provider credentials (Bedrock, Vertex, Foundry). - How do I parse output cleanly from
claude -porcodex exec? - Pass
--output-format json(orstream-json) toclaude -p, and--jsontocodex exec(which writes JSON Lines tostdoutwhile progress stays onstderr).
Article · Running Claude Code and Codex in GitHub Actions: setup, secrets, and safety → Running Claude Code and Codex inside GitHub Actions CI workflows.
Article · Using an API key with Claude Code and Codex: setup, custom endpoints, and switching back → Setting up `ANTHROPIC_API_KEY` and `CODEX_API_KEY` for scripts and CI.
Article · Scheduled tasks in Claude Code and Codex: /loop, cloud tasks, and recurring automations → Running Claude Code and Codex on a recurring schedule.
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

Fixing OpenAI and Codex 403 “unsupported_country_region_territory”
Why OpenAI and Codex return 403 unsupported_country_region_territory, how to fix false positives on VPNs or cloud IPs, and compliant alternatives.

Running Claude Code and Codex on Amazon Bedrock, Vertex AI, and Microsoft Foundry
Connect Claude Code and Codex to Amazon Bedrock, Vertex AI, and Microsoft Foundry: env vars, config.toml, auth, model pinning, and limits.

Using Claude Code with Chrome and Codex Browser: live debugging, Playwright, and previews
How to connect Claude Code to Chrome (extension, console logs, login limits) and use Codex Browser or Playwright, plus live dev-server previews in Mirasim.