Skip to content
mentoor

Topic 1 of 5 · 4 min read

Non-interactive agent runs from the shell

Goal: Write a non-interactive agent invocation with the minimum pre-approved tools and handle its exit code and output in a script.

A headless run is one command that takes a prompt, works without asking anyone, and hands back an answer and an exit code, so everything it may do must be decided before it starts.

Check yourself · 1 of 3

What is the main difference between an interactive agent session and a headless run?

Choose one answer

Why it matters

In a chat session a person clicks "allow" and steers. In a script nobody does. If the agent needs a permission you did not grant, it stops or fails. If you grant too much, it can edit or delete things while no one is watching. Headless work is where permission choices stop being a convenience and become the safety design.

How to do it

Most coding agents have a one-shot mode. Claude Code uses claude -p "prompt" (print mode). Codex uses codex exec "prompt". Both read the prompt, run the agent loop, print the result and exit.

  1. Write a complete prompt. There is no follow-up question. Name the files, the goal and the stop condition.
  2. Pre-approve the smallest tool set. In Claude Code that is --allowedTools with narrow patterns. In Codex it is the sandbox setting, for example --sandbox read-only for analysis jobs. Start read-only and add write access only when the job needs it.
  3. Choose the output format. Plain text suits a human log. JSON suits a script (next topic).
  4. Check the exit code. A non-zero code means the run failed. Never parse the output of a failed run as if it were a result.
  5. Set a timeout. Wrap the call in timeout so a hung run cannot hold a runner forever.
set -euo pipefail
out=$(timeout 600 claude -p "List the TODO comments in src/ and group them by file." \
  --allowedTools "Read,Grep,Glob" \
  --output-format json) || { echo "agent run failed" >&2; exit 1; }
echo "$out" > review/todos.json

Before and after

Weak: claude -p "fix the failing tests" --dangerously-skip-permissions in a nightly job. The agent can run any command with your credentials, and nothing limits the damage if it misreads the task.

Better: claude -p "Run npm test, then fix only the failing test files under src/." --allowedTools "Read,Edit,Bash(npm test)" inside a throwaway checkout. It can read, edit and run one command. Anything else is refused.

What goes wrong

  • Skipping permissions to make it work. A prompt failing on a denied tool is a signal to add that one tool, not to turn checks off.
  • Ignoring the exit code. A script that pipes output onward on failure ships garbage.
  • Running in your real working tree. Use a clean checkout or a container so a bad edit is easy to throw away.
  • Vague prompts. Without a person to ask, the agent guesses, and guessing in a loop is slow and costly.

Examples

A read-only nightly audit

A cron job runs codex exec --sandbox read-only "Summarise dependencies older than two years" in a clean clone. The sandbox blocks writes, so the worst case is a wrong summary, not a changed repo.

The permission that was too broad

A script allows Bash with no pattern so the agent can run tests. The agent also runs rm on a build folder it thought was stale. Allowing Bash(npm test) would have kept the test run and blocked the rest.

Key points

  • A headless run has no person to answer questions, so the prompt and the permissions must be complete before it starts.
  • Use the one-shot mode of your agent: claude -p for Claude Code, codex exec for Codex.
  • Pre-approve the smallest set of tools, and start read-only for analysis jobs.
  • Check the exit code and treat a non-zero code as a failed run, never as a result.
  • Wrap runs in a timeout and a clean checkout so a bad run is cheap to discard.

Common mistakes

  • Using a skip-all-permissions flag in an unattended job instead of allowing the specific tools needed.
  • Treating output from a failed run as valid and passing it to the next step.
  • Running the agent in the live working tree rather than a throwaway checkout or container.
  • Writing a short prompt that relies on the agent asking a clarifying question.

Check yourself · 2 of 3

A nightly job only needs to summarise open TODO comments. Which setup is best?

Choose one answer

Check yourself · 3 of 3

A script calls the agent and the command exits with status 1. What should the script do?

Choose one answer

Practice items, not counted towards your mastery.

Start here