# Codex

Codex supports custom model providers in its configuration file. Use a custom provider when Codex should send Responses API requests to AISIX instead of calling a model provider directly.

In this guide, you will point Codex at the AISIX proxy API root and authenticate with an AISIX caller API key. The Codex custom model value will be an AISIX model alias.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* Codex installed and able to read `~/.codex/config.toml`.
* A running AISIX gateway with the proxy listener available.
* An AISIX caller API key.
* A model alias that the caller API key can access and that works with the [Responses API](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md).

If AISIX is already deployed for your organization, obtain the gateway URL, model alias, and caller API key from the team that manages it. Otherwise, follow the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md) or [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md), or [contact API7](https://api7.ai/contact) for AISIX Cloud access.

## Configure Codex[​](#configure-codex "Direct link to Configure Codex")

Set the caller API key in the environment where Codex runs:

```
# Replace with your values

export AISIX_API_KEY="YOUR_CALLER_API_KEY"
```

Add an AISIX model provider to `~/.codex/config.toml`:

\~/.codex/config.toml

```
model = "gpt-4o-prod" # Use an AISIX model alias, not the upstream provider model ID.

model_provider = "aisix"



[model_providers.aisix]

name = "AISIX gateway"

# Include /v1 and omit the trailing slash

# The local quickstarts use http://127.0.0.1:3000/v1

base_url = "YOUR_AISIX_GATEWAY_URL/v1"

env_key = "AISIX_API_KEY" # Read the AISIX caller API key from the environment.

wire_api = "responses"
```

## Verify the Integration[​](#verify-the-integration "Direct link to Verify the Integration")

Start Codex from the shell where `AISIX_API_KEY` is set:

```
codex
```

Ask a short question that requires a model response.

When the request succeeds, verify the following results:

* Codex prints a model response.
* AISIX records a successful `POST /v1/responses` request for the selected model alias.

AISIX authenticates the caller API key, checks model access, resolves the model alias, applies configured policies, and dispatches to the upstream provider behind the alias.

## Use Codex With Other Providers[​](#use-codex-with-other-providers "Direct link to Use Codex With Other Providers")

Codex relies on streamed function calls for tool execution, so the alias must use an upstream whose native or translated stream returns them. The translated stream returns Claude's tool calls as function calls, both through the `anthropic` adapter and from Claude on Vertex AI.

When the model alias is backed by a provider key that does not serve the Responses API, such as an Anthropic key, AISIX translates each Codex request to Chat Completions and translates the answer back. A few behaviors on that path matter to Codex specifically:

* On translated streams that support function calls, Codex's `namespace` tools, such as `multi_agent_v1`, are offered to the model as flattened function tools, and the model's calls come back to Codex under their original name and namespace. In Responses Lite mode, Codex sends its tools in an `additional_tools` input item, including `exec` in the `functions` namespace, and the bridge forwards them like top-level tools. See [Tool Parameters](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md#tool-parameters).
* Among keys that would otherwise forward Responses natively, a Lite-mode request keeps that path only when `provider` is `openai`, `byo`, omitted in an AISIX OSS resources file, or the literal `azure` value with an adapter other than `bedrock`, `vertex`, or `azure-openai`. Every other native key, including a DeepSeek catalog key, takes the bridge. An Azure OpenAI provider key uses `adapter: azure-openai` and therefore bridges every Responses request. See [Lite-Mode Tool Requests](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md#lite-mode-tool-requests).
* When Codex's model metadata enables tool search, Codex defers its MCP tools and runs `tool_search` itself. The bridge offers the search tool, the search history, and the tools a search loaded to the model, and the model's calls to `tool_search` or to a loaded MCP tool come back as items Codex can dispatch. See [Tool Parameters](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md#tool-parameters).
* The reasoning Codex replays in its history is sent to OpenAI-compatible upstreams as `reasoning_content`, so input token counts rise over a multi-turn session. See [Replayed Conversation History](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md#replayed-conversation-history).
* When a turn fails mid-stream, AISIX ends the stream with a `response.failed` event, so Codex shows the error message. A turn stopped by an output guardrail fails with `invalid_prompt`, which Codex does not retry. See [Stream Failures on the Bridged Path](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md#stream-failures-on-the-bridged-path).
* Hosted tools such as `web_search` are dropped on this path.

## Troubleshoot Codex Requests[​](#troubleshoot-codex-requests "Direct link to Troubleshoot Codex Requests")

If Codex cannot reach AISIX, check these items:

| Symptom                                                                                    | Check                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Authentication fails                                                                       | Confirm `AISIX_API_KEY` is set in the same shell or environment where Codex runs.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| Model is not found                                                                         | Confirm `model` in `config.toml` is an AISIX model alias visible to the caller API key.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| Responses request fails                                                                    | Confirm the selected model alias works with the AISIX Responses API. Some provider-backed aliases do not support every OpenAI-specific Responses feature.                                                                                                                                                                                                                                                                                                                                                                                                                          |
| A follow-up turn fails on an OpenAI-compatible upstream after the model returned reasoning | The upstream may reject the `reasoning_content` field AISIX adds to replayed assistant messages. See [Replayed Conversation History](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md#replayed-conversation-history).                                                                                                                                                                                                                                                                                                                                                    |
| A follow-up turn fails with `400 invalid_encrypted_content`                                | The conversation's encrypted reasoning reached an upstream account that cannot read it. Either the model was changed to use a provider key for another account, a provider key was edited in place to hold another account's credentials or `api_base`, or the conversation was continued through a passthrough route or sent to the provider directly. Create a new model for the other account, and start a new conversation. See [Which Models Can Read Encrypted Context](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md#which-models-can-read-encrypted-context). |
| Requests bypass AISIX                                                                      | Confirm `model_provider = "aisix"` is in the active Codex configuration layer.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |

For endpoint behavior and provider boundaries, see [Responses API](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md).

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Responses API](https://docs.api7.ai/ai-gateway/endpoints/responses-api.md): review how AISIX handles Responses API requests.
* [Semantic Routing](https://docs.api7.ai/ai-gateway/routing/semantic-routing.md#pin-a-session-to-its-route): route each session by topic. Codex sends a session ID the gateway reads, so session affinity keeps a session on one route with no extra configuration.
* [Metrics and Usage Events](https://docs.api7.ai/ai-gateway/observability/metrics-and-usage-events.md): review gateway metrics and per-attempt usage events.
* [API Key and Model Rate Limits](https://docs.api7.ai/ai-gateway/traffic-controls/rate-limits.md): add caller or model limits for coding-agent traffic.
