Codex CLI with AICredits
Route OpenAI's Codex CLI through AICredits so every request bills to your INR wallet, and you can switch models without switching accounts.
Use this page with an AI assistant
Opens a new chat with this docs URL and the correct AICredits base URLs.
Route Codex CLI (@openai/codex) through AICredits to pay in INR, set per-key budget caps, and see every request in your usage dashboard.
Prerequisites
- An AICredits account with credits — top up here
- Codex CLI installed (
npm install -g @openai/codex)
Setup
1. Create an API Key
Go to Dashboard → API Keys and create a new key. Optionally set a budget limit (e.g. ₹500) to cap what a single Codex session can spend.
Copy the key immediately — it starts with sk- and is shown only once.
2. Add a custom provider to ~/.codex/config.toml
Codex CLI talks to providers through a model_providers table in its config file. Add an aicredits provider and point Codex at it:
[model_providers.aicredits]
name = "AICredits"
base_url = "https://api.aicredits.in/v1"
env_key = "AICREDITS_API_KEY"
wire_api = "responses"
model_provider = "aicredits"
model = "gpt-4o-mini"wire_api must be "responses", not "chat". Newer Codex CLI versions (0.150+) reject wire_api = "chat" for custom providers outright ("no longer supported"). AICredits supports the Responses API (/v1/responses) directly, so "responses" works correctly.
Then export the key under the exact env var name you set as env_key:
export AICREDITS_API_KEY=sk-your-aicredits-key-heresource ~/.zshrcenv_key can be any variable name you want — Codex just reads whichever one you name in model_providers.aicredits.env_key. AICREDITS_API_KEY avoids colliding with OPENAI_API_KEY, which Codex still checks for its own login flow.
3. Verify it works
codex exec -c model_provider=aicredits -c model=gpt-4o-mini "say hi"The session header should show provider: aicredits. Check Dashboard → Usage afterward — the request and INR cost should appear within a few seconds.
You may see failed to refresh available models in the logs — this is harmless. Codex CLI's model-catalog refresh expects a provider's /models endpoint to return {"models": [...]}; AICredits' /v1/models returns the OpenAI-standard {"data": [...]} shape instead. Codex logs an error and falls back to default model metadata, but chat/exec requests are unaffected — confirmed by testing directly against api.aicredits.in.
Switch models per session
Override model for one run without editing the config file:
codex exec -c model_provider=aicredits -c model=anthropic/claude-sonnet-4.6 "refactor this function"Any AICredits-routed model works here — see the model catalog for the current list and pricing.
Non-OpenAI Models with Responses
Codex CLI uses the Responses API for custom providers. AICredits accepts POST https://api.aicredits.in/v1/responses: OpenAI models are forwarded to OpenAI's Responses API, while non-OpenAI models are converted to Chat Completions internally and routed through the normal AICredits pipeline.
Use a non-OpenAI model ID directly in config.toml:
[model_providers.aicredits]
name = "AICredits"
base_url = "https://api.aicredits.in/v1"
env_key = "AICREDITS_API_KEY"
wire_api = "responses"
model_provider = "aicredits"
model = "deepseek/deepseek-v4-flash"Or override it for one run:
codex exec -c model_provider=aicredits -c model=deepseek/deepseek-v4-flash "summarize this diff"Known limitations for non-OpenAI models:
- Responses-only features that do not have a Chat Completions equivalent may not apply.
instructions,input,max_output_tokens,reasoning.effort, function tools, and common sampling fields are converted to their Chat Completions equivalents.- Tool-calling support depends on the selected model.
- For best compatibility, start with simple text prompts and add tools or structured output after a basic request appears in Dashboard → Usage.
Making aicredits the default provider
If you want every codex invocation to use AICredits without passing -c model_provider=aicredits each time, set it directly in the config file (as shown in step 2) rather than overriding per-command. To switch back to OpenAI directly for a single session:
codex exec -c model_provider=openai "..."Troubleshooting
401 Unauthorized: Invalid API Key
The env var named in model_providers.aicredits.env_key is missing, misspelled, or holds an invalid key. Confirm it's set (echo $AICREDITS_API_KEY) and that it's an AICredits key from Dashboard → API Keys, not an OpenAI key.
402 Payment Required Wallet balance is zero or the key has hit its budget cap. Add credits at Dashboard → Billing or raise the key's budget limit.
Error loading config.toml: wire_api = "chat" is no longer supported
Your Codex CLI version rejects wire_api = "chat" for custom providers. Change it to wire_api = "responses" — see step 2.
unknown configuration field
Codex CLI validates config.toml strictly when --strict-config is passed (and increasingly by default on newer versions). Check for typos in the model_providers.aicredits block, and make sure model_providers is a top-level table, not nested under something else.