Skip to content
Nilay Kabariya

Entry No.033·Agents··2 min read

Fix: Codex CLI error "stdin is not a terminal"

OpenAI's Codex CLI stops with stdin is not a terminal when it runs from a script, a pipe or CI. Why, and the command that works there, tested on 0.158.0.

by Nilay#codex#openai#cliAGENTS

The error, verbatim

Jump to the fix ↓
Error: stdin is not a terminal
✓ Reproduced on Windows 11 Pro

Tested on

Codex CLI
0.158.0
Node
24.14.0
OS
Windows 11 Pro
Contents
  1. The fix01
  2. What triggers it02
  3. After it starts03
  4. How this was tested04

Plain codex opens a full-screen interactive app, and that needs a real terminal to read your keys from. Run it anywhere else, from a script, a pipe, CI, a scheduled task, or a container started without a terminal, and it stops before doing anything.

The fix

Use codex exec, the non-interactive version, and give it the prompt:

codex exec "say hi"

To feed it text from another command, pipe it in. Codex reads the prompt from the pipe:

echo "prompt from pipe" | codex exec

If you pass a prompt and pipe text, the piped text is added after your prompt as a <stdin> block, which is the handy form for scripts:

your-command | codex exec "what to do with its output"

What triggers it

Every one of these was run from a shell with no terminal attached, the way scripts and CI run:

Command Result
codex Error: stdin is not a terminal
echo "say hi" | codex Error: stdin is not a terminal
codex "say hi" Error: stdin is not a terminal
codex exec "say hi" starts
echo "say hi" | codex exec starts, using the piped text as the prompt

codex exec - also works: the - tells it to take the whole prompt from the pipe, the same as leaving the prompt out.

After it starts

codex exec still needs to be signed in or to have an API key, like the interactive app. If it gets past this error and then reports a missing key, that’s a different problem: Missing environment variable: OPENAI_API_KEY in the Codex CLI.

How this was tested

Codex CLI 0.158.0 installed from npm on Windows 11 with Node 24.14.0, run from a non-interactive shell (standard input a pipe or empty, as in scripts and CI), with a separate, empty CODEX_HOME folder. Each command in the table was run as shown, and the prompt handling was checked in the “user” line Codex prints at the start of each run. Every message above is copied from those runs.

— N.K., end of entry No.033

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.045

    Fix: APIConnectionError "Connection error." (OpenAI, Anthropic)

    OpenAI's and Anthropic's SDKs both say only Connection error. Five real causes reproduced, from a local model that isn't running to antivirus HTTPS scanning.

    > APIConnectionError: Connection error.

    AI5 min
  2. No.023

    Fix: OpenAI 401 Incorrect API key provided (AuthenticationError)

    OpenAI rejects a key you just copied. How to read which key was really sent from the error itself, why an old key can beat your .env, and the fix.

    > AuthenticationError: 401 Incorrect API key provided: sk-old-s*****************1111.

    AI3 min
  3. No.021

    Fix: The OPENAI_API_KEY environment variable is missing or empty

    OpenAI's SDK or Codex CLI says your key is missing, though it's in .env. Every wording, in JavaScript, Python and Codex, and the fix for each.

    > OpenAIError: The OPENAI_API_KEY environment variable is missing or empty; either provide it,

    AI4 min

Post card · Newsletter

Get the next fix in your inbox.

One short email when a new entry is published. No spam, never shared, and you can leave any time.

— Nilay

or follow by RSSor on X

By subscribing you agree to the privacy note. One click to leave.

tip: paste the exact error text