| name | using-codex |
| description | Using Codex CLI (`codex exec`) for testing and automation. Focus on gpt-5-codex with different reasoning efforts, web search, streaming, and multi-turn conversations. |
Using Codex for Testing & Automation
Always use gpt-5-codex with different reasoning efforts. Never use o3 or 4o.
⚠️ IMPORTANT: Git Repository Requirement
- Codex requires a Git repository by default to prevent destructive changes
- If NOT in a git repo: Add
--skip-git-repo-check to every command
- If IN a git repo: Git check passes automatically
codex exec --model gpt-5-codex "task"
codex exec --model gpt-5-codex --skip-git-repo-check "task"
Official Documentation
Core Patterns
Note: All examples assume you're in a git repo. If not, add --skip-git-repo-check to every command.
1. Basic Execution (Read-Only)
codex exec --model gpt-5-codex "analyze test failures"
codex exec --model gpt-5-codex --skip-git-repo-check "analyze test failures"
2. With File Edits (Workspace-Write)
codex exec --model gpt-5-codex --full-auto "fix failing tests"
codex exec --model gpt-5-codex --skip-git-repo-check --full-auto "fix failing tests"
3. With Web Search (Critical for Up-to-Date Info)
codex exec \
--model gpt-5-codex \
--config tools.web_search=true \
"research best practices for async Rust and apply to this codebase"
codex exec \
--model gpt-5-codex \
--skip-git-repo-check \
--config tools.web_search=true \
"research best practices for async Rust and apply to this codebase"
4. Streaming Output (JSON Lines)
codex exec --model gpt-5-codex --json "run all tests" > output.jsonl
codex exec --model gpt-5-codex --json "analyze errors" | jq -r '.type'
Event types you'll see:
thread.started - session begins (contains thread_id)
turn.started - agent starts processing
item.completed - reasoning, commands, file changes
turn.completed - includes token usage
5. Multi-Turn Conversations (Save Conversation ID)
codex exec --model gpt-5-codex --json "run tests" 2>&1 > output.jsonl
cat output.jsonl | jq -r 'select(.type=="thread.started") | .thread_id' > thread_id.txt
THREAD_ID=$(cat thread_id.txt)
echo "fix those failures" | codex exec resume $THREAD_ID
echo "verify all tests pass now" | codex exec resume $THREAD_ID
echo "now check code coverage" | codex exec resume --last
Reasoning Effort Configuration
Always use gpt-5-codex - adjust reasoning effort based on task complexity:
Quick Tasks (Low Reasoning)
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=low \
"run pytest and report failures"
Standard Tasks (Medium Reasoning - Default)
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=medium \
"analyze test failures and suggest fixes"
Complex Tasks (High Reasoning)
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=high \
"debug race condition in concurrent tests"
Deep Analysis (Detailed Reasoning Summary)
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=high \
--config model_reasoning_summary=detailed \
"analyze architecture for security vulnerabilities"
Sandbox Modes
read-only (Default - Safest)
codex exec --model gpt-5-codex "analyze code coverage"
workspace-write (Allow File Edits)
codex exec --model gpt-5-codex --full-auto "fix type errors"
codex exec --model gpt-5-codex --sandbox workspace-write "fix errors"
danger-full-access (Full Disk + Network)
codex exec \
--model gpt-5-codex \
--sandbox danger-full-access \
"run integration tests against staging"
Command-Line Configuration (Flexible)
Use --config key=value for maximum flexibility. Don't use profiles.
Multiple Configs in One Command
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=high \
--config model_reasoning_summary=detailed \
--config tools.web_search=true \
--config approval_policy=never \
--config hide_agent_reasoning=true \
"research and implement caching strategy"
Common Config Combinations
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=low \
--config approval_policy=never \
--config hide_agent_reasoning=true \
"run test suite"
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=high \
--config model_reasoning_summary=detailed \
--config tools.web_search=true \
"analyze security of authentication flow"
codex exec \
--model gpt-5-codex \
--full-auto \
--config model_reasoning_effort=medium \
"refactor database module for better testability"
Structured Output (JSON Schema)
Get structured data back:
cat > schema.json << 'EOF'
{
"type": "object",
"properties": {
"test_failures": {
"type": "array",
"items": {"type": "string"}
},
"coverage_percent": {"type": "number"},
"needs_attention": {
"type": "array",
"items": {"type": "string"}
}
},
"required": ["test_failures", "coverage_percent"],
"additionalProperties": false
}
EOF
codex exec \
--model gpt-5-codex \
--output-schema schema.json \
"analyze test suite" -o results.json
cat results.json | jq '.test_failures'
Web Search (Important!)
Enable web search for up-to-date information:
codex exec \
--model gpt-5-codex \
--config tools.web_search=true \
--config model_reasoning_effort=medium \
"research 2025 best practices for React testing and apply here"
codex exec \
--model gpt-5-codex \
--config tools.web_search=true \
"check if we're using latest stable versions of dependencies"
codex exec \
--model gpt-5-codex \
--full-auto \
--config tools.web_search=true \
--config model_reasoning_effort=high \
"research this error message and fix it"
Streaming + Multi-Turn Pattern
Powerful pattern for complex workflows:
#!/bin/bash
echo "=== Turn 1: Analyzing Tests ==="
codex exec \
--model gpt-5-codex \
--json \
--config model_reasoning_effort=medium \
"run all tests and analyze failures" 2>&1 > turn1.jsonl
THREAD_ID=$(cat turn1.jsonl | jq -r 'select(.type=="thread.started") | .thread_id')
echo "Thread ID: $THREAD_ID"
echo -e "\n=== Turn 2: Fixing Failures ==="
echo "fix all test failures" | codex exec \
--json \
--full-auto \
--config model_reasoning_effort=high \
resume $THREAD_ID 2>&1 > turn2.jsonl
echo -e "\n=== Turn 3: Verifying Fixes ==="
echo "run tests again and confirm all pass" | codex exec \
--json \
resume $THREAD_ID 2>&1 > turn3.jsonl
echo -e "\n=== Final Result ==="
cat turn3.jsonl | jq -r 'select(.type=="item.completed" and .item.type=="agent_message") | .item.text' | tail -1
Practical Examples
Example 1: CI/CD Test Runner
codex exec \
--model gpt-5-codex \
--config model_reasoning_effort=low \
--config approval_policy=never \
--config hide_agent_reasoning=true \
--output-schema test-schema.json \
"run pytest and extract results" -o ci-results.json
Example 2: Deep Debugging Session
codex exec \
--model gpt-5-codex \
--json \
--config model_reasoning_effort=high \
--config model_reasoning_summary=detailed \
--config tools.web_search=true \
"analyze this segfault and research solutions" 2>&1 > debug.jsonl
THREAD_ID=$(cat debug.jsonl | jq -r 'select(.type=="thread.started") | .thread_id')
echo "apply the fix" | codex exec --full-auto resume $THREAD_ID
echo "verify it's resolved" | codex exec resume $THREAD_ID
Example 3: Research + Implementation
codex exec \
--model gpt-5-codex \
--full-auto \
--config model_reasoning_effort=high \
--config tools.web_search=true \
"research current best practices for rate limiting APIs and implement for our endpoints"
Example 4: Streaming Progress Monitor
codex exec \
--model gpt-5-codex \
--json \
--full-auto \
--config model_reasoning_effort=medium \
"refactor authentication module" 2>&1 \
| jq -r 'select(.type=="item.completed") |
"\(.item.type): \(if .item.type == "agent_message" then .item.text elif .item.type == "command_execution" then .item.command else .item.type end)"'
Key Configuration Options
--model gpt-5-codex
--skip-git-repo-check
--config model_reasoning_effort=low
--config model_reasoning_effort=medium
--config model_reasoning_effort=high
--config model_reasoning_summary=concise
--config model_reasoning_summary=detailed
--config tools.web_search=true
--config approval_policy=never
--config approval_policy=on-request
--config hide_agent_reasoning=true
--sandbox read-only
--sandbox workspace-write
--sandbox danger-full-access
Environment Variables
CODEX_API_KEY=sk-... codex exec --model gpt-5-codex "task"
CODEX_HOME=~/.codex-ci codex exec --model gpt-5-codex "task"
Output Modes
codex exec --model gpt-5-codex "task"
codex exec --model gpt-5-codex --json "task" > output.jsonl
codex exec --model gpt-5-codex --output-schema schema.json "task" -o out.json
codex exec --model gpt-5-codex "task" -o result.txt
Quick Reference
⚠️ Add --skip-git-repo-check if NOT in a git repository
codex exec --model gpt-5-codex "analyze code"
codex exec --model gpt-5-codex --full-auto "fix errors"
codex exec --model gpt-5-codex --config tools.web_search=true "research and implement feature"
codex exec --model gpt-5-codex --config model_reasoning_effort=high "debug complex issue"
codex exec --model gpt-5-codex --json "task" > output.jsonl
codex exec --model gpt-5-codex --json "step 1" 2>&1 > out.jsonl
THREAD_ID=$(cat out.jsonl | jq -r 'select(.type=="thread.started") | .thread_id')
echo "step 2" | codex exec resume $THREAD_ID
codex exec --model gpt-5-codex --skip-git-repo-check "task"
Learning from GitHub Issues
Use gh CLI to find real-world examples:
gh issue list -R openai/codex -S "codex exec" -L 20
gh issue list -R openai/codex -S "web_search" -L 10
gh issue list -R openai/codex -S "--json" -L 10
gh issue list -R openai/codex -S "resume" -L 10
gh issue view ISSUE_NUMBER -R openai/codex
gh search code -R openai/codex "codex exec" --extension yml
gh search code -R openai/codex "config.toml" --extension toml
gh api 'repos/openai/codex/issues?sort=comments&direction=desc&per_page=10' \
| jq -r '.[] | "#\(.number) (\(.comments) comments): \(.title)"'
gh api repos/openai/codex/issues/ISSUE_NUMBER/comments \
| jq -r '.[] | "\(.user.login): \(.body)\n---"'
gh release list -R openai/codex --limit 10
gh release view -R openai/codex
gh issue list -R openai/codex --state open --limit 20
gh issue list -R openai/codex --state closed --limit 20
Additional Resources