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-).
Setting Value
base_url https://api.calgine.com.cn/v1
OPENAI_API_KEY The API token you created in the console

Installation & Configuration

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

    node -v
  2. Install Codex CLI:

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

        echo 'export OPENAI_API_KEY="sk-your-token"' >> ~/.zshrc
        source ~/.zshrc
        
  4. Create the configuration file:

    mkdir -p ~/.codex

    Write the following into ~/.codex/config.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

codex

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

Mode Value Description
Suggested untrusted Default. Codex can only read files; all writes and command executions require your confirmation
Auto Edit on-request Automatically runs safe sandboxed operations; asks only when escalation is needed
Full Auto never No 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

Command Description
codex Start 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
/model Switch models within a session
/approvals Adjust the approval policy for the current session
/compact Compress conversation history to free up the context window
Scenario Recommended Model Notes
Daily coding assistance gpt-4o Balanced speed and capability for most tasks
Complex refactoring o4-mini Strong reasoning for deep-thinking tasks
Quick lightweight tasks gpt-4o-mini Fast 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:

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-...".

Next Steps