Integrate OpenAI Codex

Connect OpenAI Codex CLI to Puyun AI 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 Puyun AI, 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://ai.tracup.com/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://ai.tracup.com/v1",
  "model": "gpt-4o"
}

Using Different Models

Codex supports switching models via parameters or configuration:

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

# 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 Puyun AI, 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-4o "Hello, verifying connection"

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

Use CaseRecommended ModelNotes
General codingclaude-sonnet-4-6Excellent code understanding and generation
Fast completiongpt-4o-miniFast response, low cost
Deep reasoningo3Suitable for complex logic and architecture design
Best valuedeepseek-chatGood performance, low price

Troubleshooting

OPENAI_BASE_URL Not Taking Effect

Make sure the URL includes the /v1 suffix: https://ai.tracup.com/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 service tier rate limits. Try using the Enterprise tier or reducing request complexity.