Docs · Codex CLI

How do I configure Codex CLI with FreeModel? Two environment variables

最后更新:

FreeModel is an AI model gateway: one key for 200+ models across dozens of providers, routed by task with free capacity tried first. Codex CLI speaks the OpenAI API, so the whole setup is pointing one variable at the right base URL and handing over a key.

Setup

# OpenAI-compatible base. Note: /v1 is part of the base —
# the client appends /chat/completions itself.
export OPENAI_BASE_URL="https://freemodel.online/v1"
export OPENAI_API_KEY="sk-your-key"

Restart the shell that launches Codex CLI, or put the two lines in your shell profile so they survive a new terminal.

Which model you get

Whatever you ask for. If you name a specific model, that is what answers. If you would rather not track model names, pass a tier alias instead:

If you wantUse
Strongest available, chosen for youfm-v1-pro
Balanced defaultfm-v1-standard
Lowest cost per callfm-v1-lite

The full set of ids is at Models, and the live list is curl https://freemodel.online/v1/models.

Checking it works

# does the base answer at all? 401 = yes, it wants a key. 404 = wrong path.
curl -s -o /dev/null -w "%{http_code}\n" \
  -X POST https://freemodel.online/v1/chat/completions \
  -H "Content-Type: application/json" -d '{}'

# and a real call, which proves the key
curl -s https://freemodel.online/v1/chat/completions \
  -H "Authorization: Bearer sk-your-key" \
  -H "Content-Type: application/json" \
  -d '{"model":"fm/qwen-plus","messages":[{"role":"user","content":"hi"}]}'

A 400 with messages: ... required on the first command is the good outcome — it means the endpoint exists and parsed your (empty) body.

Common problems

SymptomCause
404 on every requestBase URL carries /v1 twice, or carries /chat/completions — the base stops at /v1
401 on every requestKey not exported in the shell that launched the client
Works in one terminal, not anotherexport is per-shell. Put it in your profile for it to stick
Model not foundThe id is not in /v1/models; routes must match exactly, prefix included

Next

Set up in three steps

The whole configuration is two environment variables. The order matters only in that the shell which exports them has to be the shell that launches the client.

  1. Step 1: Export the base and the key. OPENAI_BASE_URL takes https://freemodel.online/v1 and OPENAI_API_KEY takes your sk- key from the console. The base stops at /v1; the client appends /chat/completions on its own.
  2. Step 2: Make it survive a new terminal. On macOS and Linux that means the same two lines in ~/.zshrc or ~/.bashrc. On Windows, setx OPENAI_BASE_URL "..." writes the value for future shells, while $env:OPENAI_BASE_URL covers only the current PowerShell session.
  3. Step 3: Restart Codex CLI and check the endpoint before you blame the model. curl -s https://freemodel.online/v1/models is public and needs no key, so it separates a reachability problem from a key problem in one command.

Codex CLI is a coding agent, so whatever answers has to hold up across a long run. The 199 listed models are 77% chat models[1], and a routed request is re-decided on every call, so a provider that stops answering mid-session is skipped rather than fatal.

What you get to choose from

Naming a model pins the request to it. Naming a tier alias hands the decision to the router per request, and the aliases are a deliberately small slice of the catalogue — three of them, one per subscription plan.[1] That is the point of them: they stand in for a choice you would otherwise make yourself, and they move when a provider stops answering.

The wider catalogue is worth a look if you care about depth rather than convenience. The 199 listed models put chat, image, video, speech and embeddings in one list, and six auto/* routes cover the non-chat side — auto/image, auto/video, auto/tts, auto/asr, auto/embed, auto/rerank — each carrying a candidates field that shows how many models sit behind it.[1]

A route is not a model name: it is a standing instruction to the router, re-evaluated on every call. Two identical requests inside one session can be served by two different providers, which is what you want for uptime and not what you want while comparing two prompts against each other.

Codex CLI FAQ

Do I have to edit a config file?
No. The two environment variables are the entire setup for the OpenAI-compatible path, and nothing is written to disk. To go back to your previous provider, unset them in the shell that launches the client.
Will these variables affect other tools on the same machine?
They will, and it is worth knowing: OPENAI_BASE_URL and OPENAI_API_KEY are the names most OpenAI-shaped tools read. Anything else in that shell which reads them will also send its requests here, which is usually fine and occasionally surprising.
What happens when the model name I pass is not in the catalogue?
You get a model-not-found error rather than a silent substitution to something else. Route names have to match exactly, including the auto/ prefix. /v1/models is the authority on what this gateway accepts.
Does Codex CLI's approval mode change with this setup?
No. Approval and sandbox behaviour is decided by the client before a request is ever sent, and this setup only changes where the request goes. If a command is not running, that is a client setting rather than a gateway one.
Should I use a route or a concrete model for long sessions?
An alias, usually. A long agent session makes many calls, and a provider that rate-limits on call forty fails a session that pinned its name. fm-v1-standard moves the request to its next candidate instead of returning that failure.

Sources

Every third-party figure quoted on this site was read from the vendor documentation linked below, on the date stated. None of these links are affiliate links and none of these vendors paid to be listed. If a number here disagrees with the page it came from, the page is right — tell us and it gets corrected.

Where the percentages come from

Every share quoted on this site is counted from the live model list, not estimated. Divide the two counts in the last column, round to the nearest whole percent, and you get the number in the first.

ShareOf whatCount
77%are chat models153 of 199 listed entries
9%generate images18 of 199 listed entries
23%are not chat models46 of 199 listed entries
3%are modality routes6 of 199 listed entries
17%carry a context length34 of 199 listed entries

[1] Counted on 29 September 2026 from GET https://freemodel.online/v1/models, which needs no key and no account. Every row uses all 199 listed entries as the denominator, because the only thing the endpoint states for every entry is its modality. It no longer publishes per-model capability flags such as tool calling or reasoning, so this site quotes no shares for them. Fetch the list and recount whenever you want to check the arithmetic.

Troubleshooting Codex CLI + FreeModel

Keep a one-line curl command next to the CLI config. When something breaks, run the curl first; if it succeeds, the problem is in the CLI environment, not the gateway.