---
title: "Headless image generation through Codex CLI: what -m controls and the overwrite trap"
url: "https://www.dariuszparys.com/notes/codex-cli-headless-image-generation"
author: "Dariusz Parys"
published: 2026-11-20T00:00:00.000Z
description: "With codex exec, -m sets the agent model and not the image model, and the bundled instructions save a sibling file instead of overwriting, so checking the requested path can pass after a failed run."
tags:
  - "Agentic Coding"
  - "AI"
---

# Headless image generation through Codex CLI: what -m controls and the overwrite trap

## Context

`codex exec` can generate PNGs on a ChatGPT login, with no API key, when the `image_generation` feature is on. It calls a built-in image tool and saves the files under `~/.codex/generated_images/<thread>/`.

## Insight

- **`-m` doesn't pick the image model.** It only changes the agent model that drives the tool. The PNG's embedded C2PA metadata names ChatGPT/gpt-image as the generator whatever `-m` is set to.
- **Overwrite trap:** Codex's bundled imagegen instructions refuse to overwrite an existing file and save a sibling such as `hero-v2.png` instead. So "the requested path exists" is a false pass when a run fails or writes elsewhere.
  - Use a fresh output path, or say "overwrite" explicitly in the prompt.
  - Verify against the path in the final message written by `-o`, not against the path you asked for.
- **Non-TTY runs wait on stdin:** pass `< /dev/null`.
- **`--ignore-user-config`** avoids errors from broken plugin hooks. It only cuts input tokens from about 130k to 110k, so don't use it to save cost.

## Snippet

```bash
codex exec --ignore-user-config -o last.txt \
  "Generate a 1536x1024 hero image ... save it to ./out/hero-$(date +%s).png" < /dev/null
cat last.txt   # check the path actually written
```
