Run a prompt
Pass the prompt with-p (or --prompt):
--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, socat file | o4 -p "..." doesn’t send the file. Put the text into the prompt with command substitution instead:
Permissions in print mode
Print mode can’t show approval prompts, so it handles permissions differently from the interface:- It starts in
autopermission 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-modepicks 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:
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.- Print mode never opens the setup wizard. If no default model is set, o4 picks one based on the credentials it finds. Pass
-min 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 trustin the checkout, or see Workspace trust. --dangerously-bypass-hook-trustruns 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.
^modelmentions 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:/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.
Related pages
- CLI reference: every flag.
- Watch mode: run a check when files change and let o4 fix failures.
- Environment variables: API keys and other settings.