Skip to main content
To run Codex CLI on LaoZhang API, add a custom provider in config.toml with the base URL https://api.laozhang.ai/v1 and the Responses wire API, and keep the key in an environment variable.

Install Codex CLI

Install it globally with npm (Node.js required):

Configure LaoZhang API

1

Set the key as an environment variable

Add the key to your shell profile so every new terminal can read it:
Use ~/.bashrc for bash. In Windows PowerShell, run setx LAOZHANG_API_KEY "YOUR_LAOZHANG_API_KEY" and open a new terminal.
2

Edit config.toml

Open or create ~/.codex/config.toml and add:
  • model_provider must match the name in [model_providers.laozhang].
  • Don’t reuse the name of a Codex built-in provider, such as openai or ollama. The full list is in the Codex configuration docs.
  • env_key takes the name of the environment variable, not the key itself.
3

Send a test request

From any directory, run:
If it prints connected, the setup works. Run codex in a project directory to start an interactive session.

Use it in the Codex app and IDE extensions

The Codex desktop app, IDE extensions such as the one for VS Code, and the CLI all read the same ~/.codex/config.toml. An app or extension launched from the Dock or Start menu may not see variables set in your terminal. In that case, sign in with your key instead, which stores it in Codex’s own credentials:
  • This uses Codex’s built-in OpenAI provider, so you don’t need the [model_providers.laozhang] table or model_provider from above. Pick one setup or the other.
  • The CLI, IDE extensions, and the desktop app share these credentials. In an IDE extension you can also choose Use API Key and paste the key there.
  • Run codex login status to see which sign-in method is active.

Choose a model

Codex CLI calls models through the Responses API, so GPT-6 models can call tools without any reasoning-effort change. To switch for one session, add -m, for example codex -m gpt-6-astra. For model IDs Codex doesn’t recognize, such as gpt-5.4-mini, it warns Model metadata ... not found. The model still works, but Codex falls back to default values for details like context length. Prices are in the model catalog.

Cut costs with the Codex Discount group

For personal projects and everyday coding, you can give Codex its own key from the Codex Discount group. The six models below are billed at 30% of the catalog price, for input, output and cache reads alike.
1

Create a Codex Discount key

In Tokens, create a key in the Codex Discount group with the Usage-first billing mode.
2

Store it in its own environment variable

If other tools still use a default key, leave LAOZHANG_API_KEY alone and add a second variable:
3

Point config.toml at that variable

Change env_key to the new variable and set model to one of the six models:
If you use the login setup for the Codex app or IDE extensions, run printenv LAOZHANG_CODEX_KEY | codex login --with-api-key instead.
Before you switch, check that these differences are acceptable:
  • Billing follows the usage the route reports. Reasoning tokens count as output, so the same task usually reports more output tokens than the official API, and cache hits can be lower. The real cost is therefore not simply 30%. Check each request’s usage and charge in call logs.
  • max_output_tokens is not enforced, so you can’t use it to cap a single response.
  • Both Responses and Chat Completions work. Codex uses Responses, so it works as is. Since 2026-11-18, Chat Completions requests and Responses requests without instructions no longer count extra input tokens; a plain “hi” counts as 1 input token.
  • Only the six models above are included. gpt-6-luna, gpt-5.6-luna, gpt-5.4-mini and other models need a default key.

FAQ

I get a 503 “Current group codex_saver has no available channels”

The request used a Codex Discount key with a model outside the group, such as gpt-6-luna. Set model to one of the six models in the table above, or switch to a default key.

I get a 401 or a missing environment variable error

Codex reads the key only from the variable named in env_key. In the same terminal you run Codex from, run test -n "$LAOZHANG_API_KEY" && echo set to confirm the variable is set. After editing your shell profile, open a new terminal. In the desktop app or an IDE extension, use the sign-in setup instead.

I get a 404 or “stream disconnected”

Make sure base_url is https://api.laozhang.ai/v1, without a trailing / or /responses. If it still fails, check call logs to see whether the request arrived, and see Invalid API key or 404.

One task used a lot of tokens. Is that normal?

Yes. Each step sends the conversation history, file contents, and tool definitions, so a task can use hundreds of thousands of tokens. Give the token a quota and check each request’s usage in call logs. Repeated context is billed at the cache rate; see prompt cache billing.