# Configure LiteLLM as a gateway for a custom model provider for Codex

> Source: <https://dev.to/juliashevchenko/configure-litellm-as-a-gateway-for-a-custom-model-provider-for-codex-2e3f>
> Published: 2026-08-28 07:02:59+00:00

Recently, I've faced a need to use an LLM model through a gateway. In my case, it was LiteLLM. LiteLLM is an AI gateway where you can manage your LLMs setup in one place, track the costs, and enjoy observability tools.

I've started investigating possible options. My go-to agent for daily coding is Codex, so I was trying to find a tool that supports custom model providers and which usability is the same or very similar to Codex.

After checking OpenCode, Goose, and some other options, I wasn't happy with the UI, speed, and overall functionality. So I thought, is it possible to configure Codex the way I need? LiteLLM provides an OpenAI-compatible interface, so in theory it should be possible.

Codex itself wasn't able to configure it, so here is how to do it in the old-fashioned way - *manually*.

For Windows (Powershell): `[Environment]::SetEnvironmentVariable("LITELLM_API_KEY", "sk-1234", "User")`

Restart the terminal session and validate with a command `$env:LITELLM_API_KEY`

MacOS & Linux:

`export LITELLM_API_KEY="sk-1234"`

Note:The app resolves env_key from its own environment. On macOS, apps launched from Finder or the Dock do not inherit variables exported in your shell profile, so LITELLM_API_KEY can be missing even though Codex works fine in your terminal. Either launch the app from a terminal or set the variable at the login session level and restart the app:

`launchctl setenv LITELLM_API_KEY sk-1234`

Add the following configuration to the beginning of .codex/config.toml. Back up the original config just in case. Replace the existing sections and add the missing ones.

Set the model and effort you like to use. The most important thing here is to set model_provider.

```
model = "gpt-5.6-sol"
model_provider = "litellm"
model_reasoning_effort = "xhigh"
```

Now add provider-specific configurations. env_key should be the name of the environment variable we set in the previous step.

In this case, base_url points to a deployed instance of LiteLLM. If you have a local instance, then use localhost with the appropriate port number.

```
[model_providers.litellm]
name = "litellm"
base_url = "https://litellm.mydomain.com/v1"
env_key = "LITELLM_API_KEY"
wire_api = "responses"
stream_idle_timeout_ms = 7200000
stream_max_retries = 5
```

There is a possibility to set custom HTTP headers. In my example, I need access headers to bypass Cloudflare security guards.

```
http_headers = 
{ "CF-Access-Client-Id" = "abc", "CF-Access-Client-Secret" = "xyz" }
```

Note:With a custom provider, there is no UI for changing the model of a session in the app (see[openai/codex#15364]). A session uses whatever model was set in config.toml when the session was created. To use a different LiteLLM model, update model in config.toml and start a new session.

Restart the Codex and open a new chat session to make sure everything works as expected.

You can verify if you're using LiteLLM as a gateway in the logs in the LiteLLM dashboard.

*Resources:*

[https://docs.litellm.ai/docs/tutorials/openai_codex](https://docs.litellm.ai/docs/tutorials/openai_codex)

[https://www.codex-docs.com/en/docs/config-file/config-advanced](https://www.codex-docs.com/en/docs/config-file/config-advanced)
