Skip to main content
Print mode runs one prompt without the terminal interface. o4 works on the task, prints the model’s reply, and exits. Use it in shell scripts, Git hooks, and CI jobs, or whenever you want an answer on standard output.

Run a prompt

Pass the prompt with -p (or --prompt):
A prompt on the command line is what starts print mode. The --print flag is accepted for compatibility, but it doesn’t change anything on its own: o4 --print -p "..." behaves exactly like o4 -p "...", and o4 --print with no prompt starts the normal interface. Without a terminal, for example in a script, that plain interface waits for input, so always pass -p. The agent can read files, search, edit, run commands and start subagents, subject to the permission mode and sandbox described below. A few interactive tools aren’t offered, such as ask_user_question, undo, todo_write and the task tools; see where each tool is available. It works in the current directory unless you pass -C. Other options work as usual:

What goes where

Print mode keeps the answer and the progress apart, so you can capture one without the other: A failed model request is retried up to 3 times. When standard output is a terminal, o4 strips terminal control sequences from the reply; when you redirect it, the bytes pass through unchanged. The reply is plain text in whatever format the model wrote it, often Markdown. There is no JSON output mode for a prompt run. If you need structured output, ask for it in the prompt and check it in your script.

Exit codes

Exit code 0 means o4 finished, not that the task succeeded. If your script depends on the result, check it yourself, for example by running the tests afterwards.

Give o4 input from other commands

Print mode doesn’t read standard input, so cat file | o4 -p "..." doesn’t send the file. Put the text into the prompt with command substitution instead:
For large files, it’s usually better to name the file in the prompt and let o4 read it with its tools:
A command line can only be so long, and your operating system limits it. Very large substitutions can fail before o4 starts.

Permissions in print mode

Print mode can’t show approval prompts, so it handles permissions differently from the interface:
  • It starts in auto permission mode, which runs ordinary tool calls without asking.
  • Anything that would still need your approval is denied, and the model is told it was denied. Even in auto, this includes package installs that would run install scripts.
  • --permission-mode picks another mode for the run.
Your permission rules from the configuration still apply. See Permissions for what each mode allows. The same goes for CodeMode: a tool call inside a CodeMode program that would need approval fails instead of asking.

Sandbox for commands

Without --sandbox, commands in print mode always run in the guarded tier: o4 doesn’t apply your default_tier setting, and it starts no egress proxy, so commands have no network unless a [sandbox] table without allowedDomains lifts the limit. See Sandbox. --sandbox sets how shell commands that the model runs are sandboxed for the whole run:
See Sandbox for the details of each policy and what your platform supports.
--permission-mode bypass together with --sandbox danger-full-access lets the model run anything on the machine with no checks. Use it only in a disposable environment, such as a throwaway container.

Use it in CI

A CI job usually runs on a fresh machine with no saved login. Install o4 in the job (see Install o4) and give it an API key through the environment. See Providers and API keys for the variable each provider reads.
Things to know for automation:
  • Print mode never opens the setup wizard. If no default model is set, o4 picks one based on the credentials it finds. Pass -m in automation so the model doesn’t change when the environment does. See Choosing a model.
  • Project hooks and MCP servers from a repository only run once the workspace is trusted. Run o4 trust in the checkout, or see Workspace trust.
  • --dangerously-bypass-hook-trust runs enabled hooks without the saved hook trust, for this run only. Use it only when your pipeline already checks where the hooks come from. See Hooks.
  • Print-mode runs aren’t saved as sessions. You can’t resume them.
  • Slash commands aren’t run in print mode, except the two diagnostics below. ^model mentions are refused with an error.

Check context and usage from a script

Two slash commands work as the whole prompt in print mode. They read local data only and don’t call the model:
Each prints one JSON document on a single line: /context --json describes the model’s context window and the configured compaction threshold, and /stats --json adds session statistics. Without a session, most values are null with a reason, since nothing has been sent yet. o4 -p "/stats" without --json prints the usage statistics recorded on this machine as text. Any other argument makes these commands fail with o4: diagnostic command error: invalid_arguments on standard error and exit code 2. See Context and cost.