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

# Use OpenAI Codex CLI with A2Agent for Cheaper Models

> Redirect OpenAI’s Codex CLI to A2Agent by setting two environment variables. Access DeepSeek, Kimi, GLM, and more through a single API endpoint.

OpenAI’s Codex CLI respects the standard `OPENAI_BASE_URL` environment variable, which lets you route every request it makes to A2Agent’s OpenAI-compatible endpoint. The result is the full Codex CLI workflow backed by models like DeepSeek, Kimi, GLM, and MiniMax, with model availability, usage limits, and billing managed through your A2Agent account.

## Prerequisites

* Codex CLI installed — follow the [official installation guide](https://github.com/openai/codex) if needed
* An A2Agent API key — [sign up at a2agent.me/register](https://a2agent.me/register) to get one

## Setup

<Steps>
  <Step title="Set the required environment variables">
    Before running Codex, export your A2Agent key and base URL in your current shell session:

    ```bash theme={null}
    export OPENAI_API_KEY="YOUR_A2AGENT_KEY"
    export OPENAI_BASE_URL="https://api.a2agent.me/v1"
    ```

    <Note>
      `OPENAI_API_KEY` here is your **A2Agent** API key, not an OpenAI key. A2Agent accepts requests in the OpenAI API format and routes them to the model you select.
    </Note>
  </Step>

  <Step title="Run Codex and verify the connection">
    With those variables set, run Codex as you normally would — it will automatically route all requests through A2Agent:

    ```bash theme={null}
    codex "add error handling to this function"
    ```

    If Codex returns a response, your setup is working correctly.
  </Step>

  <Step title="(Optional) Specify a model">
    Pass the `--model` flag to select a particular A2Agent model for the task at hand:

    ```bash theme={null}
    codex --model deepseek-v4-pro "refactor this function to be more readable"
    ```
  </Step>
</Steps>

### Recommended models for coding tasks

| Model | Best for |
| :- | :- |
| `qwen3.8-max-0902 ` | Large-codebase analysis, long-context reasoning, and complex technical planning |
| `qwen3.8-max` | Large-codebase analysis and long-context reasoning |
| `deepseek-v4-pro` | Complex reasoning, refactoring, and architecture |
| `deepseek-v4-flash` | Fast iteration, debugging, and routine coding |
| `qwen3.7-max` | Long-context planning and technical analysis |
| `minimax-m3` | Agent workflows and tool-calling tasks |
| `glm-5.2` | General coding and long-context project work |

## Persist the configuration

To avoid re-exporting the variables every session, add them to your shell profile:

```bash ~/.bashrc or ~/.zshrc theme={null}
export OPENAI_API_KEY="YOUR_A2AGENT_KEY"
export OPENAI_BASE_URL="https://api.a2agent.me/v1"
```

After saving, reload your profile:

```bash theme={null}
source ~/.bashrc   # or source ~/.zshrc
```

From that point on, every new terminal session will automatically use A2Agent when you run Codex.

## Troubleshooting

<AccordionGroup>
  <Accordion title="401 Unauthorized">
    Codex is reaching A2Agent but your credentials are being rejected. Double-check that `OPENAI_API_KEY` is set to your full A2Agent key with no extra spaces, quotes, or newline characters.
  </Accordion>

  <Accordion title="Connection refused or could not resolve host">
    Confirm that `OPENAI_BASE_URL` is set to exactly `https://api.a2agent.me/v1` — including the `/v1` path and no trailing slash. Run `echo $OPENAI_BASE_URL` to verify the value in your current shell.
  </Accordion>

  <Accordion title="Model not found">
    The model name you passed with `--model` must match an A2Agent model ID exactly — for example, `deepseek-v4-pro`, `kimi-k2.7-code`, `glm-5.1`, or `minimax-m2.5`. Check the [A2Agent model list](https://a2agent.me) for all available IDs.
  </Accordion>

  <Accordion title="Environment variables not recognised after editing shell profile">
    Run `source ~/.bashrc` (or `source ~/.zshrc`) in your current terminal after editing the profile, or open a new terminal session for the changes to take effect.
  </Accordion>
</AccordionGroup>
