Skip to content

OpenAI Codex CLI Integration ​

OpenAI's official command-line AI coding tool that autonomously executes coding tasks, connecting to Calgine Tokens through a custom model provider.

Codex CLI is OpenAI's official command-line AI coding agent. It completes code generation, refactoring, debugging, file editing, and system command execution through natural language in the terminal, with full sandbox isolation and fine-grained permission control.

Prerequisites ​

  1. Sign up at the Calgine Tokens Console.
  2. Go to Workspace → API Tokens, click "Add Token", and copy the API Key once created (starts with sk-).
SettingValue
base_urlhttps://api.calgine.com.cn/v1
OPENAI_API_KEYThe API token you created in the console

Installation & Configuration ​

  1. Check your Node.js version: Node.js 22+ is required:

    bash
    node -v
  2. Install Codex CLI:

    bash
    npm install -g @openai/codex
  3. Set the API Key environment variable (choose one):

    bash
    echo 'export OPENAI_API_KEY="sk-your-token"' >> ~/.zshrc
    source ~/.zshrc
    bash
    echo 'export OPENAI_API_KEY="sk-your-token"' >> ~/.bash_profile
    source ~/.bash_profile
    powershell
    [Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "sk-your-token", "User")
  4. Create the configuration file:

    bash
    mkdir -p ~/.codex

    Write the following into ~/.codex/config.toml:

    toml
    # ~/.codex/config.toml
    model = "gpt-4o"
    model_provider = "calgine"          # Key: set the default provider
    disable_response_storage = true
    
    [model_providers.calgine]
    name = "Calgine Tokens"
    base_url = "https://api.calgine.com.cn/v1"
    env_key = "OPENAI_API_KEY"          # Environment variable name (not the key itself)
    wire_api = "responses"              # Responses API protocol (the only supported value)

    Three critical settings — all required:

    • model_provider = "calgine" — Without this, Codex routes known model names like gpt-4o directly to OpenAI
    • wire_api = "responses" — The only protocol Codex supports, using the Responses API
    • env_key = "OPENAI_API_KEY" — This is the environment variable name, not the key value itself

Verify the Setup ​

bash
codex

On first launch, you need to choose an approval mode that controls Codex's permission policy when performing actions:

ModeValueDescription
SuggesteduntrustedDefault. Codex can only read files; all writes and command executions require your confirmation
Auto Editon-requestAutomatically runs safe sandboxed operations; asks only when escalation is needed
Full AutoneverNo confirmations at all. Codex completes everything autonomously. Best for trusted environments

Once started, enter any task (e.g., "Describe the current directory structure"). A normal result means the integration is working. You can also run /status to view the current model, token usage, and configuration.

Common Commands ​

CommandDescription
codexStart an interactive session
codex "task description"Run a one-off task directly
codex --model gpt-4o "task"Run a task with a specific model
codex --image screenshot.png "fix this bug"Attach an image as context
/modelSwitch models within a session
/approvalsAdjust the approval policy for the current session
/compactCompress conversation history to free up the context window
ScenarioRecommended ModelNotes
Daily coding assistancegpt-4oBalanced speed and capability for most tasks
Complex refactoringo4-miniStrong reasoning for deep-thinking tasks
Quick lightweight tasksgpt-4o-miniFast responses, low cost

Troubleshooting ​

401 Unauthorized — still connecting to api.openai.com

model_provider = "calgine" is not set, so Codex routes known model names (like gpt-4o) to the built-in OpenAI provider. Make sure the top level of config.toml contains model_provider = "calgine". If you previously logged into Codex with ChatGPT, also clear the cached credentials:

bash
rm -f ~/.codex/auth.json

env_key configuration not working

env_key should contain the environment variable name (e.g., "OPENAI_API_KEY"), not the API Key value itself. Make sure the variable is set via export OPENAI_API_KEY="sk-...".

Note

Codex CLI's autonomous mode calls models and executes system commands frequently, consuming far more tokens than normal chat. Keep an eye on your console quota and rate limits — see Rate Limits.

Next Steps ​