OpenAI Codex CLI
OpenAI Codex CLI — coding agent chạy trong terminal, cho phép thực thi code, chỉnh sửa file và tương tác với hệ thống.
Cài đặt
npm install -g @openai/codexCấu hình
Cần API key trước khi cấu hình. Chưa có thì tạo ở trang API Keys, hoặc xem hướng dẫn chi tiết cách lấy và dùng key.
Set API key vào biến môi trường OPENAI_API_KEY:
Linux / macOS:
export OPENAI_API_KEY="sk-billing-xxx"Windows (PowerShell):
setx OPENAI_API_KEY "sk-billing-xxx"File config nằm ở đâu?
Mở (hoặc tạo mới nếu chưa có) file config.toml trong thư mục .codex ở thư mục home:
🍎 macOS:
~/.codex/config.toml → /Users/<tên-bạn>/.codex/config.toml🐧 Linux:
~/.codex/config.toml → /home/<tên-bạn>/.codex/config.toml🪟 Windows:
%USERPROFILE%\.codex\config.toml → C:\Users\<tên-bạn>\.codex\config.tomlTạo nhanh nếu chưa có — macOS / Linux:
mkdir -p ~/.codex && nano ~/.codex/config.tomlWindows (PowerShell):
mkdir "$HOME\.codex" -Force; notepad "$HOME\.codex\config.toml"Dán nội dung sau vào file — định nghĩa custom provider nexai trỏ về Responses API của nexai:
model = "gpt-5.1-codex-mini"
model_provider = "nexai"
model_reasoning_effort = "medium"
[model_providers.nexai]
name = "nexai"
base_url = "https://getnexai.net/api/v1"
env_key = "OPENAI_API_KEY"
wire_api = "responses"Nhầm lẫn số 1: env_key là TÊN biến, KHÔNG phải giá trị keyGiữ nguyên đúng
env_key = "OPENAI_API_KEY" — Codex tự đọc key từ biến môi trường tên OPENAI_API_KEY (đã set ở bước trên). Đừng dán giá trị key (sk-billing-xxx) vào field env_key.wire_api = "responses" dùng endpoint /v1/responses của nexai (tương thích Codex CLI). KHÔNG dùng OPENAI_BASE_URL env var — Codex chỉ đọc base_url từ config khi dùng custom provider.Bắt buộc: khởi động lại sạch sau khi cấu hìnhThoát hoàn toàn mọi app đang mở — VS Code, Codex app, terminal, PowerShell / CMD — rồi mở lại. Biến môi trường chỉ nạp khi tiến trình khởi động mới; mở tab terminal mới hay reload cửa sổ là KHÔNG đủ.
Chạy với model cụ thể
# Dùng GPT-4o (cân bằng)
codex --model gpt-4o "Refactor src/main.py"
# Dùng model code (nhanh, tiết kiệm)
codex --model gpt-5.1-codex-mini "Add docstrings to all functions"
# Full auto mode (không hỏi xác nhận)
codex --approval-mode full-auto "Fix lint errors in src/"Dùng
--approval-mode full-auto để Codex tự động thực hiện không cần xác nhận — tiết kiệm thời gian khi làm tác vụ lặp lại.