Codex CLI Implementation

OpenAI's Codex CLI implementation patterns including directory-walk concatenation, model tier assignment, and profile configuration

Token Optimization Guide / Codex CLI Implementation

Codex CLI Token Optimization

Layer 1 β€” Platform Mechanics

AGENTS.md is native; use the directory-walk concatenation deliberately β€” universal rules at the project root, narrower rules in nested AGENTS.md files closer to the code they govern (Codex concatenates root-down, so nested files add rather than replace). Give read-only/mapping subagents a cheaper model tier (OpenAI’s own example pairs a code_mapper subagent with a mini-tier model and a read-only sandbox) and reserve the full model for the agent that edits code. Set model_reasoning_effort to low/minimal by default β€” xhigh costs 3–5x more tokens and should be reserved for genuinely hard problems.

Key Strategies:

  • Directory-Walk Concatenation: Leverage hierarchical AGENTS.md structure
    • Universal rules at project root
    • Narrower rules in nested directories
    • Codex concatenates root-down (adds, doesn’t replace)
  • Tiered Subagent Models: Assign model tiers by subagent role
    • Read-only/mapping subagents: Mini-tier models
    • Code editing agents: Full model tier
    • Example: code_mapper with mini-tier + read-only sandbox
  • Reasoning Effort Control: Default to low/minimal
    • xhigh costs 3–5x more tokens
    • Reserve for genuinely hard problems only

Layer 2 β€” Agent Operation

Tune model_auto_compact_token_limit rather than accepting the default. Use codex exec for scripted/CI runs to skip TUI overhead. Keep a lean --profile (cheap model, low effort, read-only sandbox) separate from your interactive daily-driver profile.

Operational Best Practices:

  • Auto-Compact Tuning: Adjust model_auto_compact_token_limit from default
  • CI/Scripted Runs: Use codex exec to skip TUI overhead
  • Profile Separation: Maintain distinct profiles
    • Lean profile: Cheap model, low effort, read-only sandbox
    • Interactive profile: Full model for daily development
  • Batch Operations: Group related operations to reduce session overhead

Layer 3 β€” Codebase Architecture

Same AGENTS.md Conventions section, whether at repo root or nested per-package.

Architecture Rules:

  • Shared Conventions: Maintain consistent AGENTS.md Conventions section
  • Nested Structure: Use nested AGENTS.md for package-specific rules
  • Concatenation Order: Rules concatenate root-down, earlier rules win

Codex CLI-Specific Features

Plan Mode Configuration

Codex CLI uses plan_mode_reasoning_effort config key to control planning behavior:

  • Set appropriate reasoning effort for planning phase
  • Separate planning from execution to control costs
  • Use lower effort for initial planning, higher for implementation

Skills Configuration

Codex CLI uses [[skills.config]] for per-skill enable/disable:

  • Fine-grained control over which skills load
  • Reduce token overhead by disabling unused skills
  • Profile-specific skill configurations

Subagent Definitions

Subagent definitions in ~/.codex/agents/*.toml or .codex/agents/*.toml:

  • Each subagent has independent model/reasoning effort/sandbox
  • Support for complex multi-agent workflows
  • Isolated contexts for parallel execution

Profile System

Codex CLI’s profile system allows environment-specific configurations:

  • Development profile: Full capabilities
  • CI profile: Lean, automated, cost-optimized
  • Review profile: Read-only, cheap model tier

Interactive Tools

Practical tools to help you think systematically, build better AI agents, and master prompt engineering.

Software Engineering Playbooks

Practical, End-to-End implementation guides for building Production-ready Software. Each playbook includes working code, architecture diagrams, and step-by-step instructions.

πŸ€–

Research Agent with Gateway

Build a production-ready AI research agent using Agent Gateway for unified traffic management, authentication, and observability across LLM providers.

Agent Gateway A2A Protocol Multi-Agent
πŸ”—

RAG Pipeline with Vector Database

Implement a complete Retrieval-Augmented Generation pipeline with vector embeddings, semantic search, and context injection for accurate AI responses.

RAG Vector DB Embeddings
πŸ”„

Multi-Agent Orchestration

Create a coordinated multi-agent system with specialized agents, task distribution, and result synthesis for complex problem-solving.

Orchestration Task Distribution Synthesis
1 / 2

Future References

Explore these resources for deeper learning on AI agent development, spec-driven development, and prompt engineering tools.

Spec-Driven Development

Comprehensive guide on Spec-Driven Development practices and methodologies.

Awesome Copilot

Curated list of GitHub Copilot resources, extensions, and best practices.

Promptfoo

Tool for testing, evaluating, and improving LLM prompts and applications.

Prompts.chat

Collection of prompt engineering resources and templates.

Agent Skills

Agent skills resources and documentation for building AI agent skills.

Awesome Skills

Curated list of awesome skill repositories and collections.

Agent Skills Topic

GitHub topic for discovering agent-related skills and repositories.

AI Agent Topic

Trendshift topic for discovering AI agents.

AI Skills Topic

Trendshift topic for discovering AI skills.

Agent Governance Toolkit

Agent governance toolkit.

Pattern Sources

Our patterns are curated from industry-leading sources with proper attribution and licensing compliance.

Refactoring.Guru

Classic GoF design patterns, code smells catalog, and refactoring techniques (https://refactoring.guru).

Enterprise Integration Patterns

65 messaging patterns for integrating enterprise applications by Gregor Hohpe and Bobby Woolf (CC BY 4.0).

Microservices.io

Comprehensive patterns for microservice architectures by Chris Richardson.

Agent Catalog Patterns

Patterns for agentic systems from agentpatternscatalog.org (CC BY 4.0).

OWASP Foundation

Security patterns from OWASP Top 10 for Web Applications, LLM Applications, and Agentic Applications (CC BY-SA 4.0).

Industry Research

ML/AI patterns from Microsoft, Google, Anthropic, and academic research.

AI Agent Patterns

Spec-driven development patterns from Claude, Gemini, OpenAI, and GitHub Copilot on github/spec-kit and OpenSpec.

Data Engineering Leaders

Data platform patterns from Martin Fowler (Data Mesh), Kimball Group (Dimensional Modeling), and cloud providers.

MLOps Best Practices

Data science patterns from MLflow, Great Expectations, and MLOps practitioners.

Streaming & Analytics

Real-time patterns from Confluent/Kafka, Apache projects, and serverless analytics platforms.

Academic Papers

Rigorous ML patterns from peer-reviewed research including data leakage prevention and active learning.

5-Day AI Agents Course

Intensive Vibe Coding Course With Google by Brenda Flynn et al. (2026) on Kaggle.

The Agent Loop

Foundational Agent Definition (Perceive + Act):
Russell, S. J., & Norvig, P. (1995). Artificial Intelligence: A Modern Approach. Prentice Hall. (Current edition: 4th Ed., Pearson, 2020)

Modern Iterative LLM Agent Loop:
Yao, S., Zhao, J., Yu, D., et al. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv:2210.03629.

Historical Context:
Incorporating AIMA's perceive/act model, Classical robotics' Sense-Plan-Act loop (Brooks, 1986), and ReAct's Thought→Action→Observation cycle.