codex-agent

Original🇨🇳 Chinese
Translated

Delegate coding, review, diagnosis, planning, structured output, and native browser research tasks to independent Codex sessions via Codex CLI. Use cases include creating new tasks with `codex exec`, resuming multi-turn sessions with `codex exec resume`, performing read-only reviews with `codex exec review`, as well as scenarios requiring `--json` event streams, `-o` final message persistence, image input, or Computer Use browser operations.

6installs
Added on

NPX Install

npx skill4agent add ben2pc/g-claude-code-plugins codex-agent

Tags

Translated version includes tags in frontmatter

SKILL.md Content (Chinese)

View Translation Comparison →

Codex Agent

Delegate tasks to independent Codex sessions for execution via Codex CLI.

Prerequisites

  1. Install Codex CLI:
    npm install -g @openai/codex
  2. Ensure you have completed Codex login authentication (the first run of
    codex
    will guide you through login)
  3. It is recommended to run in the target project directory, or explicitly pass
    -C /path/to/project

Usage Methods

Create a New Session

bash
codex exec --json --sandbox workspace-write --skip-git-repo-check --model gpt-5.4 "Your task description"
The above command is only suitable for short prompts. When the prompt exceeds approximately 500 characters or contains multiple lines/special characters, do not continue using positional parameters; using stdin is more stable.
Output is in JSONL format, with one event per line. Common current events:
jsonl
{"type":"thread.started","thread_id":"019d32fc-..."}
{"type":"turn.started"}
{"type":"item.completed","item":{"id":"item_0","type":"agent_message","text":"Response content"}}
{"type":"turn.completed","usage":{"input_tokens":46879,"cached_input_tokens":2432,"output_tokens":54}}
  • Extract
    thread_id
    from the
    thread.started
    event for subsequent multi-turn conversations
  • Extract
    text
    from the
    item.completed
    event (where
    item.type == "agent_message"
    ) as Codex's response
  • Extract
    usage
    from
    turn.completed
    to record token consumption
