Original URL: https://getagenttools.com/blog/codex-exec/

# codex exec: Codex without the TUI, and the sandbox your script really gets

Updated 26 Nov 2026Marek.

- codex
- coding-agents
- agent-ops

`codex exec` runs Codex without the TUI. You pass a prompt. Codex works, then prints only its final message to stdout. Progress goes to stderr. The short form is `codex e`. It is the Codex answer to `claude -p`.

 

```bash
codex exec -s read-only "summarize this repo"
codex exec -s read-only --json -o last.txt "count the files"
codex exec -s read-only resume --last "now list them"
```

  

- **Output.** Plain text by default. `--json` turns stdout into a JSON Lines event stream.
- **Files.** `-o` writes the final message to a file. `--output-schema` makes it fit a JSON Schema.
- **Sandbox.** The docs say read-only by default. On my machine the config file won. Pass `-s` every time.
- **Resume.** `codex exec resume` takes a session ID or `--last`. It does not take `-s`.
- **Git check.** By default, local `codex exec` stops with "Not inside a trusted directory" if it can't find a Git repository for its working directory. `--skip-git-repo-check` skips that check.

 

The Claude side of this, with JSON, schemas and `--bare`, is in my [claude -p](https://getagenttools.com/blog/claude-p-headless/) post. This one is the Codex side, with a test I ran on 25 October 2026.

 

## What does codex exec do?

 

It runs one Codex task with no human in the loop. The [non-interactive mode page](https://learn.chatgpt.com/docs/non-interactive-mode) lists CI, pre-merge checks and scheduled jobs. Codex streams progress to stderr. Stdout gets the final agent message only. So you can pipe it or redirect it. For a review of a diff there is a separate subcommand, [codex review](https://getagenttools.com/blog/codex-review/).

 

I use Codex every day as a worker next to Claude Code. I made an empty folder in a temp directory and ran `codex-cli 0.163.0` there. The first run failed in 0.16 seconds with exit code 1. It was the Git check, and it gets its own section below. In a one-off folder you add `--skip-git-repo-check`.

 

The second run asked for one word. It took 4.3 seconds. Stdout held exactly `OK`. Stderr held a header with the workdir, model, approval mode, sandbox and session ID. Read that header. It says what the run was allowed to do.

 

## What does "Not inside a trusted directory and --skip-git-repo-check was not specified" mean?

 

Codex is telling you it found no Git repository for its working directory. The "trusted" in it is not about a trust list. In the 0.166.0 and 0.172.0 source, a local run goes on only with a Git repo, `--skip-git-repo-check` or `--dangerously-bypass-approvals-and-sandbox`. A run with `--remote` to an existing app server leaves that code path earlier. Two fixes worked for me. Add `--skip-git-repo-check` to the command, or run `git init` in the folder first.

 

This is the full stderr from Codex 0.166.0 in an empty folder with no Git, on 5 November 2026:

 

```text
Reading additional input from stdin...
Not inside a trusted directory and --skip-git-repo-check was not specified.
```

 

The first line is not part of the error. Codex printed it because my shell gave it no terminal on stdin. The run took 0.30 seconds. Stdout stayed empty. In the source, the check runs before Codex starts its app server. So no model call happens.

 

I ran the same prompt three ways that evening:

 

| Command | Folder | Result |
| --- | --- | --- |
| `codex exec -s read-only "Reply with the single word OK."` | empty, no Git | exit 1 in 0.30 s, the error above |
| the same plus `--skip-git-repo-check` | the same folder | exit 0 in 7.8 s, stdout `OK`, header `sandbox: read-only` |
| the same, no extra flag | a new folder after `git init` | exit 0 in 8.2 s, stdout `OK` |

 

The docs give the reason. The non-interactive mode page says Codex "requires commands to run inside a Git repository to prevent destructive changes". My reading: Git gives you a way back after a bad edit, once the files are in a commit. A temp folder with no history gives you none. So I use `--skip-git-repo-check` for read-only runs in throwaway folders. When the agent may write, I run `git init` first. That alone only passes the check. For a way back, commit the files before the run.

 

The source has one more way past the check. `--dangerously-bypass-approvals-and-sandbox` skips it too. The code comment says that user "is explicitly running in an externally sandboxed environment". I did not run that flag for this test.

 

## Is there a codex execute command?

 

No. I looked through `codex --help` on 0.163.0 and found `exec` with its alias `e`, but no `execute`. The first usage line reads `codex [OPTIONS] [PROMPT]`. Give Codex no subcommand and the help says your options go to the interactive CLI. By the help text, an unknown word lands as a prompt. I did not launch it.

 

## How do I get JSON from codex exec?

 

Add `--json`. My test prompt told Codex to run `ls` and report how many files it saw. Six and a half seconds later stdout held seven JSON objects, one per line. Trimmed a little, they looked like this:

 

```json
{"type":"thread.started","thread_id":"01a170f3-3c87-..."}
{"type":"turn.started"}
{"type":"item.completed","item":{"type":"agent_message","text":"I'll run `ls` and count the files..."}}
{"type":"item.started","item":{"type":"command_execution","command":"/bin/zsh -lc ls","status":"in_progress"}}
{"type":"item.completed","item":{"type":"command_execution","exit_code":0,"status":"completed"}}
{"type":"item.completed","item":{"type":"agent_message","text":"6 files."}}
{"type":"turn.completed","usage":{...}}
```

 

The `thread_id` is the ID you resume later. `turn.completed` carries token usage. The docs also list `turn.failed` and `error`. Those are the events a CI wrapper should watch.

 

The answer was 6. All six were output files from my own runs. Two were redirect targets of this very run, created by my shell before `ls`. Write output outside the folder the agent inspects.

 

## How do I save the last message to a file?

 

That same JSON run also had `-o last.txt` on it. When it ended, the file contained one line, `6 files.` The long name of the flag is `--output-last-message`. The docs say the message still goes to stdout too. The CLI reference suggests pairing `--json` with `-o` in CI. You get the event log and a clean summary.

 

For fields, use `--output-schema`. I gave it a schema with one required integer, `files`. It took 9.7 seconds. Stdout was `{"files":17}`. I counted the folder myself. 17 was right.

 

## Which sandbox does codex exec use?

 

The docs say read-only by default. The CLI reference says the `--sandbox` default comes from configuration. Both are true. My test shows how.

 

Without `-s`, my header did not show `read-only`. It showed the sandbox from my own config file. With `--ignore-user-config`, the same prompt ran `read-only`. Every header showed `approval: never`. The `exec` help in 0.163.0 has no `--ask-for-approval` flag. Nobody answers prompts here. The sandbox does the stopping.

 

So the default you read about is the default of a clean install. A script that runs on a machine with a config file inherits that file. I pass `-s` on every call now.

 

`--full-auto` is gone. The docs say it is "no longer supported". Use `--sandbox workspace-write` for edits. Or use `--approve-for-me`, which routes approvals to an automatic reviewer. `--dangerously-bypass-approvals-and-sandbox` drops everything. More on that in my [Codex dangerously skip permissions](https://getagenttools.com/blog/codex-dangerously-skip-permissions/) post.

 

## How does codex exec resume work?

 

`codex exec resume <SESSION_ID> "prompt"` continues a saved run. `--last` takes the most recent session from the current directory. `--all` searches every directory. The ID comes from the stderr header or from `thread_id` in JSON.

 

I resumed the read-only JSON session without a sandbox flag. The header of the result did not say `read-only`. Per that header, the resumed run did not keep the old sandbox. It showed what looks like my config default. Then I asked the model which sandbox it was in. It said `read-only`. The header said otherwise. Trust the header. I did not test a write.

 

Putting `-s` after `resume` fails:

 

```text
error: unexpected argument '-s' found
```

 

Two forms worked. Both showed `sandbox: read-only` in the header:

 

```bash
codex exec -s read-only resume <SESSION_ID> "Reply OK."
codex exec resume -c 'sandbox_mode="read-only"' <SESSION_ID> "Reply OK."
```

 

## What is codex exec-server?

 

A different command. `codex --help` marks it `[EXPERIMENTAL]` and calls it "the standalone exec-server service". Its help lists a `--listen` transport. WebSocket is the default. `--remote` registers it as a remote environment. The non-interactive page names `codex exec-server --remote` as one place `CODEX_API_KEY` works. I have not run it.

 

## codex exec vs claude -p

 

| Job | `claude -p` | `codex exec` |
| --- | --- | --- |
| Final text | stdout | stdout, progress on stderr |
| Machine output | `--output-format json` or `stream-json` | `--json` (JSONL events) |
| Fixed shape | `--json-schema` | `--output-schema` |
| Final message to file | redirect stdout | `-o` |
| Continue a run | `--resume <id>`, `--continue` | `exec resume <id>`, `--last` |
| Skip local setup | `--bare` | `--ignore-user-config`, `--ignore-rules` |
| Permissions | `--allowedTools`, `--permission-mode` | `-s`, `--approve-for-me` |

 

The big gap is permissions. Claude decides per tool. Out of the box, my `codex exec` runs never asked anything. The sandbox did the limiting. With `--approve-for-me` a reviewer agent would get those questions. Gemini CLI denies them in a headless run, see [Gemini CLI headless](https://getagenttools.com/blog/gemini-cli-headless/).

 

## Frequently asked questions

 

### How do I run codex exec in CI?

 

The docs point GitHub Actions to `openai/codex-action`. Elsewhere, set `CODEX_API_KEY` only on the Codex call. The docs warn against a job-level API key where repository code runs.

 

### Can codex exec read the prompt from stdin?

 

Yes. Use `codex exec -` and pipe the prompt in. If you pass a prompt and pipe data too, the help says stdin is appended as a `<stdin>` block.

 

### How do I stop codex exec from saving sessions?

 

Add `--ephemeral`. The docs say it runs "without persisting session rollout files to disk". One catch: in my [Codex subagents](https://getagenttools.com/blog/codex-subagents/) test an `--ephemeral` run logged `collab spawn failed`.

 

### Why does codex exec resume reject my --sandbox flag?

 

On 0.163.0 my `-s` after `resume` ended with an argument error. Moving `-s` in front of `resume` worked. So did `-c 'sandbox_mode="read-only"'`. Check the header line either way.

 

### Does a trusted directory in config.toml fix "Not inside a trusted directory"?

 

Not in the Codex 0.166.0 source I read. The config reference lists `projects.<path>.trust_level`, set to `"trusted"` or `"untrusted"`. Per the docs, it decides whether Codex loads a project's own `.codex/` config, hooks and rules. The `exec` Git check looks only at two flags and at Git. I did not test a trust entry. In Codex 0.172.0, `codex exec --trust` saves project trust for this and future chats, per its help, but not for a project explicitly marked untrusted: the source stops the run with an error there. In the 0.172.0 source the local Git check still reads only those two flags and Git, so `--trust` does not get you past this message. `--skip-git-repo-check` or `git init` is the fix I know works.

 

## Verdict

 

`codex exec` is a clean script entry point. Text on stdout, events in JSONL, a schema when you need fields. Two habits would have saved me surprises. Pass `-s` on every call, resumes included. Read the stderr header before you trust the run.

 

Sources, read on 25 October 2026: the Codex [non-interactive mode](https://learn.chatgpt.com/docs/non-interactive-mode) page and the [CLI reference](https://learn.chatgpt.com/docs/developer-commands?surface=cli), plus local `codex --help`, `codex exec --help`, `codex exec resume --help` and `codex exec-server --help` for `codex-cli 0.163.0`. The runs, times and outputs come from my test on 25 October 2026.

 

Update, 26 November 2026: the error, the `--skip-git-repo-check` run and the `git init` run in the section on "Not inside a trusted directory" come from my test with `codex-cli 0.166.0` on 5 November 2026. The FAQ answer on trust comes from the docs, the source and the CLI help, not from a test. On 26 November I got the same message again with `codex-cli 0.172.0` in a new empty folder with no Git, running `codex exec "say OK" < /dev/null`: exit code 1 after 0.26 seconds, empty stdout. I read the check again at tag [`rust-v0.172.0`](https://github.com/openai/codex/blob/rust-v0.172.0/codex-rs/exec/src/lib.rs). I also read the check in [`codex-rs/exec/src/lib.rs`](https://github.com/openai/codex/blob/rust-v0.166.0/codex-rs/exec/src/lib.rs) at tag `rust-v0.166.0`, the [config reference](https://learn.chatgpt.com/docs/config-file/config-reference) and the non-interactive mode page again, all on 5 November 2026. On 26 November 2026 I read both pages once more: the Git quote and `projects.<path>.trust_level` are still there.

Put this into practice

[See the tool list](https://getagenttools.com/tools/)[Train your team](https://getagenttools.com/#training)
