How to run Codex exec with a prompt

A non-interactive Codex run fits scripts, CI checks, and one-off terminal tasks that need a final answer without opening the full interactive interface. codex exec starts a Codex agent from a prompt, streams run progress separately, and leaves the final assistant message on stdout for a shell pipeline, redirect, or saved file.

The prompt can be a normal command argument, the entire stdin stream when the prompt value is -, or an instruction argument paired with piped context. That distinction matters because prompt-plus-stdin tells Codex what to do while passing the piped text as material to inspect, while - makes the piped text become the instruction itself.

Run prompt examples from the repository or workspace that should own the task, and keep the sandbox as narrow as the task allows. The CLI must already be authenticated before a real model run can produce a final answer, and inline prompt text can remain in shell history, so use stdin or a protected prompt file for longer or sensitive instructions.

Steps to run Codex exec with a prompt:

  1. Open a terminal in the repository that should own the run.
    $ git rev-parse --show-toplevel
    /home/user/projects/example-app

    codex exec requires a Git repository by default. Use --skip-git-repo-check only for an intentional scratch directory.
    Related: How to fix the Codex trusted-directory error
    Related: How to set the working directory for Codex
    Related: How to skip Codex git repository checks

  2. Check how the installed CLI accepts prompt input.
    $ codex exec --help
    Run Codex non-interactively
    
    Usage: codex exec [OPTIONS] [PROMPT]
           codex exec [OPTIONS] <COMMAND> [ARGS]
    
    Arguments:
      [PROMPT]
              Initial instructions for the agent.
              If not provided or if `-` is used, instructions are read from stdin.
              If stdin is piped and a prompt is also provided, stdin is appended as a `<stdin>` block.
    ##### snipped #####

    The help output confirms the two prompt shapes: prompt argument for inline instructions, or - when stdin should become the full prompt.

  3. Run a short inline prompt with an explicit read-only sandbox.
    $ codex exec --ephemeral --sandbox read-only "Reply with exactly OK."
    OK

    --ephemeral avoids retaining session files for the run. --sandbox read-only is the safest setting for inspection-only tasks, even though read-only is also the default for codex exec.

  4. Point the run at another repository with -C when the current shell is not already in the target workspace.
    $ codex exec --ephemeral --sandbox read-only -C /home/user/projects/example-app "Summarize README.md in one sentence."
    Example App demonstrates the HTTP worker used by the test suite.

    Replace /home/user/projects/example-app with the repository that should provide files, instructions, and Git context.
    Related: How to set the working directory for Codex

  5. Feed the whole prompt through stdin by using - as the prompt value.
    $ printf '%s\n' 'Reply with exactly STDIN-OK.' | codex exec --ephemeral --sandbox read-only -
    STDIN-OK

    Use this pattern for generated, multiline, or sensitive prompts that should not be typed directly as a shell argument.

  6. Pipe context into a run that still receives its instruction as the prompt argument.
    $ printf '%s\n' 'alpha beta gamma' | codex exec --ephemeral --sandbox read-only "Return only the last word from stdin."
    gamma

    With both a prompt argument and piped input, Codex appends the piped text as a <stdin> block instead of replacing the instruction.

  7. Write the final response to a file when another script should read it later.
    $ codex exec --ephemeral --sandbox read-only -o reply.txt "Reply with exactly SAVED-OK."
    SAVED-OK

    -o is the short form of --output-last-message. The final message is still printed to stdout while the same text is written to the file.
    Related: How to save the last Codex response to a file

  8. Read the saved file to verify the handoff path.
    $ cat reply.txt
    SAVED-OK

    Use --json instead when automation needs every event from the run, or --output-schema when the final answer must follow a JSON contract.
    Related: How to enable JSON output in Codex
    Related: How to use an output schema in Codex