Integrate OpenAI Codex

Connect OpenAI Codex CLI to TokenCode to use any large language model for AI coding.

Overview

OpenAI Codex CLI is OpenAI's official command-line AI coding tool, which natively uses the OpenAI Chat Completions API protocol. With TokenCode, you can call Claude, Gemini, and other models in Codex.

Configuration

Method 1: Environment Variables

Set the following environment variables in your terminal:

bash
export OPENAI_API_KEY="sk-your-api-key"
export OPENAI_BASE_URL="https://tokencode.dev/v1"

Then run Codex:

bash
codex

Method 2: Configuration File

Edit the Codex configuration file ~/.codex/config.json:

json
{
  "provider": "openai",
  "apiKey": "sk-your-api-key",
  "baseURL": "https://tokencode.dev/v1",
  "model": "gpt-5.5"
}

Using Different Models

Codex supports switching models via parameters or configuration:

bash
# Use GPT-4o
codex --model gpt-5.5

# Use Claude Sonnet (automatic protocol conversion)
codex --model claude-sonnet-4-6

# Use Gemini 2.5 Pro (automatic protocol conversion)
codex --model gemini-2.5-pro

# Use o3 reasoning model
codex --model o3

Automatic Protocol Conversion

Codex sends requests using the OpenAI protocol. When you call non-OpenAI models (such as Claude or Gemini) through TokenCode, the gateway automatically:

  1. Converts OpenAI-format requests to the target model's native format
  2. Converts the target model's native responses to OpenAI format
  3. Automatically adapts SSE event format for streaming responses

Verify Connection

bash
codex --model gpt-5.5 "Hello, verifying connection"

If you receive a normal response, the connection is configured successfully.

Use Case Recommended Model Notes
General coding claude-sonnet-4-6 Excellent code understanding and generation
Fast completion gpt-5.5-mini Fast response, low cost
Deep reasoning o3 Suitable for complex logic and architecture design
Best value deepseek-chat Good performance, low price

Troubleshooting

OPENAI_BASE_URL Not Taking Effect

Make sure the URL includes the /v1 suffix: https://tokencode.dev/v1. Codex appends endpoint paths (e.g., /chat/completions) to the base URL.

Invalid Model Name

Codex may validate model names. If you encounter an unrecognized model name error, try using a standard model name.

Interrupted Responses

If responses are interrupted during long code generation, it may be due to max_tokens limits or rate limits. Try reducing request complexity.