> ## Documentation Index
> Fetch the complete documentation index at: https://docs.laozhang.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI Codex CLI Setup Guide

> Configure OpenAI Codex CLI with LaoZhang API for stable access to GPT-5 coding capabilities

## Overview

OpenAI Codex CLI is OpenAI's official command-line programming assistant. By configuring LaoZhang API, you can:

<CardGroup cols={2}>
  <Card title="🚀 Stable Service" icon="server">
    OpenAI-compatible endpoint
  </Card>

  <Card title="💰 Flexible Billing" icon="calculator">
    Pay-per-use or per-request
  </Card>

  <Card title="⚡ GPT-5 Support" icon="sparkles">
    Access latest GPT-5 models
  </Card>

  <Card title="🔧 Simple Setup" icon="wrench">
    Complete configuration in minutes
  </Card>
</CardGroup>

## Prerequisites

### Required Software

<Steps>
  <Step title="Install Git">
    Download from [Git website](https://git-scm.com/downloads)

    <Tip>
      **Windows**: Use default installation settings\
      **macOS**: Usually pre-installed
    </Tip>
  </Step>

  <Step title="Install Node.js">
    Download from [Node.js website](https://nodejs.org/en/download)

    Requires Node.js 22 or higher
  </Step>

  <Step title="Verify Installation">
    Open terminal and verify:

    ```bash theme={null}
    git --version
    node --version
    npm --version
    ```
  </Step>
</Steps>

## Install Codex CLI

### Option 1: npm Install

```bash theme={null}
npm install -g @openai/codex
```

### Option 2: Source Install

```bash theme={null}
git clone https://github.com/openai/codex.git
cd codex
npm install
npm link
```

## Configure LaoZhang API

### 1. Get API Key

1. Visit [LaoZhang API Console](https://api2.laozhang.ai/token)
2. Create a new token
3. Copy the key

### 2. Set Environment Variables

<Tabs>
  <Tab title="macOS/Linux">
    Edit `~/.zshrc` or `~/.bashrc`:

    ```bash theme={null}
    # LaoZhang API Configuration
    export OPENAI_API_KEY="sk-your-laozhang-api-key"
    export OPENAI_BASE_URL="https://api2.laozhang.ai/v1"
    ```

    Apply changes:

    ```bash theme={null}
    source ~/.zshrc
    ```
  </Tab>

  <Tab title="Windows">
    Edit PowerShell profile:

    ```powershell theme={null}
    notepad $PROFILE

    # Add these lines
    $env:OPENAI_API_KEY = "sk-your-laozhang-api-key"
    $env:OPENAI_BASE_URL = "https://api2.laozhang.ai/v1"
    ```

    Restart PowerShell or run:

    ```powershell theme={null}
    . $PROFILE
    ```
  </Tab>
</Tabs>

### 3. Verify Configuration

```bash theme={null}
echo $OPENAI_API_KEY
echo $OPENAI_BASE_URL

# Start Codex
codex
```

## Usage Guide

### Basic Usage

```bash theme={null}
cd ~/my-project
codex

# Or ask directly
codex "Create an Express server"
```

### Interactive Mode

```
> Please optimize this function's performance
> Add unit tests to the project
> Explain this code's logic
```

### Common Commands

| Command               | Function               |
| --------------------- | ---------------------- |
| `codex`               | Start interactive mode |
| `codex "question"`    | Ask directly           |
| `codex --model gpt-5` | Specify model          |
| `codex --help`        | Show help              |

## Model Selection

Available models through LaoZhang API:

| Model            | Model ID       | Features           | Recommended For    |
| ---------------- | -------------- | ------------------ | ------------------ |
| **GPT-5**        | `gpt-5`        | Latest & strongest | Complex tasks      |
| **GPT-4.1**      | `gpt-4.1`      | Fast & capable     | Daily development  |
| **GPT-4.1 Mini** | `gpt-4.1-mini` | Lightweight        | Simple tasks       |
| **o4-mini**      | `o4-mini`      | Enhanced reasoning | Algorithm problems |

### Switch Models

```bash theme={null}
codex --model gpt-5 "Optimize this code"
codex --model o4-mini "Solve this algorithm problem"
```

## Supported Features

* ✅ Code generation and completion
* ✅ Bug fixing and debugging
* ✅ Code refactoring
* ✅ Documentation generation
* ✅ Test case writing
* ✅ Code explanation
* ✅ Multi-language support

### Supported Languages

Python, JavaScript/TypeScript, Java, C++, C#, Go, Rust, Swift, Kotlin, Ruby, PHP, HTML/CSS, SQL, Shell Scripts, and all major programming languages.

## Troubleshooting

<AccordionGroup>
  <Accordion title="Connection Timeout or Failed">
    Check configuration:

    1. Environment variables are set correctly
    2. API key is valid
    3. Network connection is stable

    ```bash theme={null}
    curl https://api2.laozhang.ai/v1/models \
      -H "Authorization: Bearer $OPENAI_API_KEY"
    ```
  </Accordion>

  <Accordion title="Invalid API Key">
    Ensure you're using a LaoZhang API key:

    1. Visit [LaoZhang API Console](https://api2.laozhang.ai/token)
    2. Check if key is valid
    3. Verify account balance
  </Accordion>

  <Accordion title="Model Unavailable">
    Some models may be temporarily unavailable:

    ```bash theme={null}
    codex --model gpt-4.1 "your question"
    ```
  </Accordion>

  <Accordion title="How to Update Codex">
    ```bash theme={null}
    npm update -g @openai/codex
    ```
  </Accordion>
</AccordionGroup>

## Best Practices

### Effective Prompts

```markdown theme={null}
✅ Good prompts:
"Create a Python function that parses JSON and returns data sorted by date"
"Refactor this class using singleton pattern"
"Add error handling and retry mechanism to this API"

❌ Avoid vague:
"Write code"
"Fix bug"
```

### Performance Tips

1. **Be specific**: Clear descriptions reduce round trips
2. **Provide context**: Tell AI about tech stack and constraints
3. **Break down tasks**: Split complex tasks into smaller steps
4. **Batch related tasks**: Describe related tasks together

### Cost Control

* Billed by token usage
* Use `gpt-4.1-mini` for simple tasks
* Check [Pricing page](/en/pricing) for details

## Tool Comparison

| Tool            | Type    | Advantages                 | Best For            |
| --------------- | ------- | -------------------------- | ------------------- |
| **Codex CLI**   | CLI     | GPT-5 support, lightweight | Terminal users      |
| **Claude Code** | CLI     | Strong Agent mode          | Complex refactoring |
| **Cursor**      | IDE     | Friendly UI                | Daily development   |
| **Cline**       | VS Code | Full Agent                 | VS Code users       |

## Related Resources

<CardGroup cols={2}>
  <Card title="OpenAI Codex" icon="bot" href="https://openai.com/codex">
    Learn about Codex technology
  </Card>

  <Card title="API Console" icon="cog" href="https://api2.laozhang.ai/token">
    Manage API keys and usage
  </Card>

  <Card title="Claude Code" icon="terminal" href="/en/scenarios/programming/claude-code">
    Claude programming assistant
  </Card>

  <Card title="Model Comparison" icon="chart-bar" href="/en/api-capabilities/model-info">
    View all coding model comparisons
  </Card>
</CardGroup>

Need help? Visit [LaoZhang API](https://api2.laozhang.ai) for support.
