AI coding tools · BYOK ·
Claude Code or Codex limit hit? Use an API key instead of your subscription
Your Claude Code week ran out, or Codex says you've reached your usage limit, and you still have work to finish. Both tools can run on a key you pay for by the token instead of your plan. This guide shows the exact settings, taken from each provider's own docs, for an Anthropic or OpenAI API key, OpenRouter, Kimi and Azure OpenAI.
If you want the next step, read npm min-release-age: allow one urgent security patch.
Before you switch, read the cost note at the end of each section. A key has no weekly ceiling unless you set one.
Quick fix
- Claude Code, Anthropic key:
export ANTHROPIC_API_KEY=sk-ant-..., startclaude, approve the key when asked. - Codex, OpenAI key:
printenv OPENAI_API_KEY | codex login --with-api-key. - Another provider: point Claude Code at it with
ANTHROPIC_BASE_URLplusANTHROPIC_AUTH_TOKEN, or give Codex amodel_providersblock in a profile file. Details below.
Claude Code
Use an Anthropic API key
Claude Code's environment variable reference says ANTHROPIC_API_KEY is sent as the X-Api-Key header and "When set, this key is used instead of your Claude Pro, Max, Team, or Enterprise subscription even if you are logged in." In interactive mode "you are prompted to approve the key once before it overrides your subscription." With -p the key is always used.
export ANTHROPIC_API_KEY="sk-ant-..." # from your Anthropic Console
claude
To go back to your plan once it resets, the same page says to run:
unset ANTHROPIC_API_KEY ANTHROPIC_AUTH_TOKEN
Use OpenRouter or Kimi
Gateways expect a Bearer token, so the key goes in ANTHROPIC_AUTH_TOKEN (the docs: "the value you set here will be prefixed with Bearer"), not ANTHROPIC_API_KEY.
OpenRouter's Claude Code guide uses:
export OPENROUTER_API_KEY="<your-openrouter-api-key>"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY="" # OpenRouter: "Must be explicitly empty"
Moonshot's Kimi guide for Claude Code puts the settings in the env field of ~/.claude/settings.json, then asks you to restart Claude Code:
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.moonshot.ai/anthropic",
"ANTHROPIC_AUTH_TOKEN": "YOUR_MOONSHOT_API_KEY",
"ANTHROPIC_MODEL": "kimi-k3[1m]",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "kimi-k3[1m]",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "kimi-k3[1m]",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "kimi-k2.7-code"
}
}
That's a shortened copy of Moonshot's example; their page sets a few more model variables. Non-Claude models in Claude Code are supported by the model provider, not by Anthropic.
Check it worked
Anthropic's gateway guide says to run /status and check two lines: "Anthropic base URL" (shown only when a gateway address is set) and the line naming ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY. If you see a "Login method" line naming your claude.ai account instead, your key isn't being used. "To clear a saved login so only the gateway credential remains, run /logout."
Common misses:
- Base URL ends in
/v1. Use the URLs above exactly. Claude Code adds/v1/messagesitself. - Key in the wrong variable. The gateway guide's troubleshooting table lists "401 errors naming an invalid or unrecognized token" when the credential is "in a header the gateway doesn't read".
- Stale exports. An old
ANTHROPIC_*line in~/.zshrcor~/.bashrccan fight yoursettings.json. Clean both and restart.
Cost: an Anthropic key bills your Console account at API rates; OpenRouter and Moonshot bill you directly. If you'd rather stay on Anthropic and just keep going past the limit, usage credits are the other route, covered in Claude Code monthly spend limits and usage credits. For why the week drained in the first place, see Claude weekly limit reached.
Codex
Use an OpenAI API key
From the Codex auth docs:
printenv OPENAI_API_KEY | codex login --with-api-key
In the desktop app, choose "Sign in another way" on the signed-out screen; in the IDE extension, "Use API Key". The docs say "OpenAI bills API key usage through your OpenAI Platform account at standard API rates", and some features that rely on ChatGPT sign-in aren't available with a key. We compare keys and credits in Codex credits explained.
Use OpenRouter, Kimi or Azure OpenAI with a profile
Keep your normal setup untouched and put each provider in its own profile. The Codex advanced config docs say that with --profile name, Codex loads ~/.codex/config.toml and then overlays ~/.codex/name.config.toml. Use top-level keys in that file, not a [profiles.name] table.
Kimi (~/.codex/kimi.config.toml), values from Moonshot's Codex guide:
model = "kimi-k3.1-pro"
model_provider = "kimi"
model_context_window = 1048576
[model_providers.kimi]
name = "Kimi"
base_url = "https://api.moonshot.ai/v1"
env_key = "KIMI_API_KEY"
wire_api = "responses"
OpenRouter (~/.codex/openrouter.config.toml), from OpenRouter's Codex guide:
model_provider = "openrouter"
model = "~openai/gpt-sol-latest"
[model_providers.openrouter]
name = "openrouter"
base_url = "https://openrouter.ai/api/v1"
[model_providers.openrouter.auth]
command = "sh"
args = ["-c", "echo $OPENROUTER_API_KEY"]
OpenRouter notes that a plain env_key = "OPENROUTER_API_KEY" also works, "but Codex won't fetch the OpenRouter model catalog in that mode."
Azure OpenAI (~/.codex/azure.config.toml), from Microsoft's Codex guide:
model = "gpt-5-codex" # your Azure deployment name
model_provider = "azure"
model_reasoning_effort = "medium"
[model_providers.azure]
name = "Azure OpenAI"
base_url = "https://YOUR_RESOURCE_NAME.openai.azure.com/openai/v1"
env_key = "AZURE_OPENAI_API_KEY"
wire_api = "responses"
Microsoft: "you must include /v1 in the base_url path" and "env_key must point to an environment variable", never the key itself.
Then run:
export KIMI_API_KEY="..." # or OPENROUTER_API_KEY / AZURE_OPENAI_API_KEY
codex --profile kimi
codex exec --profile kimi "finish the failing test in src/api"
Common misses:
- 401 from Kimi. Moonshot's troubleshooting says the key and
base_urlmay "belong to different platforms — API keys created on platform.kimi.ai only work withhttps://api.moonshot.ai/v1." Check the variable reached Codex withtest -n "$KIMI_API_KEY" && echo set || echo missing. - Variable not exported in the shell or app that starts Codex. Desktop apps launched from the dock may not see your shell exports.
- Missing
/v1inbase_urlfor Kimi and Azure.
Cost: each provider bills you at its own rates. Set a spend limit at the provider before you leave an overnight codex exec running. If the problem is "Selected model is at capacity" rather than a used-up plan, see Codex “Selected model is at capacity” first.
FAQ
Will an API key in Claude Code silently replace my subscription? Not in interactive mode: the docs say you're asked to approve the key once. With claude -p the key is always used when present.
Can I switch back when my plan resets? Yes. Claude Code: unset ANTHROPIC_API_KEY ANTHROPIC_AUTH_TOKEN and remove any env block you added. Codex: run plain codex without --profile, or sign in with ChatGPT again if you used codex login --with-api-key.
Can I put the provider block in my repo's .codex/config.toml? No. The Codex config reference says Codex ignores model_provider, model_providers, profile and profiles "when they appear in a project-local .codex/config.toml". Keep them in ~/.codex.
Get the tested profiles
Disclosure: this is our own paid product. The BYOK Profiles Kit has copy-paste profiles for Kimi, OpenRouter, Vercel AI Gateway and Azure OpenAI in Codex, Claude Code and Cursor, with keys read from environment variables, plus a table of 29 error strings and symptoms with their cause and fix. It's designed to save the trial and error of base URL paths, auth headers and model IDs that collide with built-in names. The Codex and Claude Code snippets were run against a local mock server, not the live provider APIs; the Cursor steps are checked against docs only. No API keys are included, and providers bill you directly. It costs $9 (plus VAT where applicable).
As of October 2026, from each provider's own docs (sources checked 24 Oct 2026): code.claude.com/docs/en/env-vars, code.claude.com/docs/en/llm-gateway-connect, learn.chatgpt.com/docs/auth, developers.openai.com/codex/config-advanced, openrouter.ai Claude Code and Codex guides, platform.kimi.ai Claude Code and Codex guides, learn.microsoft.com Azure OpenAI Codex guide.
Claude, Claude Code and Anthropic are trademarks of Anthropic. Codex, ChatGPT and OpenAI are trademarks of OpenAI. Kimi and Moonshot, OpenRouter, Vercel, Azure and Microsoft, and other names belong to their owners. Names are used only to identify products and compatibility. Stack & Ship is not affiliated with or endorsed by any of them.