Only parse JSON lines in automation scripts. If your runtime environment merges
stderr
warnings with
stdout
, first filter out JSON lines starting with
{
, or redirect
stderr
.

Use stdin Pipe for Long Prompts

When the prompt exceeds approximately 500 characters or contains multiple lines/special characters, do not continue using positional parameters. Such inputs may get stuck at
Reading additional input from stdin...
in practice, even if you manually close stdin. It is recommended to write the prompt to a file first, then let
codex exec
read from stdin using
-
:
bash
codex exec --json --sandbox workspace-write --full-auto --model gpt-5.4 \
  --skip-git-repo-check - < /tmp/task-prompt.txt > /tmp/task-out.jsonl 2>&1
Positional parameters are still suitable for short prompts, such as one-sentence Q&A, very short follow-up questions, or temporary command-line experiments.

Resume a Session

bash
codex exec resume --json --model gpt-5.4 "thread_id" "Follow-up question"

Resume the Most Recent Session (Shortcut)

bash
codex exec resume --json --model gpt-5.4 --last "Follow-up question"
  • --last
    only looks at the most recent session recorded in the current directory by default
  • Add
    --all
    when you need to search across directories

⚠️
--ephemeral
: Non-recoverable Session

After adding
--ephemeral
, the session will not be written to disk (not persisted to
~/.codex/sessions/
), so it cannot be recovered by
codex exec resume
or
--last
afterwards. Use only in the following scenarios:
  • One-time quick Q&A where you are certain no follow-up will be needed
  • Temporary calls in CI/scripts to avoid polluting the session list
  • Sensitive tasks where you do not want local records to be left behind
Do not add
--ephemeral
if you may need to follow up later.
It is semantically equivalent to
claude -p --no-session-persistence
.

codex exec Parameters

Output and Results

FlagDescription
--json
Output in JSONL format for easy event stream parsing
--output-schema FILE
Use JSON Schema to constrain the structure of the last message
-o, --output-last-message FILE
Write the last message directly to a file
--color COLOR
Color output:
always
/
never
/
auto

Execution Environment

FlagDescription
-s, --sandbox MODE
Sandbox mode:
read-only
/
workspace-write
/
danger-full-access
--full-auto
Currently a shortcut equivalent to
--sandbox workspace-write
--dangerously-bypass-approvals-and-sandbox
Skip all confirmations and sandbox protections, extremely dangerous
-C, --cd DIR
Specify the working directory
--skip-git-repo-check
Allow running in non-git directories
--add-dir DIR
Additional writable directory (can be repeated)

Model and Configuration

FlagDescription
-m, --model MODEL
Specify the model, explicit passing is recommended
-p, --profile PROFILE
Use the profile in
config.toml
-c, --config key=value
Override configuration items in
config.toml
--enable FEATURE
Enable feature flags (can be repeated)
--disable FEATURE
Disable feature flags (can be repeated)
--oss
Use local open-source model providers
--local-provider PROVIDER
Specify local providers (e.g.,
lmstudio
/
ollama
)

Input

Flag / MethodDescription
-i, --image FILE
Attach images (can be repeated)
PROMPT
Pass short tasks directly as command-line parameters; only recommended for short prompts
-
or stdin
Do not pass a prompt, or set the prompt to
-
to read from stdin; prefer this method for long prompts, Markdown, or multi-line structures

codex exec resume Parameters

FlagDescription
--json
Output in JSONL format
-m, --model MODEL
Specify the model
--full-auto
Shortcut equivalent to
workspace-write
sandbox
--dangerously-bypass-approvals-and-sandbox
Skip confirmations and sandbox protections
--skip-git-repo-check
Allow running in non-git directories
--ephemeral
Do not persist the session
-i, --image FILE
Attach images
-o, --output-last-message FILE
Write the last message to a file
-c, --config key=value
Override configuration items in
config.toml
--enable FEATURE
Enable features (can be repeated)
--disable FEATURE
Disable features (can be repeated)
--last
Resume the most recent session (no need to specify ID)
--all
Search all sessions (not limited to current directory)

codex exec review Parameters

Built-in code review subcommand for reviewing the current repository:
bash
codex exec review [OPTIONS] [PROMPT]
FlagDescription
--uncommitted
Review staged, unstaged, and untracked changes
--base BRANCH
Compare against the specified base branch
--commit SHA
Review changes introduced by the specified commit
--title TITLE
Title displayed in the review summary
-m, --model MODEL
Specify the model
--json
Output in JSONL format
--full-auto
Shortcut equivalent to
workspace-write
sandbox
--ephemeral
Do not persist the session
-o, --output-last-message FILE
Write the last message to a file

Multi-turn Conversations

  1. Run
    codex exec --json ...
    for the first time to get
    thread_id
  2. Use
    codex exec resume --json "thread_id" "prompt"
    for follow-up questions
  3. thread_id
    is tracked automatically, no need for users to manage it
  4. Create new sessions for different tasks; multiple
    thread_id
    s do not interfere with each other

Model Selection

Explicitly specify
--model
based on task complexity:
Task ComplexityModelApplicable Scenarios
High
gpt-5.4
Architecture design, complex refactoring, multi-file coding
Medium
gpt-5.4-mini
Single-file feature implementation, bug fixes
Low
gpt-5.3-codex-spark
Simple Q&A, code explanation

Recommended Parameter Combinations

ScenarioModelSandboxOther Flags
Complex Coding
gpt-5.4
workspace-write
--full-auto
General Coding
gpt-5.4-mini
workspace-write
--full-auto
Read-only Q&A / Analysis
gpt-5.4-mini
read-only
--skip-git-repo-check
(when in non-git directories)
Browser Research / Computer Use
gpt-5.4
read-only
-C "$PWD" -o /tmp/result.txt
, add
--json
if event streams are needed
Code Review
gpt-5.4-mini
read-only
codex exec --json ... "Review ..."
Repository Review
gpt-5.4-mini
codex exec review --base main
Quick Q&A
gpt-5.4-mini
read-only
--skip-git-repo-check --ephemeral
(⚠️ Non-recoverable)
Structured Output
gpt-5.4-mini
read-only
--output-schema schema.json -o result.json

Usage Rules

  1. Always add
    --json
    for automated calls
    : Ensure output is parsable to extract
    thread_id
    and response content
  2. Always explicitly pass
    --model
    : Avoid default model drift
  3. Always run in the target project directory: Prefer
    cd /path/to/project
    or
    -C /path/to/project
  4. Use
    workspace-write
    for coding tasks
    : Usually directly use
    --sandbox workspace-write
    or
    --full-auto
  5. Use
    read-only
    or
    codex exec review
    for review tasks
    : Prevent accidental modifications
  6. Maintain conversation continuity: Reuse
    thread_id
    for follow-ups on the same task; do not add
    --ephemeral
    if follow-up may be needed
  7. Use
    --output-schema
    +
    -o
    for stable downstream parsing
    : Constrain the final result into a machine-consumable structure
  8. Use stdin pipe for long prompts: Do not use positional parameters when prompts exceed ~500 characters or contain multi-line/special characters; write to a file first, then pass via
    - < file.txt
    to avoid getting stuck at
    Reading additional input from stdin...
  9. Report results to users: After each call, extract the final response from JSONL and summarize it briefly for users
  10. Distinguish between
    -o
    and
    --json
    responsibilities
    :
    -o
    is responsible for saving the last message to a file;
    --json
    is responsible for printing the entire event stream to stdout. Scripts often use both together.
  11. Prefer
    read-only
    for non-coding native browser tasks
    : If you only want Codex to use Computer Use to open Chrome, browse web pages, and summarize content,
    --full-auto
    is not needed; add
    Do not modify local files.
    to the prompt as a double safeguard.

Prompt References

Load corresponding references based on task types; do not load all default prompts into the main context at once:
  • Coding / Diagnosis / Planning / Narrow Fixes: Read references/task-prompt-recipes.md
  • Code Review / Challenging Review / Test Gap Check: Read references/review-prompt-recipes.md
  • Native Browser Research / Reddit or Community Sampling / Evidence-based Summary: Read references/browser-research-prompt-recipes.md
These references provide reusable or slightly modifiable default prompt templates; prioritize copying the closest template, then remove unnecessary blocks.

Examples

Coding Task

User: Use Codex to implement a TODO API in the current project

Step 1 - Create a new session:
cd /path/to/project && codex exec --json --full-auto --model gpt-5.4 "Implement a REST API for TODO items with CRUD endpoints. Use Express.js."

→ Parse output to get thread_id: "xxx", response: "Implemented server.js ..."

User: Add unit tests

Step 2 - Resume the session:
cd /path/to/project && codex exec resume --json --model gpt-5.4-mini "xxx" "Add unit tests for all the TODO API endpoints using vitest."

Resume the Most Recent Session

bash
cd /path/to/project && codex exec resume --json --model gpt-5.4-mini --last "Continue the refactor and remove the dead helper functions."
Suitable for scenarios like "continue the previous task" where you don't want to manually save
thread_id
.

Code Review

bash
# General read-only review
cd /path/to/project && codex exec --json --sandbox read-only --model gpt-5.4-mini "Review the changes in git diff HEAD~1. Focus on correctness, security, and missing tests."

# Built-in review: Compare with main
cd /path/to/project && codex exec review --json --model gpt-5.4-mini --base main

# Review uncommitted changes
cd /path/to/project && codex exec review --json --model gpt-5.4-mini --uncommitted

Structured Output and Write to File

bash
cd /path/to/project && codex exec --json --sandbox read-only --model gpt-5.4-mini \
  --output-schema ./review-schema.json \
  -o /tmp/review-result.json \
  "Review src/todo.ts and output summary, risks, and suggested tests."
Suitable for scenarios where results need to be fed to scripts, CI, or other agents.

Native Browser Research (Only Final Answer Needed)

bash
codex exec \
  -m gpt-5.4 \
  --sandbox read-only \
  --skip-git-repo-check \
  -C "$PWD" \
  -o /tmp/codex-last.txt \
  "Use Computer Use on my Mac. Open Google Chrome, go to Reddit, search for 'Duolingo review', open 3 representative posts (one positive, one negative, one long-term review), then summarize the findings in Chinese. Do not modify local files."
Suitable for manually viewing the final conclusion without caring about intermediate event streams.

Native Browser Research (Both Event Stream and Final Answer Persistence)

bash
codex exec \
  -m gpt-5.4 \
  --sandbox read-only \
  --skip-git-repo-check \
  -C "$PWD" \
  --json \
  -o /tmp/codex-last.txt \
  "Use Computer Use on my Mac. Open Google Chrome, search Reddit for Duolingo reviews, open a few representative posts, and then summarize them in Chinese. Do not modify local files."
Suitable for scripts or upper-level agents: read JSONL event stream from stdout, and read the final natural language conclusion from
/tmp/codex-last.txt
.

Image Input

bash
cd /path/to/project && codex exec --json --sandbox read-only --model gpt-5.4-mini \
  -i ./screenshots/login-bug.png \
  "Describe the UI issue in this screenshot and propose a minimal fix plan."
Suitable for visual regression, error screenshot diagnosis, and design draft difference analysis.

Long Task Prompt (Recommended)

bash
# 1. Write complex prompt to a file
cat > /tmp/task-prompt.txt <<'PROMPT_EOF'
Please complete the following tasks in the current repository:
1. First read the README and tests
2. Only modify files directly related to the issue
3. First add tests, then modify the implementation, and finally run verification
PROMPT_EOF

# 2. Read from stdin using - to avoid stuck long positional parameters
codex exec --json --sandbox workspace-write --full-auto --model gpt-5.4 \
  --skip-git-repo-check - < /tmp/task-prompt.txt > /tmp/task-out.jsonl 2>&1
Suitable for market research, multi-paragraph Markdown constraints, script-assembled prompts, or any task description that exceeds one screen.

Pass Long Prompt via stdin

bash
cat ./prompt.md | codex exec --json --sandbox workspace-write --model gpt-5.4 -
Suitable for long prompts, templated prompts, or dynamically assembled instructions fed directly to Codex via pipe. For long-term reuse in shell scripts, prefer the
- < file.txt
approach from the previous section for better readability and easier troubleshooting.