AI Personal Learning
and practical guidance
TRAE

OpenAI Codex CLI: Terminal Command Line AI Coding Assistant Released by OpenAI

General Introduction

OpenAI Codex CLI is an open source terminal coding tool, developed by OpenAI and designed for developers accustomed to terminals. It generates code, edits files, and executes commands through natural language commands, and integrates Git version control.Codex CLI uses OpenAI's latest models (such as o3 and o4-mini), supports multilingual programming, and provides a secure sandbox environment that restricts network access and directory manipulation to ensure security. The project is fully open source, encourages community contributions, and is suitable for rapid development, automating tasks, and learning to program.

OpenAI Codex CLI: Terminal Command Line AI Coding Assistant released by OpenAI-1


 

Function List

  • Code Generation: Generate multi-language code such as Python, JavaScript, etc. based on natural language.
  • File operations: create and edit files, support Git commits and versioning.
  • Command Execution: Run code or commands in the sandbox and display the results in real time.
  • Multi-modal input: support screenshot and chart input to generate corresponding code.
  • Security Sandbox: Disable the network by default and only operate the current directory for security.
  • Automated Approval: Provides three modes: manual, automatic editing, and fully automated.
  • Open Source Development: Allows users to view code, submit PR contribution features.
  • Project documentation: support for global and project-level Markdown commands, custom behavior.

 

Using Help

Installation process

To use the Codex CLI, complete the following installation steps:

  1. system requirements::
    • OS: macOS 12+, Ubuntu 20.04+/Debian 10+, or Windows 11 (via WSL2).
    • Node.js: version >= 22 (LTS version recommended).
    • Git: version >= 2.23 (recommended, for version control).
    • RAM: At least 4GB, 8GB recommended.
    • OpenAI API key: Get it from the OpenAI website.
  2. Installation steps::
    • Install globally via npm (recommended):
      npm install -g @openai/codex
      
    • or built from source code:
      git clone https://github.com/openai/codex.git
      cd codex/codex-cli
      npm install
      npm run build
      
    • Set the API key:
      export OPENAI_API_KEY="your-api-key-here"
      

      For a permanent setting, add it to the shell configuration file (e.g. ~/.zshrc maybe ~/.bashrc):

      echo 'export OPENAI_API_KEY="your-api-key-here"' >> ~/.zshrc
      source ~/.zshrc
      
    • Verify the installation:
      codex --help
      

      If the help message is displayed, the installation was successful.

  3. Linux Docker Configuration::
    • Docker is recommended for enhanced security:
      ./run_in_container.sh
      
    • Make sure Docker is installed, the script will automatically mount the current directory and configure the firewall to allow only OpenAI API access.
  4. caveat::
    • Do not use sudo npm install -gThe npm permissions should be fixed:
      sudo chown -R $(whoami) ~/.npm
      
    • Ensure that the API key is valid to avoid 401 maybe 429 Error.

Usage

Codex CLI provides an efficient coding experience by combining natural language and sandbox environment through terminal command operations. Below is the flow of how to use the main features:

1. Code generation

  • workflow::
    • Interaction Patterns:
      codex
      

      Enter a prompt such as "Create a Python function to compute a Fibonacci series.

    • Single-command mode:
      codex "生成 JavaScript 过滤数组函数"
      
    • Codex CLI generates code and displays a preview.
    • importation y Accept and save, enter n Denial.
  • Advanced Options::
    • Specify the model:
      codex --model o4-mini "生成 Go HTTP 服务器"
      
    • Non-interactive mode:
      codex -q "生成 SQL 查询" > output.sql
      
  • Featured Functions::
    • Support for Python, JavaScript, TypeScript, Go, SQL and more.
    • Can handle complex tasks such as "Generate a Flask application with user authentication".

2. File manipulation and Git integration

  • workflow::
    • Edit file:
      codex "在 src/app.js 中添加登录功能"
      
    • The Codex CLI displays the code differences, confirms them and saves them.
    • Commit to Git:
      codex "提交更改,消息为 '添加登录功能'"
      
  • Featured Functions::
    • Automatically handle Git commands (add,commit).
    • Supports batch operations such as:
      codex "重命名所有 *.jpeg 为 *.jpg 并更新引用"
      
    • Three approval modes are provided:
      • suggest: Default, all changes need to be confirmed manually.
      • auto-edit: Automatically applies file changes, commands need to be confirmed.
      • full-auto: Fully automatic and to be used with caution:
        codex --approval-mode full-auto "更新 README"
        

