Support --session-id flag for `codex exec` to specify session ID upfront
Problem
When using codex exec to start a new session, there is no way to specify the session ID upfront. The session ID is only discoverable after the process starts (from its output). This contrasts with other CLI agents:
- Claude Code supports
--session-id <id>for new sessions and--resume <id>for existing ones - Cursor Agent accepts
--resume <id>which creates a new session if the ID doesn't exist
With Codex, the only session-related flag is codex exec resume <id>, which only works for existing sessions.
Use Case
When orchestrating multiple agent sessions programmatically (e.g., from a management console or automation tool), it's very useful to assign a known session ID before the process starts. This allows:
- Pre-registering the session in a database or UI before the run begins
- Correlating logs and output without needing to parse the initial output to discover the ID
- Simplifying resume logic — the orchestrator can use the same ID for both new and resumed runs
- Race-condition-free tracking — no window between process start and ID discovery where the session is "unknown"
Proposed Solution
Add a --session-id <id> flag (or similar) to codex exec that lets the caller specify the session ID for a new run. If the session already exists, it could either error or behave like resume (TBD).
# New session with caller-specified ID
codex exec --session-id my-custom-id-123 --json -m o4-mini < prompt.txt
# Resume existing session (already supported)
codex exec resume my-custom-id-123 --json < prompt.txt
Thank you!
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