Codex with AnyRouter — free models, one API key
Run Codex through AnyRouter — set the OpenAI base URL, use free models with anyrouter/free, or any model with one sk-ar- key.
Point Codex at AnyRouter and every run flows through one gateway: unified billing, request audit logs, and a single sk-ar- key that reaches every model AnyRouter serves. Codex speaks the OpenAI-compatible wire protocol, so this is a config-only change.
Why route through AnyRouter
- One key for every model Codex can call — no per-provider keys.
- Audit logs with cost, latency, and tokens on every request.
- Swap models freely without rotating credentials.
This is the narrative "use Codex with AnyRouter" guide. For the full command reference — flags, profiles, --dry-run, and the login flow — see the canonical Codex CLI setup.
Set it up
One command handles everything:
curl -fsSL https://anyrouter.dev/setup.sh | bash
anyr codexThe first run sets you up once: paste an sk-ar- key, or log in at anyrouter.dev (a key is generated for you). Your config is saved to ~/.anyrouter/config.yaml and reused next time — and the same key also powers anyr claude for Claude Code.
Anything after -- forwards straight to Codex:
anyr codex --model auto --effort xhigh -- execSee the CLI reference for all options.
Codex reads OPENAI_BASE_URL and OPENAI_API_KEY. Point them at AnyRouter's OpenAI-compatible endpoint:
export OPENAI_BASE_URL="https://anyrouter.dev/api/v1"
export OPENAI_API_KEY="sk-ar-your-anyrouter-key"Create the key in your dashboard first — every AnyRouter key starts with sk-ar-. Every Codex request now routes through AnyRouter, billed against your credits and recorded in your audit logs.
Free models and auto routing
Set the model to an AnyRouter router id instead of a fixed model:
| Model id | What it does |
|---|---|
anyrouter/free | Routes to the best free model available right now, with automatic fallback. No credits used. |
anyrouter/auto | Picks from top models (free and paid) with health-aware failover. Paid members use credits. |
anyr codex --model anyrouter/freeanyrouter/free needs no provider keys. It has no daily cap on any plan. Details: Free Tier.
If anyrouter/auto or anyrouter/free returns 400 no_eligible_models, your privacy or routing settings (for example Zero Data Retention or blocked providers) excluded every candidate — relax them in the dashboard or pick a specific model.
Bring your own keys
Already pay for a provider? Add its key in AnyRouter and Codex uses it through the same base URL. BYOK requests are billed by your provider. See Use your own provider keys and BYOK.
Verify
Run any Codex command, then open your request logs. The call should appear within a second or two, with the model name, token counts, and cost. If it's there, you're routing through AnyRouter.
Troubleshooting
401 or 403 — confirm the key is active in your dashboard and that OPENAI_API_KEY (or your saved CLI config) holds a valid sk-ar- key.
Model not found — make sure the model id exactly matches an AnyRouter id; browse Chat-Completions-compatible models.
Related
- Free Tier
- Cline · Kilo Code · Continue
- Codex CLI setup — canonical command reference
- Claude Code
- OpenCode
- Supported models
- Chat Completions API