3. Running the code

  • workflow::
    • Execute the code:
      codex "运行 src/main.py"
      
    • The Codex CLI runs in the sandbox and displays the results.
    • If a dependency is missing, prompt to install it:
      codex "安装 numpy 并运行 script.py"
      
  • Featured Functions::
    • macOS uses the Apple Seatbelt sandbox and Linux uses Docker.
    • Networks are disabled by default to prevent accidental access.
    • Supports debugging output to show error details.

4. Multimodal inputs

  • workflow::
    • Enter the image path:
      codex "根据 ui.png 实现界面代码"
      
    • Codex CLI analyzes images and generates HTML/CSS or front-end code.
  • Featured Functions::
    • Support for UI design to code conversion.
    • Diagrams or flowcharts can be processed to generate logic code.

5. Configuration and customization

  • configuration file::
    • Global Configuration:~/.codex/config.yaml::
      model: o4-mini
      fullAutoErrorMode: ask-user
      
    • Instruction file:~/.codex/instructions.md::
      - 使用 Python 3.9
      - 禁止自动提交 Git
      
    • Project-level configuration: the codex.md::
      - 优先使用 TypeScript
      - 运行测试前执行 npm run lint
      
  • Disable project documentation::
    codex --no-project-doc
  • Approval Mode Switching::
    codex --approval-mode auto-edit
    

6. CI/CD integration

  • workflow::
    • Run it in GitHub Actions:
      - name: 更新文档
      run: |
      npm install -g @openai/codex
      export OPENAI_API_KEY="${{ secrets.OPENAI_KEY }}"
      codex -a auto-edit -q "更新 CHANGELOG"
      
    • Set the silent mode:
      export CODEX_QUIET_MODE=1
      
  • Featured Functions::
    • Supports non-interactive mode, suitable for automated processes.
    • Outputs JSON format for easy scripting:
      codex -q --json "生成配置"
      

caveat

  • Make sure the directory is tracked by Git.full-auto mode will warn you in non-Git directories.
  • Check the API key quota, frequent calls may trigger limits.
  • The project is experimental and may have unstable features, so we recommend following GitHub for updates.

 

application scenario

  1. Rapid Prototyping
    Developers can quickly generate code frameworks in natural language. For example, type "Create an Express application with a database" and the Codex CLI generates routes, models, and configuration files suitable for validating ideas.
  2. Automated tasks
    Codex CLI simplifies repetitive tasks such as "update copyright notices in all files and submit". It automatically edits files, runs scripts, and pushes to Git for batch operations.
  3. Programming Learning
    Beginners can type "Quick Sort in Python", Codex CLI generates code and explains the logic, suitable for self-study or classroom teaching.
  4. Open Source Collaboration
    Developers can participate in Codex CLI development via GitHub, submit bug fixes or new features, and the transparent codebase facilitates collaboration.
  5. CI/CD pipeline
    In CI environments, the Codex CLI can automatically update documentation, generate test cases, or fix lint bugs for more efficient development.

 

QA

  1. How does the Codex CLI differ from the 2021 Codex model?
    Codex CLI is a more powerful and open source endpoint tool based on the latest OpenAI models (o3, o4-mini), independent of the Codex models that will be discontinued in 2021.
  2. How do I ensure safe operation?
    The Codex CLI runs in a sandbox by default, uses Apple Seatbelt for macOS and Docker for Linux, disables networking and restricts directory access to avoid system risk.
  3. What programming languages are supported?
    Python, JavaScript, TypeScript, Go, SQL, etc. are supported, as determined by the OpenAI model used, and the language can be specified via a command.
  4. Is the Codex CLI free?
    The Codex CLI itself is free, but requires an OpenAI API key and may incur costs for API calls, see OpenAI Pricing for details.
  5. How do I stop the Codex CLI from modifying files?
    utilization suggest mode, all changes need to be confirmed manually:

    codex --approval-mode suggest
    
May not be reproduced without permission:Chief AI Sharing Circle " OpenAI Codex CLI: Terminal Command Line AI Coding Assistant Released by OpenAI
en_USEnglish