| name | codex-cli-best-practice |
| description | Guide to mastering Codex CLI through subagents, skills, workflows, MCP servers, and agentic engineering patterns |
| triggers | ["set up codex cli best practices","create a codex subagent","write a codex skill","configure codex mcp server","implement agentic workflow with codex","use codex orchestration pattern","add codex plugin marketplace","optimize codex configuration"] |
codex-cli-best-practice
Skill by ara.so — Codex Skills collection.
A comprehensive guide and reference implementation for Codex CLI best practices, covering the journey from vibe coding to agentic engineering. This skill teaches you how to leverage Codex CLI's advanced features: subagents, skills, MCP servers, workflows, and configuration patterns.
What This Project Provides
codex-cli-best-practice is a reference repository that demonstrates:
- Subagents: Custom TOML-configured agents for parallel orchestration
- Skills: Reusable instruction packages with progressive disclosure
- MCP Integration: Model Context Protocol servers for external tool access
- Workflows: End-to-end patterns (Agent → Skill → Output)
- Configuration: Layered TOML config system with profiles and approval policies
- Hooks: Shell scripts that inject into the agentic loop
- Memories: Cross-session memory pipeline for context retention
Installation
Prerequisites
-
Install Codex CLI (requires Codex Pro subscription):
brew install --cask codex-cli
-
Clone this repository:
git clone https://github.com/shanraisshan/codex-cli-best-practice.git
cd codex-cli-best-practice
-
Initialize Codex in your project:
codex init
Key Concepts & Configuration
1. Subagents (.codex/agents/<name>.toml)
Subagents are custom agents with dedicated role configs. Example weather agent:
[agents.weather-agent]
model = "gpt-5.4"
instructions = """
You are a weather data specialist. When asked:
1. Extract location and units from user request
2. Fetch current weather from Open-Meteo API
3. Return structured data for downstream skills
"""
temperature = 0.7
max_tokens = 2000
Invoke with:
codex
> @weather-agent Get Dubai weather in Celsius
Global agent settings in .codex/config.toml:
[agents]
max_threads = 4
max_depth = 3
job_max_runtime_seconds = 300
2. Skills (.agents/skills/<name>/SKILL.md)
Skills are reusable instruction packages. Required structure:
.agents/skills/weather-svg-creator/
├── SKILL.md # Core instructions with YAML frontmatter
├── scripts/ # Helper scripts
│ └── create_svg.py
├── references/ # Documentation
│ └── svg-spec.md
└── assets/ # Templates, images
└── template.svg
Example SKILL.md:
---
name: weather-svg-creator
description: Creates SVG weather cards from structured weather data
---
# Weather SVG Creator
You create beautiful SVG weather cards. When invoked:
1. Accept weather data (location, temp, condition, humidity)
2. Use scripts/create_svg.py to generate SVG
3. Output to specified path
## Usage Pattern
```python
# scripts/create_svg.py will be called with:
python scripts/create_svg.py \
--location "Dubai" \
--temp "32" \
--condition "Sunny" \
--output "weather.svg"
Invoke skills:
```bash
# Explicit
codex
> Use $weather-svg-creator to make a card for Dubai, 32°C, Sunny
# Implicit (by description match)
> Create an SVG weather card
3. MCP Servers (.codex/config.toml)
Connect external tools via Model Context Protocol:
[mcp_servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "/Users/shan/projects"]
supports_parallel_tool_calls = true
[mcp_servers.github]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-github"]
env = { GITHUB_PERSONAL_ACCESS_TOKEN = "${GITHUB_TOKEN}" }
[mcp_servers.weather]
command = "python"
args = ["-m", "mcp_server_weather"]
working_directory = ".codex/mcp-servers/weather"
Manage MCP servers:
codex mcp list
codex mcp add weather
codex mcp get weather
codex mcp login github
4. Orchestration Workflow (Agent → Skill)
The canonical pattern from this repo:
codex
> Fetch the current weather for Dubai in Celsius and create the SVG weather card output using the repo.
What happens:
@weather-agent fetches data from Open-Meteo
- Returns structured JSON:
{"location": "Dubai", "temp": 32, "condition": "Clear"}
- Codex matches
$weather-svg-creator skill by description
- Skill runs
scripts/create_svg.py with data
- SVG output saved and displayed
5. Configuration Layers
[features]
memories = true
codex_hooks = true
fast_mode = false
[approval_policy]
edits_outside_cwd = "prompt"
deletions = "prompt"
shell_commands = "auto_approve_safe"
[sandbox]
enabled = true
include_patterns = ["src/**", "tests/**"]
exclude_patterns = ["*.pyc", "__pycache__/**"]
[model]
default = "gpt-5.4"
review_model = "gpt-5.4-large-context"
temperature = 0.7
[memories]
max_tokens = 10000
collection_interval_seconds = 300
developer_instructions = """
Follow repo conventions:
- Use type hints in Python
- Run pytest before committing
- Update AGENTS.md for architectural changes
"""
6. Hooks (.codex/hooks.json)
Inject shell scripts into the agentic loop:
{
"hooks": {
"before_edit": {
"script": ".codex/hooks/lint-check.sh",
"description": "Run linter before edits"
},
"after_shell": {
"script": ".codex/hooks/log-command.sh",
"description": "Log all shell commands"
},
"before_commit": {
"script": ".codex/hooks/run-tests.sh",
"description": "Run test suite"
}
}
}
Hook script example:
#!/bin/bash
for file in $CODEX_HOOK_FILES; do
if [[ $file == *.py ]]; then
ruff check "$file" || exit 1
fi
done
exit 0
7. Plugins & Marketplace
Install plugin marketplaces:
codex plugin marketplace add github:openai/codex-plugins
codex plugin marketplace add ~/my-plugins
codex
> /plugins
codex plugin install security-scanner
Create a plugin (.codex-plugin/plugin.json):
{
"name": "my-workflow-plugin",
"version": "1.0.0",
"description": "Custom workflow automation",
"skills": ["skills/planner", "skills/executor"],
"mcp_servers": {
"custom-api": {
"command": "node",
"args": ["mcp-server.js"]
}
}
}
8. Memories (Cross-Session Context)
Enable in config:
[features]
memories = true
[memories]
max_tokens = 10000
collection_interval_seconds = 300
Control via TUI:
codex
> /memories use
> /memories reset
Memories are user-scoped, not project-scoped.
Common Workflows
Create a New Subagent
codex
> Create a subagent called @api-designer that specializes in REST API design.
It should use gpt-5.4-large-context and follow OpenAPI 3.0 standards.
Save to .codex/agents/api-designer.toml
Create a New Skill
codex
> Use the $skill-creator to make a new skill called database-migrator.
It should help write Alembic migrations for SQLAlchemy models.
Include example migration scripts.
Set Up MCP Server for Custom Tool
from mcp.server import Server
from mcp.server.stdio import stdio_server
app = Server("weather-mcp")
@app.tool()
async def get_weather(location: str, units: str = "celsius"):
"""Fetch current weather for a location."""
import httpx
async with httpx.AsyncClient() as client:
response = await client.get(
"https://api.open-meteo.com/v1/forecast",
params={
"latitude": ...,
"longitude": ...,
"current_weather": "true",
"temperature_unit": units
}
)
return response.json()
if __name__ == "__main__":
stdio_server(app)
Add to config:
[mcp_servers.weather]
command = "python"
args = [".codex/mcp-servers/weather/server.py"]
Implement Agent → Skill Workflow
- Create the agent:
[agents.data-fetcher]
model = "gpt-5.4"
instructions = "Fetch data from APIs and return structured JSON"
- Create the skill:
# .agents/skills/data-visualizer/SKILL.md
---
name: data-visualizer
description: Creates charts from JSON data
---
Accept JSON data and create matplotlib/plotly visualizations.
- Invoke:
codex
> @data-fetcher get GitHub stars for shanraisshan/codex-cli-best-practice,
then $data-visualizer create a trend chart
Enable Fast Mode
codex
> /fast on
> /fast status
> /fast off
Or in config:
[model]
service_tier = "fast"
Code Review Workflow
codex
> /review
> /review main..feature-branch
> /review --instructions "Focus on security and performance"
Configure review model:
[model]
review_model = "gpt-5.4-large-context"
Slash Commands Reference
| Command | Description |
|---|
/plan | Create execution plan before acting |
/fast on|off|status | Toggle fast mode (1.5x speed) |
/fork | Create parallel session branch |
/review [ref] | Code review for changes/branch |
/status | Show session info and token usage |
/mcp | Manage MCP servers |
/agent <name> | Switch to specific subagent |
/apps | Manage connected applications |
/model | Change model for session |
/permissions | Manage approval policies |
/skills | Browse and invoke skills |
/plugins | Browse plugin marketplace |
/memories use|reset | Control memory system |
Environment Variables
export OPENAI_API_KEY="sk-..."
export CODEX_HOME="$HOME/.codex"
export GITHUB_TOKEN="ghp_..."
export ANTHROPIC_API_KEY="sk-..."
Troubleshooting
Subagent Not Found
Issue: @my-agent not recognized
Solution:
ls .codex/agents/my-agent.toml
codex config validate
codex
> /exit
codex
Skill Not Triggering
Issue: Skill not invoked implicitly
Solution:
- Use explicit invoke:
$skill-name
- Check skill description is specific enough
- Verify SKILL.md has valid YAML frontmatter
- Check
.agents/skills/ directory structure
MCP Server Connection Failed
Issue: MCP server 'xyz' not responding
Solution:
codex mcp get xyz
cat ~/.codex/logs/mcp-xyz.log
codex mcp remove xyz
codex mcp add xyz
Hooks Not Running
Issue: Hooks defined but not executing
Solution:
[features]
codex_hooks = true
chmod +x .codex/hooks/*.sh
.codex/hooks/my-hook.sh
Memory Not Persisting
Issue: Context lost between sessions
Solution:
codex
> /memories use
[features]
memories = true
Approval Policy Too Restrictive
Issue: Every action requires approval
Solution:
[approval_policy]
shell_commands = "auto_approve_safe"
edits_outside_cwd = "auto_approve"
network_access = "auto_approve"
[profiles.dev]
approval_policy.shell_commands = "auto_approve_safe"
Best Practices
- Start with
/plan: Let Codex create execution plans for complex tasks
- Use AGENTS.md: Document project structure and conventions
- Namespace subagents: Use descriptive names like
@api-designer not @api
- Scope skills narrowly: One skill = one clear responsibility
- Test MCP servers: Use
codex mcp get to verify before adding to workflow
- Layer configs: Use profiles for different environments (dev, prod)
- Enable hooks selectively: Start with
after_shell for logging
- Review approval policy: Balance security and automation
- Use memories sparingly: Enable only for long-running projects
- Document workflows: Create
.md files showing Agent → Skill patterns
Resources