Skip to main content

Overview

OpenAI Codex CLI is OpenAI’s command-line programming assistant, an AI programming tool designed for developers. By configuring APIYI service, you can get:

🚀 Stable Service

Direct connection to APIYI, no proxy needed

💰 Flexible Billing

Support pay-per-token or pay-per-call

⚡ GPT-5 Support

Use latest GPT-5 models

🔧 Simple Setup

Complete setup in minutes

Environment Preparation

Required Software

1

Install Git

Visit Git Official Website to download and install
Windows Users: Recommend default installation to C drive with default settings macOS Users: Usually pre-installed, can skip this step
2

Install Node.js

Visit Node.js Official Website to download and installRequires Node.js 18 or higher
3

Verify Installation

Open terminal to verify successful installation:
git --version
node --version
npm --version

Quick Start

1. Install Codex CLI

Run the following command in terminal to install globally:
sudo npm install -g @openai/codex
Verify installation:
# Check version
codex --version

# View help
codex --help

2. Get API Key

  1. Visit APIYI Console
  2. Create new key or use default key
  3. Supports two types:
    • Pay-per-token: Based on Token usage
    • Pay-per-call: Suitable for Codex series models

3. Configure Codex

Create Configuration File

  • macOS/Linux
  • Windows
Edit configuration file:
nano ~/.codex/config.toml
If directory doesn’t exist, create it first:
mkdir -p ~/.codex

Configuration Content

Add the following to config.toml:
model_provider = "codex"
model = "gpt-5-codex-high"  # or gpt-5-codex-medium, gpt-5-codex-low
model_reasoning_effort = "high"
disable_response_storage = true

[model_providers.codex]
name = "codex"
base_url = "https://api.apiyi.com/v1"
wire_api = "chat"
env_key = "K_CODEX"  # Don't fill key here, set in environment variables
Model Selection Notes:
  • gpt-5-codex-high: Strongest programming capability, comparable to GPT-5
  • gpt-5-codex-medium: Medium performance, moderate pricing
  • gpt-5-codex-low: Lightweight version, lowest cost

4. Set Environment Variables

Set your APIYI key as environment variable:
  • macOS/Linux
  • Windows
macOS (zsh):
echo 'export K_CODEX="sk-your-APIYI-key"' >> ~/.zshrc
source ~/.zshrc
Linux (bash):
echo 'export K_CODEX="sk-your-APIYI-key"' >> ~/.bashrc
source ~/.bashrc
After configuration, recommend restarting terminal to ensure environment variables take effect.

5. Start Using

Enter your project directory and launch Codex:
# Enter project directory
cd /path/to/your/project

# Launch Codex
codex

Usage Tips

Project Initialization

First time using in project, let Codex familiarize with project structure:
codex /init
This automatically creates AGENTS.md file to record project information.

Chinese Support

By default, Codex uses English for conversation. For Chinese communication, add to AGENTS.md file:
Please always communicate with users in Chinese for this project.

Custom Prompts

Edit ~/.codex/instructions.md file to customize system prompts and define AI assistant behavior:
# Custom Instructions

- Code comments in Chinese
- Follow project code standards
- Provide detailed explanations

Codex Series Model Comparison

ModelBilling ModeFeaturesUse Cases
gpt-5-codex-highToken/CallStrongest programmingComplex algorithms, architecture design
gpt-5-codex-mediumToken/CallBalanced performanceDaily programming tasks
gpt-5-codex-lowToken/CallLightweight and efficientSimple code generation
Billing Recommendations:
  • Large Context Scenarios: Pay-per-call more cost-effective
  • Short Conversation Scenarios: Pay-per-token more economical

Features

Supported Programming Tasks

  • ✅ Code generation and completion
  • ✅ Bug fixing and debugging
  • ✅ Code refactoring and optimization
  • ✅ Unit test writing
  • ✅ Documentation generation
  • ✅ Code review
  • ✅ Algorithm implementation

Supported Languages

Codex supports 100+ programming languages, including:
  • Mainstream Languages: Python, JavaScript, TypeScript, Java, C++, C#, Go, Rust
  • Web Technologies: HTML, CSS, React, Vue, Angular
  • Databases: SQL, MongoDB, Redis
  • Scripting: Bash, PowerShell, Perl
  • Others: Swift, Kotlin, Ruby, PHP, R, MATLAB

Troubleshooting

Ensure Codex CLI is correctly installed:
npm install -g @openai/codex
If still having issues, check if npm global install path is in PATH.
  1. Confirm using APIYI key, not OpenAI official key
  2. Check environment variable is correctly set:
echo $K_CODEX  # macOS/Linux
echo %K_CODEX%  # Windows
Check base_url in config.toml is correctly set to:
base_url = "https://api.apiyi.com/v1"
Edit model field in ~/.codex/config.toml:
model = "gpt-5-codex-medium"  # Change to desired model
Uninstall Codex CLI:
npm uninstall -g @openai/codex
Disable APIYI Configuration: Delete environment variables and configuration files:
# macOS/Linux
nano ~/.zshrc  # Delete K_CODEX line
rm ~/.codex/config.toml

# Windows
# Delete K_CODEX from system environment variables

VS Code / Cursor Integration

Besides command-line use, Codex also provides IDE plugins:
Currently, Codex official plugins in VS Code and Cursor don’t support APIYI configuration, requiring official account. To use APIYI in IDE, recommend using other compatible plugins.

Alternatives

  • Cursor: Built-in AI features, supports custom API endpoints
  • Continue: Open-source AI programming assistant, supports APIYI configuration
  • Cline: VS Code plugin supporting custom APIs

Best Practices

Project Organization

  1. Create AGENTS.md: Record project standards and requirements
  2. Add .gitignore: Exclude Codex-generated temporary files
  3. Write Clear Comments: Help AI understand code intent

Prompt Tips

# Good Prompt Examples
"Refactor this function, extract duplicate code and add error handling"
"Write unit tests for this API endpoint, covering normal and exception cases"
"Optimize this code's time complexity and explain optimization approach"

# Avoid Vague Instructions
"Improve code"  # Too broad
"Fix bug"  # No specific description

Performance Optimization

  1. Choose Models Wisely:
    • Simple tasks use gpt-5-codex-low
    • Complex tasks use gpt-5-codex-high
  2. Context Management:
    • Clean unnecessary conversation history timely
    • Focus on current task
  3. Batch Processing:
    • Describe related tasks at once
    • Reduce round-trip interactions

Summary

OpenAI Codex CLI paired with APIYI service provides developers with a powerful and economical AI programming assistant. Through flexible billing modes and stable service, you can fully leverage GPT-5 Codex series programming capabilities to improve development efficiency.
Comparison Tip:
  • Command-line Experience: Codex CLI suitable for developers familiar with terminal
  • IDE Integration: For better IDE experience, consider Claude Code or other plugins supporting APIYI